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

throttling-feature added #237

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions src/signature_pad.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import Point from './point';
import Bezier from './bezier';
import throttle from './throttle';

function SignaturePad(canvas, options) {
const self = this;
Expand All @@ -8,6 +9,14 @@ function SignaturePad(canvas, options) {
this.velocityFilterWeight = opts.velocityFilterWeight || 0.7;
this.minWidth = opts.minWidth || 0.5;
this.maxWidth = opts.maxWidth || 2.5;
this.throttle = opts.throttle || 0;

if (this.throttle) {
this._strokeMoveUpdate = throttle(SignaturePad.prototype._strokeUpdate, this.throttle);
} else {
this._strokeMoveUpdate = SignaturePad.prototype._strokeUpdate;
}

this.dotSize = opts.dotSize || function () {
return (this.minWidth + this.maxWidth) / 2;
};
Expand All @@ -31,7 +40,7 @@ function SignaturePad(canvas, options) {

this._handleMouseMove = function (event) {
if (self._mouseButtonDown) {
self._strokeUpdate(event);
self._strokeMoveUpdate(event);
}
};

Expand All @@ -54,7 +63,7 @@ function SignaturePad(canvas, options) {
event.preventDefault();

const touch = event.targetTouches[0];
self._strokeUpdate(touch);
self._strokeMoveUpdate(touch);
};

this._handleTouchEnd = function (event) {
Expand Down
34 changes: 34 additions & 0 deletions src/throttle.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
/* eslint-disable */

// http://stackoverflow.com/a/27078401/815507
export default function throttle(func, wait, options) {
var context, args, result;
var timeout = null;
var previous = 0;
if (!options) options = {};
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);
context = this;
args = arguments;
if (remaining <= 0 || remaining > wait) {
if (timeout) {
clearTimeout(timeout);
timeout = null;
}
previous = now;
result = func.apply(context, args);
if (!timeout) context = args = null;
} else if (!timeout && options.trailing !== false) {
timeout = setTimeout(later, remaining);
}
return result;
};
}