Files
paint-ui/store/canvas-store.ts
Sebastian Krüger 7f1e69559f feat(phase-8): implement comprehensive selection system with marching ants
This commit completes Phase 8 of the paint-ui implementation, adding a full
selection system with multiple selection tools, operations, and visual feedback.

**New Files:**
- types/selection.ts: Selection types, masks, and state interfaces
- lib/selection-utils.ts: Selection mask generation and manipulation algorithms
- lib/selection-operations.ts: Copy/cut/paste/delete/fill/stroke operations
- store/selection-store.ts: Selection state management with Zustand
- core/commands/selection-command.ts: Undo/redo commands for selections
- tools/rectangular-selection-tool.ts: Rectangular marquee selection
- tools/elliptical-selection-tool.ts: Elliptical marquee selection
- tools/lasso-selection-tool.ts: Free-form polygon selection
- tools/magic-wand-tool.ts: Color-based flood-fill selection
- components/selection/selection-panel.tsx: Complete selection UI panel
- components/selection/index.ts: Selection components barrel export

**Updated Files:**
- components/canvas/canvas-with-tools.tsx: Added selection tools integration and marching ants animation
- components/editor/editor-layout.tsx: Integrated SelectionPanel into layout
- store/index.ts: Added selection-store export
- store/canvas-store.ts: Renamed Selection to CanvasSelection to avoid conflicts
- tools/index.ts: Added selection tool exports
- types/index.ts: Added selection types export
- types/canvas.ts: Renamed Selection interface to CanvasSelection

**Selection Tools:**

**Marquee Tools:**
-  Rectangular Selection: Click-drag rectangular regions
-  Elliptical Selection: Click-drag elliptical regions

**Free-form Tools:**
-  Lasso Selection: Draw free-form polygon selections
-  Magic Wand: Color-based flood-fill selection with tolerance

**Selection Modes:**
- 🔷 New: Replace existing selection
-  Add: Add to existing selection
-  Subtract: Remove from existing selection
-  Intersect: Keep only overlapping areas

**Selection Operations:**
- 📋 Copy/Cut/Paste: Standard clipboard operations with selection mask
- 🗑️ Delete: Remove selected pixels
- 🎨 Fill Selection: Fill with current color
- 🖌️ Stroke Selection: Outline selection with current color
- 🔄 Invert Selection: Invert selected/unselected pixels
-  Clear Selection: Deselect all

**Technical Features:**
- Marching ants animation (animated dashed outline at 50ms interval)
- Selection masks using Uint8Array (0-255 values for anti-aliasing)
- Feathering support (0-250px gaussian blur on selection edges)
- Tolerance control for magic wand (0-255 color difference threshold)
- Scanline polygon fill algorithm for lasso tool
- Flood-fill with Set-based visited tracking for magic wand
- Selection bounds calculation for optimized operations
- Keyboard shortcuts (Ctrl+C, Ctrl+X, Ctrl+V, Ctrl+D, Ctrl+Shift+I)
- Undo/redo integration via selection commands
- Non-destructive operations with proper history tracking

**Algorithm Implementations:**
- Rectangular mask: Simple bounds-based pixel marking
- Elliptical mask: Distance formula from ellipse center
- Lasso mask: Scanline polygon fill with edge intersection
- Magic wand: BFS flood-fill with color tolerance matching
- Mask combination: Per-pixel operations (max, subtract, AND)
- Feathering: Separable box blur (horizontal + vertical passes)
- Mask inversion: Per-pixel NOT operation with bounds recalculation

**UI/UX Features:**
- 264px wide selection panel with all tools and operations
- Tool selection with visual feedback (highlighted active tool)
- Selection mode toggles (new/add/subtract/intersect)
- Feather and tolerance sliders with live value display
- Disabled state when no selection exists
- Keyboard shortcut hints next to operations
- Visual marching ants animation (animated dashes)

Build verified: ✓ Compiled successfully in 1302ms

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-21 02:24:12 +01:00

172 lines
4.0 KiB
TypeScript

import { create } from 'zustand';
import type { CanvasState, CanvasSelection, Point } from '@/types';
interface CanvasStore extends CanvasState {
/** Selection state */
selection: CanvasSelection;
/** Set canvas dimensions */
setDimensions: (width: number, height: number) => void;
/** Set zoom level */
setZoom: (zoom: number) => void;
/** Zoom in (1.2x) */
zoomIn: () => void;
/** Zoom out (0.8x) */
zoomOut: () => void;
/** Fit canvas to viewport */
zoomToFit: (viewportWidth: number, viewportHeight: number) => void;
/** Zoom to actual size (100%) */
zoomToActual: () => void;
/** Set pan offset */
setPanOffset: (x: number, y: number) => void;
/** Pan by delta */
pan: (dx: number, dy: number) => void;
/** Reset pan to center */
resetPan: () => void;
/** Toggle grid visibility */
toggleGrid: () => void;
/** Set grid size */
setGridSize: (size: number) => void;
/** Toggle rulers */
toggleRulers: () => void;
/** Toggle snap to grid */
toggleSnapToGrid: () => void;
/** Set background color */
setBackgroundColor: (color: string) => void;
/** Set selection */
setSelection: (selection: Partial<CanvasSelection>) => void;
/** Clear selection */
clearSelection: () => void;
/** Convert screen coordinates to canvas coordinates */
screenToCanvas: (screenX: number, screenY: number) => Point;
/** Convert canvas coordinates to screen coordinates */
canvasToScreen: (canvasX: number, canvasY: number) => Point;
}
const DEFAULT_CANVAS_WIDTH = 800;
const DEFAULT_CANVAS_HEIGHT = 600;
const MIN_ZOOM = 0.1;
const MAX_ZOOM = 10;
const ZOOM_STEP = 1.2;
export const useCanvasStore = create<CanvasStore>((set, get) => ({
width: DEFAULT_CANVAS_WIDTH,
height: DEFAULT_CANVAS_HEIGHT,
zoom: 1,
offsetX: 0,
offsetY: 0,
backgroundColor: '#ffffff',
showGrid: false,
gridSize: 20,
showRulers: true,
snapToGrid: false,
selection: {
active: false,
x: 0,
y: 0,
width: 0,
height: 0,
},
setDimensions: (width, height) => {
set({ width, height });
},
setZoom: (zoom) => {
const clampedZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, zoom));
set({ zoom: clampedZoom });
},
zoomIn: () => {
const { zoom } = get();
get().setZoom(zoom * ZOOM_STEP);
},
zoomOut: () => {
const { zoom } = get();
get().setZoom(zoom / ZOOM_STEP);
},
zoomToFit: (viewportWidth, viewportHeight) => {
const { width, height } = get();
const padding = 40;
const scaleX = (viewportWidth - padding * 2) / width;
const scaleY = (viewportHeight - padding * 2) / height;
const zoom = Math.min(scaleX, scaleY, 1);
set({ zoom, offsetX: 0, offsetY: 0 });
},
zoomToActual: () => {
set({ zoom: 1 });
},
setPanOffset: (x, y) => {
set({ offsetX: x, offsetY: y });
},
pan: (dx, dy) => {
set((state) => ({
offsetX: state.offsetX + dx,
offsetY: state.offsetY + dy,
}));
},
resetPan: () => {
set({ offsetX: 0, offsetY: 0 });
},
toggleGrid: () => {
set((state) => ({ showGrid: !state.showGrid }));
},
setGridSize: (size) => {
set({ gridSize: Math.max(1, size) });
},
toggleRulers: () => {
set((state) => ({ showRulers: !state.showRulers }));
},
toggleSnapToGrid: () => {
set((state) => ({ snapToGrid: !state.snapToGrid }));
},
setBackgroundColor: (color) => {
set({ backgroundColor: color });
},
setSelection: (selection) => {
set((state) => ({
selection: { ...state.selection, ...selection },
}));
},
clearSelection: () => {
set({
selection: {
active: false,
x: 0,
y: 0,
width: 0,
height: 0,
},
});
},
screenToCanvas: (screenX, screenY) => {
const { zoom, offsetX, offsetY } = get();
return {
x: (screenX - offsetX) / zoom,
y: (screenY - offsetY) / zoom,
};
},
canvasToScreen: (canvasX, canvasY) => {
const { zoom, offsetX, offsetY } = get();
return {
x: canvasX * zoom + offsetX,
y: canvasY * zoom + offsetY,
};
},
}));