fix: save mask on initial

This commit is contained in:
jialin
2025-04-18 14:40:26 +08:00
parent 5d8d38bdf0
commit ed5afff5d7
+6 -42
View File
@@ -170,36 +170,6 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = forwardRef(
link.click(); link.click();
}, [generateMask]); }, [generateMask]);
const drawFillRect = (
ctx: CanvasRenderingContext2D,
stroke: Stroke | Point[],
options: {
lineWidth?: number;
color: string;
isInitial?: boolean;
}
) => {
const { color, isInitial } = options;
stroke.forEach((point) => {
const { x, y } = getTransformedPoint(point.x, point.y);
const width = getTransformLineWidth(point.lineWidth);
console.log('Drawing stroke:', point, width);
if (isInitial) {
ctx.save();
ctx.fillStyle = 'rgba(0,0,0,1)';
ctx.globalCompositeOperation = 'destination-out';
ctx.fillRect(x - width / 2, y - width / 2, width, width);
ctx.restore();
}
// draw the new stroke
ctx.globalCompositeOperation = 'source-over';
ctx.fillStyle = color;
ctx.fillRect(x - width / 2, y - width / 2, width, width);
});
};
const onReset = useCallback(() => { const onReset = useCallback(() => {
clearOverlayCanvas(); clearOverlayCanvas();
setStrokes([]); setStrokes([]);
@@ -238,6 +208,7 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = forwardRef(
ctx.putImageData(event.data.imageData, 0, 0); ctx.putImageData(event.data.imageData, 0, 0);
isLoadingMaskRef.current = false; isLoadingMaskRef.current = false;
saveImage();
} }
}; };
@@ -399,6 +370,7 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = forwardRef(
ctx.putImageData(event.data.imageData, 0, 0); ctx.putImageData(event.data.imageData, 0, 0);
isLoadingMaskRef.current = false; isLoadingMaskRef.current = false;
saveImage();
} }
}; };
@@ -434,7 +406,7 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = forwardRef(
height: canvasRef.current!.height height: canvasRef.current!.height
}); });
console.log('Image status:', imageStatus, invertMask); console.log('Image status:', imageStatus, negativeMaskRef.current);
if (imageStatus.isResetNeeded) { if (imageStatus.isResetNeeded) {
onReset(); onReset();
@@ -442,26 +414,19 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = forwardRef(
} else if ( } else if (
(strokesRef.current.length || maskStorkeRef.current.length) && (strokesRef.current.length || maskStorkeRef.current.length) &&
imageStatus.isOriginal && imageStatus.isOriginal &&
!invertMask !negativeMaskRef.current
) { ) {
redrawStrokes(strokesRef.current); redrawStrokes(strokesRef.current);
saveImage();
} else if ( } else if (
(strokesRef.current.length || maskStorkeRef.current.length) && (strokesRef.current.length || maskStorkeRef.current.length) &&
imageStatus.isOriginal && imageStatus.isOriginal &&
invertMask negativeMaskRef.current
) { ) {
invertPainting(true); invertPainting(true);
saveImage();
} }
updateCursorSize(); updateCursorSize();
}, [ }, [drawImage, imageStatus.isOriginal, imageStatus.isResetNeeded]);
drawImage,
imageStatus.isOriginal,
imageStatus.isResetNeeded,
invertMask
]);
const handleFitView = () => { const handleFitView = () => {
fitView(); fitView();
@@ -480,7 +445,6 @@ const CanvasImageEditor: React.FC<CanvasImageEditorProps> = forwardRef(
negativeMaskRef.current = e.target.checked; negativeMaskRef.current = e.target.checked;
invertPainting(e.target.checked); invertPainting(e.target.checked);
setInvertMask(e.target.checked); setInvertMask(e.target.checked);
saveImage();
}; };
useEffect(() => { useEffect(() => {