Skip to content
Source
Sign inSign up
samwillis/one-million-boxes
Open History to see the last commit

useViewState.tsx

134 lines3.4 KB

1import { createContext, useCallback, useContext, useState } from "react"
2import { H, W } from "../../shared/edge-math"
3import { DEFAULT_ZOOM, DOT_SPACING, MAX_ZOOM, MIN_ZOOM, ZOOM_STEP } from "../lib/config"
4import type { ReactNode } from "react"
5​
6// Re-export zoom constants for components that need them
7export { MIN_ZOOM, MAX_ZOOM, DEFAULT_ZOOM }
8​
9export interface ViewState {
10 centerX: number // 0..W (grid width)
11 centerY: number // 0..H (grid height)
12 zoom: number // MIN_ZOOM..MAX_ZOOM
14​
15export interface CanvasSize {
16 width: number
17 height: number
19​
20export interface ViewStateActions {
21 view: ViewState
22 canvasSize: CanvasSize
23 setCanvasSize: (size: CanvasSize) => void
24 pan: (deltaX: number, deltaY: number) => void
25 zoomTo: (newZoom: number, focalX?: number, focalY?: number) => void
26 zoomIn: () => void
27 zoomOut: () => void
28 resetView: () => void
29 jumpTo: (x: number, y: number) => void
31​
32export const ViewStateContext = createContext<ViewStateActions | null>(null)
33​
34export function useViewStateContext(): ViewStateActions {
35 const context = useContext(ViewStateContext)
36 if (!context) {
37 throw new Error(`useViewStateContext must be used within a ViewStateContext.Provider`)
38 }
39 return context
41​
42export function useViewState(): ViewStateActions {
43 const [view, setView] = useState<ViewState>({
44 centerX: W / 2,
45 centerY: H / 2,
46 zoom: DEFAULT_ZOOM,
47 })
48​
49 const [canvasSize, setCanvasSize] = useState<CanvasSize>({
50 width: 0,
51 height: 0,
52 })
53​
54 const pan = useCallback((deltaX: number, deltaY: number) => {
55 setView((v) => {
56 const scale = v.zoom * DOT_SPACING
57 return {
58 ...v,
59 centerX: Math.max(0, Math.min(W, v.centerX - deltaX / scale)),
60 centerY: Math.max(0, Math.min(H, v.centerY - deltaY / scale)),
61 }
62 })
63 }, [])
64​
65 const zoomTo = useCallback((newZoom: number, focalX?: number, focalY?: number) => {
66 setView((v) => {
67 const clampedZoom = Math.max(MIN_ZOOM, Math.min(MAX_ZOOM, newZoom))
68​
69 // If focal point provided, adjust center to keep that point fixed
70 if (focalX !== undefined && focalY !== undefined) {
71 const scale = clampedZoom / v.zoom
72 return {
73 centerX: Math.max(0, Math.min(W, focalX + (v.centerX - focalX) / scale)),
74 centerY: Math.max(0, Math.min(H, focalY + (v.centerY - focalY) / scale)),
75 zoom: clampedZoom,
76 }
77 }
78​
79 return { ...v, zoom: clampedZoom }
80 })
81 }, [])
82​
83 const zoomIn = useCallback(() => {
84 setView((v) => ({
85 ...v,
86 zoom: Math.min(MAX_ZOOM, v.zoom * ZOOM_STEP),
87 }))
88 }, [])
89​
90 const zoomOut = useCallback(() => {
91 setView((v) => ({
92 ...v,
93 zoom: Math.max(MIN_ZOOM, v.zoom / ZOOM_STEP),
94 }))
95 }, [])
96​
97 const resetView = useCallback(() => {
98 setView({
99 centerX: W / 2,
100 centerY: H / 2,
101 zoom: DEFAULT_ZOOM,
102 })
103 }, [])
104​
105 const jumpTo = useCallback((x: number, y: number) => {
106 setView((v) => ({
107 ...v,
108 centerX: Math.max(0, Math.min(W, x)),
109 centerY: Math.max(0, Math.min(H, y)),
110 }))
111 }, [])
112​
113 return {
114 view,
115 canvasSize,
116 setCanvasSize,
117 pan,
118 zoomTo,
119 zoomIn,
120 zoomOut,
121 resetView,
122 jumpTo,
123 }
125​
126export interface ViewStateProviderProps {
127 children: ReactNode
129​
130export function ViewStateProvider({ children }: ViewStateProviderProps) {
131 const viewState = useViewState()
132​
133 return <ViewStateContext.Provider value={viewState}>{children}</ViewStateContext.Provider>