Skip to content

Instantly share code, notes, and snippets.

@piotrrussw
Created March 7, 2021 18:16
Show Gist options
  • Save piotrrussw/7729e71ff1fa96086df80c2b94af75db to your computer and use it in GitHub Desktop.
Save piotrrussw/7729e71ff1fa96086df80c2b94af75db to your computer and use it in GitHub Desktop.
const onDown = useCallback((event) => {
const coordinates = getCoordinates(event)
if (coordinates) {
setPosition(coordinates)
setDrawing(true)
}
}, [])
const onUp = useCallback(() => {
setDrawing(false)
setPosition(null)
}, [])
const getCoordinates = (event) => {
if (!canvas.current) {
return null
}
const x = event.pageX || event.touches[0].pageX
const y = event.pageY || event.touches[0].pageY
return {
x: x - canvas.current.offsetLeft,
y: y - canvas.current.offsetTop
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment