From 5abbec94c47a614ec576800d2f8b6dd4e03f35b0 Mon Sep 17 00:00:00 2001 From: caranicas Date: Sat, 17 Sep 2022 12:23:50 -0400 Subject: [PATCH] working --- .../molecules/drawImage/drawImage.css.ts | 7 ++- .../components/molecules/drawImage/index.tsx | 48 ++++++++++++++++--- .../creationPanel/inpaintingPanel/index.tsx | 1 - 3 files changed, 48 insertions(+), 8 deletions(-) diff --git a/ui/frontend/build_src/src/components/molecules/drawImage/drawImage.css.ts b/ui/frontend/build_src/src/components/molecules/drawImage/drawImage.css.ts index 5e374825..7b0cc7fc 100644 --- a/ui/frontend/build_src/src/components/molecules/drawImage/drawImage.css.ts +++ b/ui/frontend/build_src/src/components/molecules/drawImage/drawImage.css.ts @@ -8,7 +8,12 @@ globalStyle(`${DrawImageMain} > canvas`, { position: "absolute", top: "0", left: "0", - opacity: ".5", + width: "100%", + height: "100%", +}); + +globalStyle(`${DrawImageMain} > canvas:first-of-type`, { + opacity: ".7", }); globalStyle(`${DrawImageMain} > img`, { diff --git a/ui/frontend/build_src/src/components/molecules/drawImage/index.tsx b/ui/frontend/build_src/src/components/molecules/drawImage/index.tsx index 4172a994..2ded6a4c 100644 --- a/ui/frontend/build_src/src/components/molecules/drawImage/index.tsx +++ b/ui/frontend/build_src/src/components/molecules/drawImage/index.tsx @@ -1,5 +1,5 @@ // @ts-nocheck -import React, { useRef, useState, useCallback, useEffect } from "react"; +import React, { useRef, useState, useEffect } from "react"; // https://github.com/embiem/react-canvas-draw @@ -23,6 +23,42 @@ export default function DrawImage({ imageData, brushSize, brushShape, brushColor const cursorRef = useRef(null); const [isUpdating, setIsUpdating] = useState(false); + const [canvasWidth, setCanvasWidth] = useState(512); + const [canvasHeight, setCanvasHeight] = useState(512); + + + useEffect(() => { + console.log(imageData); + const img = new Image(); + img.onload = () => { + setCanvasWidth(img.width); + setCanvasHeight(img.height); + + } + img.src = imageData; + + }, [imageData]); + + + useEffect(() => { + // when the brush color changes, change the color of all the + // drawn pixels to the new color + if (drawingRef.current) { + const ctx = drawingRef.current.getContext("2d"); + const imageData = ctx.getImageData(0, 0, canvasWidth, canvasHeight); + const data = imageData.data; + for (let i = 0; i < data.length; i += 4) { + if (data[i + 3] > 0) { + data[i] = parseInt(brushColor, 16); + data[i + 1] = parseInt(brushColor, 16); + data[i + 2] = parseInt(brushColor, 16); + } + } + ctx.putImageData(imageData, 0, 0); + } + + }, [brushColor]); + const _handleMouseDown = ( e: React.MouseEvent ) => { @@ -78,7 +114,7 @@ export default function DrawImage({ imageData, brushSize, brushShape, brushColor if (isErasing) { const offset = brushSize / 2; - // draw a quare outline + // draw a quare ctx.lineWidth = 2; ctx.lineCap = 'butt'; ctx.strokeStyle = brushColor; @@ -132,13 +168,13 @@ export default function DrawImage({ imageData, brushSize, brushShape, brushColor ) => { - console.log("brush shape", e.target.value); setBrushShape(e.target.value); };