I'm making a web paint app and I'm basically finished, but it works only with on computer (not with touches). So I tried to put there some touch events, but it only works like a click instead of creating custom lines/shapes etc. I get only a dot. I'm new in touch events, so could you help please? Thank you very much.
P.S: Don't blame me for not using semicolons
JS:
let canvas = document.querySelector("canvas")
let ctx = canvas.getContext("2d")
window.addEventListener("load", () => {
let currentlyPainting = false
function startingPosition(e) {
currentlyPainting = true
paint(e)
}
function endingPosition() {
currentlyPainting = false
ctx.beginPath()
}
function paint(e) {
if (!currentlyPainting) return
ctx.lineTo(e.clientX, e.clientY)
ctx.stroke()
ctx.beginPath()
ctx.moveTo(e.clientX, e.clientY)
// there's a lot more code but it's unneccesary
}
}
canvas.addEventListener("mousedown", startingPosition)
canvas.addEventListener("mouseup", endingPosition)
canvas.addEventListener("mousemove", paint)
canvas.addEventListener("touchstart", startingPosition)
canvas.addEventListener("touchend", endingPosition)
canvas.addEventListener("touchmove", paint)
})