@@ -100,7 +100,6 @@ class Parameter {
100100
101101class ImageLayer {
102102 static drawFlag = false ;
103- static cropFlag = false ;
104103 static dragFlag = false ;
105104
106105 constructor ( ) {
@@ -164,10 +163,35 @@ class ImageLayer {
164163
165164 document . addEventListener ( "keydown" , ( event ) => {
166165 if (
167- ( event . ctrlKey && event . key === "d" ) ||
168- ( event . key === "Delete" && document . activeElement === this . imgPanel )
166+ ( event . ctrlKey && event . key === "d" ) ||
167+ ( event . key === "Delete" && document . activeElement === this . imgPanel )
169168 ) {
170- deleteImagePanel ( ) ;
169+ deleteImagePanel ( ) ;
170+ } else if (
171+ document . activeElement === this . imgPanel &&
172+ ( event . key === "ArrowLeft" || event . key === "ArrowRight" )
173+ ) {
174+ if ( event . ctrlKey && event . key === "ArrowLeft" ) {
175+ Parameter . num = 0 ;
176+ imageLayerQueue [ Parameter . num ] . imgPanel . focus ( ) ;
177+ imageLayerQueue [ Parameter . num ] . updateFocus ( ) ;
178+ imageLayerQueue [ Parameter . num ] . updateSio ( ) ;
179+ } else if ( event . ctrlKey && event . key === "ArrowRight" ) {
180+ Parameter . num = imageLayerQueue . length - 1 ;
181+ imageLayerQueue [ Parameter . num ] . imgPanel . focus ( ) ;
182+ imageLayerQueue [ Parameter . num ] . updateFocus ( ) ;
183+ imageLayerQueue [ Parameter . num ] . updateSio ( ) ;
184+ } else if ( event . key === "ArrowLeft" && Parameter . num > 0 ) {
185+ Parameter . num -- ;
186+ imageLayerQueue [ Parameter . num ] . imgPanel . focus ( ) ;
187+ imageLayerQueue [ Parameter . num ] . updateFocus ( ) ;
188+ imageLayerQueue [ Parameter . num ] . updateSio ( ) ;
189+ } else if ( event . key === "ArrowRight" && Parameter . num < imageLayerQueue . length - 1 ) {
190+ Parameter . num ++ ;
191+ imageLayerQueue [ Parameter . num ] . imgPanel . focus ( ) ;
192+ imageLayerQueue [ Parameter . num ] . updateFocus ( ) ;
193+ imageLayerQueue [ Parameter . num ] . updateSio ( ) ;
194+ }
171195 }
172196 } ) ;
173197
@@ -337,7 +361,7 @@ class ImageLayer {
337361 event . clientY - this . canvas . getBoundingClientRect ( ) . top
338362 ) ;
339363 this . canvas . addEventListener ( "mousemove" , ( evt ) => {
340- if ( ImageLayer . dragFlag && ImageLayer . drawFlag ) {
364+ if ( ImageLayer . dragFlag ) {
341365 this . ctx . lineTo (
342366 evt . x - this . canvas . getBoundingClientRect ( ) . left ,
343367 evt . y - this . canvas . getBoundingClientRect ( ) . top
@@ -348,40 +372,66 @@ class ImageLayer {
348372 this . ctx . closePath ( ) ;
349373 }
350374
351- if ( ImageLayer . cropFlag ) {
375+ if ( document . body . style . cursor === "crosshair" ) {
352376 var ctxs = this . canvas . getContext ( "2d" ) ;
353- this . realPosX = event . clientX ;
354- this . realPosY = event . clientY ;
355- this . initialX =
356- event . clientX - this . canvas . getBoundingClientRect ( ) . left ;
357- this . initialY = event . clientY - this . canvas . getBoundingClientRect ( ) . top ;
377+ const rect = this . canvas . getBoundingClientRect ( ) ;
378+ const startX = event . clientX - rect . left ;
379+ const startY = event . clientY - rect . top ;
380+ this . initialX = startX ;
381+ this . initialY = startY ;
358382 ctxs . setLineDash ( [ 2 ] ) ;
359383
360- this . canvas . addEventListener ( "mousemove" , ( evt ) => {
361- if ( ImageLayer . dragFlag && ImageLayer . cropFlag ) {
362- ctxs . clearRect (
363- 0 ,
364- 0 ,
365- this . canvas . clientWidth ,
366- this . canvas . clientHeight
367- ) ;
368- this . cropWidth = evt . clientX - event . clientX ;
369- this . cropHeight = evt . clientY - event . clientY ;
370- ctxs . drawImage ( this . image , 0 , 0 ) ;
371- ctxs . strokeRect (
372- this . initialX ,
373- this . initialY ,
374- this . cropWidth ,
375- this . cropHeight
376- ) ;
377- }
378- } ) ;
384+ const mouseMoveHandler = ( evt ) => {
385+ ctxs . clearRect ( 0 , 0 , this . canvas . clientWidth , this . canvas . clientHeight ) ;
386+ ctxs . drawImage ( this . image , 0 , 0 ) ;
387+ const currX = evt . clientX - rect . left ;
388+ const currY = evt . clientY - rect . top ;
389+ // Calculate top-left and width/height regardless of drag direction
390+ const x = Math . min ( startX , currX ) ;
391+ const y = Math . min ( startY , currY ) ;
392+ const w = Math . abs ( currX - startX ) ;
393+ const h = Math . abs ( currY - startY ) ;
394+ this . initialX = x ;
395+ this . initialY = y ;
396+ this . cropWidth = w ;
397+ this . cropHeight = h ;
398+ ctxs . strokeRect ( x , y , w , h ) ;
399+ } ;
400+ const mouseUpHandler = ( evt ) => {
401+ this . canvas . removeEventListener ( "mousemove" , mouseMoveHandler ) ;
402+ document . removeEventListener ( "mouseup" , mouseUpHandler ) ;
403+ ImageLayer . dragFlag = false ;
404+ } ;
405+ this . canvas . addEventListener ( "mousemove" , mouseMoveHandler ) ;
406+ document . addEventListener ( "mouseup" , mouseUpHandler ) ;
379407 }
380408 } ) ;
381409
382410 this . canvas . addEventListener ( "mouseup" , ( event ) => {
383- if ( ImageLayer . drawFlag ) {
384- //paste
411+ ImageLayer . dragFlag = false ;
412+ if ( document . body . style . cursor === "crosshair" ) {
413+ // cropWidth/cropHeight는 항상 양수, initialX/initialY는 항상 좌상단
414+ const cropX = Math . round ( this . initialX ) ;
415+ const cropY = Math . round ( this . initialY ) ;
416+ const cropW = Math . round ( this . cropWidth ) ;
417+ const cropH = Math . round ( this . cropHeight ) ;
418+
419+ if (
420+ cropW > 0 &&
421+ cropH > 0 &&
422+ this . buffer &&
423+ this . information &&
424+ ( cropX + cropW ) <= this . information . width &&
425+ ( cropY + cropH ) <= this . information . height
426+ ) {
427+ sharp ( this . buffer )
428+ . extract ( { left : cropX , top : cropY , width : cropW , height : cropH } )
429+ . toBuffer ( ( err , buf , info ) => {
430+ if ( ! err && buf && info ) {
431+ this . updatePreviewImg ( buf , info ) ;
432+ }
433+ } ) ;
434+ }
385435 }
386436 } ) ;
387437
@@ -793,7 +843,6 @@ module.exports = {
793843 ImageLayer : ImageLayer ,
794844 Parameter : Parameter ,
795845 drawFlag : ImageLayer . drawFlag ,
796- cropFlag : ImageLayer . cropFlag ,
797846 dragFlag : ImageLayer . dragFlag ,
798847 imageLayerQueue : imageLayerQueue ,
799848} ;
0 commit comments