This file contains hidden or 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 onMove = useCallback( | |
(event) => { | |
if (drawing) { | |
const newPosition = getCoordinates(event) | |
if (position && newPosition) { | |
drawLine(position, newPosition) | |
setPosition(newPosition) | |
} | |
} | |
}, |
This file contains hidden or 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) |
This file contains hidden or 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
function ReactCanvasPaint(props) { | |
const canvas = useRef(null) | |
const [drawing, setDrawing] = useState(false) | |
const [position, setPosition] = useState(null) | |
return ( | |
<canvas | |
ref={canvas} | |
onMouseDown={onDown} | |
onTouchStart={onDown} |
This file contains hidden or 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
function ReactCanvasPaint(props) { | |
return ( | |
<canvas | |
ref={canvas} | |
onMouseDown={onDown} | |
onTouchStart={onDown} | |
onMouseUp={onUp} | |
onTouchEnd={onUp} | |
onMouseLeave={onUp} | |
onMouseMove={onMove} |
This file contains hidden or 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
function ReactCanvasPaint(props) { | |
return ( | |
<div className={styles.container}> | |
<canvas | |
width={props.width} | |
height={props.height} | |
/> | |
</div> | |
) | |
} |