|
1 | 1 | import paper from '@turbowarp/paper'; |
2 | 2 | import {getRaster, getGuideLayer, createCanvas} from '../layer'; |
3 | 3 | import {doesColorRequireMask, forEachLinePoint, getBrushMark} from '../bitmap'; |
| 4 | +import {snapDeltaToAngle} from '../math'; |
4 | 5 |
|
5 | 6 | /** |
6 | 7 | * Tool for drawing with the bitmap brush and eraser |
@@ -87,6 +88,19 @@ class BrushTool extends paper.Tool { |
87 | 88 | this.lastSize = this.size; |
88 | 89 | this.lastColor = this.color; |
89 | 90 | } |
| 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 | + } |
90 | 104 | handleMouseMove (event) { |
91 | 105 | this.updateCursorIfNeeded(); |
92 | 106 | this.cursorPreview.position = new paper.Point(~~event.point.x, ~~event.point.y); |
@@ -115,19 +129,22 @@ class BrushTool extends paper.Tool { |
115 | 129 | } |
116 | 130 | } |
117 | 131 |
|
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; |
120 | 135 | } |
121 | 136 | handleMouseDrag (event) { |
122 | 137 | if (event.event.button > 0 || !this.active) return; // only first mouse button |
123 | 138 |
|
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; |
126 | 142 | } |
127 | 143 | handleMouseUp (event) { |
128 | 144 | if (event.event.button > 0 || !this.active) return; // only first mouse button |
129 | 145 |
|
130 | | - this.drawNextLine(this.lastPoint, event.point); |
| 146 | + const point = this.constrainPoint(event.delta, event.point, event.modifiers); |
| 147 | + this.drawNextLine(this.lastPoint, point); |
131 | 148 | if (!this.isEraser) { |
132 | 149 | getRaster().drawImage(this.drawTarget.canvas, new paper.Point(0, 0)); |
133 | 150 | this.drawTarget.remove(); |
|
0 commit comments