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

Support Windows 10 stylus with pointer events (almost) #214

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Changes from 1 commit
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
43 changes: 31 additions & 12 deletions jquery.signaturepad.js
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,14 @@ function SignaturePad (selector, options) {
*/
, typeItNumChars = 0

/**
* Remembers last scroll position
*
* @private
*
* @type {Number}
*/
, lastScrollY = 0

/**
* Clears the mouseLeaveTimeout
Expand Down Expand Up @@ -193,8 +201,8 @@ function SignaturePad (selector, options) {
newX = Math.floor(e.targetTouches[0].pageX - offset.left)
newY = Math.floor(e.targetTouches[0].pageY - offset.top)
} else {
newX = Math.floor(e.pageX - offset.left)
newY = Math.floor(e.pageY - offset.top)
newX = Math.floor(e.originalEvent.pageX - offset.left)
newY = Math.floor(e.originalEvent.pageY - offset.top)
}

if (previous.x === newX && previous.y === newY)
Expand Down Expand Up @@ -258,12 +266,16 @@ function SignaturePad (selector, options) {
// this.removeEventListener('MSPointerMove', drawLine)
})
} else {
canvas.unbind('mousemove.signaturepad')
canvas.unbind('pointermove.signaturepad')
}

if (output.length > 0 && settings.onDrawEnd && typeof settings.onDrawEnd === 'function')
settings.onDrawEnd.apply(self)
}

document.body.style.position = '';
document.body.style.top = '';
window.scrollTo(0, lastScrollY);

previous.x = null
previous.y = null
Expand Down Expand Up @@ -339,11 +351,18 @@ function SignaturePad (selector, options) {
* @param {Object} touchObject The object context registered to the event; canvas
*/
function startDrawing (e, touchObject) {
if($(document).height() > $(window).height())
document.body.style.overflowY = "scroll";

lastScrollY = window.scrollY;
document.body.style.position = 'fixed';
Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had to fix the body or the stylus would scroll the page up and down 😞

document.body.style.top = -lastScrollY+"px";

if (touchable) {
touchObject.addEventListener('touchmove', onMouseMove, false)
// touchObject.addEventListener('MSPointerMove', onMouseMove, false)
} else {
canvas.bind('mousemove.signaturepad', onMouseMove)
canvas.bind('pointermove.signaturepad', onMouseMove)
}

// Draws a single point on initial mouse down, for people with periods in their name
Expand All @@ -369,10 +388,10 @@ function SignaturePad (selector, options) {
// this.removeEventListener('MSPointerMove', drawLine)
}
})
$(document).unbind('mouseup.signaturepad')
canvas.unbind('mousedown.signaturepad')
canvas.unbind('mousemove.signaturepad')
canvas.unbind('mouseleave.signaturepad')
$(document).unbind('pointerup.signaturepad')
canvas.unbind('pointerdown.signaturepad')
canvas.unbind('pointermove.signaturepad')
canvas.unbind('pointerleave.signaturepad')

$(settings.clear, context).unbind('click.signaturepad')
}
Expand Down Expand Up @@ -406,15 +425,15 @@ function SignaturePad (selector, options) {
// this.addEventListener('MSPointerCancel', stopDrawingWrapper, false)
})

canvas.unbind('mousedown.signaturepad')
canvas.unbind('pointerdown.signaturepad')
} else {
$(document).bind('mouseup.signaturepad', function () {
$(document).bind('pointerup.signaturepad', function () {
if (mouseButtonDown) {
stopDrawing()
clearMouseLeaveTimeout()
}
})
canvas.bind('mouseleave.signaturepad', function (e) {
canvas.bind('pointerleave.signaturepad', function (e) {
if (mouseButtonDown) stopDrawing(e)

if (mouseButtonDown && !mouseLeaveTimeout) {
Expand Down Expand Up @@ -451,7 +470,7 @@ function SignaturePad (selector, options) {
this.addEventListener('touchstart', startTouchDrawing)
})

canvas.bind('mousedown.signaturepad', function (e) {
canvas.bind('pointerdown.signaturepad', function (e) {
e.preventDefault()

// Only allow left mouse clicks to trigger signature drawing
Expand Down