Skip to content

Commit d53e761

Browse files
author
HarshKhandeparkar
committed
refactor: move _lastCoords to the DOM events
1 parent b633229 commit d53e761

File tree

4 files changed

+26
-23
lines changed

4 files changed

+26
-23
lines changed

src/renderers/RealDrawBoard/RealDrawBoard.ts

Lines changed: 26 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -104,11 +104,13 @@ export class RealDrawBoard extends RealRenderer {
104104
this.canvas.addEventListener('mousemove', this._mouseMoveEventListener);
105105

106106
if (this._currentSnapshotIndex < this._snapshots.length - 1 && this._maxSnapshots > 0) this._snapshots.splice(this._currentSnapshotIndex + 1); // Delete all redo snapshots
107+
const coords = this._getMouseCoords(e);
107108

108109
this._startStroke(
109-
this._getMouseCoords(e),
110+
coords,
110111
'mouse'
111112
)
113+
this._lastCoords.set('mouse', coords);
112114
}
113115
}
114116

@@ -118,7 +120,10 @@ export class RealDrawBoard extends RealRenderer {
118120
this.canvas.removeEventListener('mousemove', this._mouseMoveEventListener);
119121
this._removeDOMEvents();
120122

121-
if(this._lastCoords.has('mouse')) this._endStroke(endCoords, 'mouse');
123+
if(this._lastCoords.has('mouse')) {
124+
this._endStroke(endCoords, 'mouse');
125+
this._lastCoords.delete('mouse');
126+
}
122127

123128
this._display(this.graphPixels);
124129

@@ -140,6 +145,7 @@ export class RealDrawBoard extends RealRenderer {
140145
if(this._lastCoords.has('mouse')) {
141146
this._removeDOMEvents();
142147
this._endStroke(this._getMouseCoords(e), 'mouse');
148+
this._lastCoords.delete('mouse');
143149
this._display(this.graphPixels);
144150

145151
setTimeout(() => { // Delay to let the canvas 'settle'
@@ -157,6 +163,7 @@ export class RealDrawBoard extends RealRenderer {
157163
_mouseMoveEventListener = (e: MouseEvent) => {
158164
const coords = this._getMouseCoords(e);
159165
this._doStroke(coords, 'mouse');
166+
this._lastCoords.set('mouse', coords);
160167
}
161168

162169
_previewMouseMoveEventListener = (e: MouseEvent) => {
@@ -176,17 +183,26 @@ export class RealDrawBoard extends RealRenderer {
176183
for (let i = 0; i < e.touches.length; i++) {
177184
if (this._currentSnapshotIndex < this._snapshots.length - 1 && this._maxSnapshots > 0) this._snapshots.splice(this._currentSnapshotIndex + 1); // Delete all redo snapshots
178185

186+
const coords = this._getTouchCoords(e.touches.item(i));
179187
this._startStroke(
180-
this._getTouchCoords(e.touches.item(i)),
188+
coords,
181189
e.touches.item(i).identifier.toString()
182190
)
191+
this._lastCoords.set(
192+
e.touches.item(i).identifier.toString(),
193+
coords
194+
)
183195
}
184196
}
185197

186198
_touchEndEventListener = (e: TouchEvent) => {
187199
for (let i = 0; i < e.changedTouches.length; i++) {
200+
const coords = this._getTouchCoords(e.changedTouches.item(i));
188201
this._endStroke(
189-
this._getTouchCoords(e.changedTouches.item(i)),
202+
coords,
203+
e.changedTouches.item(i).identifier.toString()
204+
)
205+
this._lastCoords.delete(
190206
e.changedTouches.item(i).identifier.toString()
191207
)
192208
}
@@ -204,10 +220,15 @@ export class RealDrawBoard extends RealRenderer {
204220

205221
_touchMoveEventListener = (e: TouchEvent) => {
206222
for (let i = 0; i < e.touches.length; i++) {
223+
const coords = this._getTouchCoords(e.touches.item(i));
207224
this._doStroke(
208-
this._getTouchCoords(e.touches.item(i)),
225+
coords,
209226
e.touches.item(i).identifier.toString()
210227
)
228+
this._lastCoords.set(
229+
e.touches.item(i).identifier.toString(),
230+
coords
231+
)
211232
}
212233
}
213234

src/renderers/RealDrawBoard/tools/brush.ts

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export function _startStroke(
1010
) {
1111
this._doPreview = false;
1212
this._plot(coords[0], coords[1], this.brushSize, this.brushColor);
13-
14-
this._lastCoords.set(identifier, coords);
1513
}
1614

1715
export function _endStroke(
@@ -20,9 +18,6 @@ export function _endStroke(
2018
identifier: string
2119
) {
2220
this._plot(endCoords[0], endCoords[1], this.brushSize, this.brushColor);
23-
24-
this._lastCoords.delete(identifier);
25-
2621
this._doPreview = true;
2722
}
2823

@@ -33,8 +28,6 @@ export function _doStroke(
3328
) {
3429
this._plot(coords[0], coords[1], this.brushSize, this.brushColor);
3530
this._stroke(coords[0], coords[1], this.brushSize, this.brushColor, identifier);
36-
37-
this._lastCoords.set(identifier, coords);
3831
}
3932

4033
export function _toolPreview(

src/renderers/RealDrawBoard/tools/eraser.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ export function _startStroke(
1010
) {
1111
this._doPreview = false;
1212
this._plot(coords[0], coords[1], this.eraserSize, this.bgColor);
13-
14-
this._lastCoords.set(identifier, coords);
1513
}
1614

1715
export function _endStroke(
@@ -21,8 +19,6 @@ export function _endStroke(
2119
) {
2220
this._doPreview = true;
2321
this._plot(endCoords[0], endCoords[1], this.eraserSize, this.bgColor);
24-
25-
this._lastCoords.delete(identifier);
2622
}
2723

2824
export function _doStroke(
@@ -32,8 +28,6 @@ export function _doStroke(
3228
) {
3329
this._plot(coords[0], coords[1], this.eraserSize, this.bgColor);
3430
this._stroke(coords[0], coords[1], this.eraserSize, this.bgColor, identifier);
35-
36-
this._lastCoords.set(identifier, coords);
3731
}
3832

3933
export function _toolPreview(

src/renderers/RealDrawBoard/tools/line.ts

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,6 @@ export function _startStroke(
1414
identifier: string
1515
) {
1616
this._plot(coords[0], coords[1], this.brushSize, this.brushColor);
17-
18-
this._lastCoords.set(identifier, coords);
1917
_startCoords.set(identifier, coords);
2018
}
2119

@@ -32,8 +30,6 @@ export function _endStroke(
3230
this.brushColor
3331
)
3432
this._plot(endCoords[0], endCoords[1], this.brushSize, this.brushColor);
35-
36-
this._lastCoords.delete(identifier);
3733
_startCoords.delete(identifier);
3834
}
3935

@@ -42,7 +38,6 @@ export function _doStroke(
4238
coords: [number, number],
4339
identifier: string
4440
) {
45-
this._lastCoords.set(identifier, coords);
4641
}
4742

4843
export function _toolPreview(

0 commit comments

Comments
 (0)