From 7b00f72c8bdfbf2592171800690080bd6572c3ca Mon Sep 17 00:00:00 2001 From: lolabird Date: Mon, 13 Nov 2023 11:30:12 -0700 Subject: [PATCH] Get touch to work with canvas --- script.js | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/script.js b/script.js index 3eff424..9d5b444 100644 --- a/script.js +++ b/script.js @@ -144,9 +144,26 @@ function createGrid() { const gridToggle = document.querySelector("#gridToggle"); gridItems.forEach((item) => { - item.addEventListener("touchstart", colorGrid); item.addEventListener("pointerover", colorGrid); }); + document.addEventListener("touchmove", function(e) { + e.preventDefault(); + + gridItems.forEach((item) => { + const rect = item.getBoundingClientRect(); + const touch = e.touches[0]; + + // Check if the touch is within the boundaries of the current grid item + if ( + touch.clientX >= rect.left && + touch.clientX <= rect.right && + touch.clientY >= rect.top && + touch.clientY <= rect.bottom + ) { + colorGrid.call(item); + } + }); + }, {passive: false}); gridToggle.addEventListener("click", () => { gridItems.forEach((item) => {