Skip to content

Commit 9463d7e

Browse files
Update RectangularSelectionTool.js
Fixed issue lospec#120
1 parent 8fcc50b commit 9463d7e

File tree

1 file changed

+54
-50
lines changed

1 file changed

+54
-50
lines changed

js/tools/RectangularSelectionTool.js

+54-50
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
class RectangularSelectionTool extends SelectionTool {
2-
3-
constructor (name, options, switchFunc, moveTool) {
2+
constructor(name, options, switchFunc, moveTool) {
43
super(name, options, switchFunc, moveTool);
54
Events.on('click', this.mainButton, switchFunc, this);
65

6+
// Tutorial setup
77
this.resetTutorial();
88
this.addTutorialTitle("Rectangular selection tool");
99
this.addTutorialKey("M", " to select the rectangular selection tool");
@@ -20,82 +20,79 @@ class RectangularSelectionTool extends SelectionTool {
2020
onStart(mousePos, mouseTarget) {
2121
super.onStart(mousePos, mouseTarget);
2222

23+
// Validate initial position within the canvas
2324
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]))
2526
return;
2627

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));
3431

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];
4134

42-
// Drawing the rect
43-
this.drawSelection(this.startMousePos[0], this.startMousePos[1]);
35+
// Draw the initial selection rectangle
36+
this.drawSelection();
4437
}
4538

4639
onDrag(mousePos, mouseTarget) {
4740
super.onDrag(mousePos, mouseTarget);
4841

42+
// Validate drag position within the canvas
4943
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]))
5145
return;
5246

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();
5652
}
5753

5854
onEnd(mousePos, mouseTarget) {
5955
super.onEnd(mousePos, mouseTarget);
60-
56+
6157
if (Util.isChildOfByClass(mouseTarget, "editor-top-menu"))
6258
return;
6359

6460
new HistoryState().EditCanvas();
6561

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)];
6864

69-
// Inverting end and start (start must always be the top left corner)
65+
// Ensure startMousePos is top-left and endMousePos is bottom-right
7066
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]];
7468
}
75-
// Same for the y
7669
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]];
8071
}
8172

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];
8779
}
8880

89-
// Switch to the move tool so that the user can move the selection
81+
// Switch to the move tool to move the selection
9082
this.switchFunc(this.moveTool);
91-
// Obtain the selected pixels
9283
this.moveTool.setSelectionData(this.getSelection(), this);
9384
}
9485

9586
cutSelection() {
9687
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+
);
9996
}
10097

10198
onSelect() {
@@ -107,15 +104,22 @@ class RectangularSelectionTool extends SelectionTool {
107104
}
108105

109106
drawSelection() {
110-
// Getting the vfx context
107+
// Access the VFX context for visualizing the selection rectangle
111108
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
114111
vfxContext.clearRect(0, 0, currFile.VFXLayer.canvas.width, currFile.VFXLayer.canvas.height);
115112

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+
}
120124
}
121-
}
125+
}

0 commit comments

Comments
 (0)