@@ -24,6 +24,7 @@ const fuiCanvasComponent = {
2424 currentLayer : Object ,
2525 fuiImages : Array ,
2626 imageDataCache : Object ,
27+ library : String ,
2728 } ,
2829 data ( ) {
2930 return {
@@ -48,7 +49,7 @@ const fuiCanvasComponent = {
4849 return {
4950 "fui-canvas_select" : this . activeTool === "select" ,
5051 "fui-canvas_moving" : this . isMoving ,
51- "fui-canvas_draw" : this . activeTool === "pen " ,
52+ "fui-canvas_draw" : this . activeTool === "draw " ,
5253 } ;
5354 } ,
5455 canvasWidth ( ) {
@@ -77,7 +78,7 @@ const fuiCanvasComponent = {
7778
7879 document . addEventListener ( "mouseup" , this . canvasMouseUp ) ;
7980 this . $refs . screen . addEventListener ( "contextmenu" , ( event ) => {
80- if ( this . isDrawing || this . isMoving || this . activeTool === "pen " ) {
81+ if ( this . isDrawing || this . isMoving || this . activeTool === "draw " ) {
8182 event . preventDefault ( ) ;
8283 }
8384 } ) ;
@@ -118,11 +119,10 @@ const fuiCanvasComponent = {
118119 } ,
119120 canvasMouseDown ( e ) {
120121 e . preventDefault ( ) ;
121- const isRightClickDrawing = this . activeTool === "pen " && e . button === 2 ;
122+ const isRightClickDrawing = this . activeTool === "draw " && e . button === 2 ;
122123 if ( e . button !== 0 && ! isRightClickDrawing || this . isDrawing || this . isMoving ) {
123124 return ;
124125 }
125- let layerProps = { } ;
126126 const [ x , y ] = [ e . offsetX , e . offsetY ] ;
127127 this . oX = scaleDown ( x ) ;
128128 this . oY = scaleDown ( y ) ;
@@ -131,54 +131,28 @@ const fuiCanvasComponent = {
131131 this . isDrawing = true ;
132132 this . isEraser = e . button === 2 ;
133133
134- if ( this . currentLayer && this . currentLayer . type !== "pen " || this . activeTool === "select" ) {
134+ if ( this . currentLayer && this . currentLayer . type !== "draw " || this . activeTool === "select" ) {
135135 this . $emit ( "updateCurrentLayer" , undefined ) ;
136136 }
137- layerProps = {
138- name : "" ,
137+ const uid = generateUID ( ) ;
138+ let layerProps = {
139+ name : `${ this . activeTool } _${ uid } ` ,
139140 type : this . activeTool ,
140141 index : this . layerIndex ,
141142 x : scaleDown ( x ) ,
142143 y : scaleDown ( y ) ,
144+ id : uid ,
143145 } ;
144146
145- if ( [ "pen" ] . includes ( this . activeTool ) ) {
146- if ( this . currentLayer && this . currentLayer . type === "pen" ) {
147- layerProps = {
148- ...this . currentLayer ,
149- imageData : addImageDataPadding ( this . currentLayer . imageData , this . currentLayer . x , this . currentLayer . y , this . canvasWidth , this . canvasHeight ) ,
150- x : this . currentLayer . x > 0 ? 0 : this . currentLayer . x ,
151- y : this . currentLayer . y > 0 ? 0 : this . currentLayer . y ,
152- width : this . currentLayer . imageData . width ,
153- height : this . currentLayer . imageData . height ,
154- }
155- this . $emit ( "updateCurrentLayer" , layerProps ) ;
156- } else {
157- layerProps . x = 0 ;
158- layerProps . y = 0 ;
159- const imageDataArraylength = 4 * this . canvasWidth * this . canvasHeight ;
160- layerProps . imageData = new ImageData (
161- new Uint8ClampedArray ( imageDataArraylength ) ,
162- this . canvasWidth ,
163- this . canvasHeight ,
164- ) ;
165- this . $emit ( "updateCurrentLayer" , {
166- ...layerProps ,
167- width : layerProps . imageData . width ,
168- height : layerProps . imageData . height ,
169- } ) ;
170- this . $emit ( "addScreenLayer" ) ;
147+ if ( [ "draw" ] . includes ( this . activeTool ) ) {
148+ const isDrawingCurrent = this . currentLayer && this . currentLayer . type === "draw" ;
149+
150+ layerProps = startDrawing ( isDrawingCurrent , layerProps , this . currentLayer , this . canvasWidth , this . canvasHeight , this . oX , this . oY , this . isEraser ) ;
151+
152+ this . $emit ( "updateCurrentLayer" , layerProps ) ;
153+ if ( ! isDrawingCurrent ) {
154+ this . $emit ( "addScreenLayer" , layerProps ) ;
171155 }
172- drawingLayer = this . currentLayer ? this . currentLayer : layerProps ;
173- drawLine ( drawingLayer . imageData ,
174- this . oX - drawingLayer . x ,
175- this . oY - drawingLayer . y ,
176- this . oX - drawingLayer . x ,
177- this . oY - drawingLayer . y ,
178- drawingLayer . imageData . width ,
179- drawingLayer . imageData . height ,
180- this . isEraser ,
181- ) ;
182156 } else if ( [ "frame" , "box" , "dot" , "circle" , "disc" ] . includes ( this . activeTool ) ) {
183157 this . $emit ( "updateCurrentLayer" , {
184158 ...layerProps ,
@@ -281,7 +255,7 @@ const fuiCanvasComponent = {
281255 layerProps . x = scaleDown ( x ) ;
282256 layerProps . y = scaleDown ( y ) ;
283257 }
284- } else if ( this . activeTool === "pen " ) {
258+ } else if ( this . activeTool === "draw " ) {
285259 drawLine ( this . currentLayer . imageData ,
286260 this . oX - this . currentLayer . x ,
287261 this . oY - this . currentLayer . y ,
@@ -334,7 +308,7 @@ const fuiCanvasComponent = {
334308 canvasMouseLeave ( e ) {
335309 e . preventDefault ( ) ;
336310 // this.$refs.cursor.style.transform = `translate3d(-1000px, -1000px, 0)`;
337- if ( [ "select" , "pen " ] . includes ( this . activeTool ) && this . isMoving ) {
311+ if ( [ "select" , "draw " ] . includes ( this . activeTool ) && this . isMoving ) {
338312 this . isDrawing = false ;
339313 this . stopDrawing ( e ) ;
340314 }
@@ -350,7 +324,9 @@ const fuiCanvasComponent = {
350324 this . stopDrawing ( e ) ;
351325 this . isDrawing = false ;
352326 }
353- this . redrawCanvas ( this . screenElements ) ;
327+ if ( this . isDrawing || this . isMoving ) {
328+ this . redrawCanvas ( this . screenElements ) ;
329+ }
354330 } ,
355331 stopDrawing ( ) {
356332 this . isEraser = false ;
@@ -377,15 +353,7 @@ const fuiCanvasComponent = {
377353 }
378354 const { isCustom, width, height } = this . fuiImages [ name ] ;
379355 const layer = {
380- type : "icon" ,
381- name : name ,
382- index : this . layerIndex ,
383- x : x ,
384- y : y ,
385- width : width ,
386- height : height ,
387- isOverlay : false ,
388- isCustom : isCustom ,
356+ type : "icon" , name : name , index : this . layerIndex , x : x , y : y , width : width , height : height , isOverlay : false , isCustom : isCustom ,
389357 } ;
390358 this . $emit ( "updateCurrentLayer" , layer ) ;
391359 this . $emit ( "addScreenLayer" , layer ) ;
@@ -419,46 +387,23 @@ const fuiCanvasComponent = {
419387 }
420388 break ;
421389 case "line" :
422- drawLine (
423- imgData ,
424- x ,
425- y ,
426- x2 ,
427- y2 ,
428- this . canvasWidth ,
429- this . canvasHeight ,
430- this . scale
431- ) ;
390+ drawLine ( imgData , x , y , x2 , y2 , this . canvasWidth , this . canvasHeight , false ) ;
432391 this . CTX . putImageData ( imgData , 0 , 0 ) ;
433392 break ;
434393 case "circle" :
435- drawCircle (
436- imgData ,
437- x + radius ,
438- y + radius ,
439- radius ,
440- this . canvasWidth ,
441- this . canvasHeight
442- ) ;
394+ drawCircle ( imgData , x + radius , y + radius , radius , this . canvasWidth , this . canvasHeight ) ;
443395 this . CTX . putImageData ( imgData , 0 , 0 ) ;
444396 break ;
445397 case "disc" :
446- drawDisc (
447- imgData ,
448- x + radius ,
449- y + radius ,
450- radius ,
451- this . canvasWidth ,
452- this . canvasHeight
453- ) ;
398+ drawDisc ( imgData , x + radius , y + radius , radius , this . canvasWidth , this . canvasHeight ) ;
454399 this . CTX . putImageData ( imgData , 0 , 0 ) ;
455400 break ;
456401 case "str" :
457402 const fontSize = fontSizes [ font ] ;
458403 this . CTX . font = `${ fontSize } px ${ font } ` ;
459404 this . CTX . fillText ( text , x , y ) ;
460405 break ;
461- case "pen " :
406+ case "draw " :
462407 const newImageData = maskAndMixImageData ( imgData , screenElement . imageData , x , y ) ;
463408 this . CTX . putImageData ( newImageData , 0 , 0 ) ;
464409 break ;
@@ -469,10 +414,17 @@ const fuiCanvasComponent = {
469414 this . CTX . restore ( ) ;
470415 } ,
471416 keyDownHandler ( event ) {
472- if ( event . isComposing || event . target !== document . body ) {
417+ if ( event . isComposing || ! Object . values ( KEYS ) . includes ( event . keyCode ) ) {
418+ return ;
419+ }
420+ if ( event . keyCode === KEYS . ESC ) {
421+ this . $emit ( "updateCurrentLayer" ) ;
422+ return ;
423+ }
424+ if ( event . target !== document . body ) {
473425 return ;
474426 }
475- if ( this . currentLayer && Object . values ( KEYS ) . includes ( event . keyCode ) ) {
427+ if ( this . currentLayer ) {
476428 event . preventDefault ( ) ;
477429 const shift = event . shiftKey ? 10 : 1 ;
478430 switch ( event . keyCode ) {
0 commit comments