Skip to content

Commit

Permalink
Fix canvas disposing
Browse files Browse the repository at this point in the history
  • Loading branch information
roman-drozd-it committed Oct 3, 2024
1 parent caccf19 commit 63b4e07
Showing 1 changed file with 24 additions and 32 deletions.
56 changes: 24 additions & 32 deletions src/components/drawing/DrawingView.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,48 +105,41 @@ const DrawingView = ({ width, height }: DrawingViewProps): JSX.Element => {

});
}

return () => {
setCanvas((prevCanvas) => {
prevCanvas?.dispose();

return undefined;
});
};
}, []);

/* set canvas size */
useEffect(() => {
const resizeCanvas = () => {

const currWidth = width;
const currHeight = height - (menuRef.current?.clientHeight ?? 0);

// const currHeight = (height / aspectRatio);
const currWidth = width;
const currHeight = height - (menuRef.current?.clientHeight ?? 0);

const currScaleFactor = Math.min(currWidth / 1920, currHeight / 1080);
// const currHeight = (height / aspectRatio);

setCanvas((prevState) => {
const currScaleFactor = Math.min(currWidth / 1920, currHeight / 1080);

if (prevState) {
prevState.setWidth(currWidth);
prevState.setHeight(currHeight);
prevState.setZoom(currScaleFactor);
prevState.renderAll();
}
setCanvas((prevState) => {

return prevState;
});

setCanvasWidth(currWidth);
setCanvasHeight(currHeight);
handleSetZoom(currScaleFactor);

};
if (prevState) {
prevState.setWidth(currWidth);
prevState.setHeight(currHeight);
prevState.setZoom(currScaleFactor);
prevState.renderAll();
}

resizeCanvas();

window.addEventListener('resize', resizeCanvas);

return () => {

window.removeEventListener('resize', resizeCanvas);
return prevState;
});

canvas?.dispose();

};
setCanvasWidth(currWidth);
setCanvasHeight(currHeight);
handleSetZoom(currScaleFactor);

}, [ width, height ]);

Expand All @@ -170,7 +163,6 @@ const DrawingView = ({ width, height }: DrawingViewProps): JSX.Element => {
setSize(eraserSize);
setSizeRange(eraserSizeRange);
break;

}

}, [ canvas, tool, color, pencilBrushSize, textSize, eraserSize, zoom ]);
Expand Down

0 comments on commit 63b4e07

Please sign in to comment.