Line data Source code
1 : import { ref, computed } from 'vue'
2 :
3 : // Define the state interface for better readability
4 : interface CanvasState {
5 : mask: ImageData | ImageBitmap
6 : rgb: ImageData | ImageBitmap
7 : img: ImageData | ImageBitmap
8 : }
9 :
10 : export interface CanvasHistoryLayers {
11 : maskCanvas: HTMLCanvasElement | null
12 : maskCtx: CanvasRenderingContext2D | null
13 : rgbCanvas: HTMLCanvasElement | null
14 : rgbCtx: CanvasRenderingContext2D | null
15 : imgCanvas: HTMLCanvasElement | null
16 : imgCtx: CanvasRenderingContext2D | null
17 : }
18 :
19 2355 : export function useCanvasHistory(layers: CanvasHistoryLayers, maxStates = 20) {
20 2355 : const states = ref<CanvasState[]>([])
21 2355 : const currentStateIndex = ref(-1)
22 2355 : const initialized = ref(false)
23 :
24 2355 : const canUndo = computed(
25 : () => states.value.length > 1 && currentStateIndex.value > 0
26 : )
27 :
28 2355 : const canRedo = computed(() => {
29 1 : return (
30 : states.value.length > 1 &&
31 : currentStateIndex.value < states.value.length - 1
32 : )
33 : })
34 :
35 2355 : const saveInitialState = () => {
36 29 : const { maskCtx, rgbCtx, imgCtx, maskCanvas, rgbCanvas, imgCanvas } = layers
37 :
38 : // Ensure all 3 contexts and canvases are ready
39 29 : if (
40 : !maskCtx ||
41 : !rgbCtx ||
42 : !imgCtx ||
43 : !maskCanvas ||
44 : !rgbCanvas ||
45 : !imgCanvas
46 : ) {
47 0 : requestAnimationFrame(saveInitialState)
48 0 : return
49 : }
50 :
51 29 : if (!maskCanvas.width || !rgbCanvas.width || !imgCanvas.width) {
52 0 : requestAnimationFrame(saveInitialState)
53 0 : return
54 : }
55 :
56 29 : states.value = []
57 :
58 : // Capture all three layers
59 29 : const maskState = maskCtx.getImageData(
60 : 0,
61 : 0,
62 : maskCanvas.width,
63 : maskCanvas.height
64 : )
65 29 : const rgbState = rgbCtx.getImageData(
66 : 0,
67 : 0,
68 : rgbCanvas.width,
69 : rgbCanvas.height
70 : )
71 29 : const imgState = imgCtx.getImageData(
72 : 0,
73 : 0,
74 : imgCanvas.width,
75 : imgCanvas.height
76 : )
77 :
78 29 : states.value.push({ mask: maskState, rgb: rgbState, img: imgState })
79 29 : currentStateIndex.value = 0
80 29 : initialized.value = true
81 : }
82 :
83 2355 : const saveState = (
84 : providedMaskData?: ImageData | ImageBitmap,
85 : providedRgbData?: ImageData | ImageBitmap,
86 : providedImgData?: ImageData | ImageBitmap
87 : ) => {
88 21 : const { maskCtx, rgbCtx, imgCtx, maskCanvas, rgbCanvas, imgCanvas } = layers
89 :
90 21 : if (
91 : !maskCtx ||
92 : !rgbCtx ||
93 : !imgCtx ||
94 : !maskCanvas ||
95 : !rgbCanvas ||
96 : !imgCanvas
97 : )
98 0 : return
99 :
100 21 : if (!initialized.value || currentStateIndex.value === -1) {
101 0 : saveInitialState()
102 0 : return
103 : }
104 :
105 : // Clear redo history
106 21 : states.value = states.value.slice(0, currentStateIndex.value + 1)
107 :
108 21 : let maskState: ImageData | ImageBitmap
109 21 : let rgbState: ImageData | ImageBitmap
110 21 : let imgState: ImageData | ImageBitmap
111 :
112 21 : if (providedMaskData && providedRgbData && providedImgData) {
113 0 : maskState = providedMaskData
114 0 : rgbState = providedRgbData
115 0 : imgState = providedImgData
116 : } else {
117 21 : maskState = maskCtx.getImageData(
118 : 0,
119 : 0,
120 : maskCanvas.width,
121 : maskCanvas.height
122 : )
123 21 : rgbState = rgbCtx.getImageData(0, 0, rgbCanvas.width, rgbCanvas.height)
124 21 : imgState = imgCtx.getImageData(0, 0, imgCanvas.width, imgCanvas.height)
125 : }
126 :
127 21 : states.value.push({ mask: maskState, rgb: rgbState, img: imgState })
128 21 : currentStateIndex.value++
129 :
130 : // Maintain max history size and clean up memory
131 21 : if (states.value.length > maxStates) {
132 0 : const removed = states.value.shift()
133 0 : if (removed) {
134 0 : cleanupState(removed)
135 : }
136 0 : currentStateIndex.value--
137 : }
138 : }
139 :
140 2355 : const undo = () => {
141 4 : if (!canUndo.value) return
142 4 : currentStateIndex.value--
143 4 : restoreState(states.value[currentStateIndex.value])
144 : }
145 :
146 2355 : const redo = () => {
147 1 : if (!canRedo.value) return
148 1 : currentStateIndex.value++
149 1 : restoreState(states.value[currentStateIndex.value])
150 : }
151 :
152 2355 : const restoreState = (state: CanvasState) => {
153 5 : const { maskCtx, rgbCtx, imgCtx, maskCanvas, rgbCanvas, imgCanvas } = layers
154 5 : if (
155 : !maskCtx ||
156 : !rgbCtx ||
157 : !imgCtx ||
158 : !maskCanvas ||
159 : !rgbCanvas ||
160 : !imgCanvas
161 : )
162 0 : return
163 :
164 : // Update canvas dimensions to match state (handles rotation undo/redo)
165 5 : const refData = state.mask
166 5 : const newWidth = refData.width
167 5 : const newHeight = refData.height
168 :
169 5 : if (maskCanvas.width !== newWidth || maskCanvas.height !== newHeight) {
170 0 : maskCanvas.width = newWidth
171 0 : maskCanvas.height = newHeight
172 0 : rgbCanvas.width = newWidth
173 0 : rgbCanvas.height = newHeight
174 0 : imgCanvas.width = newWidth
175 0 : imgCanvas.height = newHeight
176 : }
177 :
178 : const targets = [
179 : { ctx: maskCtx, data: state.mask },
180 : { ctx: rgbCtx, data: state.rgb },
181 : { ctx: imgCtx, data: state.img }
182 : ]
183 :
184 5 : targets.forEach(({ ctx, data }) => {
185 15 : if (data instanceof ImageBitmap) {
186 0 : ctx.clearRect(0, 0, data.width, data.height)
187 0 : ctx.drawImage(data, 0, 0)
188 : } else {
189 15 : ctx.putImageData(data, 0, 0)
190 : }
191 : })
192 : }
193 :
194 2355 : const cleanupState = (state: CanvasState) => {
195 13 : if (state.mask instanceof ImageBitmap) state.mask.close()
196 13 : if (state.rgb instanceof ImageBitmap) state.rgb.close()
197 13 : if (state.img instanceof ImageBitmap) state.img.close()
198 : }
199 :
200 2355 : const clearStates = () => {
201 7 : states.value.forEach(cleanupState)
202 7 : states.value = []
203 7 : currentStateIndex.value = -1
204 7 : initialized.value = false
205 : }
206 :
207 2355 : return {
208 : canUndo,
209 : canRedo,
210 : currentStateIndex,
211 : saveInitialState,
212 : saveState,
213 : undo,
214 : redo,
215 : clearStates
216 : }
217 : }
|