172 lines
4.0 KiB
TypeScript
172 lines
4.0 KiB
TypeScript
|
|
import { create } from 'zustand';
|
||
|
|
import type { CanvasState, Selection, Point } from '@/types';
|
||
|
|
|
||
|
|
interface CanvasStore extends CanvasState {
|
||
|
|
/** Selection state */
|
||
|
|
selection: Selection;
|
||
|
|
|
||
|
|
/** 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<Selection>) => 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,
|
||
|
|
};
|
||
|
|
},
|
||
|
|
}));
|