Skip to content

Commit

Permalink
Chore: Reduce redundant point creation via buffering (#237)
Browse files Browse the repository at this point in the history
  • Loading branch information
JustinHoldstock authored and pramodsum committed Oct 22, 2018
1 parent afe6ab6 commit 2bfc2f1
Showing 1 changed file with 20 additions and 4 deletions.
24 changes: 20 additions & 4 deletions src/doc/DocDrawingThread.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ class DocDrawingThread extends DrawingThread {
/** @property {HTMLElement} - Page element being observed */
pageEl;

/** @property {boolean} - Whether or not to wait until next frame to create another point in the drawing */
isBuffering = false;

//--------------------------------------------------------------------------
// Public
//--------------------------------------------------------------------------
Expand All @@ -31,6 +34,15 @@ class DocDrawingThread extends DrawingThread {
this.reconstructBrowserCoordFromLocation = this.reconstructBrowserCoordFromLocation.bind(this);
}

/**
* Toggles current buffering state.
*
* @return {void}
*/
toggleBufferingState = () => {
this.isBuffering = !this.isBuffering;
};

/**
* Handle a pointer movement
*
Expand All @@ -48,11 +60,15 @@ class DocDrawingThread extends DrawingThread {
return;
}

const [x, y] = getBrowserCoordinatesFromLocation(location, this.pageEl);
const browserLocation = createLocation(x, y);

if (this.pendingPath) {
// If the current path is being buffered, don't create redundant points
if (!this.isBuffering && this.pendingPath) {
const [x, y] = getBrowserCoordinatesFromLocation(location, this.pageEl);
const browserLocation = createLocation(x, y);
this.pendingPath.addCoordinate(location, browserLocation);

// On next browser frame, reset to allow for another point to be added to the path
this.toggleBufferingState();
window.requestAnimationFrame(this.toggleBufferingState);
}
}

Expand Down

0 comments on commit 2bfc2f1

Please sign in to comment.