Skip to content

Commit

Permalink
feat(elements): add throttledPointerMove method to Canvas class
Browse files Browse the repository at this point in the history
  • Loading branch information
Shreypatel13ll authored and christianliebel committed Feb 20, 2024
1 parent 972c223 commit 0a5cdbd
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/elements/canvas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export class Canvas extends LitElement {

private pointerDown = false;
private previewColor: 'primary' | 'secondary' = 'primary';
private lastPointerEventTime = 0;

static get styles(): CSSResultGroup {
return css`
Expand Down Expand Up @@ -167,15 +168,23 @@ export class Canvas extends LitElement {
clearCanvas(this.drawingContext);
this.drawingContext.document.dirty = false;
updateContext(this);
document.addEventListener('pointermove', (event) => this.throttledPointerMove(event));

document.addEventListener('pointermove', (event) =>
this.onPointerMove(event),
);
document.addEventListener('pointerup', (event) => this.onPointerUp(event));

this.dispatchEvent(
new CustomEvent('canvas-ready', { bubbles: true, composed: true }),
);

}

throttledPointerMove(event: PointerEvent): void {
const currentTime = Date.now();
if (currentTime - this.lastPointerEventTime < 8) { // Throttle mouse polling rate to ~125hz 1000/125 = 8
return;
}
this.lastPointerEventTime = currentTime;
this.onPointerMove(event);
}

getToolEventArgs(
Expand Down

0 comments on commit 0a5cdbd

Please sign in to comment.