drawing basics

This commit is contained in:
caranicas 2022-09-16 18:32:48 -04:00
parent b15ac8b13e
commit 94a208bd91
2 changed files with 51 additions and 56 deletions

View File

@ -11,6 +11,7 @@ globalStyle(`${DrawImageMain} > canvas`, {
position: "absolute", position: "absolute",
top: "0", top: "0",
left: "0", left: "0",
opacity: "0.5",
}); });
globalStyle(`${DrawImageMain} > img`, { globalStyle(`${DrawImageMain} > img`, {

View File

@ -1,5 +1,5 @@
// @ts-nocheck // @ts-nocheck
import React, { useRef, useEffect } from "react"; import React, { useRef, useState } from "react";
// https://github.com/embiem/react-canvas-draw // https://github.com/embiem/react-canvas-draw
@ -15,77 +15,70 @@ import {
export default function DrawImage({ imageData }: DrawImageProps) { export default function DrawImage({ imageData }: DrawImageProps) {
const canvasRef = useRef<HTMLCanvasElement>(null); const canvasRef = useRef<HTMLCanvasElement>(null);
const draw = (ctx: CanvasRenderingContext2D) => { const [isDrawing, setIsDrawing] = useState(false);
ctx.fillStyle = "red";
ctx.fillRect(0, 0, 100, 100);
};
// useEffect(() => {
// const canvas = canvasRef.current;
// if (canvas) {
// if (imageData) {
// const ctx = canvas.getContext("2d");
// if (ctx) {
// const img = new Image();
// img.onload = () => {
// ctx.drawImage(img, 0, 0);
// };
// img.src = imageData;
// }
// } else {
// const ctx = canvas.getContext("2d");
// if (ctx) {
// draw(ctx);
// }
// }
// } else {
// console.log("canvas is null");
// }
// }, [imageData, draw]);
const _handleMouseDown = ( const _handleMouseDown = (
e: React.MouseEvent<HTMLCanvasElement, MouseEvent> e: React.MouseEvent<HTMLCanvasElement, MouseEvent>
) => { ) => {
console.log("mouse down", e); console.log("mouse down", e);
const canvas = canvasRef.current;
if (canvas) {
const ctx = canvas.getContext("2d");
ctx.strokeStyle = "#red";
const {
nativeEvent: { offsetX, offsetY },
} = e;
// console.log("x: " + x + " y: " + y); const {
ctx.beginPath(); nativeEvent: { offsetX, offsetY },
ctx.moveTo(offsetX, offsetY); } = e;
// ctx.lineTo(x + 1, y + 1);
// ctx.stroke();
} setIsDrawing(true);
}; };
const _handleMouseUp = ( const _handleMouseUp = (
e: React.MouseEvent<HTMLCanvasElement, MouseEvent> e: React.MouseEvent<HTMLCanvasElement, MouseEvent>
) => { ) => {
console.log("mouse up"); setIsDrawing(false);
const canvas = canvasRef.current; const canvas = canvasRef.current;
if (canvas) { if (canvas) {
const ctx = canvas.getContext("2d"); const data = canvas.toDataURL();
if (ctx) { console.log("data", data);
// draw(ctx);
}
const {
nativeEvent: { offsetX, offsetY },
} = e;
// console.log("x: " + x + " y: " + y);
// ctx.moveTo(x, y);
ctx?.lineTo(offsetX, offsetY);
ctx.stroke();
ctx.closePath();
} }
}; };
const _handleMouseMove = (
e: React.MouseEvent<HTMLCanvasElement, MouseEvent>
) => {
if (isDrawing) {
const canvas = canvasRef.current;
if (canvas) {
const ctx = canvas.getContext("2d");
ctx.strokeStyle = "red";
const {
nativeEvent: { offsetX, offsetY },
} = e;
ctx.beginPath();
ctx.lineWidth = 20;
// Sets the end of the lines drawn
// to a round shape.
ctx.lineCap = 'round';
ctx.strokeStyle = 'white';
// The cursor to start drawing
// moves to this coordinate
ctx.moveTo(offsetX, offsetY);
// A line is traced from start
// coordinate to this coordinate
ctx.lineTo(offsetX, offsetY);
// Draws the line.
ctx.stroke();
}
}
};
return ( return (
<div className={DrawImageMain}> <div className={DrawImageMain}>
<img src={imageData} /> <img src={imageData} />
@ -94,6 +87,7 @@ export default function DrawImage({ imageData }: DrawImageProps) {
width={512} width={512}
height={512} height={512}
onMouseDown={_handleMouseDown} onMouseDown={_handleMouseDown}
onMouseMove={_handleMouseMove}
onMouseUp={_handleMouseUp} onMouseUp={_handleMouseUp}
></canvas> ></canvas>
</div> </div>