From 91f498547151e71ed3741fd3ddb3661b11403423 Mon Sep 17 00:00:00 2001 From: JonRelf Date: Mon, 2 Jan 2017 13:24:08 +0000 Subject: [PATCH 1/2] Add files via upload Add touch events --- Cloth.js | 47 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 47 insertions(+) diff --git a/Cloth.js b/Cloth.js index 61ef9aa..f692bf3 100644 --- a/Cloth.js +++ b/Cloth.js @@ -224,6 +224,53 @@ canvas.onmouseup = () => (mouse.down = false) canvas.oncontextmenu = (e) => e.preventDefault() +// Add touch events + +function setTouch (e) { + let rect = canvas.getBoundingClientRect() + mouse.px = mouse.x + mouse.py = mouse.y + mouse.x = e.touches[0].clientX - rect.left + mouse.y = e.touches[0].clientY - rect.top +} + +canvas.addEventListener('touchstart', function (e) { + mouse.down = true + let rect = canvas.getBoundingClientRect() + mouse.x = e.touches[0].clientX - rect.left + mouse.y = e.touches[0].clientY - rect.top + setTouch(e) +}, false); + +canvas.addEventListener('touchmove', function (e) { + setTouch(e) +}, false); + +window.addEventListener('touchend', function (e) { + mouse.down = false +}, false); + +// Prevent scrolling when touching the canvas + +document.body.addEventListener('touchstart', function (e) { + if (e.target === canvas) { + e.preventDefault(); + } +}, false); + +document.body.addEventListener('touchend', function (e) { + if (e.target === canvas) { + e.preventDefault(); + } +}, false); + +document.body.addEventListener('touchmove', function (e) { + if (e.target === canvas) { + e.preventDefault(); + } +}, false); + + let cloth = new Cloth() ;(function update (time) { From 5062d38cd25e714cba76b8e0787ddb278b923d6c Mon Sep 17 00:00:00 2001 From: JonRelf Date: Mon, 2 Jan 2017 16:27:47 +0000 Subject: [PATCH 2/2] Update Cloth.js Trigger mouseup when mouse is outside of the canvas element, otherwise mouse.down gets 'stuck'. --- Cloth.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cloth.js b/Cloth.js index f692bf3..bf0bf72 100644 --- a/Cloth.js +++ b/Cloth.js @@ -220,7 +220,7 @@ canvas.onmousedown = (e) => { canvas.onmousemove = setMouse -canvas.onmouseup = () => (mouse.down = false) +window.onmouseup = () => (mouse.down = false) canvas.oncontextmenu = (e) => e.preventDefault() @@ -273,7 +273,7 @@ document.body.addEventListener('touchmove', function (e) { let cloth = new Cloth() -;(function update (time) { +(function update (time) { ctx.clearRect(0, 0, canvas.width, canvas.height) cloth.update(0.016)