Skip to content

Instantly share code, notes, and snippets.

View richarddprasad's full-sized avatar
🎯
Focusing

Richard Prasad richarddprasad

🎯
Focusing
View GitHub Profile
package main
func main() {
}
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);
function handleMouseMove(evt: MouseEvent) {
if (mouseDown && context) {
start = {
x: end.x,
y: end.y,
};
end = {
x: evt.clientX - canvasOffsetLeft,
y: evt.clientY - canvasOffsetTop,
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);
}
function handleMouseDown(evt: MouseEvent) {
mouseDown = true;
start = {
x: evt.clientX - canvasOffsetLeft,
y: evt.clientY - canvasOffsetTop,
};
end = {
x: evt.clientX - canvasOffsetLeft,
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;
}
import * as React from 'react';
type Coordinates = {
x: number;
y: number;
};
function App() {...}
React.useEffect(() => {
if (canvasRef.current) {
const renderCtx = canvasRef.current.getContext('2d');
if (renderCtx) {
setContext(renderCtx);
}
}
// Draw a rectangle
React.useEffect(() => {
if (canvasRef.current) {
const renderCtx = canvasRef.current.getContext('2d');
if (renderCtx) {
setContext(renderCtx);
}
}
// Draw a rectangle
React.useEffect(() => {
if (canvasRef.current) {
const renderCtx = canvasRef.current.getContext('2d');
if (renderCtx) {
setContext(renderCtx);
}
}
}, [context]);