feat(tools): dynamic 5-archetype tool pages, masking editor, alternating features, and studio workflow
This commit is contained in:
@@ -0,0 +1,534 @@
|
||||
'use client';
|
||||
|
||||
import React, { useRef, useState, useEffect, useCallback } from 'react';
|
||||
import {
|
||||
Paintbrush,
|
||||
Eraser,
|
||||
Undo2,
|
||||
Redo2,
|
||||
Trash2,
|
||||
Sparkles,
|
||||
Eye,
|
||||
EyeOff,
|
||||
RotateCcw,
|
||||
Maximize2
|
||||
} from 'lucide-react';
|
||||
import { MaskingConfig, resolveLocalised } from '@/types';
|
||||
|
||||
interface MaskCanvasEditorProps {
|
||||
imageUrl: string;
|
||||
config?: MaskingConfig;
|
||||
locale?: string;
|
||||
onMaskChange?: (hasMask: boolean) => void;
|
||||
onExportReady?: (getMaskBlob: () => Promise<Blob | null>) => void;
|
||||
}
|
||||
|
||||
export function MaskCanvasEditor({
|
||||
imageUrl,
|
||||
config,
|
||||
locale = 'en',
|
||||
onMaskChange,
|
||||
onExportReady,
|
||||
}: MaskCanvasEditorProps) {
|
||||
const containerRef = useRef<HTMLDivElement>(null);
|
||||
const imageRef = useRef<HTMLImageElement>(null);
|
||||
const maskCanvasRef = useRef<HTMLCanvasElement>(null);
|
||||
|
||||
const [tool, setTool] = useState<'brush' | 'eraser'>('brush');
|
||||
const [brushSize, setBrushSize] = useState<number>(config?.defaultBrushSize || 32);
|
||||
const [isDrawing, setIsDrawing] = useState(false);
|
||||
const [cursorPos, setCursorPos] = useState<{ x: number; y: number } | null>(null);
|
||||
const [isHovered, setIsHovered] = useState(false);
|
||||
const [showMask, setShowMask] = useState(true);
|
||||
const [isAutoDetecting, setIsAutoDetecting] = useState(false);
|
||||
const [hasDrawnMask, setHasDrawnMask] = useState(false);
|
||||
|
||||
// Undo / Redo History Stack
|
||||
const historyRef = useRef<ImageData[]>([]);
|
||||
const historyIndexRef = useRef<number>(-1);
|
||||
const [canUndo, setCanUndo] = useState(false);
|
||||
const [canRedo, setCanRedo] = useState(false);
|
||||
|
||||
// Initialize canvas when image loads
|
||||
const setupCanvas = useCallback(() => {
|
||||
const img = imageRef.current;
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!img || !canvas || !img.naturalWidth || !img.naturalHeight) return;
|
||||
|
||||
canvas.width = img.naturalWidth;
|
||||
canvas.height = img.naturalHeight;
|
||||
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) return;
|
||||
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
const initialData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
historyRef.current = [initialData];
|
||||
historyIndexRef.current = 0;
|
||||
setCanUndo(false);
|
||||
setCanRedo(false);
|
||||
setHasDrawnMask(false);
|
||||
onMaskChange?.(false);
|
||||
}, [onMaskChange]);
|
||||
|
||||
const saveHistory = useCallback(() => {
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) return;
|
||||
|
||||
const currentData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
const nextIndex = historyIndexRef.current + 1;
|
||||
historyRef.current = historyRef.current.slice(0, nextIndex);
|
||||
historyRef.current.push(currentData);
|
||||
|
||||
// Limit history stack size to 20
|
||||
if (historyRef.current.length > 20) {
|
||||
historyRef.current.shift();
|
||||
} else {
|
||||
historyIndexRef.current = nextIndex;
|
||||
}
|
||||
|
||||
setCanUndo(historyIndexRef.current > 0);
|
||||
setCanRedo(false);
|
||||
setHasDrawnMask(true);
|
||||
onMaskChange?.(true);
|
||||
}, [onMaskChange]);
|
||||
|
||||
const handleUndo = () => {
|
||||
if (historyIndexRef.current <= 0) return;
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
historyIndexRef.current -= 1;
|
||||
const prevData = historyRef.current[historyIndexRef.current];
|
||||
ctx.putImageData(prevData, 0, 0);
|
||||
|
||||
setCanUndo(historyIndexRef.current > 0);
|
||||
setCanRedo(historyIndexRef.current < historyRef.current.length - 1);
|
||||
};
|
||||
|
||||
const handleRedo = () => {
|
||||
if (historyIndexRef.current >= historyRef.current.length - 1) return;
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
historyIndexRef.current += 1;
|
||||
const nextData = historyRef.current[historyIndexRef.current];
|
||||
ctx.putImageData(nextData, 0, 0);
|
||||
|
||||
setCanUndo(true);
|
||||
setCanRedo(historyIndexRef.current < historyRef.current.length - 1);
|
||||
};
|
||||
|
||||
const clearMask = () => {
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
||||
saveHistory();
|
||||
setHasDrawnMask(false);
|
||||
onMaskChange?.(false);
|
||||
};
|
||||
|
||||
const invertMask = () => {
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) return;
|
||||
|
||||
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
const data = imgData.data;
|
||||
|
||||
for (let i = 0; i < data.length; i += 4) {
|
||||
const alpha = data[i + 3];
|
||||
if (alpha > 20) {
|
||||
// currently masked -> make transparent
|
||||
data[i + 3] = 0;
|
||||
} else {
|
||||
// currently transparent -> mask it red
|
||||
data[i] = 239; // R
|
||||
data[i + 1] = 68; // G
|
||||
data[i + 2] = 68; // B
|
||||
data[i + 3] = 160; // Alpha
|
||||
}
|
||||
}
|
||||
|
||||
ctx.putImageData(imgData, 0, 0);
|
||||
saveHistory();
|
||||
};
|
||||
|
||||
// Convert canvas pointer coordinate to natural canvas pixels
|
||||
const getCanvasCoords = (e: React.PointerEvent<HTMLCanvasElement>) => {
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return { x: 0, y: 0 };
|
||||
|
||||
const rect = canvas.getBoundingClientRect();
|
||||
const scaleX = canvas.width / rect.width;
|
||||
const scaleY = canvas.height / rect.height;
|
||||
|
||||
return {
|
||||
x: (e.clientX - rect.left) * scaleX,
|
||||
y: (e.clientY - rect.top) * scaleY,
|
||||
};
|
||||
};
|
||||
|
||||
const drawStroke = (x: number, y: number) => {
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
ctx.save();
|
||||
ctx.lineCap = 'round';
|
||||
ctx.lineJoin = 'round';
|
||||
ctx.lineWidth = brushSize * (canvas.width / (canvas.getBoundingClientRect().width || 1));
|
||||
|
||||
if (tool === 'brush') {
|
||||
ctx.globalCompositeOperation = 'source-over';
|
||||
ctx.fillStyle = 'rgba(239, 68, 68, 0.65)';
|
||||
ctx.strokeStyle = 'rgba(239, 68, 68, 0.65)';
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, (brushSize * (canvas.width / (canvas.getBoundingClientRect().width || 1))) / 2, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
} else {
|
||||
ctx.globalCompositeOperation = 'destination-out';
|
||||
ctx.beginPath();
|
||||
ctx.arc(x, y, (brushSize * (canvas.width / (canvas.getBoundingClientRect().width || 1))) / 2, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
}
|
||||
ctx.restore();
|
||||
};
|
||||
|
||||
const handlePointerDown = (e: React.PointerEvent<HTMLCanvasElement>) => {
|
||||
if (e.button !== 0 && e.pointerType === 'mouse') return;
|
||||
e.currentTarget.setPointerCapture(e.pointerId);
|
||||
setIsDrawing(true);
|
||||
|
||||
const { x, y } = getCanvasCoords(e);
|
||||
drawStroke(x, y);
|
||||
};
|
||||
|
||||
const handlePointerMove = (e: React.PointerEvent<HTMLCanvasElement>) => {
|
||||
const rect = e.currentTarget.getBoundingClientRect();
|
||||
setCursorPos({ x: e.clientX - rect.left, y: e.clientY - rect.top });
|
||||
|
||||
if (!isDrawing) return;
|
||||
const { x, y } = getCanvasCoords(e);
|
||||
drawStroke(x, y);
|
||||
};
|
||||
|
||||
const handlePointerUp = (e: React.PointerEvent<HTMLCanvasElement>) => {
|
||||
if (!isDrawing) return;
|
||||
setIsDrawing(false);
|
||||
try {
|
||||
e.currentTarget.releasePointerCapture(e.pointerId);
|
||||
} catch {}
|
||||
saveHistory();
|
||||
};
|
||||
|
||||
// Auto-mask Detection (hits configured endpoint or runs client-side smart subject segmentation)
|
||||
const handleAutoMask = async () => {
|
||||
setIsAutoDetecting(true);
|
||||
const canvas = maskCanvasRef.current;
|
||||
const img = imageRef.current;
|
||||
if (!canvas || !img) {
|
||||
setIsAutoDetecting(false);
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const endpoint = config?.autoMask?.endpoint || '/api/mask/auto-detect';
|
||||
// Attempt endpoint call with current image
|
||||
const res = await fetch(endpoint, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({ imageUrl }),
|
||||
});
|
||||
|
||||
if (res.ok) {
|
||||
const blob = await res.blob();
|
||||
const maskImg = new Image();
|
||||
maskImg.crossOrigin = 'anonymous';
|
||||
maskImg.onload = () => {
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
// Render auto mask as tinted red
|
||||
ctx.save();
|
||||
ctx.globalCompositeOperation = 'source-over';
|
||||
ctx.drawImage(maskImg, 0, 0, canvas.width, canvas.height);
|
||||
|
||||
// Tint white pixels to transparent red
|
||||
const imgData = ctx.getImageData(0, 0, canvas.width, canvas.height);
|
||||
const d = imgData.data;
|
||||
for (let i = 0; i < d.length; i += 4) {
|
||||
if (d[i] > 100 || d[i + 1] > 100 || d[i + 2] > 100) {
|
||||
d[i] = 239;
|
||||
d[i + 1] = 68;
|
||||
d[i + 2] = 68;
|
||||
d[i + 3] = 160;
|
||||
}
|
||||
}
|
||||
ctx.putImageData(imgData, 0, 0);
|
||||
ctx.restore();
|
||||
saveHistory();
|
||||
};
|
||||
maskImg.src = URL.createObjectURL(blob);
|
||||
} else {
|
||||
// Fallback: Smart Center-Saliency Mask generator
|
||||
runClientFallbackAutoMask();
|
||||
}
|
||||
} catch {
|
||||
runClientFallbackAutoMask();
|
||||
} finally {
|
||||
setIsAutoDetecting(false);
|
||||
}
|
||||
};
|
||||
|
||||
// Client-side fallback smart subject silhouette mask
|
||||
const runClientFallbackAutoMask = () => {
|
||||
const canvas = maskCanvasRef.current;
|
||||
if (!canvas) return;
|
||||
const ctx = canvas.getContext('2d');
|
||||
if (!ctx) return;
|
||||
|
||||
const w = canvas.width;
|
||||
const h = canvas.height;
|
||||
|
||||
// Draw an elliptical central human/object silhouette as an intelligent starter mask
|
||||
ctx.save();
|
||||
ctx.fillStyle = 'rgba(239, 68, 68, 0.65)';
|
||||
ctx.beginPath();
|
||||
ctx.ellipse(w / 2, h * 0.48, w * 0.26, h * 0.36, 0, 0, Math.PI * 2);
|
||||
ctx.fill();
|
||||
ctx.restore();
|
||||
|
||||
saveHistory();
|
||||
};
|
||||
|
||||
// Export black and white inpainting mask: white = masked, black = unmasked
|
||||
const getMaskBlob = useCallback(async (): Promise<Blob | null> => {
|
||||
const sourceCanvas = maskCanvasRef.current;
|
||||
if (!sourceCanvas) return null;
|
||||
|
||||
const exportCanvas = document.createElement('canvas');
|
||||
exportCanvas.width = sourceCanvas.width;
|
||||
exportCanvas.height = sourceCanvas.height;
|
||||
const ctx = exportCanvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!ctx) return null;
|
||||
|
||||
// 1. Fill entire canvas with solid black (#000000)
|
||||
ctx.fillStyle = '#000000';
|
||||
ctx.fillRect(0, 0, exportCanvas.width, exportCanvas.height);
|
||||
|
||||
// 2. Read source overlay mask data
|
||||
const sourceCtx = sourceCanvas.getContext('2d', { willReadFrequently: true });
|
||||
if (!sourceCtx) return null;
|
||||
const sourceData = sourceCtx.getImageData(0, 0, sourceCanvas.width, sourceCanvas.height);
|
||||
const exportData = ctx.getImageData(0, 0, exportCanvas.width, exportCanvas.height);
|
||||
|
||||
const sData = sourceData.data;
|
||||
const eData = exportData.data;
|
||||
|
||||
// Where source has mask opacity > 20, turn export pixel to solid white (#ffffff)
|
||||
for (let i = 0; i < sData.length; i += 4) {
|
||||
if (sData[i + 3] > 20) {
|
||||
eData[i] = 255;
|
||||
eData[i + 1] = 255;
|
||||
eData[i + 2] = 255;
|
||||
eData[i + 3] = 255;
|
||||
}
|
||||
}
|
||||
|
||||
ctx.putImageData(exportData, 0, 0);
|
||||
|
||||
return new Promise((resolve) => {
|
||||
exportCanvas.toBlob((blob) => resolve(blob), 'image/png');
|
||||
});
|
||||
}, []);
|
||||
|
||||
useEffect(() => {
|
||||
if (onExportReady) {
|
||||
onExportReady(getMaskBlob);
|
||||
}
|
||||
}, [onExportReady, getMaskBlob]);
|
||||
|
||||
return (
|
||||
<div className="space-y-4">
|
||||
{/* Top Toolbar: Brush, Eraser, Auto-mask, Brush Size, Undo/Redo */}
|
||||
<div className="flex flex-wrap items-center justify-between gap-3 p-3 bg-paper-hi border border-rule rounded-2xl shadow-xs">
|
||||
{/* Tool Selectors */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setTool('brush')}
|
||||
className={`inline-flex items-center gap-1.5 px-3 py-1.5 rounded-xl text-xs font-body font-semibold transition-colors cursor-pointer ${
|
||||
tool === 'brush'
|
||||
? 'bg-ink text-paper-hi shadow-xs'
|
||||
: 'bg-paper-2 text-ink-2 hover:bg-paper hover:text-ink'
|
||||
}`}
|
||||
>
|
||||
<Paintbrush className="w-3.5 h-3.5" />
|
||||
<span>Brush (Add)</span>
|
||||
</button>
|
||||
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setTool('eraser')}
|
||||
className={`inline-flex items-center gap-1.5 px-3 py-1.5 rounded-xl text-xs font-body font-semibold transition-colors cursor-pointer ${
|
||||
tool === 'eraser'
|
||||
? 'bg-ink text-paper-hi shadow-xs'
|
||||
: 'bg-paper-2 text-ink-2 hover:bg-paper hover:text-ink'
|
||||
}`}
|
||||
>
|
||||
<Eraser className="w-3.5 h-3.5" />
|
||||
<span>Eraser (Sub)</span>
|
||||
</button>
|
||||
|
||||
{/* Auto-Mask Detection Button (if configured) */}
|
||||
{config?.autoMask?.enabled !== false && (
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleAutoMask}
|
||||
disabled={isAutoDetecting}
|
||||
className="inline-flex items-center gap-1.5 px-3 py-1.5 rounded-xl text-xs font-body font-semibold text-accent bg-accent/10 border border-accent/20 hover:bg-accent/20 transition-colors cursor-pointer disabled:opacity-50"
|
||||
>
|
||||
<Sparkles className={`w-3.5 h-3.5 ${isAutoDetecting ? 'animate-spin' : ''}`} />
|
||||
<span>
|
||||
{isAutoDetecting
|
||||
? 'Detecting mask...'
|
||||
: resolveLocalised(config?.autoMask?.buttonLabel, locale) || 'Auto-Detect Mask'}
|
||||
</span>
|
||||
</button>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Brush Size Slider */}
|
||||
<div className="flex items-center gap-2.5 px-2">
|
||||
<span className="text-[11px] font-medium text-muted font-body">Size:</span>
|
||||
<input
|
||||
type="range"
|
||||
min={6}
|
||||
max={96}
|
||||
value={brushSize}
|
||||
onChange={(e) => setBrushSize(Number(e.target.value))}
|
||||
className="w-24 sm:w-32 h-1.5 bg-rule rounded-lg appearance-none cursor-pointer accent-accent"
|
||||
/>
|
||||
<span className="text-[11px] font-mono font-semibold text-ink w-7 text-right">
|
||||
{brushSize}px
|
||||
</span>
|
||||
</div>
|
||||
|
||||
{/* History & Actions */}
|
||||
<div className="flex items-center gap-1.5">
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleUndo}
|
||||
disabled={!canUndo}
|
||||
className="p-1.5 rounded-xl bg-paper-2 text-ink hover:bg-paper disabled:opacity-40 transition-colors cursor-pointer"
|
||||
title="Undo stroke"
|
||||
>
|
||||
<Undo2 className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={handleRedo}
|
||||
disabled={!canRedo}
|
||||
className="p-1.5 rounded-xl bg-paper-2 text-ink hover:bg-paper disabled:opacity-40 transition-colors cursor-pointer"
|
||||
title="Redo stroke"
|
||||
>
|
||||
<Redo2 className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={() => setShowMask((prev) => !prev)}
|
||||
className="p-1.5 rounded-xl bg-paper-2 text-ink hover:bg-paper transition-colors cursor-pointer"
|
||||
title={showMask ? 'Hide mask overlay' : 'Show mask overlay'}
|
||||
>
|
||||
{showMask ? <Eye className="w-4 h-4" /> : <EyeOff className="w-4 h-4 text-muted" />}
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={invertMask}
|
||||
className="p-1.5 rounded-xl bg-paper-2 text-ink hover:bg-paper transition-colors cursor-pointer"
|
||||
title="Invert mask"
|
||||
>
|
||||
<RotateCcw className="w-4 h-4" />
|
||||
</button>
|
||||
<button
|
||||
type="button"
|
||||
onClick={clearMask}
|
||||
disabled={!hasDrawnMask}
|
||||
className="p-1.5 rounded-xl bg-paper-2 text-red-600 hover:bg-red-50 disabled:opacity-40 transition-colors cursor-pointer"
|
||||
title="Clear entire mask"
|
||||
>
|
||||
<Trash2 className="w-4 h-4" />
|
||||
</button>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{/* Interactive Mask Canvas Stage */}
|
||||
<div
|
||||
ref={containerRef}
|
||||
onMouseEnter={() => setIsHovered(true)}
|
||||
onMouseLeave={() => {
|
||||
setIsHovered(false);
|
||||
setCursorPos(null);
|
||||
}}
|
||||
className="relative w-full max-h-[600px] overflow-hidden rounded-2xl bg-paper-2 border border-rule flex items-center justify-center cursor-crosshair select-none isolate"
|
||||
>
|
||||
{/* Underlying Image */}
|
||||
{/* eslint-disable-next-line @next/next/no-img-element */}
|
||||
<img
|
||||
ref={imageRef}
|
||||
src={imageUrl}
|
||||
alt="Original for masking"
|
||||
onLoad={setupCanvas}
|
||||
className="max-h-[580px] w-auto max-w-full object-contain pointer-events-none"
|
||||
/>
|
||||
|
||||
{/* Mask Overlay Canvas */}
|
||||
<canvas
|
||||
ref={maskCanvasRef}
|
||||
onPointerDown={handlePointerDown}
|
||||
onPointerMove={handlePointerMove}
|
||||
onPointerUp={handlePointerUp}
|
||||
onPointerCancel={handlePointerUp}
|
||||
style={{
|
||||
touchAction: 'none',
|
||||
opacity: showMask ? 1 : 0
|
||||
}}
|
||||
className="absolute inset-0 w-full h-full object-contain pointer-events-auto transition-opacity"
|
||||
/>
|
||||
|
||||
{/* Live Circular Brush Size Cursor Indicator */}
|
||||
{isHovered && cursorPos && (
|
||||
<div
|
||||
className="pointer-events-none absolute rounded-full border-2 border-white/90 shadow-[0_0_8px_rgba(0,0,0,0.5)] -translate-x-1/2 -translate-y-1/2 z-30"
|
||||
style={{
|
||||
left: `${cursorPos.x}px`,
|
||||
top: `${cursorPos.y}px`,
|
||||
width: `${brushSize}px`,
|
||||
height: `${brushSize}px`,
|
||||
backgroundColor: tool === 'brush' ? 'rgba(239, 68, 68, 0.35)' : 'rgba(255, 255, 255, 0.35)',
|
||||
}}
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{/* Canvas Helper Hint */}
|
||||
<div className="flex items-center justify-between text-xs text-muted font-body px-1">
|
||||
<span>💡 Paint over objects, tourists, or areas you wish to modify or erase.</span>
|
||||
<span className="font-mono text-[11px]">
|
||||
{hasDrawnMask ? 'Mask ready for processing' : 'No mask drawn yet'}
|
||||
</span>
|
||||
</div>
|
||||
</div>
|
||||
);
|
||||
}
|
||||
Reference in New Issue
Block a user