Skip to content

Commit 386839a

Browse files
author
HarshKhandeparkar
committed
fix: mouseup event listener displays pixels
1 parent 4e846e6 commit 386839a

File tree

1 file changed

+8
-11
lines changed

1 file changed

+8
-11
lines changed

src/renderers/RealDrawBoard/RealDrawBoard.ts

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ export class RealDrawBoard extends RealRenderer {
5050
* For mouse, the key is 'mouse', for touches, stringified identifier -> https://developer.mozilla.org/en-US/docs/Web/API/Touch/identifier
5151
*/
5252
_lastCoords: Map<string, [number, number]> = new Map(); /* key -> identifier, value -> coordinate*/
53+
_frameHandler: () => void; // requestAnimationFrame handler
5354

5455
protected _initializeKernels = _initializeKernels;
5556
protected _stroke = _stroke;
@@ -95,12 +96,12 @@ export class RealDrawBoard extends RealRenderer {
9596
this._initializeKernels();
9697
if (this._maxSnapshots > 0) this._snapshots[0] = this.getData();
9798

98-
const frameHandler = () => {
99-
if (this._isStroking) this._display(this.graphPixels);
100-
window.requestAnimationFrame(frameHandler);
99+
this._frameHandler = () => {
100+
if (this._isDrawing) {
101+
if (this._isStroking) this._display(this.graphPixels);
102+
window.requestAnimationFrame(this._frameHandler);
103+
}
101104
}
102-
103-
window.requestAnimationFrame(frameHandler);
104105
}
105106
// --- DOM Event Listeners ---
106107

@@ -121,6 +122,7 @@ export class RealDrawBoard extends RealRenderer {
121122
if (e.button === 0 /* Left Click */) {
122123
const endCoords = this._getMouseCoords(e);
123124
if(this._lastCoords.has('mouse')) this._endStroke(endCoords, 'mouse');
125+
this._display(this.graphPixels);
124126

125127
this._isStroking = false;
126128
this.canvas.removeEventListener('mousemove', this._mouseMoveEventListener);
@@ -170,8 +172,6 @@ export class RealDrawBoard extends RealRenderer {
170172
}
171173

172174
_touchEndEventListener = (e: TouchEvent) => {
173-
e.preventDefault();
174-
175175
for (let i = 0; i < e.changedTouches.length; i++) {
176176
this._endStroke(
177177
this._getTouchCoords(e.changedTouches.item(i)),
@@ -183,8 +183,6 @@ export class RealDrawBoard extends RealRenderer {
183183
}
184184

185185
_touchMoveEventListener = (e: TouchEvent) => {
186-
e.preventDefault();
187-
188186
for (let i = 0; i < e.touches.length; i++) {
189187
this._doStroke(
190188
this._getTouchCoords(e.touches.item(i)),
@@ -194,8 +192,6 @@ export class RealDrawBoard extends RealRenderer {
194192
}
195193

196194
_previewTouchMoveEventListener = (e: TouchEvent) => {
197-
e.preventDefault();
198-
199195
for (let i = 0; i < e.touches.length; i++) {
200196
// if (!this._isStroking) {
201197
// this._display(
@@ -211,6 +207,7 @@ export class RealDrawBoard extends RealRenderer {
211207
startRender() {
212208
this._addDOMEvents();
213209
this._isDrawing = true;
210+
window.requestAnimationFrame(this._frameHandler);
214211

215212
return this;
216213
}

0 commit comments

Comments
 (0)