Created
March 7, 2021 18:16
-
-
Save piotrrussw/7729e71ff1fa96086df80c2b94af75db to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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