Skip to content

Commit

Permalink
fix: prevent multiply pointers drawing
Browse files Browse the repository at this point in the history
  • Loading branch information
wokayme committed Aug 30, 2024
1 parent 267935b commit 369110d
Showing 1 changed file with 21 additions and 5 deletions.
26 changes: 21 additions & 5 deletions src/signature_pad.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,11 @@ export default class SignaturePad extends SignatureEventTarget {
public on(): void {
// Disable panning/zooming when touching canvas element
this.canvas.style.touchAction = 'none';
(this.canvas.style as CSSStyleDeclaration & { msTouchAction: string | null }).msTouchAction = 'none';
(
this.canvas.style as CSSStyleDeclaration & {
msTouchAction: string | null;
}
).msTouchAction = 'none';
this.canvas.style.userSelect = 'none';

const isIOS =
Expand All @@ -211,7 +215,11 @@ export default class SignaturePad extends SignatureEventTarget {
public off(): void {
// Enable panning/zooming when touching canvas element
this.canvas.style.touchAction = 'auto';
(this.canvas.style as CSSStyleDeclaration & { msTouchAction: string | null }).msTouchAction = 'auto';
(
this.canvas.style as CSSStyleDeclaration & {
msTouchAction: string | null;
}
).msTouchAction = 'auto';
this.canvas.style.userSelect = 'auto';

this.canvas.removeEventListener('pointerdown', this._handlePointerDown);
Expand All @@ -225,7 +233,7 @@ export default class SignaturePad extends SignatureEventTarget {
const canvasWindow =
window.document === this.canvas.ownerDocument
? window
: this.canvas.ownerDocument.defaultView ?? this.canvas.ownerDocument;
: (this.canvas.ownerDocument.defaultView ?? this.canvas.ownerDocument);

return {
addEventListener: canvasWindow.addEventListener.bind(
Expand Down Expand Up @@ -376,7 +384,11 @@ export default class SignaturePad extends SignatureEventTarget {
};

private _handlePointerDown = (event: PointerEvent): void => {
if (!this._isLeftButtonPressed(event) || this._drawingStroke) {
if (
!event.isPrimary ||
!this._isLeftButtonPressed(event) ||
this._drawingStroke
) {
return;
}

Expand All @@ -386,7 +398,11 @@ export default class SignaturePad extends SignatureEventTarget {
};

private _handlePointerMove = (event: PointerEvent): void => {
if (!this._isLeftButtonPressed(event, true) || !this._drawingStroke) {
if (
!event.isPrimary ||
!this._isLeftButtonPressed(event, true) ||
!this._drawingStroke
) {
// Stop when primary button not pressed or multiple buttons pressed
this._strokeEnd(this._pointerEventToSignatureEvent(event), false);
return;
Expand Down

0 comments on commit 369110d

Please sign in to comment.