I am going to add Grassator to the answer by adding a link to this answer, which describes in more detail the code necessary for this solution to work: fooobar.com/questions/1481563 / ....
Before continuing, note that Apple has changed the way iOS scrolls on newer devices. To handle this change, you need to add a few extra features; Thanks to Christopher Vickers for sharing this:
function preventDefault(e) { e.preventDefault(); } function disableScroll() { document.body.addEventListener('touchmove', preventDefault, { passive: false }); } function enableScroll() { document.body.removeEventListener('touchmove', preventDefault); }
All methods for canvas are called in the form of a box as follows:
var drawer = { isDrawing: false, touchstart: function (coors) { ctx.beginPath(); ctx.moveTo(coors.x, coors.y); this.isDrawing = true; disableScroll();
In addition, the EventListener
specially ordered so that touch input is executed first:
var touchAvailable = ('createTouch' in document) || ('onstarttouch' in window); if (touchAvailable) { canvas.addEventListener('touchstart', draw, false); canvas.addEventListener('touchmove', draw, false); canvas.addEventListener('touchend', draw, false); } else { canvas.addEventListener('mousedown', draw, false); canvas.addEventListener('mousemove', draw, false); canvas.addEventListener('mouseup', draw, false); }
Finally, resilient scrolling is prevented by adding another EventListener near the end of the code.
document.body.addEventListener('touchmove', function (event) { event.preventDefault(); }, false);
All this is inside window.addEventListener('load', function () {})
.