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
package main | |
func main() { | |
} |
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
import * as React from 'react'; | |
type Coordinates = { | |
x: number; | |
y: number; | |
}; | |
function App() { | |
const canvasRef = React.useRef<HTMLCanvasElement>(null); | |
const [context, setContext] = React.useState<CanvasRenderingContext2D | null>(null); |
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
function handleMouseMove(evt: MouseEvent) { | |
if (mouseDown && context) { | |
start = { | |
x: end.x, | |
y: end.y, | |
}; | |
end = { | |
x: evt.clientX - canvasOffsetLeft, | |
y: evt.clientY - canvasOffsetTop, |
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
React.useEffect(() => { | |
let mouseDown: boolean = false; | |
... | |
return function cleanup() { | |
if (canvasRef.current) { | |
canvasRef.current.removeEventListener('mousedown', handleMouseDown); | |
canvasRef.current.removeEventListener('mouseup', handleMouseUp); | |
canvasRef.current.removeEventListener('mousemove', handleMouseMove); | |
} |
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
function handleMouseDown(evt: MouseEvent) { | |
mouseDown = true; | |
start = { | |
x: evt.clientX - canvasOffsetLeft, | |
y: evt.clientY - canvasOffsetTop, | |
}; | |
end = { | |
x: evt.clientX - canvasOffsetLeft, |
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
React.useEffect(() => { | |
let mouseDown: boolean = false; | |
let start: Coordinates = { x: 0, y: 0 }; | |
let end: Coordinates = { x: 0, y: 0 }; | |
let canvasOffsetLeft: number = 0; | |
let canvasOffsetTop: number = 0; | |
function handleMouseDown(evt: MouseEvent) { | |
mouseDown = true; | |
} |
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
import * as React from 'react'; | |
type Coordinates = { | |
x: number; | |
y: number; | |
}; | |
function App() {...} |
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
React.useEffect(() => { | |
if (canvasRef.current) { | |
const renderCtx = canvasRef.current.getContext('2d'); | |
if (renderCtx) { | |
setContext(renderCtx); | |
} | |
} | |
// Draw a rectangle |
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
React.useEffect(() => { | |
if (canvasRef.current) { | |
const renderCtx = canvasRef.current.getContext('2d'); | |
if (renderCtx) { | |
setContext(renderCtx); | |
} | |
} | |
// Draw a rectangle |
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
React.useEffect(() => { | |
if (canvasRef.current) { | |
const renderCtx = canvasRef.current.getContext('2d'); | |
if (renderCtx) { | |
setContext(renderCtx); | |
} | |
} | |
}, [context]); |
NewerOlder