Skip to content

Commit

Permalink
Use dynamic pointer movement throttling to improve ink/eraser resolution
Browse files Browse the repository at this point in the history
  • Loading branch information
mrtcode committed Sep 11, 2023
1 parent fe277a7 commit a9b846b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions src/common/lib/utilities.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,32 @@ export function setCaretToEnd(target) {
// as much as it can, without ever going more than once per `wait` duration;
// but if you'd like to disable the execution on the leading edge, pass
// `{leading: false}`. To disable execution on the trailing edge, ditto.
// Note: The function is modified to support dynamic wait by accept a function as wait argument
export function throttle(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};

// Helper function to get the wait time dynamically
var getWaitTime = function() {
return typeof wait === 'function' ? wait() : wait;
};

var later = function () {
previous = options.leading === false ? 0 : Date.now();
timeout = null;
result = func.apply(context, args);
if (!timeout) context = args = null;
};

return function () {
var now = Date.now();
if (!previous && options.leading === false) previous = now;
var remaining = wait - (now - previous);
var remaining = getWaitTime() - (now - previous); // Use the helper function
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (remaining <= 0 || remaining > getWaitTime()) { // Use the helper function
if (timeout) {
clearTimeout(timeout);
timeout = null;
Expand Down
2 changes: 1 addition & 1 deletion src/pdf/pdf-view.js
Original file line number Diff line number Diff line change
Expand Up @@ -1915,7 +1915,7 @@ class PDFView {
this._onSetSelectionPopup();
}
this._render();
}, 50);
}, () => ['ink', 'eraser'].includes(this._tool.type) ? 0 : 50);

_getAnnotationFromSelectionRanges(selectionRanges, type, color) {
if (selectionRanges[0].collapsed) {
Expand Down

0 comments on commit a9b846b

Please sign in to comment.