Skip to content

Commit

Permalink
Merge pull request #66 from deco-finter/fix/fill-tool-leak
Browse files Browse the repository at this point in the history
Fix fill tool leak
  • Loading branch information
daystram committed Oct 16, 2021
2 parents 59bda1d + 47180be commit 6a6f22d
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 20 deletions.
31 changes: 11 additions & 20 deletions fableous-fe/src/components/canvas/Canvas.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import React, {
} from "react";
import cloneDeep from "lodash.clonedeep";
import {
clearCanvas,
convHEXtoRGBA,
getTextBounds,
scaleDownXY,
Expand Down Expand Up @@ -252,7 +253,7 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
expandLeft = false;
}
}
if (x < width) {
if (x < width - 1) {
if (checkPixel(pixel + 4, startColor) && !expandRight) {
stack.push([x + 1, y]);
expandRight = true;
Expand Down Expand Up @@ -311,8 +312,7 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
const ctx = canvasRef.current.getContext(
"2d"
) as CanvasRenderingContext2D;
const { width, height } = canvasRef.current;
ctx.clearRect(0, 0, width, height);
clearCanvas(canvasRef);
ctx.textAlign = "center";
ctx.textBaseline = "middle";
Object.entries(textShapesRef.current).forEach(([id, shape]) => {
Expand Down Expand Up @@ -548,12 +548,7 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
"2d"
) as CanvasRenderingContext2D;
if (!newCheckpoint) {
ctx.clearRect(
0,
0,
canvasRef.current.width,
canvasRef.current.height
);
clearCanvas(canvasRef);
setTextShapes({});
} else if (
newCheckpoint.tool === ToolMode.Paint ||
Expand Down Expand Up @@ -608,11 +603,7 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
);

const resetCanvas = () => {
const ctx = canvasRef.current.getContext(
"2d"
) as CanvasRenderingContext2D;
const { width, height } = canvasRef.current;
ctx.clearRect(0, 0, width, height);
clearCanvas(canvasRef);
setAudioPaths([]);
setTextShapes({});
setTextId(1);
Expand Down Expand Up @@ -865,18 +856,18 @@ const Canvas = forwardRef<ImperativeCanvasRef, CanvasProps>(
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [role, wsConn, adjustCanvasSize]);

// cleanup before moving to next page
useEffect(() => {
resetCanvas();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pageNum]);

// workaround to recalculate width when canvas appears or becomes hidden
useEffect(() => {
adjustCanvasSize();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [adjustCanvasSize, isShown]);

// cleanup before moving to next page
useEffect(() => {
resetCanvas();
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [pageNum, isShown]);

// initialize text event listener
useEffect(() => {
if (isShown && role !== pb.ControllerRole.HUB) {
Expand Down
14 changes: 14 additions & 0 deletions fableous-fe/src/components/canvas/helpers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,3 +68,17 @@ export const getTextBounds = (
}
return [0, 0, 0, 0];
};

export const clearCanvas = (canvasRef: MutableRefObject<HTMLCanvasElement>) => {
const ctx = canvasRef.current.getContext("2d");
if (ctx) {
const { width, height } = canvasRef.current;
ctx.clearRect(0, 0, width, height);
ctx.beginPath();
ctx.strokeStyle = "white";
ctx.lineWidth = 200;
ctx.rect(-100, -100, width + 200, height + 200);
ctx.stroke();
ctx.closePath();
}
};

0 comments on commit 6a6f22d

Please sign in to comment.