From e8a2afb017c56eb66cf89fd9631b207dc708d798 Mon Sep 17 00:00:00 2001 From: Francesco Luzzana Date: Thu, 22 Dec 2022 23:55:07 +0100 Subject: [PATCH] test test mobile --- public/draw.js | 18 ++++++++++-------- 1 file changed, 10 insertions(+), 8 deletions(-) diff --git a/public/draw.js b/public/draw.js index d4dfc00..ad99ddb 100644 --- a/public/draw.js +++ b/public/draw.js @@ -12,6 +12,7 @@ var buffer = []; // Contains the last positions of the mouse cursor const startDrawing = (e) => { e.preventDefault(); + // console.log("start"); bufferSize = 2; path = document.createElementNS("http://www.w3.org/2000/svg", "path"); path.setAttribute("fill", "none"); @@ -28,6 +29,7 @@ const startDrawing = (e) => { const draw = (e) => { e.preventDefault(); if (path) { + // console.log("draw"); appendToBuffer(getMousePosition(e)); updateSvgPath(); } @@ -35,14 +37,15 @@ const draw = (e) => { const stopDrawing = () => { if (path) { + // console.log("stop"); path = null; } }; var getMousePosition = function (e) { return { - x: e.pageX - rect.left, - y: e.pageY - rect.top, + x: (e.pageX || e?.changedTouches[0]?.pageX) - rect.left, + y: (e.pageY || e?.changedTouches[0]?.pageY) - rect.top, }; }; @@ -96,12 +99,11 @@ var updateSvgPath = function () { }; svgElement.addEventListener("mousedown", (e) => startDrawing(e)); -svgElement.addEventListener("touchstart", (e) => startDrawing(e)); +svgElement.addEventListener("touchstart", (e) => startDrawing(e), { passive: false }); svgElement.addEventListener("mousemove", (e) => draw(e)); -svgElement.addEventListener("touchmove", (e) => draw(e)); +svgElement.addEventListener("touchmove", (e) => draw(e), { passive: false }); -svgElement.addEventListener("mouseup", () => stopDrawing(), { passive: false }); -svgElement.addEventListener("mouseleave", () => stopDrawing(), { passive: false }); -svgElement.addEventListener("touchend", () => stopDrawing(), { passive: false }); -svgElement.addEventListener("touchcancel", () => stopDrawing(), { passive: false }); +svgElement.addEventListener("mouseup", () => stopDrawing()); +svgElement.addEventListener("mouseleave", () => stopDrawing()); +svgElement.addEventListener("touchend", () => stopDrawing());