1
1
class RectangularSelectionTool extends SelectionTool {
2
-
3
- constructor ( name , options , switchFunc , moveTool ) {
2
+ constructor ( name , options , switchFunc , moveTool ) {
4
3
super ( name , options , switchFunc , moveTool ) ;
5
4
Events . on ( 'click' , this . mainButton , switchFunc , this ) ;
6
5
6
+ // Tutorial setup
7
7
this . resetTutorial ( ) ;
8
8
this . addTutorialTitle ( "Rectangular selection tool" ) ;
9
9
this . addTutorialKey ( "M" , " to select the rectangular selection tool" ) ;
@@ -20,82 +20,79 @@ class RectangularSelectionTool extends SelectionTool {
20
20
onStart ( mousePos , mouseTarget ) {
21
21
super . onStart ( mousePos , mouseTarget ) ;
22
22
23
+ // Validate initial position within the canvas
23
24
if ( Util . isChildOfByClass ( mouseTarget , "editor-top-menu" ) ||
24
- ! Util . cursorInCanvas ( currFile . canvasSize , [ mousePos [ 0 ] / currFile . zoom , mousePos [ 1 ] / currFile . zoom ] ) )
25
+ ! Util . cursorInCanvas ( currFile . canvasSize , [ mousePos [ 0 ] / currFile . zoom , mousePos [ 1 ] / currFile . zoom ] ) )
25
26
return ;
26
27
27
- // Avoiding external selections
28
- if ( this . startMousePos [ 0 ] < 0 ) {
29
- this . startMousePos [ 0 ] = 0 ;
30
- }
31
- else if ( this . startMousePos [ 0 ] > currFile . currentLayer . canvas . width ) {
32
- this . startMousePos [ 0 ] = currFile . currentLayer . canvas . width ;
33
- }
28
+ // Constrain start position to canvas boundaries
29
+ this . startMousePos [ 0 ] = Math . max ( 0 , Math . min ( this . startMousePos [ 0 ] , currFile . currentLayer . canvas . width ) ) ;
30
+ this . startMousePos [ 1 ] = Math . max ( 0 , Math . min ( this . startMousePos [ 1 ] , currFile . currentLayer . canvas . height ) ) ;
34
31
35
- if ( this . startMousePos [ 1 ] < 0 ) {
36
- this . startMousePos [ 1 ] = 0 ;
37
- }
38
- else if ( this . startMousePos [ 1 ] > currFile . currentLayer . canvas . height ) {
39
- this . startMousePos [ 1 ] = currFile . currentLayer . canvas . height ;
40
- }
32
+ // Initialize the endMousePos to startMousePos to draw an initial dot
33
+ this . endMousePos = [ ...this . startMousePos ] ;
41
34
42
- // Drawing the rect
43
- this . drawSelection ( this . startMousePos [ 0 ] , this . startMousePos [ 1 ] ) ;
35
+ // Draw the initial selection rectangle
36
+ this . drawSelection ( ) ;
44
37
}
45
38
46
39
onDrag ( mousePos , mouseTarget ) {
47
40
super . onDrag ( mousePos , mouseTarget ) ;
48
41
42
+ // Validate drag position within the canvas
49
43
if ( Util . isChildOfByClass ( mouseTarget , "editor-top-menu" ) ||
50
- ! Util . cursorInCanvas ( currFile . canvasSize , [ mousePos [ 0 ] / currFile . zoom , mousePos [ 1 ] / currFile . zoom ] ) )
44
+ ! Util . cursorInCanvas ( currFile . canvasSize , [ mousePos [ 0 ] / currFile . zoom , mousePos [ 1 ] / currFile . zoom ] ) )
51
45
return ;
52
46
53
- // Drawing the rect
54
- this . endMousePos = [ Math . floor ( mousePos [ 0 ] / currFile . zoom ) , Math . floor ( mousePos [ 1 ] / currFile . zoom ) ] ;
55
- this . drawSelection ( Math . floor ( mousePos [ 0 ] / currFile . zoom ) , Math . floor ( mousePos [ 1 ] / currFile . zoom ) ) ;
47
+ // Update the end position with precise rounding
48
+ this . endMousePos = [ Math . round ( mousePos [ 0 ] / currFile . zoom ) , Math . round ( mousePos [ 1 ] / currFile . zoom ) ] ;
49
+
50
+ // Draw the updated selection rectangle
51
+ this . drawSelection ( ) ;
56
52
}
57
53
58
54
onEnd ( mousePos , mouseTarget ) {
59
55
super . onEnd ( mousePos , mouseTarget ) ;
60
-
56
+
61
57
if ( Util . isChildOfByClass ( mouseTarget , "editor-top-menu" ) )
62
58
return ;
63
59
64
60
new HistoryState ( ) . EditCanvas ( ) ;
65
61
66
- // Getting the end position
67
- this . endMousePos = [ Math . floor ( mousePos [ 0 ] / currFile . zoom ) , Math . floor ( mousePos [ 1 ] / currFile . zoom ) ] ;
62
+ // Finalize the end position with precise rounding
63
+ this . endMousePos = [ Math . round ( mousePos [ 0 ] / currFile . zoom ) , Math . round ( mousePos [ 1 ] / currFile . zoom ) ] ;
68
64
69
- // Inverting end and start (start must always be the top left corner)
65
+ // Ensure startMousePos is top-left and endMousePos is bottom-right
70
66
if ( this . endMousePos [ 0 ] < this . startMousePos [ 0 ] ) {
71
- let tmp = this . endMousePos [ 0 ] ;
72
- this . endMousePos [ 0 ] = this . startMousePos [ 0 ] ;
73
- this . startMousePos [ 0 ] = tmp ;
67
+ [ this . startMousePos [ 0 ] , this . endMousePos [ 0 ] ] = [ this . endMousePos [ 0 ] , this . startMousePos [ 0 ] ] ;
74
68
}
75
- // Same for the y
76
69
if ( this . endMousePos [ 1 ] < this . startMousePos [ 1 ] ) {
77
- let tmp = this . endMousePos [ 1 ] ;
78
- this . endMousePos [ 1 ] = this . startMousePos [ 1 ] ;
79
- this . startMousePos [ 1 ] = tmp ;
70
+ [ this . startMousePos [ 1 ] , this . endMousePos [ 1 ] ] = [ this . endMousePos [ 1 ] , this . startMousePos [ 1 ] ] ;
80
71
}
81
72
82
- if ( Util . cursorInCanvas ( currFile . canvasSize , [ mousePos [ 0 ] / currFile . zoom , mousePos [ 1 ] / currFile . zoom ] ) ) {
83
- this . boundingBox . minX = this . startMousePos [ 0 ] - 1 ;
84
- this . boundingBox . maxX = this . endMousePos [ 0 ] + 1 ;
85
- this . boundingBox . minY = this . startMousePos [ 1 ] - 1 ;
86
- this . boundingBox . maxY = this . endMousePos [ 1 ] + 1 ;
73
+ // Set the bounding box exactly within the selection without expanding
74
+ if ( Util . cursorInCanvas ( currFile . canvasSize , [ mousePos [ 0 ] / currFile . zoom , mousePos [ 1 ] / currFile . zoom ] ) ) {
75
+ this . boundingBox . minX = this . startMousePos [ 0 ] ;
76
+ this . boundingBox . maxX = this . endMousePos [ 0 ] ;
77
+ this . boundingBox . minY = this . startMousePos [ 1 ] ;
78
+ this . boundingBox . maxY = this . endMousePos [ 1 ] ;
87
79
}
88
80
89
- // Switch to the move tool so that the user can move the selection
81
+ // Switch to the move tool to move the selection
90
82
this . switchFunc ( this . moveTool ) ;
91
- // Obtain the selected pixels
92
83
this . moveTool . setSelectionData ( this . getSelection ( ) , this ) ;
93
84
}
94
85
95
86
cutSelection ( ) {
96
87
super . cutSelection ( ) ;
97
- currFile . currentLayer . context . clearRect ( this . currSelection . left - 0.5 , this . currSelection . top - 0.5 ,
98
- this . currSelection . width , this . currSelection . height ) ;
88
+
89
+ // Clear the selected area without fractional offsets
90
+ currFile . currentLayer . context . clearRect (
91
+ this . currSelection . left ,
92
+ this . currSelection . top ,
93
+ this . currSelection . width ,
94
+ this . currSelection . height
95
+ ) ;
99
96
}
100
97
101
98
onSelect ( ) {
@@ -107,15 +104,22 @@ class RectangularSelectionTool extends SelectionTool {
107
104
}
108
105
109
106
drawSelection ( ) {
110
- // Getting the vfx context
107
+ // Access the VFX context for visualizing the selection rectangle
111
108
let vfxContext = currFile . VFXLayer . context ;
112
-
113
- // Clearing the vfx canvas
109
+
110
+ // Clear the VFX canvas to ensure no previous selection visuals are visible
114
111
vfxContext . clearRect ( 0 , 0 , currFile . VFXLayer . canvas . width , currFile . VFXLayer . canvas . height ) ;
115
112
116
- currFile . VFXLayer . drawLine ( this . startMousePos [ 0 ] , this . startMousePos [ 1 ] , this . endMousePos [ 0 ] , this . startMousePos [ 1 ] , 1 ) ;
117
- currFile . VFXLayer . drawLine ( this . endMousePos [ 0 ] , this . startMousePos [ 1 ] , this . endMousePos [ 0 ] , this . endMousePos [ 1 ] , 1 ) ;
118
- currFile . VFXLayer . drawLine ( this . endMousePos [ 0 ] , this . endMousePos [ 1 ] , this . startMousePos [ 0 ] , this . endMousePos [ 1 ] , 1 ) ;
119
- currFile . VFXLayer . drawLine ( this . startMousePos [ 0 ] , this . endMousePos [ 1 ] , this . startMousePos [ 0 ] , this . startMousePos [ 1 ] , 1 ) ;
113
+ // Draw the selection rectangle with precise lines
114
+ if ( this . startMousePos && this . endMousePos ) {
115
+ vfxContext . strokeStyle = 'rgba(0, 0, 255, 1)' ; // Example color for the rectangle
116
+ vfxContext . lineWidth = 1 ;
117
+ vfxContext . strokeRect (
118
+ Math . min ( this . startMousePos [ 0 ] , this . endMousePos [ 0 ] ) ,
119
+ Math . min ( this . startMousePos [ 1 ] , this . endMousePos [ 1 ] ) ,
120
+ Math . abs ( this . endMousePos [ 0 ] - this . startMousePos [ 0 ] ) ,
121
+ Math . abs ( this . endMousePos [ 1 ] - this . startMousePos [ 1 ] )
122
+ ) ;
123
+ }
120
124
}
121
- }
125
+ }
0 commit comments