1+ <template >
2+ <div class =" layui-color-picker" :class =" uid" >
3+ <ColorBox :color =" color" @click =" handleToggle" :size =" size" />
4+ <div :hidden =" isHidden" class =" layui-anim layui-anim-upbit layui-colorpicker-main" >
5+ <div class =" layui-colorpicker-main-wrapper" >
6+ <div ref =" basis" class =" layui-colorpicker-basis" >
7+ <canvas ref =" canvas" @mousedown =" handleCanvasMouseDown" width =" 260" height =" 180" />
8+ <div
9+ @mousedown =" handleMouseDown"
10+ ref =" choose"
11+ class =" layui-colorpicker-basis-cursor"
12+ :style =" {
13+ left: left + 'px',
14+ top: top + 'px'
15+ }"
16+ ></div >
17+ </div >
18+ <color-side @change =" sideChange" />
19+ </div >
20+ <div class =" layui-colorpicker-main-alpha" >
21+ <div class =" layui-colorpicker-alpha-bgcolor" >
22+ <div ref =" alphaslider" class =" layui-colorpicker-alpha-slider" style =" left : 280px ;" ></div >
23+ </div >
24+ </div >
25+ <div class =" layui-colorpicker-main-input" >
26+ <div class =" layui-inline" >
27+ <input :value =" color" type =" text" class =" layui-input" >
28+ </div >
29+ <div class =" layui-btn-container" >
30+ <button class =" layui-btn layui-btn-primary layui-btn-sm" @click =" handleClear" >清空</button >
31+ <button class =" layui-btn layui-btn-sm" @click =" handleConfirm" >确定</button >
32+ </div >
33+ </div >
34+ </div >
35+ </div >
36+ </template >
37+
38+ <script >
39+ /**
40+ * 改为画布实现
41+ * 已知bug 边界颜色选择不正确
42+ * 目前只实现选择颜色,未实现输入颜色色板进行变化
43+ * 参考 https://blog.csdn.net/e4cqss6c/article/details/55100232
44+ */
45+ import ColorBox from " ./color-box" ;
46+ import ColorSide from " ./color-side" ;
47+
48+ import { rgb2hex , hex2rgb } from " ./color" ;
49+ export default {
50+ name: " LayColorPicker" ,
51+ components: {
52+ ColorBox,
53+ ColorSide
54+ },
55+ props: {
56+ value: String ,
57+ size: String ,
58+ type: String
59+ },
60+ data () {
61+ return {
62+ startLeft: 0 ,
63+ left: 0 ,
64+ top: 0 ,
65+ startTop: 0 ,
66+ isHidden: true ,
67+ color: this .value || " " ,
68+ uid: " color-picker-" + Math .random () + " "
69+ };
70+ },
71+ mounted () {
72+ this .genBase ();
73+ },
74+ methods: {
75+ genBase (color = " #f00" ) {
76+ const ctx = this .$refs .canvas .getContext (" 2d" );
77+ const width = 260 ;
78+ var base = ctx .createLinearGradient (0 , 0 , width, 0 );
79+ base .addColorStop (1 , color);
80+ base .addColorStop (0 , " rgba(255,255,255,1)" );
81+ ctx .fillStyle = base;
82+ ctx .fillRect (0 , 0 , width, width);
83+
84+ var my_gradient1 = ctx .createLinearGradient (0 , 0 , 0 , width);
85+ my_gradient1 .addColorStop (0 , " rgba(0,0,0,0)" );
86+ my_gradient1 .addColorStop (1 , " rgba(0,0,0,1)" );
87+ ctx .fillStyle = my_gradient1;
88+ ctx .fillRect (0 , 0 , width, width);
89+ },
90+ sideChange (color ) {
91+ this .genBase (color);
92+ this .change ();
93+ },
94+ handleCanvasMouseDown (e ) {
95+ this .left = e .offsetX - 6 ;
96+ this .top = e .offsetY - 6 ;
97+
98+ this .handleMouseDown (e);
99+ this .change ();
100+ },
101+ handleMouseDown (e ) {
102+ this .clientX = e .clientX ;
103+ this .clientY = e .clientY ;
104+
105+ this .startLeft = this .left ;
106+ this .startTop = this .top ;
107+ window .addEventListener (" mousemove" , this .onDragging );
108+ window .addEventListener (" mouseup" , this .onDragEnd );
109+ },
110+ onDragging (e ) {
111+ let left = e .clientX - this .clientX + this .startLeft ;
112+ let top = e .clientY - this .clientY + this .startTop ;
113+
114+ if (top < - 6 ) top = - 6 ;
115+ if (top > 174 ) top = 174 ;
116+ if (left < - 6 ) left = - 6 ;
117+ if (left > 254 ) left = 254 ;
118+ this .left = left;
119+ this .top = top;
120+ this .change ();
121+ e .preventDefault ();
122+ },
123+ onDragEnd (e ) {
124+ window .removeEventListener (" mousemove" , this .onDragging );
125+ window .removeEventListener (" mouseup" , this .onDragEnd );
126+ },
127+ change () {
128+ const ctx = this .$refs .canvas .getContext (" 2d" );
129+ var imgData = ctx .getImageData (this .left + 5 , this .top + 6 , 1 , 1 );
130+ const [r , g , b , a ] = imgData .data ;
131+ if (this .type == ' rgb' ){
132+ this .color = ` rgb(${ r} , ${ g} , ${ b} )`
133+ } else {
134+ this .color = " #" + rgb2hex ([r, g, b, a]);
135+ }
136+
137+ },
138+ handleConfirm () {
139+ this .isHidden = true ;
140+ window .removeEventListener (" click" , this .hidden );
141+ this .$emit (" input" , this .color );
142+ this .$emit (" change" , this .color );
143+ },
144+ handleClear () {
145+ this .color = " " ;
146+ },
147+ handleToggle (e ) {
148+ this .isHidden = ! this .isHidden ;
149+ if (! this .isHidden ) {
150+ window .addEventListener (" click" , this .hidden );
151+ } else {
152+ window .removeEventListener (" click" , this .hidden );
153+ }
154+ this .color = this .value ;
155+ },
156+ hidden (e ) {
157+ const res = e .path
158+ .map (o => o .className )
159+ .find (o => o && o .includes (this .uid ));
160+ if (res) {
161+ return false ;
162+ }
163+ window .removeEventListener (" click" , this .hidden );
164+
165+ this .handleToggle ();
166+ }
167+ },
168+ watch: {
169+ value (){
170+ this .color = this .value
171+ console .log (this .value )
172+ }
173+ }
174+ };
175+ </script >
176+
177+ <style scoped>
178+ .layui-color-picker {
179+ position : relative ;
180+ }
181+ </style >
0 commit comments