From fa0cd8eaa4407e05971121dbc6c9992c84628095 Mon Sep 17 00:00:00 2001 From: Sean Sube Date: Mon, 13 Feb 2023 08:43:11 -0600 Subject: [PATCH] fix(gui): remove duplicate dots caused by mouse click and down handlers both firing --- gui/src/components/input/MaskCanvas.tsx | 92 +++++++++++-------------- 1 file changed, 42 insertions(+), 50 deletions(-) diff --git a/gui/src/components/input/MaskCanvas.tsx b/gui/src/components/input/MaskCanvas.tsx index 205e6a43..5f3aeb19 100644 --- a/gui/src/components/input/MaskCanvas.tsx +++ b/gui/src/components/input/MaskCanvas.tsx @@ -42,7 +42,7 @@ export interface MaskCanvasProps { onSave: (blob: Blob) => void; } -const logger = createLogger({ name: 'react', level: 'info' }); // TODO: hackeroni and cheese +const logger = createLogger({ name: 'react', level: 'debug' }); // TODO: hackeroni and cheese export function MaskCanvas(props: MaskCanvasProps) { const { source, mask } = props; @@ -106,6 +106,23 @@ export function MaskCanvas(props: MaskCanvasProps) { } } + function drawMouse(event: React.MouseEvent) { + const canvas = mustExist(viewRef.current); + const bounds = canvas.getBoundingClientRect(); + + if (painting.current) { + drawClicks([{ + x: event.clientX - bounds.left, + y: event.clientY - bounds.top, + }]); + } else { + drawBrush({ + x: event.clientX - bounds.left, + y: event.clientY - bounds.top, + }); + } + } + function drawUndo(): void { if (doesExist(maskRef.current) && doesExist(undoRef.current)) { logger.debug('draw undo'); @@ -131,6 +148,16 @@ export function MaskCanvas(props: MaskCanvasProps) { } } + function drawFill(fn: FloodFn): void { + saveUndo(); + floodCanvas(maskRef, fn); + composite(); + dirty.current = true; + } + + /** + * Save the current mask to the undo canvas. + */ function saveUndo(): void { if (doesExist(maskRef.current) && doesExist(undoRef.current)) { logger.debug('save undo'); @@ -139,6 +166,9 @@ export function MaskCanvas(props: MaskCanvasProps) { } } + /** + * Save the current mask to state, so that it persists between tabs. + */ function saveMask(): void { if (doesExist(maskRef.current)) { logger.debug('save mask', { dirty: dirty.current }); @@ -183,7 +213,7 @@ export function MaskCanvas(props: MaskCanvasProps) { useEffect(() => { if (doesExist(maskRef.current) && doesExist(mask)) { drawMask(mask).catch((err) => { - // TODO: handle + logger.error(err, 'error drawing mask for effect'); }); } }, [mask]); @@ -246,41 +276,18 @@ export function MaskCanvas(props: MaskCanvasProps) { height={params.height.default} width={params.width.default} style={backgroundStyle} - onClick={(event) => { - logger.debug('mouse click', { state: painting.current }); - const canvas = mustExist(viewRef.current); - const bounds = canvas.getBoundingClientRect(); - - drawClicks([{ - x: event.clientX - bounds.left, - y: event.clientY - bounds.top, - }]); - }} - onMouseDown={() => { + onMouseDown={(event) => { logger.debug('mouse down', { state: painting.current }); - painting.current = true; saveUndo(); + painting.current = true; + + drawMouse(event); }} onMouseLeave={finishPainting} onMouseOut={finishPainting} onMouseUp={finishPainting} - onMouseMove={(event) => { - const canvas = mustExist(viewRef.current); - const bounds = canvas.getBoundingClientRect(); - - if (painting.current) { - drawClicks([{ - x: event.clientX - bounds.left, - y: event.clientY - bounds.top, - }]); - } else { - drawBrush({ - x: event.clientX - bounds.left, - y: event.clientY - bounds.top, - }); - } - }} + onMouseMove={drawMouse} /> Black pixels in the mask will stay the same, white pixels will be replaced. The masked pixels will be blended @@ -330,10 +337,7 @@ export function MaskCanvas(props: MaskCanvasProps) { variant='outlined' startIcon={} onClick={() => { - saveUndo(); - floodCanvas(maskRef, floodBlack); - composite(); - dirty.current = true; + drawFill(floodBlack); }}> Fill with black @@ -341,10 +345,7 @@ export function MaskCanvas(props: MaskCanvasProps) { variant='outlined' startIcon={} onClick={() => { - saveUndo(); - floodCanvas(maskRef, floodWhite); - composite(); - dirty.current = true; + drawFill(floodWhite); }}> Fill with white @@ -352,10 +353,7 @@ export function MaskCanvas(props: MaskCanvasProps) { variant='outlined' startIcon={} onClick={() => { - saveUndo(); - floodCanvas(maskRef, floodInvert); - composite(); - dirty.current = true; + drawFill(floodInvert); }}> Invert @@ -363,10 +361,7 @@ export function MaskCanvas(props: MaskCanvasProps) { variant='outlined' startIcon={} onClick={() => { - saveUndo(); - floodCanvas(maskRef, floodBelow); - composite(); - dirty.current = true; + drawFill(floodBelow); }}> Gray to black @@ -374,10 +369,7 @@ export function MaskCanvas(props: MaskCanvasProps) { variant='outlined' startIcon={} onClick={() => { - saveUndo(); - floodCanvas(maskRef, floodAbove); - composite(); - dirty.current = true; + drawFill(floodAbove); }}> Gray to white