Skip to content

Commit c8169ef

Browse files
authored
constraint bitmap brush
1 parent a118b14 commit c8169ef

1 file changed

Lines changed: 22 additions & 5 deletions

File tree

src/helper/bit-tools/brush-tool.js

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import paper from '@turbowarp/paper';
22
import {getRaster, getGuideLayer, createCanvas} from '../layer';
33
import {doesColorRequireMask, forEachLinePoint, getBrushMark} from '../bitmap';
4+
import {snapDeltaToAngle} from '../math';
45

56
/**
67
* Tool for drawing with the bitmap brush and eraser
@@ -87,6 +88,19 @@ class BrushTool extends paper.Tool {
8788
this.lastSize = this.size;
8889
this.lastColor = this.color;
8990
}
91+
constrainPoint(delta, lastPoint, modifiers) {
92+
if (modifiers.shift) {
93+
// 45 degree movement
94+
delta = snapDeltaToAngle(delta, Math.PI / 4);
95+
} else if (modifiers.alt) {
96+
// vertical movement
97+
delta = new paper.Point(0, delta.y);
98+
} else if (modifiers.control || modifiers.meta) {
99+
// horizontal movement
100+
delta = new paper.Point(delta.x, 0);
101+
}
102+
return lastPoint.add(delta);
103+
}
90104
handleMouseMove (event) {
91105
this.updateCursorIfNeeded();
92106
this.cursorPreview.position = new paper.Point(~~event.point.x, ~~event.point.y);
@@ -115,19 +129,22 @@ class BrushTool extends paper.Tool {
115129
}
116130
}
117131

118-
this.drawNextLine(event.point, event.point);
119-
this.lastPoint = event.point;
132+
const point = this.constrainPoint(event.delta, event.point, event.modifiers);
133+
this.drawNextLine(point, point);
134+
this.lastPoint = point;
120135
}
121136
handleMouseDrag (event) {
122137
if (event.event.button > 0 || !this.active) return; // only first mouse button
123138

124-
this.drawNextLine(this.lastPoint, event.point);
125-
this.lastPoint = event.point;
139+
const point = this.constrainPoint(event.delta, event.point, event.modifiers);
140+
this.drawNextLine(this.lastPoint, point);
141+
this.lastPoint = point;
126142
}
127143
handleMouseUp (event) {
128144
if (event.event.button > 0 || !this.active) return; // only first mouse button
129145

130-
this.drawNextLine(this.lastPoint, event.point);
146+
const point = this.constrainPoint(event.delta, event.point, event.modifiers);
147+
this.drawNextLine(this.lastPoint, point);
131148
if (!this.isEraser) {
132149
getRaster().drawImage(this.drawTarget.canvas, new paper.Point(0, 0));
133150
this.drawTarget.remove();

0 commit comments

Comments
 (0)