@pixerate/editor 0.10.0 → 0.12.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/agent/index.cjs +52 -1
- package/dist/agent/index.cjs.map +1 -1
- package/dist/agent/index.d.cts +68 -1
- package/dist/agent/index.d.ts +68 -1
- package/dist/agent/index.js +50 -1
- package/dist/agent/index.js.map +1 -1
- package/dist/canvas/index.cjs +171 -8
- package/dist/canvas/index.cjs.map +1 -1
- package/dist/canvas/index.d.cts +91 -1
- package/dist/canvas/index.d.ts +91 -1
- package/dist/canvas/index.js +163 -8
- package/dist/canvas/index.js.map +1 -1
- package/dist/image-editor/index.cjs +1350 -0
- package/dist/image-editor/index.cjs.map +1 -0
- package/dist/image-editor/index.d.cts +339 -0
- package/dist/image-editor/index.d.ts +339 -0
- package/dist/image-editor/index.js +1307 -0
- package/dist/image-editor/index.js.map +1 -0
- package/dist/index.cjs +1550 -14
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +3 -2
- package/dist/index.d.ts +3 -2
- package/dist/index.js +1523 -14
- package/dist/index.js.map +1 -1
- package/package.json +6 -1
|
@@ -0,0 +1,1307 @@
|
|
|
1
|
+
// src/image-editor/filters.ts
|
|
2
|
+
function clamp(value, min = 0, max = 255) {
|
|
3
|
+
return Math.max(min, Math.min(max, value));
|
|
4
|
+
}
|
|
5
|
+
function applyAdjustments(imageData, adjustments) {
|
|
6
|
+
const { brightness, contrast, saturation, exposure, temperature, opacity, blur } = adjustments;
|
|
7
|
+
const data = imageData.data;
|
|
8
|
+
const len = data.length;
|
|
9
|
+
const hasBrightness = brightness !== 0;
|
|
10
|
+
const hasContrast = contrast !== 0;
|
|
11
|
+
const hasSaturation = saturation !== 0;
|
|
12
|
+
const hasExposure = exposure !== 0;
|
|
13
|
+
const hasTemperature = temperature !== 0;
|
|
14
|
+
const hasOpacity = opacity < 1;
|
|
15
|
+
const contrastFactor = hasContrast ? 259 * (contrast + 255) / (255 * (259 - contrast)) : 1;
|
|
16
|
+
const exposureMultiplier = hasExposure ? Math.pow(2, exposure / 50) : 1;
|
|
17
|
+
const satMultiplier = hasSaturation ? 1 + saturation / 100 : 1;
|
|
18
|
+
const tempOffset = hasTemperature ? temperature * 0.8 : 0;
|
|
19
|
+
if (hasBrightness || hasContrast || hasSaturation || hasExposure || hasTemperature || hasOpacity) {
|
|
20
|
+
for (let i = 0; i < len; i += 4) {
|
|
21
|
+
let r = data[i];
|
|
22
|
+
let g = data[i + 1];
|
|
23
|
+
let b = data[i + 2];
|
|
24
|
+
let a = data[i + 3];
|
|
25
|
+
if (hasExposure) {
|
|
26
|
+
r = clamp(r * exposureMultiplier);
|
|
27
|
+
g = clamp(g * exposureMultiplier);
|
|
28
|
+
b = clamp(b * exposureMultiplier);
|
|
29
|
+
}
|
|
30
|
+
if (hasBrightness) {
|
|
31
|
+
const bOffset = brightness * 2.55;
|
|
32
|
+
r = clamp(r + bOffset);
|
|
33
|
+
g = clamp(g + bOffset);
|
|
34
|
+
b = clamp(b + bOffset);
|
|
35
|
+
}
|
|
36
|
+
if (hasContrast) {
|
|
37
|
+
r = clamp(contrastFactor * (r - 128) + 128);
|
|
38
|
+
g = clamp(contrastFactor * (g - 128) + 128);
|
|
39
|
+
b = clamp(contrastFactor * (b - 128) + 128);
|
|
40
|
+
}
|
|
41
|
+
if (hasSaturation) {
|
|
42
|
+
const luminance = 0.2989 * r + 0.587 * g + 0.114 * b;
|
|
43
|
+
r = clamp(luminance + (r - luminance) * satMultiplier);
|
|
44
|
+
g = clamp(luminance + (g - luminance) * satMultiplier);
|
|
45
|
+
b = clamp(luminance + (b - luminance) * satMultiplier);
|
|
46
|
+
}
|
|
47
|
+
if (hasTemperature) {
|
|
48
|
+
r = clamp(r + tempOffset);
|
|
49
|
+
b = clamp(b - tempOffset);
|
|
50
|
+
}
|
|
51
|
+
if (hasOpacity) {
|
|
52
|
+
a = clamp(a * opacity);
|
|
53
|
+
}
|
|
54
|
+
data[i] = r;
|
|
55
|
+
data[i + 1] = g;
|
|
56
|
+
data[i + 2] = b;
|
|
57
|
+
data[i + 3] = a;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
if (blur > 0) {
|
|
61
|
+
applyFastBoxBlur(imageData, Math.min(Math.round(blur), 40));
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
function applyFastBoxBlur(imageData, radius) {
|
|
65
|
+
if (radius < 1) return;
|
|
66
|
+
const w = imageData.width;
|
|
67
|
+
const h = imageData.height;
|
|
68
|
+
const src = imageData.data;
|
|
69
|
+
const temp = new Uint8ClampedArray(src.length);
|
|
70
|
+
for (let y = 0; y < h; y++) {
|
|
71
|
+
for (let x = 0; x < w; x++) {
|
|
72
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
73
|
+
let count = 0;
|
|
74
|
+
for (let k = -radius; k <= radius; k++) {
|
|
75
|
+
const px = Math.min(w - 1, Math.max(0, x + k));
|
|
76
|
+
const idx = (y * w + px) * 4;
|
|
77
|
+
r += src[idx];
|
|
78
|
+
g += src[idx + 1];
|
|
79
|
+
b += src[idx + 2];
|
|
80
|
+
a += src[idx + 3];
|
|
81
|
+
count++;
|
|
82
|
+
}
|
|
83
|
+
const dstIdx = (y * w + x) * 4;
|
|
84
|
+
temp[dstIdx] = Math.round(r / count);
|
|
85
|
+
temp[dstIdx + 1] = Math.round(g / count);
|
|
86
|
+
temp[dstIdx + 2] = Math.round(b / count);
|
|
87
|
+
temp[dstIdx + 3] = Math.round(a / count);
|
|
88
|
+
}
|
|
89
|
+
}
|
|
90
|
+
for (let x = 0; x < w; x++) {
|
|
91
|
+
for (let y = 0; y < h; y++) {
|
|
92
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
93
|
+
let count = 0;
|
|
94
|
+
for (let k = -radius; k <= radius; k++) {
|
|
95
|
+
const py = Math.min(h - 1, Math.max(0, y + k));
|
|
96
|
+
const idx = (py * w + x) * 4;
|
|
97
|
+
r += temp[idx];
|
|
98
|
+
g += temp[idx + 1];
|
|
99
|
+
b += temp[idx + 2];
|
|
100
|
+
a += temp[idx + 3];
|
|
101
|
+
count++;
|
|
102
|
+
}
|
|
103
|
+
const dstIdx = (y * w + x) * 4;
|
|
104
|
+
src[dstIdx] = Math.round(r / count);
|
|
105
|
+
src[dstIdx + 1] = Math.round(g / count);
|
|
106
|
+
src[dstIdx + 2] = Math.round(b / count);
|
|
107
|
+
src[dstIdx + 3] = Math.round(a / count);
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
function applyDepthMask(mainImageData, maskImageData, options) {
|
|
112
|
+
const { mode, depthRange, softness, invert } = options;
|
|
113
|
+
const mainData = mainImageData.data;
|
|
114
|
+
const maskData = maskImageData.data;
|
|
115
|
+
const len = Math.min(mainData.length, maskData.length);
|
|
116
|
+
const effectiveSoftness = Math.max(1, softness || 10);
|
|
117
|
+
const minDepth = depthRange[0];
|
|
118
|
+
const maxDepth = depthRange[1];
|
|
119
|
+
for (let i = 0; i < len; i += 4) {
|
|
120
|
+
let maskValue = maskData[i];
|
|
121
|
+
if (invert) {
|
|
122
|
+
maskValue = 255 - maskValue;
|
|
123
|
+
}
|
|
124
|
+
let alphaValue;
|
|
125
|
+
if (mode === "threshold") {
|
|
126
|
+
if (maskValue >= maxDepth + effectiveSoftness) {
|
|
127
|
+
alphaValue = 0;
|
|
128
|
+
} else if (maskValue > maxDepth) {
|
|
129
|
+
alphaValue = Math.round((maxDepth + effectiveSoftness - maskValue) / effectiveSoftness * 255);
|
|
130
|
+
} else if (maskValue >= minDepth) {
|
|
131
|
+
alphaValue = 255;
|
|
132
|
+
} else if (maskValue < minDepth - effectiveSoftness) {
|
|
133
|
+
alphaValue = 0;
|
|
134
|
+
} else {
|
|
135
|
+
alphaValue = Math.round((maskValue - (minDepth - effectiveSoftness)) / effectiveSoftness * 255);
|
|
136
|
+
}
|
|
137
|
+
} else {
|
|
138
|
+
alphaValue = clamp(maskValue + minDepth);
|
|
139
|
+
}
|
|
140
|
+
mainData[i + 3] = clamp(mainData[i + 3] * alphaValue / 255);
|
|
141
|
+
}
|
|
142
|
+
}
|
|
143
|
+
function removeAlpha(imageData) {
|
|
144
|
+
const data = imageData.data;
|
|
145
|
+
const len = data.length;
|
|
146
|
+
for (let i = 3; i < len; i += 4) {
|
|
147
|
+
data[i] = 255;
|
|
148
|
+
}
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
// src/image-editor/renderer.ts
|
|
152
|
+
var ImageCache = class {
|
|
153
|
+
cache = /* @__PURE__ */ new Map();
|
|
154
|
+
async get(src) {
|
|
155
|
+
const existing = this.cache.get(src);
|
|
156
|
+
if (existing && existing.complete) {
|
|
157
|
+
return existing;
|
|
158
|
+
}
|
|
159
|
+
return new Promise((resolve, reject) => {
|
|
160
|
+
if (typeof window === "undefined" || typeof Image === "undefined") {
|
|
161
|
+
reject(new Error("Image loading is only supported in browser/DOM environments"));
|
|
162
|
+
return;
|
|
163
|
+
}
|
|
164
|
+
const img = new Image();
|
|
165
|
+
img.crossOrigin = "anonymous";
|
|
166
|
+
img.onload = () => {
|
|
167
|
+
this.cache.set(src, img);
|
|
168
|
+
resolve(img);
|
|
169
|
+
};
|
|
170
|
+
img.onerror = (e) => reject(new Error(`Failed to load image from: ${src}`));
|
|
171
|
+
img.src = src;
|
|
172
|
+
});
|
|
173
|
+
}
|
|
174
|
+
set(src, img) {
|
|
175
|
+
this.cache.set(src, img);
|
|
176
|
+
}
|
|
177
|
+
clear() {
|
|
178
|
+
this.cache.clear();
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var globalImageCache = new ImageCache();
|
|
182
|
+
var ImageEditorRenderer = class {
|
|
183
|
+
imageCache = globalImageCache;
|
|
184
|
+
cachedBaseCanvas = null;
|
|
185
|
+
cachedBaseKey = "";
|
|
186
|
+
/**
|
|
187
|
+
* Helper to create a canvas element safely in browser/JSDOM.
|
|
188
|
+
*/
|
|
189
|
+
createCanvas(width, height) {
|
|
190
|
+
if (typeof document !== "undefined" && document.createElement) {
|
|
191
|
+
const c = document.createElement("canvas");
|
|
192
|
+
c.width = Math.max(1, Math.round(width));
|
|
193
|
+
c.height = Math.max(1, Math.round(height));
|
|
194
|
+
return c;
|
|
195
|
+
}
|
|
196
|
+
return {
|
|
197
|
+
width: Math.max(1, Math.round(width)),
|
|
198
|
+
height: Math.max(1, Math.round(height)),
|
|
199
|
+
getContext: () => null,
|
|
200
|
+
toDataURL: () => "",
|
|
201
|
+
toBlob: () => {
|
|
202
|
+
}
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
/**
|
|
206
|
+
* Renders the editor state onto a canvas (viewport or preview).
|
|
207
|
+
*/
|
|
208
|
+
async renderToCanvas(targetCanvas, imageSource, state, options = {}) {
|
|
209
|
+
const ctx = targetCanvas.getContext("2d");
|
|
210
|
+
if (!ctx) return;
|
|
211
|
+
let img;
|
|
212
|
+
if (typeof imageSource === "string") {
|
|
213
|
+
img = await this.imageCache.get(imageSource);
|
|
214
|
+
} else {
|
|
215
|
+
img = imageSource;
|
|
216
|
+
}
|
|
217
|
+
const naturalWidth = img.naturalWidth || img.videoWidth || img.width || 800;
|
|
218
|
+
const naturalHeight = img.naturalHeight || img.videoHeight || img.height || 600;
|
|
219
|
+
const { crop, transform, adjustments, depthMask, annotations, selectedAnnotationId, zoom, pan, isOriginalCompared } = state;
|
|
220
|
+
const vpWidth = options.viewportWidth || targetCanvas.width;
|
|
221
|
+
const vpHeight = options.viewportHeight || targetCanvas.height;
|
|
222
|
+
targetCanvas.width = vpWidth;
|
|
223
|
+
targetCanvas.height = vpHeight;
|
|
224
|
+
ctx.clearRect(0, 0, vpWidth, vpHeight);
|
|
225
|
+
if (isOriginalCompared) {
|
|
226
|
+
this.drawFittedImage(ctx, img, naturalWidth, naturalHeight, vpWidth, vpHeight, zoom, pan);
|
|
227
|
+
return;
|
|
228
|
+
}
|
|
229
|
+
const isActivelyCropping = Boolean(options.showCropOverlay || state.activeTool === "crop");
|
|
230
|
+
const cropX = !isActivelyCropping && crop ? Math.max(0, crop.x) : 0;
|
|
231
|
+
const cropY = !isActivelyCropping && crop ? Math.max(0, crop.y) : 0;
|
|
232
|
+
const cropW = !isActivelyCropping && crop ? Math.min(naturalWidth - cropX, crop.width) : naturalWidth;
|
|
233
|
+
const cropH = !isActivelyCropping && crop ? Math.min(naturalHeight - cropY, crop.height) : naturalHeight;
|
|
234
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
235
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
236
|
+
const baseW = isSwapped ? cropH : cropW;
|
|
237
|
+
const baseH = isSwapped ? cropW : cropH;
|
|
238
|
+
const baseKey = `${state.sourceUrl}-${naturalWidth}x${naturalHeight}-${cropX},${cropY},${cropW},${cropH}-${angle},${transform.flipH},${transform.flipV}-${adjustments.brightness},${adjustments.contrast},${adjustments.saturation},${adjustments.exposure},${adjustments.temperature},${adjustments.blur},${adjustments.opacity}-${depthMask.enabled},${depthMask.depthRange?.[0]}-${depthMask.depthRange?.[1]},${depthMask.softness},${depthMask.invert},${depthMask.mode}`;
|
|
239
|
+
let baseCanvas = this.cachedBaseCanvas;
|
|
240
|
+
if (!baseCanvas || this.cachedBaseKey !== baseKey) {
|
|
241
|
+
baseCanvas = this.createCanvas(baseW, baseH);
|
|
242
|
+
const baseCtx = baseCanvas.getContext("2d");
|
|
243
|
+
if (baseCtx) {
|
|
244
|
+
baseCtx.save();
|
|
245
|
+
const rad = angle * Math.PI / 180;
|
|
246
|
+
baseCtx.translate(baseW / 2, baseH / 2);
|
|
247
|
+
baseCtx.rotate(rad);
|
|
248
|
+
baseCtx.scale(transform.flipH ? -1 : 1, transform.flipV ? -1 : 1);
|
|
249
|
+
baseCtx.drawImage(
|
|
250
|
+
img,
|
|
251
|
+
cropX,
|
|
252
|
+
cropY,
|
|
253
|
+
cropW,
|
|
254
|
+
cropH,
|
|
255
|
+
-cropW / 2,
|
|
256
|
+
-cropH / 2,
|
|
257
|
+
cropW,
|
|
258
|
+
cropH
|
|
259
|
+
);
|
|
260
|
+
baseCtx.restore();
|
|
261
|
+
const imageData = baseCtx.getImageData(0, 0, baseW, baseH);
|
|
262
|
+
if (imageData) {
|
|
263
|
+
applyAdjustments(imageData, adjustments);
|
|
264
|
+
if (depthMask.enabled && depthMask.maskSource) {
|
|
265
|
+
try {
|
|
266
|
+
const maskImg = await this.imageCache.get(depthMask.maskSource);
|
|
267
|
+
const maskCanvas = this.createCanvas(baseW, baseH);
|
|
268
|
+
const maskCtx = maskCanvas.getContext("2d");
|
|
269
|
+
if (maskCtx) {
|
|
270
|
+
maskCtx.drawImage(maskImg, cropX, cropY, cropW, cropH, 0, 0, baseW, baseH);
|
|
271
|
+
const maskData = maskCtx.getImageData(0, 0, baseW, baseH);
|
|
272
|
+
if (maskData) {
|
|
273
|
+
applyDepthMask(imageData, maskData, depthMask);
|
|
274
|
+
}
|
|
275
|
+
}
|
|
276
|
+
} catch {
|
|
277
|
+
}
|
|
278
|
+
}
|
|
279
|
+
baseCtx.putImageData(imageData, 0, 0);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
this.cachedBaseCanvas = baseCanvas;
|
|
283
|
+
this.cachedBaseKey = baseKey;
|
|
284
|
+
}
|
|
285
|
+
ctx.save();
|
|
286
|
+
ctx.translate(vpWidth / 2 + pan.x, vpHeight / 2 + pan.y);
|
|
287
|
+
ctx.scale(zoom, zoom);
|
|
288
|
+
const fitScale = Math.min(vpWidth * 0.85 / baseW, vpHeight * 0.85 / baseH, 1);
|
|
289
|
+
const renderW = baseW * fitScale;
|
|
290
|
+
const renderH = baseH * fitScale;
|
|
291
|
+
ctx.drawImage(baseCanvas, -renderW / 2, -renderH / 2, renderW, renderH);
|
|
292
|
+
const scaleFactor = fitScale;
|
|
293
|
+
ctx.save();
|
|
294
|
+
ctx.translate(-renderW / 2, -renderH / 2);
|
|
295
|
+
ctx.scale(scaleFactor, scaleFactor);
|
|
296
|
+
for (const ann of annotations) {
|
|
297
|
+
await this.drawAnnotation(ctx, ann);
|
|
298
|
+
if (options.showSelectionOverlay && ann.id === selectedAnnotationId) {
|
|
299
|
+
this.drawAnnotationSelection(ctx, ann);
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (options.draftAnnotation) {
|
|
303
|
+
await this.drawAnnotation(ctx, options.draftAnnotation);
|
|
304
|
+
}
|
|
305
|
+
ctx.restore();
|
|
306
|
+
ctx.restore();
|
|
307
|
+
const isCroppingActive = Boolean(options.showCropOverlay || state.activeTool === "crop");
|
|
308
|
+
const activeCrop = options.draftCrop !== void 0 && options.draftCrop !== null ? options.draftCrop : crop ?? (isCroppingActive ? {
|
|
309
|
+
x: 0,
|
|
310
|
+
y: 0,
|
|
311
|
+
width: naturalWidth,
|
|
312
|
+
height: naturalHeight
|
|
313
|
+
} : null);
|
|
314
|
+
if (isCroppingActive && activeCrop) {
|
|
315
|
+
this.drawCropOverlay(ctx, state, vpWidth, vpHeight, naturalWidth, naturalHeight, activeCrop);
|
|
316
|
+
}
|
|
317
|
+
}
|
|
318
|
+
/**
|
|
319
|
+
* Draws an individual annotation onto the canvas.
|
|
320
|
+
*/
|
|
321
|
+
async drawAnnotation(ctx, ann) {
|
|
322
|
+
ctx.save();
|
|
323
|
+
if (ann.opacity !== void 0) {
|
|
324
|
+
ctx.globalAlpha = ann.opacity;
|
|
325
|
+
}
|
|
326
|
+
if (ann.rotation) {
|
|
327
|
+
ctx.translate(ann.x, ann.y);
|
|
328
|
+
ctx.rotate(ann.rotation * Math.PI / 180);
|
|
329
|
+
ctx.translate(-ann.x, -ann.y);
|
|
330
|
+
}
|
|
331
|
+
switch (ann.type) {
|
|
332
|
+
case "pen": {
|
|
333
|
+
const pen = ann;
|
|
334
|
+
if (pen.points.length > 0) {
|
|
335
|
+
ctx.beginPath();
|
|
336
|
+
ctx.strokeStyle = pen.strokeColor;
|
|
337
|
+
ctx.lineWidth = pen.strokeWidth;
|
|
338
|
+
ctx.lineCap = "round";
|
|
339
|
+
ctx.lineJoin = "round";
|
|
340
|
+
ctx.moveTo(pen.points[0].x, pen.points[0].y);
|
|
341
|
+
for (let i = 1; i < pen.points.length; i++) {
|
|
342
|
+
ctx.lineTo(pen.points[i].x, pen.points[i].y);
|
|
343
|
+
}
|
|
344
|
+
ctx.stroke();
|
|
345
|
+
}
|
|
346
|
+
break;
|
|
347
|
+
}
|
|
348
|
+
case "rect": {
|
|
349
|
+
const rect = ann;
|
|
350
|
+
ctx.strokeStyle = rect.strokeColor;
|
|
351
|
+
ctx.lineWidth = rect.strokeWidth;
|
|
352
|
+
if (rect.fillColor) {
|
|
353
|
+
ctx.fillStyle = rect.fillColor;
|
|
354
|
+
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
|
|
355
|
+
}
|
|
356
|
+
if (rect.strokeWidth > 0) {
|
|
357
|
+
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
|
358
|
+
}
|
|
359
|
+
break;
|
|
360
|
+
}
|
|
361
|
+
case "circle": {
|
|
362
|
+
const circle = ann;
|
|
363
|
+
ctx.beginPath();
|
|
364
|
+
ctx.strokeStyle = circle.strokeColor;
|
|
365
|
+
ctx.lineWidth = circle.strokeWidth;
|
|
366
|
+
ctx.ellipse(circle.x, circle.y, Math.abs(circle.radiusX), Math.abs(circle.radiusY), 0, 0, 2 * Math.PI);
|
|
367
|
+
if (circle.fillColor) {
|
|
368
|
+
ctx.fillStyle = circle.fillColor;
|
|
369
|
+
ctx.fill();
|
|
370
|
+
}
|
|
371
|
+
if (circle.strokeWidth > 0) {
|
|
372
|
+
ctx.stroke();
|
|
373
|
+
}
|
|
374
|
+
break;
|
|
375
|
+
}
|
|
376
|
+
case "arrow": {
|
|
377
|
+
const arrow = ann;
|
|
378
|
+
ctx.strokeStyle = arrow.strokeColor;
|
|
379
|
+
ctx.fillStyle = arrow.strokeColor;
|
|
380
|
+
ctx.lineWidth = arrow.strokeWidth;
|
|
381
|
+
ctx.beginPath();
|
|
382
|
+
ctx.moveTo(arrow.x, arrow.y);
|
|
383
|
+
ctx.lineTo(arrow.endX, arrow.endY);
|
|
384
|
+
ctx.stroke();
|
|
385
|
+
const angle = Math.atan2(arrow.endY - arrow.y, arrow.endX - arrow.x);
|
|
386
|
+
const headLen = Math.max(12, arrow.strokeWidth * 3);
|
|
387
|
+
ctx.beginPath();
|
|
388
|
+
ctx.moveTo(arrow.endX, arrow.endY);
|
|
389
|
+
ctx.lineTo(
|
|
390
|
+
arrow.endX - headLen * Math.cos(angle - Math.PI / 6),
|
|
391
|
+
arrow.endY - headLen * Math.sin(angle - Math.PI / 6)
|
|
392
|
+
);
|
|
393
|
+
ctx.lineTo(
|
|
394
|
+
arrow.endX - headLen * Math.cos(angle + Math.PI / 6),
|
|
395
|
+
arrow.endY - headLen * Math.sin(angle + Math.PI / 6)
|
|
396
|
+
);
|
|
397
|
+
ctx.closePath();
|
|
398
|
+
ctx.fill();
|
|
399
|
+
break;
|
|
400
|
+
}
|
|
401
|
+
case "line": {
|
|
402
|
+
const line = ann;
|
|
403
|
+
ctx.beginPath();
|
|
404
|
+
ctx.strokeStyle = line.strokeColor;
|
|
405
|
+
ctx.lineWidth = line.strokeWidth;
|
|
406
|
+
ctx.moveTo(line.x, line.y);
|
|
407
|
+
ctx.lineTo(line.endX, line.endY);
|
|
408
|
+
ctx.stroke();
|
|
409
|
+
break;
|
|
410
|
+
}
|
|
411
|
+
case "text": {
|
|
412
|
+
const textAnn = ann;
|
|
413
|
+
ctx.font = `${textAnn.fontWeight || "normal"} ${textAnn.fontSize}px ${textAnn.fontFamily || "sans-serif"}`;
|
|
414
|
+
ctx.fillStyle = textAnn.color;
|
|
415
|
+
ctx.textBaseline = "top";
|
|
416
|
+
ctx.fillText(textAnn.text, textAnn.x, textAnn.y);
|
|
417
|
+
break;
|
|
418
|
+
}
|
|
419
|
+
case "image": {
|
|
420
|
+
const imgAnn = ann;
|
|
421
|
+
try {
|
|
422
|
+
const loadedImg = await this.imageCache.get(imgAnn.src);
|
|
423
|
+
ctx.drawImage(loadedImg, imgAnn.x, imgAnn.y, imgAnn.width, imgAnn.height);
|
|
424
|
+
} catch {
|
|
425
|
+
}
|
|
426
|
+
break;
|
|
427
|
+
}
|
|
428
|
+
}
|
|
429
|
+
ctx.restore();
|
|
430
|
+
}
|
|
431
|
+
/**
|
|
432
|
+
* Draws selection box around active annotation.
|
|
433
|
+
*/
|
|
434
|
+
drawAnnotationSelection(ctx, ann) {
|
|
435
|
+
ctx.save();
|
|
436
|
+
ctx.strokeStyle = "#3b82f6";
|
|
437
|
+
ctx.lineWidth = 1.5;
|
|
438
|
+
ctx.setLineDash([4, 4]);
|
|
439
|
+
const bounds = this.getAnnotationBounds(ann);
|
|
440
|
+
const pad = 4;
|
|
441
|
+
ctx.strokeRect(bounds.x - pad, bounds.y - pad, bounds.width + pad * 2, bounds.height + pad * 2);
|
|
442
|
+
ctx.fillStyle = "#ffffff";
|
|
443
|
+
ctx.strokeStyle = "#3b82f6";
|
|
444
|
+
ctx.setLineDash([]);
|
|
445
|
+
const handleSize = 6;
|
|
446
|
+
const corners = [
|
|
447
|
+
{ x: bounds.x - pad, y: bounds.y - pad },
|
|
448
|
+
{ x: bounds.x + bounds.width + pad, y: bounds.y - pad },
|
|
449
|
+
{ x: bounds.x - pad, y: bounds.y + bounds.height + pad },
|
|
450
|
+
{ x: bounds.x + bounds.width + pad, y: bounds.y + bounds.height + pad }
|
|
451
|
+
];
|
|
452
|
+
for (const c of corners) {
|
|
453
|
+
ctx.fillRect(c.x - handleSize / 2, c.y - handleSize / 2, handleSize, handleSize);
|
|
454
|
+
ctx.strokeRect(c.x - handleSize / 2, c.y - handleSize / 2, handleSize, handleSize);
|
|
455
|
+
}
|
|
456
|
+
ctx.restore();
|
|
457
|
+
}
|
|
458
|
+
getAnnotationBounds(ann) {
|
|
459
|
+
switch (ann.type) {
|
|
460
|
+
case "rect":
|
|
461
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
462
|
+
case "circle":
|
|
463
|
+
return {
|
|
464
|
+
x: ann.x - Math.abs(ann.radiusX),
|
|
465
|
+
y: ann.y - Math.abs(ann.radiusY),
|
|
466
|
+
width: Math.abs(ann.radiusX) * 2,
|
|
467
|
+
height: Math.abs(ann.radiusY) * 2
|
|
468
|
+
};
|
|
469
|
+
case "arrow":
|
|
470
|
+
case "line": {
|
|
471
|
+
const minX = Math.min(ann.x, ann.endX);
|
|
472
|
+
const minY = Math.min(ann.y, ann.endY);
|
|
473
|
+
return {
|
|
474
|
+
x: minX,
|
|
475
|
+
y: minY,
|
|
476
|
+
width: Math.max(Math.abs(ann.endX - ann.x), 10),
|
|
477
|
+
height: Math.max(Math.abs(ann.endY - ann.y), 10)
|
|
478
|
+
};
|
|
479
|
+
}
|
|
480
|
+
case "pen": {
|
|
481
|
+
if (!ann.points.length) return { x: ann.x, y: ann.y, width: 0, height: 0 };
|
|
482
|
+
let minX = ann.points[0].x, maxX = ann.points[0].x;
|
|
483
|
+
let minY = ann.points[0].y, maxY = ann.points[0].y;
|
|
484
|
+
for (const pt of ann.points) {
|
|
485
|
+
if (pt.x < minX) minX = pt.x;
|
|
486
|
+
if (pt.x > maxX) maxX = pt.x;
|
|
487
|
+
if (pt.y < minY) minY = pt.y;
|
|
488
|
+
if (pt.y > maxY) maxY = pt.y;
|
|
489
|
+
}
|
|
490
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
491
|
+
}
|
|
492
|
+
case "text":
|
|
493
|
+
return {
|
|
494
|
+
x: ann.x,
|
|
495
|
+
y: ann.y,
|
|
496
|
+
width: ann.text.length * ann.fontSize * 0.6,
|
|
497
|
+
height: ann.fontSize * 1.2
|
|
498
|
+
};
|
|
499
|
+
case "image":
|
|
500
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
501
|
+
}
|
|
502
|
+
}
|
|
503
|
+
/**
|
|
504
|
+
* Draws a rule-of-thirds crop grid overlay.
|
|
505
|
+
*/
|
|
506
|
+
drawCropOverlay(ctx, state, vpWidth, vpHeight, imgWidth, imgHeight, cropOverride) {
|
|
507
|
+
const crop = cropOverride !== void 0 && cropOverride !== null ? cropOverride : state.crop ?? {
|
|
508
|
+
x: 0,
|
|
509
|
+
y: 0,
|
|
510
|
+
width: imgWidth,
|
|
511
|
+
height: imgHeight
|
|
512
|
+
};
|
|
513
|
+
if (!crop) return;
|
|
514
|
+
const { transform, pan, zoom } = state;
|
|
515
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
516
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
517
|
+
const baseW = isSwapped ? imgHeight : imgWidth;
|
|
518
|
+
const baseH = isSwapped ? imgWidth : imgHeight;
|
|
519
|
+
ctx.save();
|
|
520
|
+
ctx.translate(vpWidth / 2 + pan.x, vpHeight / 2 + pan.y);
|
|
521
|
+
ctx.scale(zoom, zoom);
|
|
522
|
+
const fitScale = Math.min(vpWidth * 0.85 / baseW, vpHeight * 0.85 / baseH, 1);
|
|
523
|
+
const renderW = baseW * fitScale;
|
|
524
|
+
const renderH = baseH * fitScale;
|
|
525
|
+
const startX = -renderW / 2 + crop.x / imgWidth * renderW;
|
|
526
|
+
const startY = -renderH / 2 + crop.y / imgHeight * renderH;
|
|
527
|
+
const cropBoxW = crop.width / imgWidth * renderW;
|
|
528
|
+
const cropBoxH = crop.height / imgHeight * renderH;
|
|
529
|
+
ctx.fillStyle = "rgba(0, 0, 0, 0.45)";
|
|
530
|
+
ctx.fillRect(-renderW / 2, -renderH / 2, renderW, startY - -renderH / 2);
|
|
531
|
+
ctx.fillRect(-renderW / 2, startY + cropBoxH, renderW, renderH / 2 - (startY + cropBoxH));
|
|
532
|
+
ctx.fillRect(-renderW / 2, startY, startX - -renderW / 2, cropBoxH);
|
|
533
|
+
ctx.fillRect(startX + cropBoxW, startY, renderW / 2 - (startX + cropBoxW), cropBoxH);
|
|
534
|
+
ctx.strokeStyle = "#ffffff";
|
|
535
|
+
ctx.lineWidth = 2;
|
|
536
|
+
ctx.strokeRect(startX, startY, cropBoxW, cropBoxH);
|
|
537
|
+
ctx.strokeStyle = "rgba(255, 255, 255, 0.4)";
|
|
538
|
+
ctx.lineWidth = 1;
|
|
539
|
+
ctx.beginPath();
|
|
540
|
+
ctx.moveTo(startX + cropBoxW / 3, startY);
|
|
541
|
+
ctx.lineTo(startX + cropBoxW / 3, startY + cropBoxH);
|
|
542
|
+
ctx.moveTo(startX + cropBoxW * 2 / 3, startY);
|
|
543
|
+
ctx.lineTo(startX + cropBoxW * 2 / 3, startY + cropBoxH);
|
|
544
|
+
ctx.moveTo(startX, startY + cropBoxH / 3);
|
|
545
|
+
ctx.lineTo(startX + cropBoxW, startY + cropBoxH / 3);
|
|
546
|
+
ctx.moveTo(startX, startY + cropBoxH * 2 / 3);
|
|
547
|
+
ctx.lineTo(startX + cropBoxW, startY + cropBoxH * 2 / 3);
|
|
548
|
+
ctx.stroke();
|
|
549
|
+
ctx.fillStyle = "#ffffff";
|
|
550
|
+
ctx.strokeStyle = "#0284c7";
|
|
551
|
+
ctx.lineWidth = 1.5;
|
|
552
|
+
const handleSize = 10;
|
|
553
|
+
const handles = [
|
|
554
|
+
// Corners
|
|
555
|
+
{ x: startX, y: startY },
|
|
556
|
+
{ x: startX + cropBoxW, y: startY },
|
|
557
|
+
{ x: startX, y: startY + cropBoxH },
|
|
558
|
+
{ x: startX + cropBoxW, y: startY + cropBoxH },
|
|
559
|
+
// Edges
|
|
560
|
+
{ x: startX + cropBoxW / 2, y: startY },
|
|
561
|
+
{ x: startX + cropBoxW / 2, y: startY + cropBoxH },
|
|
562
|
+
{ x: startX, y: startY + cropBoxH / 2 },
|
|
563
|
+
{ x: startX + cropBoxW, y: startY + cropBoxH / 2 }
|
|
564
|
+
];
|
|
565
|
+
for (const h of handles) {
|
|
566
|
+
ctx.fillRect(h.x - handleSize / 2, h.y - handleSize / 2, handleSize, handleSize);
|
|
567
|
+
ctx.strokeRect(h.x - handleSize / 2, h.y - handleSize / 2, handleSize, handleSize);
|
|
568
|
+
}
|
|
569
|
+
ctx.restore();
|
|
570
|
+
}
|
|
571
|
+
drawFittedImage(ctx, img, imgW, imgH, vpW, vpH, zoom, pan) {
|
|
572
|
+
ctx.save();
|
|
573
|
+
ctx.translate(vpW / 2 + pan.x, vpH / 2 + pan.y);
|
|
574
|
+
ctx.scale(zoom, zoom);
|
|
575
|
+
const fitScale = Math.min(vpW * 0.85 / imgW, vpH * 0.85 / imgH, 1);
|
|
576
|
+
const renderW = imgW * fitScale;
|
|
577
|
+
const renderH = imgH * fitScale;
|
|
578
|
+
ctx.drawImage(img, -renderW / 2, -renderH / 2, renderW, renderH);
|
|
579
|
+
ctx.restore();
|
|
580
|
+
}
|
|
581
|
+
/**
|
|
582
|
+
* Performs an export render at exact pixel dimensions without zoom/pan viewport offsets.
|
|
583
|
+
*/
|
|
584
|
+
async renderExport(imageSource, state, options = {}) {
|
|
585
|
+
let img;
|
|
586
|
+
if (typeof imageSource === "string") {
|
|
587
|
+
img = await this.imageCache.get(imageSource);
|
|
588
|
+
} else {
|
|
589
|
+
img = imageSource;
|
|
590
|
+
}
|
|
591
|
+
const naturalWidth = img.naturalWidth || img.videoWidth || img.width || 800;
|
|
592
|
+
const naturalHeight = img.naturalHeight || img.videoHeight || img.height || 600;
|
|
593
|
+
const { crop, transform, adjustments, depthMask, annotations } = state;
|
|
594
|
+
const cropX = crop ? Math.max(0, crop.x) : 0;
|
|
595
|
+
const cropY = crop ? Math.max(0, crop.y) : 0;
|
|
596
|
+
const cropW = crop ? Math.min(naturalWidth - cropX, crop.width) : naturalWidth;
|
|
597
|
+
const cropH = crop ? Math.min(naturalHeight - cropY, crop.height) : naturalHeight;
|
|
598
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
599
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
600
|
+
const baseW = isSwapped ? cropH : cropW;
|
|
601
|
+
const baseH = isSwapped ? cropW : cropH;
|
|
602
|
+
const exportW = options.width || baseW;
|
|
603
|
+
const exportH = options.height || baseH;
|
|
604
|
+
const exportCanvas = this.createCanvas(exportW, exportH);
|
|
605
|
+
const ctx = exportCanvas.getContext("2d");
|
|
606
|
+
if (!ctx) return exportCanvas;
|
|
607
|
+
const intermediateCanvas = this.createCanvas(baseW, baseH);
|
|
608
|
+
const interCtx = intermediateCanvas.getContext("2d");
|
|
609
|
+
if (interCtx) {
|
|
610
|
+
interCtx.save();
|
|
611
|
+
const rad = angle * Math.PI / 180;
|
|
612
|
+
interCtx.translate(baseW / 2, baseH / 2);
|
|
613
|
+
interCtx.rotate(rad);
|
|
614
|
+
interCtx.scale(transform.flipH ? -1 : 1, transform.flipV ? -1 : 1);
|
|
615
|
+
interCtx.drawImage(img, cropX, cropY, cropW, cropH, -cropW / 2, -cropH / 2, cropW, cropH);
|
|
616
|
+
interCtx.restore();
|
|
617
|
+
const imageData = interCtx.getImageData(0, 0, baseW, baseH);
|
|
618
|
+
if (imageData) {
|
|
619
|
+
applyAdjustments(imageData, adjustments);
|
|
620
|
+
if (depthMask.enabled && depthMask.maskSource) {
|
|
621
|
+
try {
|
|
622
|
+
const maskImg = await this.imageCache.get(depthMask.maskSource);
|
|
623
|
+
const maskCanvas = this.createCanvas(baseW, baseH);
|
|
624
|
+
const maskCtx = maskCanvas.getContext("2d");
|
|
625
|
+
if (maskCtx) {
|
|
626
|
+
maskCtx.drawImage(maskImg, cropX, cropY, cropW, cropH, 0, 0, baseW, baseH);
|
|
627
|
+
const maskData = maskCtx.getImageData(0, 0, baseW, baseH);
|
|
628
|
+
if (maskData) {
|
|
629
|
+
applyDepthMask(imageData, maskData, depthMask);
|
|
630
|
+
}
|
|
631
|
+
}
|
|
632
|
+
} catch {
|
|
633
|
+
}
|
|
634
|
+
}
|
|
635
|
+
interCtx.putImageData(imageData, 0, 0);
|
|
636
|
+
}
|
|
637
|
+
}
|
|
638
|
+
ctx.drawImage(intermediateCanvas, 0, 0, baseW, baseH, 0, 0, exportW, exportH);
|
|
639
|
+
if (options.includeAnnotations !== false && annotations.length > 0) {
|
|
640
|
+
const annScaleX = exportW / baseW;
|
|
641
|
+
const annScaleY = exportH / baseH;
|
|
642
|
+
ctx.save();
|
|
643
|
+
ctx.scale(annScaleX, annScaleY);
|
|
644
|
+
for (const ann of annotations) {
|
|
645
|
+
await this.drawAnnotation(ctx, ann);
|
|
646
|
+
}
|
|
647
|
+
ctx.restore();
|
|
648
|
+
}
|
|
649
|
+
return exportCanvas;
|
|
650
|
+
}
|
|
651
|
+
};
|
|
652
|
+
|
|
653
|
+
// src/image-editor/controller.ts
|
|
654
|
+
var DEFAULT_ADJUSTMENTS = {
|
|
655
|
+
brightness: 0,
|
|
656
|
+
contrast: 0,
|
|
657
|
+
saturation: 0,
|
|
658
|
+
exposure: 0,
|
|
659
|
+
temperature: 0,
|
|
660
|
+
blur: 0,
|
|
661
|
+
opacity: 1
|
|
662
|
+
};
|
|
663
|
+
var DEFAULT_TRANSFORM = {
|
|
664
|
+
rotate: 0,
|
|
665
|
+
flipH: false,
|
|
666
|
+
flipV: false
|
|
667
|
+
};
|
|
668
|
+
var DEFAULT_DEPTH_MASK = {
|
|
669
|
+
enabled: false,
|
|
670
|
+
maskSource: null,
|
|
671
|
+
mode: "threshold",
|
|
672
|
+
depthRange: [0, 255],
|
|
673
|
+
softness: 10,
|
|
674
|
+
invert: false
|
|
675
|
+
};
|
|
676
|
+
var ImageEditorController = class {
|
|
677
|
+
state;
|
|
678
|
+
renderer;
|
|
679
|
+
imageElement = null;
|
|
680
|
+
listeners = /* @__PURE__ */ new Set();
|
|
681
|
+
onStateChange;
|
|
682
|
+
// History stacks
|
|
683
|
+
undoStack = [];
|
|
684
|
+
redoStack = [];
|
|
685
|
+
maxHistoryDepth;
|
|
686
|
+
isApplyingHistory = false;
|
|
687
|
+
initialSnapshot;
|
|
688
|
+
constructor(options = {}) {
|
|
689
|
+
this.maxHistoryDepth = options.maxHistoryDepth ?? 50;
|
|
690
|
+
this.onStateChange = options.onStateChange;
|
|
691
|
+
this.renderer = new ImageEditorRenderer();
|
|
692
|
+
const initialAdjustments = {
|
|
693
|
+
...DEFAULT_ADJUSTMENTS,
|
|
694
|
+
...options.initialState?.adjustments
|
|
695
|
+
};
|
|
696
|
+
const initialTransform = {
|
|
697
|
+
...DEFAULT_TRANSFORM,
|
|
698
|
+
...options.initialState?.transform
|
|
699
|
+
};
|
|
700
|
+
const initialDepthMask = {
|
|
701
|
+
...DEFAULT_DEPTH_MASK,
|
|
702
|
+
...options.initialState?.depthMask
|
|
703
|
+
};
|
|
704
|
+
this.state = {
|
|
705
|
+
sourceUrl: typeof options.image === "string" ? options.image : null,
|
|
706
|
+
imageDimensions: { width: 800, height: 600 },
|
|
707
|
+
crop: options.initialState?.crop ?? null,
|
|
708
|
+
transform: initialTransform,
|
|
709
|
+
adjustments: initialAdjustments,
|
|
710
|
+
depthMask: initialDepthMask,
|
|
711
|
+
annotations: options.initialState?.annotations ?? [],
|
|
712
|
+
selectedAnnotationId: null,
|
|
713
|
+
activeTool: "select",
|
|
714
|
+
zoom: 1,
|
|
715
|
+
pan: { x: 0, y: 0 },
|
|
716
|
+
isOriginalCompared: false
|
|
717
|
+
};
|
|
718
|
+
if (options.image instanceof Object && "width" in options.image) {
|
|
719
|
+
this.imageElement = options.image;
|
|
720
|
+
this.state.imageDimensions = {
|
|
721
|
+
width: options.image.naturalWidth || options.image.width || 800,
|
|
722
|
+
height: options.image.naturalHeight || options.image.height || 600
|
|
723
|
+
};
|
|
724
|
+
}
|
|
725
|
+
this.initialSnapshot = this.serializeState();
|
|
726
|
+
if (typeof options.image === "string") {
|
|
727
|
+
this.loadImage(options.image);
|
|
728
|
+
}
|
|
729
|
+
}
|
|
730
|
+
// --- Image Loading ---
|
|
731
|
+
async loadImage(src) {
|
|
732
|
+
if (typeof src === "string") {
|
|
733
|
+
this.state.sourceUrl = src;
|
|
734
|
+
try {
|
|
735
|
+
const img = await this.renderer.imageCache.get(src);
|
|
736
|
+
this.imageElement = img;
|
|
737
|
+
this.state.imageDimensions = {
|
|
738
|
+
width: img.naturalWidth || img.width || 800,
|
|
739
|
+
height: img.naturalHeight || img.height || 600
|
|
740
|
+
};
|
|
741
|
+
} catch (err) {
|
|
742
|
+
}
|
|
743
|
+
} else {
|
|
744
|
+
this.imageElement = src;
|
|
745
|
+
this.state.sourceUrl = src.src || null;
|
|
746
|
+
this.state.imageDimensions = {
|
|
747
|
+
width: src.naturalWidth || src.width || 800,
|
|
748
|
+
height: src.naturalHeight || src.height || 600
|
|
749
|
+
};
|
|
750
|
+
}
|
|
751
|
+
this.initialSnapshot = this.serializeState();
|
|
752
|
+
this.notify();
|
|
753
|
+
}
|
|
754
|
+
getImageElement() {
|
|
755
|
+
return this.imageElement;
|
|
756
|
+
}
|
|
757
|
+
// --- State & Subscription ---
|
|
758
|
+
getState() {
|
|
759
|
+
return this.state;
|
|
760
|
+
}
|
|
761
|
+
subscribe(listener) {
|
|
762
|
+
this.listeners.add(listener);
|
|
763
|
+
listener(this.state);
|
|
764
|
+
return () => {
|
|
765
|
+
this.listeners.delete(listener);
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
notify() {
|
|
769
|
+
const currentState = { ...this.state };
|
|
770
|
+
this.onStateChange?.(currentState);
|
|
771
|
+
this.listeners.forEach((l) => l(currentState));
|
|
772
|
+
}
|
|
773
|
+
// --- History (Undo / Redo / Reset) ---
|
|
774
|
+
recordHistory() {
|
|
775
|
+
if (this.isApplyingHistory) return;
|
|
776
|
+
this.undoStack.push(this.serializeState());
|
|
777
|
+
if (this.undoStack.length > this.maxHistoryDepth) {
|
|
778
|
+
this.undoStack.shift();
|
|
779
|
+
}
|
|
780
|
+
this.redoStack = [];
|
|
781
|
+
}
|
|
782
|
+
get canUndo() {
|
|
783
|
+
return this.undoStack.length > 0;
|
|
784
|
+
}
|
|
785
|
+
get canRedo() {
|
|
786
|
+
return this.redoStack.length > 0;
|
|
787
|
+
}
|
|
788
|
+
undo() {
|
|
789
|
+
if (!this.canUndo) return false;
|
|
790
|
+
this.isApplyingHistory = true;
|
|
791
|
+
const current = this.serializeState();
|
|
792
|
+
this.redoStack.push(current);
|
|
793
|
+
const prev = this.undoStack.pop();
|
|
794
|
+
this.applySerializedState(prev);
|
|
795
|
+
this.isApplyingHistory = false;
|
|
796
|
+
this.notify();
|
|
797
|
+
return true;
|
|
798
|
+
}
|
|
799
|
+
redo() {
|
|
800
|
+
if (!this.canRedo) return false;
|
|
801
|
+
this.isApplyingHistory = true;
|
|
802
|
+
const current = this.serializeState();
|
|
803
|
+
this.undoStack.push(current);
|
|
804
|
+
const next = this.redoStack.pop();
|
|
805
|
+
this.applySerializedState(next);
|
|
806
|
+
this.isApplyingHistory = false;
|
|
807
|
+
this.notify();
|
|
808
|
+
return true;
|
|
809
|
+
}
|
|
810
|
+
reset() {
|
|
811
|
+
this.recordHistory();
|
|
812
|
+
this.applySerializedState(this.initialSnapshot);
|
|
813
|
+
this.state.zoom = 1;
|
|
814
|
+
this.state.pan = { x: 0, y: 0 };
|
|
815
|
+
this.state.activeTool = "select";
|
|
816
|
+
this.state.selectedAnnotationId = null;
|
|
817
|
+
this.notify();
|
|
818
|
+
}
|
|
819
|
+
// --- Active Tool ---
|
|
820
|
+
setTool(tool) {
|
|
821
|
+
if (this.state.activeTool === tool) return;
|
|
822
|
+
this.state.activeTool = tool;
|
|
823
|
+
if (tool !== "select") {
|
|
824
|
+
this.state.selectedAnnotationId = null;
|
|
825
|
+
}
|
|
826
|
+
if (tool === "crop" && !this.state.crop) {
|
|
827
|
+
this.state.crop = {
|
|
828
|
+
x: 0,
|
|
829
|
+
y: 0,
|
|
830
|
+
width: this.state.imageDimensions.width,
|
|
831
|
+
height: this.state.imageDimensions.height
|
|
832
|
+
};
|
|
833
|
+
}
|
|
834
|
+
this.notify();
|
|
835
|
+
}
|
|
836
|
+
// --- Crop ---
|
|
837
|
+
setCrop(crop, addToHistory = true) {
|
|
838
|
+
if (addToHistory) {
|
|
839
|
+
this.recordHistory();
|
|
840
|
+
}
|
|
841
|
+
this.state.crop = crop;
|
|
842
|
+
this.notify();
|
|
843
|
+
}
|
|
844
|
+
applyCropPreset(ratio) {
|
|
845
|
+
this.recordHistory();
|
|
846
|
+
const { width, height } = this.state.imageDimensions;
|
|
847
|
+
if (!ratio) {
|
|
848
|
+
this.state.crop = { x: 0, y: 0, width, height };
|
|
849
|
+
} else {
|
|
850
|
+
let targetW = width;
|
|
851
|
+
let targetH = targetW / ratio;
|
|
852
|
+
if (targetH > height) {
|
|
853
|
+
targetH = height;
|
|
854
|
+
targetW = targetH * ratio;
|
|
855
|
+
}
|
|
856
|
+
const x = Math.max(0, Math.round((width - targetW) / 2));
|
|
857
|
+
const y = Math.max(0, Math.round((height - targetH) / 2));
|
|
858
|
+
this.state.crop = {
|
|
859
|
+
x,
|
|
860
|
+
y,
|
|
861
|
+
width: Math.round(targetW),
|
|
862
|
+
height: Math.round(targetH)
|
|
863
|
+
};
|
|
864
|
+
}
|
|
865
|
+
this.notify();
|
|
866
|
+
}
|
|
867
|
+
resetCrop() {
|
|
868
|
+
this.recordHistory();
|
|
869
|
+
this.state.crop = null;
|
|
870
|
+
this.notify();
|
|
871
|
+
}
|
|
872
|
+
// --- Transformations (Rotate / Flip) ---
|
|
873
|
+
rotate(degrees) {
|
|
874
|
+
this.recordHistory();
|
|
875
|
+
const current = this.state.transform.rotate;
|
|
876
|
+
this.state.transform.rotate = (current + degrees) % 360;
|
|
877
|
+
this.notify();
|
|
878
|
+
}
|
|
879
|
+
setRotation(degrees) {
|
|
880
|
+
this.recordHistory();
|
|
881
|
+
this.state.transform.rotate = degrees % 360;
|
|
882
|
+
this.notify();
|
|
883
|
+
}
|
|
884
|
+
flipHorizontal() {
|
|
885
|
+
this.recordHistory();
|
|
886
|
+
this.state.transform.flipH = !this.state.transform.flipH;
|
|
887
|
+
this.notify();
|
|
888
|
+
}
|
|
889
|
+
flipVertical() {
|
|
890
|
+
this.recordHistory();
|
|
891
|
+
this.state.transform.flipV = !this.state.transform.flipV;
|
|
892
|
+
this.notify();
|
|
893
|
+
}
|
|
894
|
+
// --- Adjustments ---
|
|
895
|
+
setAdjustments(partial, commitToHistory = true) {
|
|
896
|
+
if (commitToHistory) {
|
|
897
|
+
this.recordHistory();
|
|
898
|
+
}
|
|
899
|
+
this.state.adjustments = {
|
|
900
|
+
...this.state.adjustments,
|
|
901
|
+
...partial
|
|
902
|
+
};
|
|
903
|
+
this.notify();
|
|
904
|
+
}
|
|
905
|
+
resetAdjustments() {
|
|
906
|
+
this.recordHistory();
|
|
907
|
+
this.state.adjustments = { ...DEFAULT_ADJUSTMENTS };
|
|
908
|
+
this.notify();
|
|
909
|
+
}
|
|
910
|
+
// --- Depth Masking (Gleamforge) ---
|
|
911
|
+
setDepthMask(partial) {
|
|
912
|
+
this.recordHistory();
|
|
913
|
+
this.state.depthMask = {
|
|
914
|
+
...this.state.depthMask,
|
|
915
|
+
...partial
|
|
916
|
+
};
|
|
917
|
+
this.notify();
|
|
918
|
+
}
|
|
919
|
+
resetDepthMask() {
|
|
920
|
+
this.recordHistory();
|
|
921
|
+
this.state.depthMask = { ...DEFAULT_DEPTH_MASK };
|
|
922
|
+
this.notify();
|
|
923
|
+
}
|
|
924
|
+
// --- Annotations & Layers ---
|
|
925
|
+
addAnnotation(annotation) {
|
|
926
|
+
this.recordHistory();
|
|
927
|
+
const id = `ann_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`;
|
|
928
|
+
const fullAnnotation = { ...annotation, id };
|
|
929
|
+
this.state.annotations = [...this.state.annotations, fullAnnotation];
|
|
930
|
+
this.state.selectedAnnotationId = id;
|
|
931
|
+
this.notify();
|
|
932
|
+
return id;
|
|
933
|
+
}
|
|
934
|
+
updateAnnotation(id, partial, commitToHistory = true) {
|
|
935
|
+
if (commitToHistory) {
|
|
936
|
+
this.recordHistory();
|
|
937
|
+
}
|
|
938
|
+
this.state.annotations = this.state.annotations.map((ann) => {
|
|
939
|
+
if (ann.id === id) {
|
|
940
|
+
return { ...ann, ...partial };
|
|
941
|
+
}
|
|
942
|
+
return ann;
|
|
943
|
+
});
|
|
944
|
+
this.notify();
|
|
945
|
+
}
|
|
946
|
+
removeAnnotation(id) {
|
|
947
|
+
this.recordHistory();
|
|
948
|
+
this.state.annotations = this.state.annotations.filter((ann) => ann.id !== id);
|
|
949
|
+
if (this.state.selectedAnnotationId === id) {
|
|
950
|
+
this.state.selectedAnnotationId = null;
|
|
951
|
+
}
|
|
952
|
+
this.notify();
|
|
953
|
+
}
|
|
954
|
+
clearAnnotations() {
|
|
955
|
+
this.recordHistory();
|
|
956
|
+
this.state.annotations = [];
|
|
957
|
+
this.state.selectedAnnotationId = null;
|
|
958
|
+
this.notify();
|
|
959
|
+
}
|
|
960
|
+
selectAnnotation(id) {
|
|
961
|
+
this.state.selectedAnnotationId = id;
|
|
962
|
+
this.notify();
|
|
963
|
+
}
|
|
964
|
+
// --- Zoom & Pan ---
|
|
965
|
+
setZoom(zoom) {
|
|
966
|
+
this.state.zoom = Math.max(0.1, Math.min(10, zoom));
|
|
967
|
+
this.notify();
|
|
968
|
+
}
|
|
969
|
+
zoomIn(factor = 1.2) {
|
|
970
|
+
this.setZoom(this.state.zoom * factor);
|
|
971
|
+
}
|
|
972
|
+
zoomOut(factor = 1.2) {
|
|
973
|
+
this.setZoom(this.state.zoom / factor);
|
|
974
|
+
}
|
|
975
|
+
resetZoom() {
|
|
976
|
+
this.state.zoom = 1;
|
|
977
|
+
this.state.pan = { x: 0, y: 0 };
|
|
978
|
+
this.notify();
|
|
979
|
+
}
|
|
980
|
+
setPan(pan) {
|
|
981
|
+
this.state.pan = pan;
|
|
982
|
+
this.notify();
|
|
983
|
+
}
|
|
984
|
+
setComparing(comparing) {
|
|
985
|
+
if (this.state.isOriginalCompared === comparing) return;
|
|
986
|
+
this.state.isOriginalCompared = comparing;
|
|
987
|
+
this.notify();
|
|
988
|
+
}
|
|
989
|
+
// --- Rendering & Export ---
|
|
990
|
+
async render(targetCanvas, options) {
|
|
991
|
+
const source = this.imageElement || this.state.sourceUrl;
|
|
992
|
+
if (!source) return;
|
|
993
|
+
await this.renderer.renderToCanvas(targetCanvas, source, this.state, options);
|
|
994
|
+
}
|
|
995
|
+
async renderExport(options) {
|
|
996
|
+
const source = this.imageElement || this.state.sourceUrl;
|
|
997
|
+
if (!source) {
|
|
998
|
+
return this.renderer.createCanvas(1, 1);
|
|
999
|
+
}
|
|
1000
|
+
return this.renderer.renderExport(source, this.state, options);
|
|
1001
|
+
}
|
|
1002
|
+
async toDataURL(options = {}) {
|
|
1003
|
+
const canvas = await this.renderExport(options);
|
|
1004
|
+
const format = options.format || "image/png";
|
|
1005
|
+
const quality = options.quality ?? 0.92;
|
|
1006
|
+
return canvas.toDataURL(format, quality);
|
|
1007
|
+
}
|
|
1008
|
+
async toBlob(options = {}) {
|
|
1009
|
+
const canvas = await this.renderExport(options);
|
|
1010
|
+
const format = options.format || "image/png";
|
|
1011
|
+
const quality = options.quality ?? 0.92;
|
|
1012
|
+
return new Promise((resolve, reject) => {
|
|
1013
|
+
if (canvas.toBlob) {
|
|
1014
|
+
canvas.toBlob((blob) => {
|
|
1015
|
+
if (blob) resolve(blob);
|
|
1016
|
+
else reject(new Error("Failed to create Blob from canvas"));
|
|
1017
|
+
}, format, quality);
|
|
1018
|
+
} else {
|
|
1019
|
+
try {
|
|
1020
|
+
const dataUrl = canvas.toDataURL(format, quality);
|
|
1021
|
+
const parts = dataUrl.split(",");
|
|
1022
|
+
const byteStr = atob(parts[1]);
|
|
1023
|
+
const ab = new ArrayBuffer(byteStr.length);
|
|
1024
|
+
const ia = new Uint8Array(ab);
|
|
1025
|
+
for (let i = 0; i < byteStr.length; i++) {
|
|
1026
|
+
ia[i] = byteStr.charCodeAt(i);
|
|
1027
|
+
}
|
|
1028
|
+
resolve(new Blob([ab], { type: format }));
|
|
1029
|
+
} catch (err) {
|
|
1030
|
+
reject(err);
|
|
1031
|
+
}
|
|
1032
|
+
}
|
|
1033
|
+
});
|
|
1034
|
+
}
|
|
1035
|
+
// --- Serialization ---
|
|
1036
|
+
toJSON() {
|
|
1037
|
+
return this.serializeState();
|
|
1038
|
+
}
|
|
1039
|
+
loadJSON(data) {
|
|
1040
|
+
this.recordHistory();
|
|
1041
|
+
this.applySerializedState(data);
|
|
1042
|
+
this.notify();
|
|
1043
|
+
}
|
|
1044
|
+
serializeState() {
|
|
1045
|
+
return {
|
|
1046
|
+
version: 1,
|
|
1047
|
+
crop: this.state.crop ? { ...this.state.crop } : null,
|
|
1048
|
+
transform: { ...this.state.transform },
|
|
1049
|
+
adjustments: { ...this.state.adjustments },
|
|
1050
|
+
depthMask: {
|
|
1051
|
+
...this.state.depthMask,
|
|
1052
|
+
depthRange: [...this.state.depthMask.depthRange]
|
|
1053
|
+
},
|
|
1054
|
+
annotations: JSON.parse(JSON.stringify(this.state.annotations))
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
applySerializedState(data) {
|
|
1058
|
+
this.state.crop = data.crop ? { ...data.crop } : null;
|
|
1059
|
+
this.state.transform = { ...data.transform };
|
|
1060
|
+
this.state.adjustments = { ...data.adjustments };
|
|
1061
|
+
this.state.depthMask = {
|
|
1062
|
+
...data.depthMask,
|
|
1063
|
+
depthRange: [...data.depthMask.depthRange]
|
|
1064
|
+
};
|
|
1065
|
+
this.state.annotations = JSON.parse(JSON.stringify(data.annotations || []));
|
|
1066
|
+
}
|
|
1067
|
+
};
|
|
1068
|
+
|
|
1069
|
+
// src/image-editor/geometry.ts
|
|
1070
|
+
function getViewportMetrics(state, vpWidth, vpHeight, isActivelyCropping = false, cropOverride) {
|
|
1071
|
+
const { imageDimensions, crop: stateCrop, transform, zoom, pan } = state;
|
|
1072
|
+
const naturalWidth = imageDimensions.width || 800;
|
|
1073
|
+
const naturalHeight = imageDimensions.height || 600;
|
|
1074
|
+
const activeCrop = cropOverride !== void 0 && cropOverride !== null ? cropOverride : stateCrop ?? (isActivelyCropping ? { x: 0, y: 0, width: naturalWidth, height: naturalHeight } : null);
|
|
1075
|
+
const cropW = !isActivelyCropping && activeCrop ? activeCrop.width : naturalWidth;
|
|
1076
|
+
const cropH = !isActivelyCropping && activeCrop ? activeCrop.height : naturalHeight;
|
|
1077
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
1078
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
1079
|
+
const baseW = isSwapped ? cropH : cropW;
|
|
1080
|
+
const baseH = isSwapped ? cropW : cropH;
|
|
1081
|
+
const fitScale = Math.min(vpWidth * 0.85 / baseW, vpHeight * 0.85 / baseH, 1);
|
|
1082
|
+
const renderW = baseW * fitScale;
|
|
1083
|
+
const renderH = baseH * fitScale;
|
|
1084
|
+
const totalScale = zoom * fitScale;
|
|
1085
|
+
const centerX = vpWidth / 2 + pan.x;
|
|
1086
|
+
const centerY = vpHeight / 2 + pan.y;
|
|
1087
|
+
const imageRect = {
|
|
1088
|
+
x: centerX - renderW * zoom / 2,
|
|
1089
|
+
y: centerY - renderH * zoom / 2,
|
|
1090
|
+
width: renderW * zoom,
|
|
1091
|
+
height: renderH * zoom
|
|
1092
|
+
};
|
|
1093
|
+
let cropRect = null;
|
|
1094
|
+
if (activeCrop) {
|
|
1095
|
+
const startX = -renderW / 2 + activeCrop.x / naturalWidth * renderW;
|
|
1096
|
+
const startY = -renderH / 2 + activeCrop.y / naturalHeight * renderH;
|
|
1097
|
+
const cropBoxW = activeCrop.width / naturalWidth * renderW;
|
|
1098
|
+
const cropBoxH = activeCrop.height / naturalHeight * renderH;
|
|
1099
|
+
cropRect = {
|
|
1100
|
+
x: centerX + zoom * startX,
|
|
1101
|
+
y: centerY + zoom * startY,
|
|
1102
|
+
width: zoom * cropBoxW,
|
|
1103
|
+
height: zoom * cropBoxH
|
|
1104
|
+
};
|
|
1105
|
+
}
|
|
1106
|
+
return {
|
|
1107
|
+
vpWidth,
|
|
1108
|
+
vpHeight,
|
|
1109
|
+
centerX,
|
|
1110
|
+
centerY,
|
|
1111
|
+
baseW,
|
|
1112
|
+
baseH,
|
|
1113
|
+
fitScale,
|
|
1114
|
+
renderW,
|
|
1115
|
+
renderH,
|
|
1116
|
+
totalScale,
|
|
1117
|
+
imageRect,
|
|
1118
|
+
cropRect
|
|
1119
|
+
};
|
|
1120
|
+
}
|
|
1121
|
+
function canvasToImagePoint(canvasPt, state, vpWidth, vpHeight, isActivelyCropping = false) {
|
|
1122
|
+
const metrics = getViewportMetrics(state, vpWidth, vpHeight, isActivelyCropping);
|
|
1123
|
+
const unzoomedX = (canvasPt.x - metrics.centerX) / state.zoom;
|
|
1124
|
+
const unzoomedY = (canvasPt.y - metrics.centerY) / state.zoom;
|
|
1125
|
+
const x = (unzoomedX + metrics.renderW / 2) / metrics.fitScale;
|
|
1126
|
+
const y = (unzoomedY + metrics.renderH / 2) / metrics.fitScale;
|
|
1127
|
+
return { x: Math.round(x), y: Math.round(y) };
|
|
1128
|
+
}
|
|
1129
|
+
function imageToCanvasPoint(imgPt, state, vpWidth, vpHeight, isActivelyCropping = false) {
|
|
1130
|
+
const metrics = getViewportMetrics(state, vpWidth, vpHeight, isActivelyCropping);
|
|
1131
|
+
const unzoomedX = (imgPt.x - metrics.baseW / 2) * metrics.fitScale;
|
|
1132
|
+
const unzoomedY = (imgPt.y - metrics.baseH / 2) * metrics.fitScale;
|
|
1133
|
+
const canvasX = metrics.centerX + unzoomedX * state.zoom;
|
|
1134
|
+
const canvasY = metrics.centerY + unzoomedY * state.zoom;
|
|
1135
|
+
return { x: Math.round(canvasX), y: Math.round(canvasY) };
|
|
1136
|
+
}
|
|
1137
|
+
function hitTestCrop(canvasPt, state, vpWidth, vpHeight, handleThreshold = 14, cropOverride) {
|
|
1138
|
+
const metrics = getViewportMetrics(state, vpWidth, vpHeight, true, cropOverride);
|
|
1139
|
+
if (!metrics.cropRect) return null;
|
|
1140
|
+
const { x, y, width: w, height: h } = metrics.cropRect;
|
|
1141
|
+
const px = canvasPt.x;
|
|
1142
|
+
const py = canvasPt.y;
|
|
1143
|
+
const dist = (x1, y1, x2, y2) => Math.hypot(x1 - x2, y1 - y2);
|
|
1144
|
+
if (dist(px, py, x, y) <= handleThreshold) return "nw";
|
|
1145
|
+
if (dist(px, py, x + w, y) <= handleThreshold) return "ne";
|
|
1146
|
+
if (dist(px, py, x + w, y + h) <= handleThreshold) return "se";
|
|
1147
|
+
if (dist(px, py, x, y + h) <= handleThreshold) return "sw";
|
|
1148
|
+
const nearX = px >= x - handleThreshold && px <= x + w + handleThreshold;
|
|
1149
|
+
const nearY = py >= y - handleThreshold && py <= y + h + handleThreshold;
|
|
1150
|
+
if (nearX && Math.abs(py - y) <= handleThreshold) return "n";
|
|
1151
|
+
if (nearX && Math.abs(py - (y + h)) <= handleThreshold) return "s";
|
|
1152
|
+
if (nearY && Math.abs(px - x) <= handleThreshold) return "w";
|
|
1153
|
+
if (nearY && Math.abs(px - (x + w)) <= handleThreshold) return "e";
|
|
1154
|
+
if (px >= x && px <= x + w && py >= y && py <= y + h) {
|
|
1155
|
+
return "inside";
|
|
1156
|
+
}
|
|
1157
|
+
return null;
|
|
1158
|
+
}
|
|
1159
|
+
function getCropCursor(handle) {
|
|
1160
|
+
switch (handle) {
|
|
1161
|
+
case "nw":
|
|
1162
|
+
case "se":
|
|
1163
|
+
return "nwse-resize";
|
|
1164
|
+
case "ne":
|
|
1165
|
+
case "sw":
|
|
1166
|
+
return "nesw-resize";
|
|
1167
|
+
case "n":
|
|
1168
|
+
case "s":
|
|
1169
|
+
return "ns-resize";
|
|
1170
|
+
case "e":
|
|
1171
|
+
case "w":
|
|
1172
|
+
return "ew-resize";
|
|
1173
|
+
case "inside":
|
|
1174
|
+
return "move";
|
|
1175
|
+
default:
|
|
1176
|
+
return "crosshair";
|
|
1177
|
+
}
|
|
1178
|
+
}
|
|
1179
|
+
function calculateCropDrag(startCrop, handle, startImgPt, currentImgPt, imgWidth, imgHeight, aspectRatio) {
|
|
1180
|
+
const dx = currentImgPt.x - startImgPt.x;
|
|
1181
|
+
const dy = currentImgPt.y - startImgPt.y;
|
|
1182
|
+
if (handle === "inside") {
|
|
1183
|
+
const maxX = Math.max(0, imgWidth - startCrop.width);
|
|
1184
|
+
const maxY = Math.max(0, imgHeight - startCrop.height);
|
|
1185
|
+
const newX = Math.max(0, Math.min(maxX, startCrop.x + dx));
|
|
1186
|
+
const newY = Math.max(0, Math.min(maxY, startCrop.y + dy));
|
|
1187
|
+
return {
|
|
1188
|
+
x: Math.round(newX),
|
|
1189
|
+
y: Math.round(newY),
|
|
1190
|
+
width: startCrop.width,
|
|
1191
|
+
height: startCrop.height
|
|
1192
|
+
};
|
|
1193
|
+
}
|
|
1194
|
+
let left = startCrop.x;
|
|
1195
|
+
let top = startCrop.y;
|
|
1196
|
+
let right = startCrop.x + startCrop.width;
|
|
1197
|
+
let bottom = startCrop.y + startCrop.height;
|
|
1198
|
+
const minSize = 20;
|
|
1199
|
+
if (handle === "e" || handle === "ne" || handle === "se") {
|
|
1200
|
+
right = Math.min(imgWidth, Math.max(left + minSize, startCrop.x + startCrop.width + dx));
|
|
1201
|
+
}
|
|
1202
|
+
if (handle === "w" || handle === "nw" || handle === "sw") {
|
|
1203
|
+
left = Math.max(0, Math.min(right - minSize, startCrop.x + dx));
|
|
1204
|
+
}
|
|
1205
|
+
if (handle === "s" || handle === "se" || handle === "sw") {
|
|
1206
|
+
bottom = Math.min(imgHeight, Math.max(top + minSize, startCrop.y + startCrop.height + dy));
|
|
1207
|
+
}
|
|
1208
|
+
if (handle === "n" || handle === "nw" || handle === "ne") {
|
|
1209
|
+
top = Math.max(0, Math.min(bottom - minSize, startCrop.y + dy));
|
|
1210
|
+
}
|
|
1211
|
+
if (aspectRatio && aspectRatio > 0) {
|
|
1212
|
+
let currentW = right - left;
|
|
1213
|
+
let currentH = bottom - top;
|
|
1214
|
+
if (handle === "e" || handle === "w") {
|
|
1215
|
+
currentH = Math.min(imgHeight, currentW / aspectRatio);
|
|
1216
|
+
bottom = Math.min(imgHeight, top + currentH);
|
|
1217
|
+
} else if (handle === "n" || handle === "s") {
|
|
1218
|
+
currentW = Math.min(imgWidth, currentH * aspectRatio);
|
|
1219
|
+
right = Math.min(imgWidth, left + currentW);
|
|
1220
|
+
} else {
|
|
1221
|
+
const targetH = currentW / aspectRatio;
|
|
1222
|
+
if (top + targetH <= imgHeight) {
|
|
1223
|
+
bottom = top + targetH;
|
|
1224
|
+
} else {
|
|
1225
|
+
const targetW = currentH * aspectRatio;
|
|
1226
|
+
right = Math.min(imgWidth, left + targetW);
|
|
1227
|
+
}
|
|
1228
|
+
}
|
|
1229
|
+
}
|
|
1230
|
+
return {
|
|
1231
|
+
x: Math.round(left),
|
|
1232
|
+
y: Math.round(top),
|
|
1233
|
+
width: Math.round(Math.max(minSize, right - left)),
|
|
1234
|
+
height: Math.round(Math.max(minSize, bottom - top))
|
|
1235
|
+
};
|
|
1236
|
+
}
|
|
1237
|
+
function getAnnotationBounds(ann) {
|
|
1238
|
+
switch (ann.type) {
|
|
1239
|
+
case "rect":
|
|
1240
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
1241
|
+
case "circle":
|
|
1242
|
+
return {
|
|
1243
|
+
x: ann.x - ann.radiusX,
|
|
1244
|
+
y: ann.y - ann.radiusY,
|
|
1245
|
+
width: ann.radiusX * 2,
|
|
1246
|
+
height: ann.radiusY * 2
|
|
1247
|
+
};
|
|
1248
|
+
case "arrow":
|
|
1249
|
+
case "line": {
|
|
1250
|
+
const minX = Math.min(ann.x, ann.endX);
|
|
1251
|
+
const minY = Math.min(ann.y, ann.endY);
|
|
1252
|
+
return {
|
|
1253
|
+
x: minX,
|
|
1254
|
+
y: minY,
|
|
1255
|
+
width: Math.max(Math.abs(ann.endX - ann.x), 10),
|
|
1256
|
+
height: Math.max(Math.abs(ann.endY - ann.y), 10)
|
|
1257
|
+
};
|
|
1258
|
+
}
|
|
1259
|
+
case "pen": {
|
|
1260
|
+
if (!ann.points.length) return { x: ann.x, y: ann.y, width: 0, height: 0 };
|
|
1261
|
+
let minX = ann.points[0].x;
|
|
1262
|
+
let maxX = ann.points[0].x;
|
|
1263
|
+
let minY = ann.points[0].y;
|
|
1264
|
+
let maxY = ann.points[0].y;
|
|
1265
|
+
for (const pt of ann.points) {
|
|
1266
|
+
if (pt.x < minX) minX = pt.x;
|
|
1267
|
+
if (pt.x > maxX) maxX = pt.x;
|
|
1268
|
+
if (pt.y < minY) minY = pt.y;
|
|
1269
|
+
if (pt.y > maxY) maxY = pt.y;
|
|
1270
|
+
}
|
|
1271
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
1272
|
+
}
|
|
1273
|
+
case "text":
|
|
1274
|
+
return {
|
|
1275
|
+
x: ann.x,
|
|
1276
|
+
y: ann.y,
|
|
1277
|
+
width: ann.text.length * ann.fontSize * 0.6,
|
|
1278
|
+
height: ann.fontSize * 1.2
|
|
1279
|
+
};
|
|
1280
|
+
case "image":
|
|
1281
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
1282
|
+
}
|
|
1283
|
+
}
|
|
1284
|
+
function hitTestAnnotation(imgPt, ann, padding = 10) {
|
|
1285
|
+
const b = getAnnotationBounds(ann);
|
|
1286
|
+
return imgPt.x >= b.x - padding && imgPt.x <= b.x + b.width + padding && imgPt.y >= b.y - padding && imgPt.y <= b.y + b.height + padding;
|
|
1287
|
+
}
|
|
1288
|
+
export {
|
|
1289
|
+
DEFAULT_ADJUSTMENTS,
|
|
1290
|
+
DEFAULT_DEPTH_MASK,
|
|
1291
|
+
DEFAULT_TRANSFORM,
|
|
1292
|
+
ImageEditorController,
|
|
1293
|
+
ImageEditorRenderer,
|
|
1294
|
+
applyAdjustments,
|
|
1295
|
+
applyDepthMask,
|
|
1296
|
+
calculateCropDrag,
|
|
1297
|
+
canvasToImagePoint,
|
|
1298
|
+
getAnnotationBounds,
|
|
1299
|
+
getCropCursor,
|
|
1300
|
+
getViewportMetrics,
|
|
1301
|
+
globalImageCache,
|
|
1302
|
+
hitTestAnnotation,
|
|
1303
|
+
hitTestCrop,
|
|
1304
|
+
imageToCanvasPoint,
|
|
1305
|
+
removeAlpha
|
|
1306
|
+
};
|
|
1307
|
+
//# sourceMappingURL=index.js.map
|