Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chore: Reduce redundant point creation via buffering #237

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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