@pixerate/editor 0.11.0 → 0.13.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/image-editor/index.cjs +1442 -0
- package/dist/image-editor/index.cjs.map +1 -0
- package/dist/image-editor/index.d.cts +372 -0
- package/dist/image-editor/index.d.ts +372 -0
- package/dist/image-editor/index.js +1399 -0
- package/dist/image-editor/index.js.map +1 -0
- package/dist/index.cjs +1419 -5
- package/dist/index.cjs.map +1 -1
- package/dist/index.d.cts +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +1402 -5
- package/dist/index.js.map +1 -1
- package/package.json +7 -2
package/dist/index.js
CHANGED
|
@@ -1277,10 +1277,10 @@ var HorizontalRule$1 = HorizontalRule.extend({
|
|
|
1277
1277
|
};
|
|
1278
1278
|
}
|
|
1279
1279
|
});
|
|
1280
|
-
var
|
|
1280
|
+
var Image2 = Node$1.create({
|
|
1281
1281
|
name: "image"
|
|
1282
1282
|
});
|
|
1283
|
-
var Image$1 =
|
|
1283
|
+
var Image$1 = Image2.extend({
|
|
1284
1284
|
/**
|
|
1285
1285
|
* @return {{markdown: MarkdownNodeSpec}}
|
|
1286
1286
|
*/
|
|
@@ -1993,7 +1993,7 @@ function handleImageInsertion(view, file, options) {
|
|
|
1993
1993
|
}
|
|
1994
1994
|
}
|
|
1995
1995
|
}
|
|
1996
|
-
var
|
|
1996
|
+
var Image3 = BaseImage.extend({
|
|
1997
1997
|
addOptions() {
|
|
1998
1998
|
return {
|
|
1999
1999
|
...this.parent?.(),
|
|
@@ -2188,7 +2188,7 @@ function createRichTextPreset(options = {}) {
|
|
|
2188
2188
|
if (options.image !== false) {
|
|
2189
2189
|
const imageConfig = typeof options.image === "object" ? options.image : {};
|
|
2190
2190
|
extensions2.push(
|
|
2191
|
-
|
|
2191
|
+
Image3.configure({
|
|
2192
2192
|
HTMLAttributes: {
|
|
2193
2193
|
class: "rounded-md max-w-full h-auto my-2",
|
|
2194
2194
|
...imageConfig.HTMLAttributes || {}
|
|
@@ -5448,11 +5448,1394 @@ var EDITOR_MCP_TOOLS = [
|
|
|
5448
5448
|
MCP_TOOL_EDIT_PROMPT,
|
|
5449
5449
|
MCP_TOOL_VIEW_EDITOR
|
|
5450
5450
|
];
|
|
5451
|
+
|
|
5452
|
+
// src/image-editor/filters.ts
|
|
5453
|
+
function clamp(value, min = 0, max = 255) {
|
|
5454
|
+
return Math.max(min, Math.min(max, value));
|
|
5455
|
+
}
|
|
5456
|
+
function applyAdjustments(imageData, adjustments) {
|
|
5457
|
+
const { brightness, contrast, saturation, exposure, temperature, opacity, blur } = adjustments;
|
|
5458
|
+
const data = imageData.data;
|
|
5459
|
+
const len = data.length;
|
|
5460
|
+
const hasBrightness = brightness !== 0;
|
|
5461
|
+
const hasContrast = contrast !== 0;
|
|
5462
|
+
const hasSaturation = saturation !== 0;
|
|
5463
|
+
const hasExposure = exposure !== 0;
|
|
5464
|
+
const hasTemperature = temperature !== 0;
|
|
5465
|
+
const hasOpacity = opacity < 1;
|
|
5466
|
+
const contrastFactor = hasContrast ? 259 * (contrast + 255) / (255 * (259 - contrast)) : 1;
|
|
5467
|
+
const exposureMultiplier = hasExposure ? Math.pow(2, exposure / 50) : 1;
|
|
5468
|
+
const satMultiplier = hasSaturation ? 1 + saturation / 100 : 1;
|
|
5469
|
+
const tempOffset = hasTemperature ? temperature * 0.8 : 0;
|
|
5470
|
+
if (hasBrightness || hasContrast || hasSaturation || hasExposure || hasTemperature || hasOpacity) {
|
|
5471
|
+
for (let i = 0; i < len; i += 4) {
|
|
5472
|
+
let r = data[i];
|
|
5473
|
+
let g = data[i + 1];
|
|
5474
|
+
let b = data[i + 2];
|
|
5475
|
+
let a = data[i + 3];
|
|
5476
|
+
if (hasExposure) {
|
|
5477
|
+
r = clamp(r * exposureMultiplier);
|
|
5478
|
+
g = clamp(g * exposureMultiplier);
|
|
5479
|
+
b = clamp(b * exposureMultiplier);
|
|
5480
|
+
}
|
|
5481
|
+
if (hasBrightness) {
|
|
5482
|
+
const bOffset = brightness * 2.55;
|
|
5483
|
+
r = clamp(r + bOffset);
|
|
5484
|
+
g = clamp(g + bOffset);
|
|
5485
|
+
b = clamp(b + bOffset);
|
|
5486
|
+
}
|
|
5487
|
+
if (hasContrast) {
|
|
5488
|
+
r = clamp(contrastFactor * (r - 128) + 128);
|
|
5489
|
+
g = clamp(contrastFactor * (g - 128) + 128);
|
|
5490
|
+
b = clamp(contrastFactor * (b - 128) + 128);
|
|
5491
|
+
}
|
|
5492
|
+
if (hasSaturation) {
|
|
5493
|
+
const luminance = 0.2989 * r + 0.587 * g + 0.114 * b;
|
|
5494
|
+
r = clamp(luminance + (r - luminance) * satMultiplier);
|
|
5495
|
+
g = clamp(luminance + (g - luminance) * satMultiplier);
|
|
5496
|
+
b = clamp(luminance + (b - luminance) * satMultiplier);
|
|
5497
|
+
}
|
|
5498
|
+
if (hasTemperature) {
|
|
5499
|
+
r = clamp(r + tempOffset);
|
|
5500
|
+
b = clamp(b - tempOffset);
|
|
5501
|
+
}
|
|
5502
|
+
if (hasOpacity) {
|
|
5503
|
+
a = clamp(a * opacity);
|
|
5504
|
+
}
|
|
5505
|
+
data[i] = r;
|
|
5506
|
+
data[i + 1] = g;
|
|
5507
|
+
data[i + 2] = b;
|
|
5508
|
+
data[i + 3] = a;
|
|
5509
|
+
}
|
|
5510
|
+
}
|
|
5511
|
+
if (blur > 0) {
|
|
5512
|
+
applyFastBoxBlur(imageData, Math.min(Math.round(blur), 40));
|
|
5513
|
+
}
|
|
5514
|
+
}
|
|
5515
|
+
function applyFastBoxBlur(imageData, radius) {
|
|
5516
|
+
if (radius < 1) return;
|
|
5517
|
+
const w = imageData.width;
|
|
5518
|
+
const h = imageData.height;
|
|
5519
|
+
const src = imageData.data;
|
|
5520
|
+
const temp = new Uint8ClampedArray(src.length);
|
|
5521
|
+
for (let y = 0; y < h; y++) {
|
|
5522
|
+
for (let x = 0; x < w; x++) {
|
|
5523
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
5524
|
+
let count = 0;
|
|
5525
|
+
for (let k = -radius; k <= radius; k++) {
|
|
5526
|
+
const px = Math.min(w - 1, Math.max(0, x + k));
|
|
5527
|
+
const idx = (y * w + px) * 4;
|
|
5528
|
+
r += src[idx];
|
|
5529
|
+
g += src[idx + 1];
|
|
5530
|
+
b += src[idx + 2];
|
|
5531
|
+
a += src[idx + 3];
|
|
5532
|
+
count++;
|
|
5533
|
+
}
|
|
5534
|
+
const dstIdx = (y * w + x) * 4;
|
|
5535
|
+
temp[dstIdx] = Math.round(r / count);
|
|
5536
|
+
temp[dstIdx + 1] = Math.round(g / count);
|
|
5537
|
+
temp[dstIdx + 2] = Math.round(b / count);
|
|
5538
|
+
temp[dstIdx + 3] = Math.round(a / count);
|
|
5539
|
+
}
|
|
5540
|
+
}
|
|
5541
|
+
for (let x = 0; x < w; x++) {
|
|
5542
|
+
for (let y = 0; y < h; y++) {
|
|
5543
|
+
let r = 0, g = 0, b = 0, a = 0;
|
|
5544
|
+
let count = 0;
|
|
5545
|
+
for (let k = -radius; k <= radius; k++) {
|
|
5546
|
+
const py = Math.min(h - 1, Math.max(0, y + k));
|
|
5547
|
+
const idx = (py * w + x) * 4;
|
|
5548
|
+
r += temp[idx];
|
|
5549
|
+
g += temp[idx + 1];
|
|
5550
|
+
b += temp[idx + 2];
|
|
5551
|
+
a += temp[idx + 3];
|
|
5552
|
+
count++;
|
|
5553
|
+
}
|
|
5554
|
+
const dstIdx = (y * w + x) * 4;
|
|
5555
|
+
src[dstIdx] = Math.round(r / count);
|
|
5556
|
+
src[dstIdx + 1] = Math.round(g / count);
|
|
5557
|
+
src[dstIdx + 2] = Math.round(b / count);
|
|
5558
|
+
src[dstIdx + 3] = Math.round(a / count);
|
|
5559
|
+
}
|
|
5560
|
+
}
|
|
5561
|
+
}
|
|
5562
|
+
function applyDepthMask(mainImageData, maskImageData, options) {
|
|
5563
|
+
const { mode, depthRange, softness, invert } = options;
|
|
5564
|
+
const mainData = mainImageData.data;
|
|
5565
|
+
const maskData = maskImageData.data;
|
|
5566
|
+
const len = Math.min(mainData.length, maskData.length);
|
|
5567
|
+
const effectiveSoftness = Math.max(1, softness || 10);
|
|
5568
|
+
const minDepth = depthRange[0];
|
|
5569
|
+
const maxDepth = depthRange[1];
|
|
5570
|
+
for (let i = 0; i < len; i += 4) {
|
|
5571
|
+
let maskValue = maskData[i];
|
|
5572
|
+
if (invert) {
|
|
5573
|
+
maskValue = 255 - maskValue;
|
|
5574
|
+
}
|
|
5575
|
+
let alphaValue;
|
|
5576
|
+
if (mode === "threshold") {
|
|
5577
|
+
if (maskValue >= maxDepth + effectiveSoftness) {
|
|
5578
|
+
alphaValue = 0;
|
|
5579
|
+
} else if (maskValue > maxDepth) {
|
|
5580
|
+
alphaValue = Math.round((maxDepth + effectiveSoftness - maskValue) / effectiveSoftness * 255);
|
|
5581
|
+
} else if (maskValue >= minDepth) {
|
|
5582
|
+
alphaValue = 255;
|
|
5583
|
+
} else if (maskValue < minDepth - effectiveSoftness) {
|
|
5584
|
+
alphaValue = 0;
|
|
5585
|
+
} else {
|
|
5586
|
+
alphaValue = Math.round((maskValue - (minDepth - effectiveSoftness)) / effectiveSoftness * 255);
|
|
5587
|
+
}
|
|
5588
|
+
} else {
|
|
5589
|
+
alphaValue = clamp(maskValue + minDepth);
|
|
5590
|
+
}
|
|
5591
|
+
mainData[i + 3] = clamp(mainData[i + 3] * alphaValue / 255);
|
|
5592
|
+
}
|
|
5593
|
+
}
|
|
5594
|
+
function removeAlpha(imageData) {
|
|
5595
|
+
const data = imageData.data;
|
|
5596
|
+
const len = data.length;
|
|
5597
|
+
for (let i = 3; i < len; i += 4) {
|
|
5598
|
+
data[i] = 255;
|
|
5599
|
+
}
|
|
5600
|
+
}
|
|
5601
|
+
|
|
5602
|
+
// src/image-editor/renderer.ts
|
|
5603
|
+
var ImageCache = class {
|
|
5604
|
+
cache = /* @__PURE__ */ new Map();
|
|
5605
|
+
async get(src) {
|
|
5606
|
+
const existing = this.cache.get(src);
|
|
5607
|
+
if (existing && existing.complete) {
|
|
5608
|
+
return existing;
|
|
5609
|
+
}
|
|
5610
|
+
return new Promise((resolve, reject) => {
|
|
5611
|
+
if (typeof window === "undefined" || typeof Image === "undefined") {
|
|
5612
|
+
reject(new Error("Image loading is only supported in browser/DOM environments"));
|
|
5613
|
+
return;
|
|
5614
|
+
}
|
|
5615
|
+
const img = new Image();
|
|
5616
|
+
img.crossOrigin = "anonymous";
|
|
5617
|
+
img.onload = () => {
|
|
5618
|
+
this.cache.set(src, img);
|
|
5619
|
+
resolve(img);
|
|
5620
|
+
};
|
|
5621
|
+
img.onerror = (e) => reject(new Error(`Failed to load image from: ${src}`));
|
|
5622
|
+
img.src = src;
|
|
5623
|
+
if (typeof navigator !== "undefined" && /jsdom/i.test(navigator.userAgent)) {
|
|
5624
|
+
setTimeout(() => {
|
|
5625
|
+
try {
|
|
5626
|
+
Object.defineProperty(img, "naturalWidth", { value: 800, configurable: true });
|
|
5627
|
+
Object.defineProperty(img, "naturalHeight", { value: 600, configurable: true });
|
|
5628
|
+
Object.defineProperty(img, "complete", { value: true, configurable: true });
|
|
5629
|
+
} catch {
|
|
5630
|
+
}
|
|
5631
|
+
this.cache.set(src, img);
|
|
5632
|
+
resolve(img);
|
|
5633
|
+
}, 10);
|
|
5634
|
+
}
|
|
5635
|
+
});
|
|
5636
|
+
}
|
|
5637
|
+
set(src, img) {
|
|
5638
|
+
this.cache.set(src, img);
|
|
5639
|
+
}
|
|
5640
|
+
clear() {
|
|
5641
|
+
this.cache.clear();
|
|
5642
|
+
}
|
|
5643
|
+
};
|
|
5644
|
+
var globalImageCache = new ImageCache();
|
|
5645
|
+
var ImageEditorRenderer = class {
|
|
5646
|
+
imageCache = globalImageCache;
|
|
5647
|
+
cachedBaseCanvas = null;
|
|
5648
|
+
cachedBaseKey = "";
|
|
5649
|
+
/**
|
|
5650
|
+
* Helper to create a canvas element safely in browser/JSDOM.
|
|
5651
|
+
*/
|
|
5652
|
+
createCanvas(width, height) {
|
|
5653
|
+
if (typeof document !== "undefined" && document.createElement) {
|
|
5654
|
+
const c = document.createElement("canvas");
|
|
5655
|
+
c.width = Math.max(1, Math.round(width));
|
|
5656
|
+
c.height = Math.max(1, Math.round(height));
|
|
5657
|
+
return c;
|
|
5658
|
+
}
|
|
5659
|
+
return {
|
|
5660
|
+
width: Math.max(1, Math.round(width)),
|
|
5661
|
+
height: Math.max(1, Math.round(height)),
|
|
5662
|
+
getContext: () => null,
|
|
5663
|
+
toDataURL: () => "",
|
|
5664
|
+
toBlob: () => {
|
|
5665
|
+
}
|
|
5666
|
+
};
|
|
5667
|
+
}
|
|
5668
|
+
/**
|
|
5669
|
+
* Renders the editor state onto a canvas (viewport or preview).
|
|
5670
|
+
*/
|
|
5671
|
+
async renderToCanvas(targetCanvas, imageSource, state, options = {}) {
|
|
5672
|
+
const ctx = targetCanvas.getContext("2d");
|
|
5673
|
+
if (!ctx) return;
|
|
5674
|
+
let img;
|
|
5675
|
+
if (typeof imageSource === "string") {
|
|
5676
|
+
img = await this.imageCache.get(imageSource);
|
|
5677
|
+
} else {
|
|
5678
|
+
img = imageSource;
|
|
5679
|
+
}
|
|
5680
|
+
const naturalWidth = img.naturalWidth || img.videoWidth || img.width || 800;
|
|
5681
|
+
const naturalHeight = img.naturalHeight || img.videoHeight || img.height || 600;
|
|
5682
|
+
const { crop, transform, adjustments, depthMask, annotations, selectedAnnotationId, zoom, pan, isOriginalCompared } = state;
|
|
5683
|
+
const vpWidth = options.viewportWidth || targetCanvas.width;
|
|
5684
|
+
const vpHeight = options.viewportHeight || targetCanvas.height;
|
|
5685
|
+
targetCanvas.width = vpWidth;
|
|
5686
|
+
targetCanvas.height = vpHeight;
|
|
5687
|
+
ctx.clearRect(0, 0, vpWidth, vpHeight);
|
|
5688
|
+
if (isOriginalCompared) {
|
|
5689
|
+
this.drawFittedImage(ctx, img, naturalWidth, naturalHeight, vpWidth, vpHeight, zoom, pan);
|
|
5690
|
+
return;
|
|
5691
|
+
}
|
|
5692
|
+
const isActivelyCropping = Boolean(options.showCropOverlay || state.activeTool === "crop");
|
|
5693
|
+
const cropX = !isActivelyCropping && crop ? Math.max(0, crop.x) : 0;
|
|
5694
|
+
const cropY = !isActivelyCropping && crop ? Math.max(0, crop.y) : 0;
|
|
5695
|
+
const cropW = !isActivelyCropping && crop ? Math.min(naturalWidth - cropX, crop.width) : naturalWidth;
|
|
5696
|
+
const cropH = !isActivelyCropping && crop ? Math.min(naturalHeight - cropY, crop.height) : naturalHeight;
|
|
5697
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
5698
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
5699
|
+
const baseW = isSwapped ? cropH : cropW;
|
|
5700
|
+
const baseH = isSwapped ? cropW : cropH;
|
|
5701
|
+
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}`;
|
|
5702
|
+
let baseCanvas = this.cachedBaseCanvas;
|
|
5703
|
+
if (!baseCanvas || this.cachedBaseKey !== baseKey) {
|
|
5704
|
+
baseCanvas = this.createCanvas(baseW, baseH);
|
|
5705
|
+
const baseCtx = baseCanvas.getContext("2d");
|
|
5706
|
+
if (baseCtx) {
|
|
5707
|
+
baseCtx.save();
|
|
5708
|
+
const rad = angle * Math.PI / 180;
|
|
5709
|
+
baseCtx.translate(baseW / 2, baseH / 2);
|
|
5710
|
+
baseCtx.rotate(rad);
|
|
5711
|
+
baseCtx.scale(transform.flipH ? -1 : 1, transform.flipV ? -1 : 1);
|
|
5712
|
+
baseCtx.drawImage(
|
|
5713
|
+
img,
|
|
5714
|
+
cropX,
|
|
5715
|
+
cropY,
|
|
5716
|
+
cropW,
|
|
5717
|
+
cropH,
|
|
5718
|
+
-cropW / 2,
|
|
5719
|
+
-cropH / 2,
|
|
5720
|
+
cropW,
|
|
5721
|
+
cropH
|
|
5722
|
+
);
|
|
5723
|
+
baseCtx.restore();
|
|
5724
|
+
const imageData = baseCtx.getImageData(0, 0, baseW, baseH);
|
|
5725
|
+
if (imageData) {
|
|
5726
|
+
applyAdjustments(imageData, adjustments);
|
|
5727
|
+
if (depthMask.enabled && depthMask.maskSource) {
|
|
5728
|
+
try {
|
|
5729
|
+
const maskImg = await this.imageCache.get(depthMask.maskSource);
|
|
5730
|
+
const maskCanvas = this.createCanvas(baseW, baseH);
|
|
5731
|
+
const maskCtx = maskCanvas.getContext("2d");
|
|
5732
|
+
if (maskCtx) {
|
|
5733
|
+
maskCtx.drawImage(maskImg, cropX, cropY, cropW, cropH, 0, 0, baseW, baseH);
|
|
5734
|
+
const maskData = maskCtx.getImageData(0, 0, baseW, baseH);
|
|
5735
|
+
if (maskData) {
|
|
5736
|
+
applyDepthMask(imageData, maskData, depthMask);
|
|
5737
|
+
}
|
|
5738
|
+
}
|
|
5739
|
+
} catch {
|
|
5740
|
+
}
|
|
5741
|
+
}
|
|
5742
|
+
baseCtx.putImageData(imageData, 0, 0);
|
|
5743
|
+
}
|
|
5744
|
+
}
|
|
5745
|
+
this.cachedBaseCanvas = baseCanvas;
|
|
5746
|
+
this.cachedBaseKey = baseKey;
|
|
5747
|
+
}
|
|
5748
|
+
ctx.save();
|
|
5749
|
+
ctx.translate(vpWidth / 2 + pan.x, vpHeight / 2 + pan.y);
|
|
5750
|
+
ctx.scale(zoom, zoom);
|
|
5751
|
+
const fitScale = Math.min(vpWidth * 0.85 / baseW, vpHeight * 0.85 / baseH, 1);
|
|
5752
|
+
const renderW = baseW * fitScale;
|
|
5753
|
+
const renderH = baseH * fitScale;
|
|
5754
|
+
ctx.drawImage(baseCanvas, -renderW / 2, -renderH / 2, renderW, renderH);
|
|
5755
|
+
const scaleFactor = fitScale;
|
|
5756
|
+
ctx.save();
|
|
5757
|
+
ctx.translate(-renderW / 2, -renderH / 2);
|
|
5758
|
+
ctx.scale(scaleFactor, scaleFactor);
|
|
5759
|
+
for (const ann of annotations) {
|
|
5760
|
+
await this.drawAnnotation(ctx, ann);
|
|
5761
|
+
if (options.showSelectionOverlay && ann.id === selectedAnnotationId) {
|
|
5762
|
+
this.drawAnnotationSelection(ctx, ann);
|
|
5763
|
+
}
|
|
5764
|
+
}
|
|
5765
|
+
if (options.draftAnnotation) {
|
|
5766
|
+
await this.drawAnnotation(ctx, options.draftAnnotation);
|
|
5767
|
+
}
|
|
5768
|
+
ctx.restore();
|
|
5769
|
+
ctx.restore();
|
|
5770
|
+
const isCroppingActive = Boolean(options.showCropOverlay || state.activeTool === "crop");
|
|
5771
|
+
const activeCrop = options.draftCrop !== void 0 && options.draftCrop !== null ? options.draftCrop : crop ?? (isCroppingActive ? {
|
|
5772
|
+
x: 0,
|
|
5773
|
+
y: 0,
|
|
5774
|
+
width: naturalWidth,
|
|
5775
|
+
height: naturalHeight
|
|
5776
|
+
} : null);
|
|
5777
|
+
if (isCroppingActive && activeCrop) {
|
|
5778
|
+
this.drawCropOverlay(ctx, state, vpWidth, vpHeight, naturalWidth, naturalHeight, activeCrop);
|
|
5779
|
+
}
|
|
5780
|
+
}
|
|
5781
|
+
/**
|
|
5782
|
+
* Draws an individual annotation onto the canvas.
|
|
5783
|
+
*/
|
|
5784
|
+
async drawAnnotation(ctx, ann) {
|
|
5785
|
+
ctx.save();
|
|
5786
|
+
if (ann.opacity !== void 0) {
|
|
5787
|
+
ctx.globalAlpha = ann.opacity;
|
|
5788
|
+
}
|
|
5789
|
+
if (ann.rotation) {
|
|
5790
|
+
ctx.translate(ann.x, ann.y);
|
|
5791
|
+
ctx.rotate(ann.rotation * Math.PI / 180);
|
|
5792
|
+
ctx.translate(-ann.x, -ann.y);
|
|
5793
|
+
}
|
|
5794
|
+
switch (ann.type) {
|
|
5795
|
+
case "pen": {
|
|
5796
|
+
const pen = ann;
|
|
5797
|
+
if (pen.points.length > 0) {
|
|
5798
|
+
ctx.beginPath();
|
|
5799
|
+
ctx.strokeStyle = pen.strokeColor;
|
|
5800
|
+
ctx.lineWidth = pen.strokeWidth;
|
|
5801
|
+
ctx.lineCap = "round";
|
|
5802
|
+
ctx.lineJoin = "round";
|
|
5803
|
+
ctx.moveTo(pen.points[0].x, pen.points[0].y);
|
|
5804
|
+
for (let i = 1; i < pen.points.length; i++) {
|
|
5805
|
+
ctx.lineTo(pen.points[i].x, pen.points[i].y);
|
|
5806
|
+
}
|
|
5807
|
+
ctx.stroke();
|
|
5808
|
+
}
|
|
5809
|
+
break;
|
|
5810
|
+
}
|
|
5811
|
+
case "rect": {
|
|
5812
|
+
const rect = ann;
|
|
5813
|
+
ctx.strokeStyle = rect.strokeColor;
|
|
5814
|
+
ctx.lineWidth = rect.strokeWidth;
|
|
5815
|
+
if (rect.fillColor) {
|
|
5816
|
+
ctx.fillStyle = rect.fillColor;
|
|
5817
|
+
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
|
|
5818
|
+
}
|
|
5819
|
+
if (rect.strokeWidth > 0) {
|
|
5820
|
+
ctx.strokeRect(rect.x, rect.y, rect.width, rect.height);
|
|
5821
|
+
}
|
|
5822
|
+
break;
|
|
5823
|
+
}
|
|
5824
|
+
case "circle": {
|
|
5825
|
+
const circle = ann;
|
|
5826
|
+
ctx.beginPath();
|
|
5827
|
+
ctx.strokeStyle = circle.strokeColor;
|
|
5828
|
+
ctx.lineWidth = circle.strokeWidth;
|
|
5829
|
+
ctx.ellipse(circle.x, circle.y, Math.abs(circle.radiusX), Math.abs(circle.radiusY), 0, 0, 2 * Math.PI);
|
|
5830
|
+
if (circle.fillColor) {
|
|
5831
|
+
ctx.fillStyle = circle.fillColor;
|
|
5832
|
+
ctx.fill();
|
|
5833
|
+
}
|
|
5834
|
+
if (circle.strokeWidth > 0) {
|
|
5835
|
+
ctx.stroke();
|
|
5836
|
+
}
|
|
5837
|
+
break;
|
|
5838
|
+
}
|
|
5839
|
+
case "arrow": {
|
|
5840
|
+
const arrow = ann;
|
|
5841
|
+
ctx.strokeStyle = arrow.strokeColor;
|
|
5842
|
+
ctx.fillStyle = arrow.strokeColor;
|
|
5843
|
+
ctx.lineWidth = arrow.strokeWidth;
|
|
5844
|
+
ctx.beginPath();
|
|
5845
|
+
ctx.moveTo(arrow.x, arrow.y);
|
|
5846
|
+
ctx.lineTo(arrow.endX, arrow.endY);
|
|
5847
|
+
ctx.stroke();
|
|
5848
|
+
const angle = Math.atan2(arrow.endY - arrow.y, arrow.endX - arrow.x);
|
|
5849
|
+
const headLen = Math.max(12, arrow.strokeWidth * 3);
|
|
5850
|
+
ctx.beginPath();
|
|
5851
|
+
ctx.moveTo(arrow.endX, arrow.endY);
|
|
5852
|
+
ctx.lineTo(
|
|
5853
|
+
arrow.endX - headLen * Math.cos(angle - Math.PI / 6),
|
|
5854
|
+
arrow.endY - headLen * Math.sin(angle - Math.PI / 6)
|
|
5855
|
+
);
|
|
5856
|
+
ctx.lineTo(
|
|
5857
|
+
arrow.endX - headLen * Math.cos(angle + Math.PI / 6),
|
|
5858
|
+
arrow.endY - headLen * Math.sin(angle + Math.PI / 6)
|
|
5859
|
+
);
|
|
5860
|
+
ctx.closePath();
|
|
5861
|
+
ctx.fill();
|
|
5862
|
+
break;
|
|
5863
|
+
}
|
|
5864
|
+
case "line": {
|
|
5865
|
+
const line = ann;
|
|
5866
|
+
ctx.beginPath();
|
|
5867
|
+
ctx.strokeStyle = line.strokeColor;
|
|
5868
|
+
ctx.lineWidth = line.strokeWidth;
|
|
5869
|
+
ctx.moveTo(line.x, line.y);
|
|
5870
|
+
ctx.lineTo(line.endX, line.endY);
|
|
5871
|
+
ctx.stroke();
|
|
5872
|
+
break;
|
|
5873
|
+
}
|
|
5874
|
+
case "text": {
|
|
5875
|
+
const textAnn = ann;
|
|
5876
|
+
ctx.font = `${textAnn.fontWeight || "normal"} ${textAnn.fontSize}px ${textAnn.fontFamily || "sans-serif"}`;
|
|
5877
|
+
ctx.fillStyle = textAnn.color;
|
|
5878
|
+
ctx.textBaseline = "top";
|
|
5879
|
+
ctx.fillText(textAnn.text, textAnn.x, textAnn.y);
|
|
5880
|
+
break;
|
|
5881
|
+
}
|
|
5882
|
+
case "image": {
|
|
5883
|
+
const imgAnn = ann;
|
|
5884
|
+
try {
|
|
5885
|
+
const loadedImg = await this.imageCache.get(imgAnn.src);
|
|
5886
|
+
ctx.drawImage(loadedImg, imgAnn.x, imgAnn.y, imgAnn.width, imgAnn.height);
|
|
5887
|
+
} catch {
|
|
5888
|
+
}
|
|
5889
|
+
break;
|
|
5890
|
+
}
|
|
5891
|
+
}
|
|
5892
|
+
ctx.restore();
|
|
5893
|
+
}
|
|
5894
|
+
/**
|
|
5895
|
+
* Draws selection box around active annotation.
|
|
5896
|
+
*/
|
|
5897
|
+
drawAnnotationSelection(ctx, ann) {
|
|
5898
|
+
ctx.save();
|
|
5899
|
+
ctx.strokeStyle = "#3b82f6";
|
|
5900
|
+
ctx.lineWidth = 1.5;
|
|
5901
|
+
ctx.setLineDash([4, 4]);
|
|
5902
|
+
const bounds = this.getAnnotationBounds(ann);
|
|
5903
|
+
const pad = 4;
|
|
5904
|
+
ctx.strokeRect(bounds.x - pad, bounds.y - pad, bounds.width + pad * 2, bounds.height + pad * 2);
|
|
5905
|
+
ctx.fillStyle = "#ffffff";
|
|
5906
|
+
ctx.strokeStyle = "#3b82f6";
|
|
5907
|
+
ctx.setLineDash([]);
|
|
5908
|
+
const handleSize = 6;
|
|
5909
|
+
const corners = [
|
|
5910
|
+
{ x: bounds.x - pad, y: bounds.y - pad },
|
|
5911
|
+
{ x: bounds.x + bounds.width + pad, y: bounds.y - pad },
|
|
5912
|
+
{ x: bounds.x - pad, y: bounds.y + bounds.height + pad },
|
|
5913
|
+
{ x: bounds.x + bounds.width + pad, y: bounds.y + bounds.height + pad }
|
|
5914
|
+
];
|
|
5915
|
+
for (const c of corners) {
|
|
5916
|
+
ctx.fillRect(c.x - handleSize / 2, c.y - handleSize / 2, handleSize, handleSize);
|
|
5917
|
+
ctx.strokeRect(c.x - handleSize / 2, c.y - handleSize / 2, handleSize, handleSize);
|
|
5918
|
+
}
|
|
5919
|
+
ctx.restore();
|
|
5920
|
+
}
|
|
5921
|
+
getAnnotationBounds(ann) {
|
|
5922
|
+
switch (ann.type) {
|
|
5923
|
+
case "rect":
|
|
5924
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
5925
|
+
case "circle":
|
|
5926
|
+
return {
|
|
5927
|
+
x: ann.x - Math.abs(ann.radiusX),
|
|
5928
|
+
y: ann.y - Math.abs(ann.radiusY),
|
|
5929
|
+
width: Math.abs(ann.radiusX) * 2,
|
|
5930
|
+
height: Math.abs(ann.radiusY) * 2
|
|
5931
|
+
};
|
|
5932
|
+
case "arrow":
|
|
5933
|
+
case "line": {
|
|
5934
|
+
const minX = Math.min(ann.x, ann.endX);
|
|
5935
|
+
const minY = Math.min(ann.y, ann.endY);
|
|
5936
|
+
return {
|
|
5937
|
+
x: minX,
|
|
5938
|
+
y: minY,
|
|
5939
|
+
width: Math.max(Math.abs(ann.endX - ann.x), 10),
|
|
5940
|
+
height: Math.max(Math.abs(ann.endY - ann.y), 10)
|
|
5941
|
+
};
|
|
5942
|
+
}
|
|
5943
|
+
case "pen": {
|
|
5944
|
+
if (!ann.points.length) return { x: ann.x, y: ann.y, width: 0, height: 0 };
|
|
5945
|
+
let minX = ann.points[0].x, maxX = ann.points[0].x;
|
|
5946
|
+
let minY = ann.points[0].y, maxY = ann.points[0].y;
|
|
5947
|
+
for (const pt of ann.points) {
|
|
5948
|
+
if (pt.x < minX) minX = pt.x;
|
|
5949
|
+
if (pt.x > maxX) maxX = pt.x;
|
|
5950
|
+
if (pt.y < minY) minY = pt.y;
|
|
5951
|
+
if (pt.y > maxY) maxY = pt.y;
|
|
5952
|
+
}
|
|
5953
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
5954
|
+
}
|
|
5955
|
+
case "text":
|
|
5956
|
+
return {
|
|
5957
|
+
x: ann.x,
|
|
5958
|
+
y: ann.y,
|
|
5959
|
+
width: ann.text.length * ann.fontSize * 0.6,
|
|
5960
|
+
height: ann.fontSize * 1.2
|
|
5961
|
+
};
|
|
5962
|
+
case "image":
|
|
5963
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
5964
|
+
}
|
|
5965
|
+
}
|
|
5966
|
+
/**
|
|
5967
|
+
* Draws a rule-of-thirds crop grid overlay.
|
|
5968
|
+
*/
|
|
5969
|
+
drawCropOverlay(ctx, state, vpWidth, vpHeight, imgWidth, imgHeight, cropOverride) {
|
|
5970
|
+
const crop = cropOverride !== void 0 && cropOverride !== null ? cropOverride : state.crop ?? {
|
|
5971
|
+
x: 0,
|
|
5972
|
+
y: 0,
|
|
5973
|
+
width: imgWidth,
|
|
5974
|
+
height: imgHeight
|
|
5975
|
+
};
|
|
5976
|
+
if (!crop) return;
|
|
5977
|
+
const { transform, pan, zoom } = state;
|
|
5978
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
5979
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
5980
|
+
const baseW = isSwapped ? imgHeight : imgWidth;
|
|
5981
|
+
const baseH = isSwapped ? imgWidth : imgHeight;
|
|
5982
|
+
ctx.save();
|
|
5983
|
+
ctx.translate(vpWidth / 2 + pan.x, vpHeight / 2 + pan.y);
|
|
5984
|
+
ctx.scale(zoom, zoom);
|
|
5985
|
+
const fitScale = Math.min(vpWidth * 0.85 / baseW, vpHeight * 0.85 / baseH, 1);
|
|
5986
|
+
const renderW = baseW * fitScale;
|
|
5987
|
+
const renderH = baseH * fitScale;
|
|
5988
|
+
const startX = -renderW / 2 + crop.x / imgWidth * renderW;
|
|
5989
|
+
const startY = -renderH / 2 + crop.y / imgHeight * renderH;
|
|
5990
|
+
const cropBoxW = crop.width / imgWidth * renderW;
|
|
5991
|
+
const cropBoxH = crop.height / imgHeight * renderH;
|
|
5992
|
+
ctx.fillStyle = "rgba(0, 0, 0, 0.45)";
|
|
5993
|
+
ctx.fillRect(-renderW / 2, -renderH / 2, renderW, startY - -renderH / 2);
|
|
5994
|
+
ctx.fillRect(-renderW / 2, startY + cropBoxH, renderW, renderH / 2 - (startY + cropBoxH));
|
|
5995
|
+
ctx.fillRect(-renderW / 2, startY, startX - -renderW / 2, cropBoxH);
|
|
5996
|
+
ctx.fillRect(startX + cropBoxW, startY, renderW / 2 - (startX + cropBoxW), cropBoxH);
|
|
5997
|
+
ctx.strokeStyle = "#ffffff";
|
|
5998
|
+
ctx.lineWidth = 2;
|
|
5999
|
+
ctx.strokeRect(startX, startY, cropBoxW, cropBoxH);
|
|
6000
|
+
ctx.strokeStyle = "rgba(255, 255, 255, 0.4)";
|
|
6001
|
+
ctx.lineWidth = 1;
|
|
6002
|
+
ctx.beginPath();
|
|
6003
|
+
ctx.moveTo(startX + cropBoxW / 3, startY);
|
|
6004
|
+
ctx.lineTo(startX + cropBoxW / 3, startY + cropBoxH);
|
|
6005
|
+
ctx.moveTo(startX + cropBoxW * 2 / 3, startY);
|
|
6006
|
+
ctx.lineTo(startX + cropBoxW * 2 / 3, startY + cropBoxH);
|
|
6007
|
+
ctx.moveTo(startX, startY + cropBoxH / 3);
|
|
6008
|
+
ctx.lineTo(startX + cropBoxW, startY + cropBoxH / 3);
|
|
6009
|
+
ctx.moveTo(startX, startY + cropBoxH * 2 / 3);
|
|
6010
|
+
ctx.lineTo(startX + cropBoxW, startY + cropBoxH * 2 / 3);
|
|
6011
|
+
ctx.stroke();
|
|
6012
|
+
ctx.fillStyle = "#ffffff";
|
|
6013
|
+
ctx.strokeStyle = "#0284c7";
|
|
6014
|
+
ctx.lineWidth = 1.5;
|
|
6015
|
+
const handleSize = 10;
|
|
6016
|
+
const handles = [
|
|
6017
|
+
// Corners
|
|
6018
|
+
{ x: startX, y: startY },
|
|
6019
|
+
{ x: startX + cropBoxW, y: startY },
|
|
6020
|
+
{ x: startX, y: startY + cropBoxH },
|
|
6021
|
+
{ x: startX + cropBoxW, y: startY + cropBoxH },
|
|
6022
|
+
// Edges
|
|
6023
|
+
{ x: startX + cropBoxW / 2, y: startY },
|
|
6024
|
+
{ x: startX + cropBoxW / 2, y: startY + cropBoxH },
|
|
6025
|
+
{ x: startX, y: startY + cropBoxH / 2 },
|
|
6026
|
+
{ x: startX + cropBoxW, y: startY + cropBoxH / 2 }
|
|
6027
|
+
];
|
|
6028
|
+
for (const h of handles) {
|
|
6029
|
+
ctx.fillRect(h.x - handleSize / 2, h.y - handleSize / 2, handleSize, handleSize);
|
|
6030
|
+
ctx.strokeRect(h.x - handleSize / 2, h.y - handleSize / 2, handleSize, handleSize);
|
|
6031
|
+
}
|
|
6032
|
+
ctx.restore();
|
|
6033
|
+
}
|
|
6034
|
+
drawFittedImage(ctx, img, imgW, imgH, vpW, vpH, zoom, pan) {
|
|
6035
|
+
ctx.save();
|
|
6036
|
+
ctx.translate(vpW / 2 + pan.x, vpH / 2 + pan.y);
|
|
6037
|
+
ctx.scale(zoom, zoom);
|
|
6038
|
+
const fitScale = Math.min(vpW * 0.85 / imgW, vpH * 0.85 / imgH, 1);
|
|
6039
|
+
const renderW = imgW * fitScale;
|
|
6040
|
+
const renderH = imgH * fitScale;
|
|
6041
|
+
ctx.drawImage(img, -renderW / 2, -renderH / 2, renderW, renderH);
|
|
6042
|
+
ctx.restore();
|
|
6043
|
+
}
|
|
6044
|
+
/**
|
|
6045
|
+
* Performs an export render at exact pixel dimensions without zoom/pan viewport offsets.
|
|
6046
|
+
*/
|
|
6047
|
+
async renderExport(imageSource, state, options = {}) {
|
|
6048
|
+
let img;
|
|
6049
|
+
if (typeof imageSource === "string") {
|
|
6050
|
+
img = await this.imageCache.get(imageSource);
|
|
6051
|
+
} else {
|
|
6052
|
+
img = imageSource;
|
|
6053
|
+
}
|
|
6054
|
+
const naturalWidth = img.naturalWidth || img.videoWidth || img.width || 800;
|
|
6055
|
+
const naturalHeight = img.naturalHeight || img.videoHeight || img.height || 600;
|
|
6056
|
+
const { crop, transform, adjustments, depthMask, annotations } = state;
|
|
6057
|
+
const cropX = crop ? Math.max(0, crop.x) : 0;
|
|
6058
|
+
const cropY = crop ? Math.max(0, crop.y) : 0;
|
|
6059
|
+
const cropW = crop ? Math.min(naturalWidth - cropX, crop.width) : naturalWidth;
|
|
6060
|
+
const cropH = crop ? Math.min(naturalHeight - cropY, crop.height) : naturalHeight;
|
|
6061
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
6062
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
6063
|
+
const baseW = isSwapped ? cropH : cropW;
|
|
6064
|
+
const baseH = isSwapped ? cropW : cropH;
|
|
6065
|
+
const exportW = options.width || baseW;
|
|
6066
|
+
const exportH = options.height || baseH;
|
|
6067
|
+
const exportCanvas = this.createCanvas(exportW, exportH);
|
|
6068
|
+
const ctx = exportCanvas.getContext("2d");
|
|
6069
|
+
if (!ctx) return exportCanvas;
|
|
6070
|
+
const intermediateCanvas = this.createCanvas(baseW, baseH);
|
|
6071
|
+
const interCtx = intermediateCanvas.getContext("2d");
|
|
6072
|
+
if (interCtx) {
|
|
6073
|
+
interCtx.save();
|
|
6074
|
+
const rad = angle * Math.PI / 180;
|
|
6075
|
+
interCtx.translate(baseW / 2, baseH / 2);
|
|
6076
|
+
interCtx.rotate(rad);
|
|
6077
|
+
interCtx.scale(transform.flipH ? -1 : 1, transform.flipV ? -1 : 1);
|
|
6078
|
+
interCtx.drawImage(img, cropX, cropY, cropW, cropH, -cropW / 2, -cropH / 2, cropW, cropH);
|
|
6079
|
+
interCtx.restore();
|
|
6080
|
+
const imageData = interCtx.getImageData(0, 0, baseW, baseH);
|
|
6081
|
+
if (imageData) {
|
|
6082
|
+
applyAdjustments(imageData, adjustments);
|
|
6083
|
+
if (depthMask.enabled && depthMask.maskSource) {
|
|
6084
|
+
try {
|
|
6085
|
+
const maskImg = await this.imageCache.get(depthMask.maskSource);
|
|
6086
|
+
const maskCanvas = this.createCanvas(baseW, baseH);
|
|
6087
|
+
const maskCtx = maskCanvas.getContext("2d");
|
|
6088
|
+
if (maskCtx) {
|
|
6089
|
+
maskCtx.drawImage(maskImg, cropX, cropY, cropW, cropH, 0, 0, baseW, baseH);
|
|
6090
|
+
const maskData = maskCtx.getImageData(0, 0, baseW, baseH);
|
|
6091
|
+
if (maskData) {
|
|
6092
|
+
applyDepthMask(imageData, maskData, depthMask);
|
|
6093
|
+
}
|
|
6094
|
+
}
|
|
6095
|
+
} catch {
|
|
6096
|
+
}
|
|
6097
|
+
}
|
|
6098
|
+
interCtx.putImageData(imageData, 0, 0);
|
|
6099
|
+
}
|
|
6100
|
+
}
|
|
6101
|
+
ctx.drawImage(intermediateCanvas, 0, 0, baseW, baseH, 0, 0, exportW, exportH);
|
|
6102
|
+
if (options.includeAnnotations !== false && annotations.length > 0) {
|
|
6103
|
+
const annScaleX = exportW / baseW;
|
|
6104
|
+
const annScaleY = exportH / baseH;
|
|
6105
|
+
ctx.save();
|
|
6106
|
+
ctx.scale(annScaleX, annScaleY);
|
|
6107
|
+
for (const ann of annotations) {
|
|
6108
|
+
await this.drawAnnotation(ctx, ann);
|
|
6109
|
+
}
|
|
6110
|
+
ctx.restore();
|
|
6111
|
+
}
|
|
6112
|
+
return exportCanvas;
|
|
6113
|
+
}
|
|
6114
|
+
/**
|
|
6115
|
+
* Generates a binary mask canvas (white for mask/selected area, black for unmasked).
|
|
6116
|
+
* Useful for AI inpainting and selective generative fill.
|
|
6117
|
+
*/
|
|
6118
|
+
async renderMask(state, options) {
|
|
6119
|
+
const naturalWidth = state.imageDimensions.width;
|
|
6120
|
+
const naturalHeight = state.imageDimensions.height;
|
|
6121
|
+
const maskCanvas = this.createCanvas(naturalWidth, naturalHeight);
|
|
6122
|
+
const ctx = maskCanvas.getContext("2d");
|
|
6123
|
+
if (!ctx) return maskCanvas;
|
|
6124
|
+
ctx.fillStyle = "#000000";
|
|
6125
|
+
ctx.fillRect(0, 0, naturalWidth, naturalHeight);
|
|
6126
|
+
const useCrop = options?.useCrop ?? true;
|
|
6127
|
+
const useAnnotations = options?.useAnnotations ?? true;
|
|
6128
|
+
if (useCrop && state.crop) {
|
|
6129
|
+
ctx.fillStyle = "#ffffff";
|
|
6130
|
+
ctx.fillRect(state.crop.x, state.crop.y, state.crop.width, state.crop.height);
|
|
6131
|
+
}
|
|
6132
|
+
if (useAnnotations && state.annotations.length > 0) {
|
|
6133
|
+
for (const ann of state.annotations) {
|
|
6134
|
+
ctx.save();
|
|
6135
|
+
if (ann.rotation) {
|
|
6136
|
+
ctx.translate(ann.x, ann.y);
|
|
6137
|
+
ctx.rotate(ann.rotation * Math.PI / 180);
|
|
6138
|
+
ctx.translate(-ann.x, -ann.y);
|
|
6139
|
+
}
|
|
6140
|
+
if (ann.type === "pen") {
|
|
6141
|
+
const pen = ann;
|
|
6142
|
+
if (pen.points.length > 0) {
|
|
6143
|
+
ctx.beginPath();
|
|
6144
|
+
ctx.strokeStyle = "#ffffff";
|
|
6145
|
+
ctx.lineWidth = Math.max(pen.strokeWidth, 8);
|
|
6146
|
+
ctx.lineCap = "round";
|
|
6147
|
+
ctx.lineJoin = "round";
|
|
6148
|
+
ctx.moveTo(pen.points[0].x, pen.points[0].y);
|
|
6149
|
+
for (let i = 1; i < pen.points.length; i++) {
|
|
6150
|
+
ctx.lineTo(pen.points[i].x, pen.points[i].y);
|
|
6151
|
+
}
|
|
6152
|
+
ctx.stroke();
|
|
6153
|
+
}
|
|
6154
|
+
} else if (ann.type === "rect") {
|
|
6155
|
+
const rect = ann;
|
|
6156
|
+
ctx.fillStyle = "#ffffff";
|
|
6157
|
+
ctx.fillRect(rect.x, rect.y, rect.width, rect.height);
|
|
6158
|
+
} else if (ann.type === "circle") {
|
|
6159
|
+
const circ = ann;
|
|
6160
|
+
ctx.fillStyle = "#ffffff";
|
|
6161
|
+
ctx.beginPath();
|
|
6162
|
+
ctx.ellipse(circ.x, circ.y, circ.radiusX, circ.radiusY, 0, 0, Math.PI * 2);
|
|
6163
|
+
ctx.fill();
|
|
6164
|
+
} else {
|
|
6165
|
+
ctx.strokeStyle = "#ffffff";
|
|
6166
|
+
ctx.fillStyle = "#ffffff";
|
|
6167
|
+
await this.drawAnnotation(ctx, ann);
|
|
6168
|
+
}
|
|
6169
|
+
ctx.restore();
|
|
6170
|
+
}
|
|
6171
|
+
}
|
|
6172
|
+
return maskCanvas;
|
|
6173
|
+
}
|
|
6174
|
+
};
|
|
6175
|
+
|
|
6176
|
+
// src/image-editor/controller.ts
|
|
6177
|
+
var DEFAULT_ADJUSTMENTS = {
|
|
6178
|
+
brightness: 0,
|
|
6179
|
+
contrast: 0,
|
|
6180
|
+
saturation: 0,
|
|
6181
|
+
exposure: 0,
|
|
6182
|
+
temperature: 0,
|
|
6183
|
+
blur: 0,
|
|
6184
|
+
opacity: 1
|
|
6185
|
+
};
|
|
6186
|
+
var DEFAULT_TRANSFORM = {
|
|
6187
|
+
rotate: 0,
|
|
6188
|
+
flipH: false,
|
|
6189
|
+
flipV: false
|
|
6190
|
+
};
|
|
6191
|
+
var DEFAULT_DEPTH_MASK = {
|
|
6192
|
+
enabled: false,
|
|
6193
|
+
maskSource: null,
|
|
6194
|
+
mode: "threshold",
|
|
6195
|
+
depthRange: [0, 255],
|
|
6196
|
+
softness: 10,
|
|
6197
|
+
invert: false
|
|
6198
|
+
};
|
|
6199
|
+
var ImageEditorController = class {
|
|
6200
|
+
state;
|
|
6201
|
+
renderer;
|
|
6202
|
+
imageElement = null;
|
|
6203
|
+
listeners = /* @__PURE__ */ new Set();
|
|
6204
|
+
onStateChange;
|
|
6205
|
+
// History stacks
|
|
6206
|
+
undoStack = [];
|
|
6207
|
+
redoStack = [];
|
|
6208
|
+
maxHistoryDepth;
|
|
6209
|
+
isApplyingHistory = false;
|
|
6210
|
+
initialSnapshot;
|
|
6211
|
+
constructor(options = {}) {
|
|
6212
|
+
this.maxHistoryDepth = options.maxHistoryDepth ?? 50;
|
|
6213
|
+
this.onStateChange = options.onStateChange;
|
|
6214
|
+
this.renderer = new ImageEditorRenderer();
|
|
6215
|
+
const initialAdjustments = {
|
|
6216
|
+
...DEFAULT_ADJUSTMENTS,
|
|
6217
|
+
...options.initialState?.adjustments
|
|
6218
|
+
};
|
|
6219
|
+
const initialTransform = {
|
|
6220
|
+
...DEFAULT_TRANSFORM,
|
|
6221
|
+
...options.initialState?.transform
|
|
6222
|
+
};
|
|
6223
|
+
const initialDepthMask = {
|
|
6224
|
+
...DEFAULT_DEPTH_MASK,
|
|
6225
|
+
...options.initialState?.depthMask
|
|
6226
|
+
};
|
|
6227
|
+
this.state = {
|
|
6228
|
+
sourceUrl: typeof options.image === "string" ? options.image : null,
|
|
6229
|
+
imageDimensions: { width: 800, height: 600 },
|
|
6230
|
+
crop: options.initialState?.crop ?? null,
|
|
6231
|
+
transform: initialTransform,
|
|
6232
|
+
adjustments: initialAdjustments,
|
|
6233
|
+
depthMask: initialDepthMask,
|
|
6234
|
+
annotations: options.initialState?.annotations ?? [],
|
|
6235
|
+
selectedAnnotationId: null,
|
|
6236
|
+
activeTool: "select",
|
|
6237
|
+
zoom: 1,
|
|
6238
|
+
pan: { x: 0, y: 0 },
|
|
6239
|
+
isOriginalCompared: false
|
|
6240
|
+
};
|
|
6241
|
+
if (options.image instanceof Object && "width" in options.image) {
|
|
6242
|
+
this.imageElement = options.image;
|
|
6243
|
+
this.state.imageDimensions = {
|
|
6244
|
+
width: options.image.naturalWidth || options.image.width || 800,
|
|
6245
|
+
height: options.image.naturalHeight || options.image.height || 600
|
|
6246
|
+
};
|
|
6247
|
+
}
|
|
6248
|
+
this.initialSnapshot = this.serializeState();
|
|
6249
|
+
if (typeof options.image === "string") {
|
|
6250
|
+
this.loadImage(options.image);
|
|
6251
|
+
}
|
|
6252
|
+
}
|
|
6253
|
+
// --- Image Loading ---
|
|
6254
|
+
async loadImage(src) {
|
|
6255
|
+
if (typeof src === "string") {
|
|
6256
|
+
this.state.sourceUrl = src;
|
|
6257
|
+
try {
|
|
6258
|
+
const img = await this.renderer.imageCache.get(src);
|
|
6259
|
+
this.imageElement = img;
|
|
6260
|
+
this.state.imageDimensions = {
|
|
6261
|
+
width: img.naturalWidth || img.width || 800,
|
|
6262
|
+
height: img.naturalHeight || img.height || 600
|
|
6263
|
+
};
|
|
6264
|
+
} catch (err) {
|
|
6265
|
+
}
|
|
6266
|
+
} else {
|
|
6267
|
+
this.imageElement = src;
|
|
6268
|
+
this.state.sourceUrl = src.src || null;
|
|
6269
|
+
this.state.imageDimensions = {
|
|
6270
|
+
width: src.naturalWidth || src.width || 800,
|
|
6271
|
+
height: src.naturalHeight || src.height || 600
|
|
6272
|
+
};
|
|
6273
|
+
}
|
|
6274
|
+
this.initialSnapshot = this.serializeState();
|
|
6275
|
+
this.notify();
|
|
6276
|
+
}
|
|
6277
|
+
getImageElement() {
|
|
6278
|
+
return this.imageElement;
|
|
6279
|
+
}
|
|
6280
|
+
// --- State & Subscription ---
|
|
6281
|
+
getState() {
|
|
6282
|
+
return this.state;
|
|
6283
|
+
}
|
|
6284
|
+
subscribe(listener) {
|
|
6285
|
+
this.listeners.add(listener);
|
|
6286
|
+
listener(this.state);
|
|
6287
|
+
return () => {
|
|
6288
|
+
this.listeners.delete(listener);
|
|
6289
|
+
};
|
|
6290
|
+
}
|
|
6291
|
+
notify() {
|
|
6292
|
+
const currentState = { ...this.state };
|
|
6293
|
+
this.onStateChange?.(currentState);
|
|
6294
|
+
this.listeners.forEach((l) => l(currentState));
|
|
6295
|
+
}
|
|
6296
|
+
// --- History (Undo / Redo / Reset) ---
|
|
6297
|
+
recordHistory() {
|
|
6298
|
+
if (this.isApplyingHistory) return;
|
|
6299
|
+
this.undoStack.push(this.serializeState());
|
|
6300
|
+
if (this.undoStack.length > this.maxHistoryDepth) {
|
|
6301
|
+
this.undoStack.shift();
|
|
6302
|
+
}
|
|
6303
|
+
this.redoStack = [];
|
|
6304
|
+
}
|
|
6305
|
+
get canUndo() {
|
|
6306
|
+
return this.undoStack.length > 0;
|
|
6307
|
+
}
|
|
6308
|
+
get canRedo() {
|
|
6309
|
+
return this.redoStack.length > 0;
|
|
6310
|
+
}
|
|
6311
|
+
undo() {
|
|
6312
|
+
if (!this.canUndo) return false;
|
|
6313
|
+
this.isApplyingHistory = true;
|
|
6314
|
+
const current = this.serializeState();
|
|
6315
|
+
this.redoStack.push(current);
|
|
6316
|
+
const prev = this.undoStack.pop();
|
|
6317
|
+
this.applySerializedState(prev);
|
|
6318
|
+
this.isApplyingHistory = false;
|
|
6319
|
+
this.notify();
|
|
6320
|
+
return true;
|
|
6321
|
+
}
|
|
6322
|
+
redo() {
|
|
6323
|
+
if (!this.canRedo) return false;
|
|
6324
|
+
this.isApplyingHistory = true;
|
|
6325
|
+
const current = this.serializeState();
|
|
6326
|
+
this.undoStack.push(current);
|
|
6327
|
+
const next = this.redoStack.pop();
|
|
6328
|
+
this.applySerializedState(next);
|
|
6329
|
+
this.isApplyingHistory = false;
|
|
6330
|
+
this.notify();
|
|
6331
|
+
return true;
|
|
6332
|
+
}
|
|
6333
|
+
reset() {
|
|
6334
|
+
this.recordHistory();
|
|
6335
|
+
this.applySerializedState(this.initialSnapshot);
|
|
6336
|
+
this.state.zoom = 1;
|
|
6337
|
+
this.state.pan = { x: 0, y: 0 };
|
|
6338
|
+
this.state.activeTool = "select";
|
|
6339
|
+
this.state.selectedAnnotationId = null;
|
|
6340
|
+
this.notify();
|
|
6341
|
+
}
|
|
6342
|
+
// --- Active Tool ---
|
|
6343
|
+
setTool(tool) {
|
|
6344
|
+
if (this.state.activeTool === tool) return;
|
|
6345
|
+
this.state.activeTool = tool;
|
|
6346
|
+
if (tool !== "select") {
|
|
6347
|
+
this.state.selectedAnnotationId = null;
|
|
6348
|
+
}
|
|
6349
|
+
if (tool === "crop" && !this.state.crop) {
|
|
6350
|
+
this.state.crop = {
|
|
6351
|
+
x: 0,
|
|
6352
|
+
y: 0,
|
|
6353
|
+
width: this.state.imageDimensions.width,
|
|
6354
|
+
height: this.state.imageDimensions.height
|
|
6355
|
+
};
|
|
6356
|
+
}
|
|
6357
|
+
this.notify();
|
|
6358
|
+
}
|
|
6359
|
+
// --- Crop ---
|
|
6360
|
+
setCrop(crop, addToHistory = true) {
|
|
6361
|
+
if (addToHistory) {
|
|
6362
|
+
this.recordHistory();
|
|
6363
|
+
}
|
|
6364
|
+
this.state.crop = crop;
|
|
6365
|
+
this.notify();
|
|
6366
|
+
}
|
|
6367
|
+
applyCropPreset(ratio) {
|
|
6368
|
+
this.recordHistory();
|
|
6369
|
+
const { width, height } = this.state.imageDimensions;
|
|
6370
|
+
if (!ratio) {
|
|
6371
|
+
this.state.crop = { x: 0, y: 0, width, height };
|
|
6372
|
+
} else {
|
|
6373
|
+
let targetW = width;
|
|
6374
|
+
let targetH = targetW / ratio;
|
|
6375
|
+
if (targetH > height) {
|
|
6376
|
+
targetH = height;
|
|
6377
|
+
targetW = targetH * ratio;
|
|
6378
|
+
}
|
|
6379
|
+
const x = Math.max(0, Math.round((width - targetW) / 2));
|
|
6380
|
+
const y = Math.max(0, Math.round((height - targetH) / 2));
|
|
6381
|
+
this.state.crop = {
|
|
6382
|
+
x,
|
|
6383
|
+
y,
|
|
6384
|
+
width: Math.round(targetW),
|
|
6385
|
+
height: Math.round(targetH)
|
|
6386
|
+
};
|
|
6387
|
+
}
|
|
6388
|
+
this.notify();
|
|
6389
|
+
}
|
|
6390
|
+
resetCrop() {
|
|
6391
|
+
this.recordHistory();
|
|
6392
|
+
this.state.crop = null;
|
|
6393
|
+
this.notify();
|
|
6394
|
+
}
|
|
6395
|
+
// --- Transformations (Rotate / Flip) ---
|
|
6396
|
+
rotate(degrees) {
|
|
6397
|
+
this.recordHistory();
|
|
6398
|
+
const current = this.state.transform.rotate;
|
|
6399
|
+
this.state.transform.rotate = (current + degrees) % 360;
|
|
6400
|
+
this.notify();
|
|
6401
|
+
}
|
|
6402
|
+
setRotation(degrees) {
|
|
6403
|
+
this.recordHistory();
|
|
6404
|
+
this.state.transform.rotate = degrees % 360;
|
|
6405
|
+
this.notify();
|
|
6406
|
+
}
|
|
6407
|
+
flipHorizontal() {
|
|
6408
|
+
this.recordHistory();
|
|
6409
|
+
this.state.transform.flipH = !this.state.transform.flipH;
|
|
6410
|
+
this.notify();
|
|
6411
|
+
}
|
|
6412
|
+
flipVertical() {
|
|
6413
|
+
this.recordHistory();
|
|
6414
|
+
this.state.transform.flipV = !this.state.transform.flipV;
|
|
6415
|
+
this.notify();
|
|
6416
|
+
}
|
|
6417
|
+
// --- Adjustments ---
|
|
6418
|
+
setAdjustments(partial, commitToHistory = true) {
|
|
6419
|
+
if (commitToHistory) {
|
|
6420
|
+
this.recordHistory();
|
|
6421
|
+
}
|
|
6422
|
+
this.state.adjustments = {
|
|
6423
|
+
...this.state.adjustments,
|
|
6424
|
+
...partial
|
|
6425
|
+
};
|
|
6426
|
+
this.notify();
|
|
6427
|
+
}
|
|
6428
|
+
resetAdjustments() {
|
|
6429
|
+
this.recordHistory();
|
|
6430
|
+
this.state.adjustments = { ...DEFAULT_ADJUSTMENTS };
|
|
6431
|
+
this.notify();
|
|
6432
|
+
}
|
|
6433
|
+
// --- Depth Masking (Gleamforge) ---
|
|
6434
|
+
setDepthMask(partial) {
|
|
6435
|
+
this.recordHistory();
|
|
6436
|
+
this.state.depthMask = {
|
|
6437
|
+
...this.state.depthMask,
|
|
6438
|
+
...partial
|
|
6439
|
+
};
|
|
6440
|
+
this.notify();
|
|
6441
|
+
}
|
|
6442
|
+
resetDepthMask() {
|
|
6443
|
+
this.recordHistory();
|
|
6444
|
+
this.state.depthMask = { ...DEFAULT_DEPTH_MASK };
|
|
6445
|
+
this.notify();
|
|
6446
|
+
}
|
|
6447
|
+
// --- Annotations & Layers ---
|
|
6448
|
+
addAnnotation(annotation) {
|
|
6449
|
+
this.recordHistory();
|
|
6450
|
+
const id = `ann_${Date.now()}_${Math.random().toString(36).slice(2, 7)}`;
|
|
6451
|
+
const fullAnnotation = { ...annotation, id };
|
|
6452
|
+
this.state.annotations = [...this.state.annotations, fullAnnotation];
|
|
6453
|
+
this.state.selectedAnnotationId = id;
|
|
6454
|
+
this.notify();
|
|
6455
|
+
return id;
|
|
6456
|
+
}
|
|
6457
|
+
updateAnnotation(id, partial, commitToHistory = true) {
|
|
6458
|
+
if (commitToHistory) {
|
|
6459
|
+
this.recordHistory();
|
|
6460
|
+
}
|
|
6461
|
+
this.state.annotations = this.state.annotations.map((ann) => {
|
|
6462
|
+
if (ann.id === id) {
|
|
6463
|
+
return { ...ann, ...partial };
|
|
6464
|
+
}
|
|
6465
|
+
return ann;
|
|
6466
|
+
});
|
|
6467
|
+
this.notify();
|
|
6468
|
+
}
|
|
6469
|
+
removeAnnotation(id) {
|
|
6470
|
+
this.recordHistory();
|
|
6471
|
+
this.state.annotations = this.state.annotations.filter((ann) => ann.id !== id);
|
|
6472
|
+
if (this.state.selectedAnnotationId === id) {
|
|
6473
|
+
this.state.selectedAnnotationId = null;
|
|
6474
|
+
}
|
|
6475
|
+
this.notify();
|
|
6476
|
+
}
|
|
6477
|
+
clearAnnotations() {
|
|
6478
|
+
this.recordHistory();
|
|
6479
|
+
this.state.annotations = [];
|
|
6480
|
+
this.state.selectedAnnotationId = null;
|
|
6481
|
+
this.notify();
|
|
6482
|
+
}
|
|
6483
|
+
selectAnnotation(id) {
|
|
6484
|
+
this.state.selectedAnnotationId = id;
|
|
6485
|
+
this.notify();
|
|
6486
|
+
}
|
|
6487
|
+
// --- Zoom & Pan ---
|
|
6488
|
+
setZoom(zoom) {
|
|
6489
|
+
this.state.zoom = Math.max(0.1, Math.min(10, zoom));
|
|
6490
|
+
this.notify();
|
|
6491
|
+
}
|
|
6492
|
+
zoomIn(factor = 1.2) {
|
|
6493
|
+
this.setZoom(this.state.zoom * factor);
|
|
6494
|
+
}
|
|
6495
|
+
zoomOut(factor = 1.2) {
|
|
6496
|
+
this.setZoom(this.state.zoom / factor);
|
|
6497
|
+
}
|
|
6498
|
+
resetZoom() {
|
|
6499
|
+
this.state.zoom = 1;
|
|
6500
|
+
this.state.pan = { x: 0, y: 0 };
|
|
6501
|
+
this.notify();
|
|
6502
|
+
}
|
|
6503
|
+
setPan(pan) {
|
|
6504
|
+
this.state.pan = pan;
|
|
6505
|
+
this.notify();
|
|
6506
|
+
}
|
|
6507
|
+
setComparing(comparing) {
|
|
6508
|
+
if (this.state.isOriginalCompared === comparing) return;
|
|
6509
|
+
this.state.isOriginalCompared = comparing;
|
|
6510
|
+
this.notify();
|
|
6511
|
+
}
|
|
6512
|
+
// --- Rendering & Export ---
|
|
6513
|
+
async render(targetCanvas, options) {
|
|
6514
|
+
const source = this.imageElement || this.state.sourceUrl;
|
|
6515
|
+
if (!source) return;
|
|
6516
|
+
try {
|
|
6517
|
+
await this.renderer.renderToCanvas(targetCanvas, source, this.state, options);
|
|
6518
|
+
} catch {
|
|
6519
|
+
}
|
|
6520
|
+
}
|
|
6521
|
+
async renderExport(options) {
|
|
6522
|
+
const source = this.imageElement || this.state.sourceUrl;
|
|
6523
|
+
if (!source) {
|
|
6524
|
+
return this.renderer.createCanvas(1, 1);
|
|
6525
|
+
}
|
|
6526
|
+
return this.renderer.renderExport(source, this.state, options);
|
|
6527
|
+
}
|
|
6528
|
+
async toDataURL(options = {}) {
|
|
6529
|
+
const canvas = await this.renderExport(options);
|
|
6530
|
+
const format = options.format || "image/png";
|
|
6531
|
+
const quality = options.quality ?? 0.92;
|
|
6532
|
+
return canvas.toDataURL(format, quality);
|
|
6533
|
+
}
|
|
6534
|
+
async toBlob(options = {}) {
|
|
6535
|
+
const canvas = await this.renderExport(options);
|
|
6536
|
+
const format = options.format || "image/png";
|
|
6537
|
+
const quality = options.quality ?? 0.92;
|
|
6538
|
+
return new Promise((resolve, reject) => {
|
|
6539
|
+
if (canvas.toBlob) {
|
|
6540
|
+
canvas.toBlob((blob) => {
|
|
6541
|
+
if (blob) resolve(blob);
|
|
6542
|
+
else reject(new Error("Failed to create Blob from canvas"));
|
|
6543
|
+
}, format, quality);
|
|
6544
|
+
} else {
|
|
6545
|
+
try {
|
|
6546
|
+
const dataUrl = canvas.toDataURL(format, quality);
|
|
6547
|
+
const parts = dataUrl.split(",");
|
|
6548
|
+
const byteStr = atob(parts[1]);
|
|
6549
|
+
const ab = new ArrayBuffer(byteStr.length);
|
|
6550
|
+
const ia = new Uint8Array(ab);
|
|
6551
|
+
for (let i = 0; i < byteStr.length; i++) {
|
|
6552
|
+
ia[i] = byteStr.charCodeAt(i);
|
|
6553
|
+
}
|
|
6554
|
+
resolve(new Blob([ab], { type: format }));
|
|
6555
|
+
} catch (err) {
|
|
6556
|
+
reject(err);
|
|
6557
|
+
}
|
|
6558
|
+
}
|
|
6559
|
+
});
|
|
6560
|
+
}
|
|
6561
|
+
// --- AI Masking & Transformation Helpers ---
|
|
6562
|
+
async renderMask(options) {
|
|
6563
|
+
return this.renderer.renderMask(this.state, options);
|
|
6564
|
+
}
|
|
6565
|
+
async toMaskDataURL(options) {
|
|
6566
|
+
const canvas = await this.renderMask(options);
|
|
6567
|
+
return canvas.toDataURL("image/png");
|
|
6568
|
+
}
|
|
6569
|
+
async applyInpaintedImage(newDataUrl) {
|
|
6570
|
+
this.recordHistory();
|
|
6571
|
+
await this.loadImage(newDataUrl);
|
|
6572
|
+
this.clearAnnotations();
|
|
6573
|
+
}
|
|
6574
|
+
async applyBackgroundRemovedImage(newDataUrl) {
|
|
6575
|
+
this.recordHistory();
|
|
6576
|
+
await this.loadImage(newDataUrl);
|
|
6577
|
+
}
|
|
6578
|
+
// --- Serialization ---
|
|
6579
|
+
toJSON() {
|
|
6580
|
+
return this.serializeState();
|
|
6581
|
+
}
|
|
6582
|
+
loadJSON(data) {
|
|
6583
|
+
this.recordHistory();
|
|
6584
|
+
this.applySerializedState(data);
|
|
6585
|
+
this.notify();
|
|
6586
|
+
}
|
|
6587
|
+
serializeState() {
|
|
6588
|
+
return {
|
|
6589
|
+
version: 1,
|
|
6590
|
+
crop: this.state.crop ? { ...this.state.crop } : null,
|
|
6591
|
+
transform: { ...this.state.transform },
|
|
6592
|
+
adjustments: { ...this.state.adjustments },
|
|
6593
|
+
depthMask: {
|
|
6594
|
+
...this.state.depthMask,
|
|
6595
|
+
depthRange: [...this.state.depthMask.depthRange]
|
|
6596
|
+
},
|
|
6597
|
+
annotations: JSON.parse(JSON.stringify(this.state.annotations))
|
|
6598
|
+
};
|
|
6599
|
+
}
|
|
6600
|
+
applySerializedState(data) {
|
|
6601
|
+
this.state.crop = data.crop ? { ...data.crop } : null;
|
|
6602
|
+
this.state.transform = { ...data.transform };
|
|
6603
|
+
this.state.adjustments = { ...data.adjustments };
|
|
6604
|
+
this.state.depthMask = {
|
|
6605
|
+
...data.depthMask,
|
|
6606
|
+
depthRange: [...data.depthMask.depthRange]
|
|
6607
|
+
};
|
|
6608
|
+
this.state.annotations = JSON.parse(JSON.stringify(data.annotations || []));
|
|
6609
|
+
}
|
|
6610
|
+
};
|
|
6611
|
+
|
|
6612
|
+
// src/image-editor/geometry.ts
|
|
6613
|
+
function getViewportMetrics(state, vpWidth, vpHeight, isActivelyCropping = false, cropOverride) {
|
|
6614
|
+
const { imageDimensions, crop: stateCrop, transform, zoom, pan } = state;
|
|
6615
|
+
const naturalWidth = imageDimensions.width || 800;
|
|
6616
|
+
const naturalHeight = imageDimensions.height || 600;
|
|
6617
|
+
const activeCrop = cropOverride !== void 0 && cropOverride !== null ? cropOverride : stateCrop ?? (isActivelyCropping ? { x: 0, y: 0, width: naturalWidth, height: naturalHeight } : null);
|
|
6618
|
+
const cropW = !isActivelyCropping && activeCrop ? activeCrop.width : naturalWidth;
|
|
6619
|
+
const cropH = !isActivelyCropping && activeCrop ? activeCrop.height : naturalHeight;
|
|
6620
|
+
const angle = (transform.rotate % 360 + 360) % 360;
|
|
6621
|
+
const isSwapped = angle === 90 || angle === 270;
|
|
6622
|
+
const baseW = isSwapped ? cropH : cropW;
|
|
6623
|
+
const baseH = isSwapped ? cropW : cropH;
|
|
6624
|
+
const fitScale = Math.min(vpWidth * 0.85 / baseW, vpHeight * 0.85 / baseH, 1);
|
|
6625
|
+
const renderW = baseW * fitScale;
|
|
6626
|
+
const renderH = baseH * fitScale;
|
|
6627
|
+
const totalScale = zoom * fitScale;
|
|
6628
|
+
const centerX = vpWidth / 2 + pan.x;
|
|
6629
|
+
const centerY = vpHeight / 2 + pan.y;
|
|
6630
|
+
const imageRect = {
|
|
6631
|
+
x: centerX - renderW * zoom / 2,
|
|
6632
|
+
y: centerY - renderH * zoom / 2,
|
|
6633
|
+
width: renderW * zoom,
|
|
6634
|
+
height: renderH * zoom
|
|
6635
|
+
};
|
|
6636
|
+
let cropRect = null;
|
|
6637
|
+
if (activeCrop) {
|
|
6638
|
+
const startX = -renderW / 2 + activeCrop.x / naturalWidth * renderW;
|
|
6639
|
+
const startY = -renderH / 2 + activeCrop.y / naturalHeight * renderH;
|
|
6640
|
+
const cropBoxW = activeCrop.width / naturalWidth * renderW;
|
|
6641
|
+
const cropBoxH = activeCrop.height / naturalHeight * renderH;
|
|
6642
|
+
cropRect = {
|
|
6643
|
+
x: centerX + zoom * startX,
|
|
6644
|
+
y: centerY + zoom * startY,
|
|
6645
|
+
width: zoom * cropBoxW,
|
|
6646
|
+
height: zoom * cropBoxH
|
|
6647
|
+
};
|
|
6648
|
+
}
|
|
6649
|
+
return {
|
|
6650
|
+
vpWidth,
|
|
6651
|
+
vpHeight,
|
|
6652
|
+
centerX,
|
|
6653
|
+
centerY,
|
|
6654
|
+
baseW,
|
|
6655
|
+
baseH,
|
|
6656
|
+
fitScale,
|
|
6657
|
+
renderW,
|
|
6658
|
+
renderH,
|
|
6659
|
+
totalScale,
|
|
6660
|
+
imageRect,
|
|
6661
|
+
cropRect
|
|
6662
|
+
};
|
|
6663
|
+
}
|
|
6664
|
+
function canvasToImagePoint(canvasPt, state, vpWidth, vpHeight, isActivelyCropping = false) {
|
|
6665
|
+
const metrics = getViewportMetrics(state, vpWidth, vpHeight, isActivelyCropping);
|
|
6666
|
+
const unzoomedX = (canvasPt.x - metrics.centerX) / state.zoom;
|
|
6667
|
+
const unzoomedY = (canvasPt.y - metrics.centerY) / state.zoom;
|
|
6668
|
+
const x = (unzoomedX + metrics.renderW / 2) / metrics.fitScale;
|
|
6669
|
+
const y = (unzoomedY + metrics.renderH / 2) / metrics.fitScale;
|
|
6670
|
+
return { x: Math.round(x), y: Math.round(y) };
|
|
6671
|
+
}
|
|
6672
|
+
function imageToCanvasPoint(imgPt, state, vpWidth, vpHeight, isActivelyCropping = false) {
|
|
6673
|
+
const metrics = getViewportMetrics(state, vpWidth, vpHeight, isActivelyCropping);
|
|
6674
|
+
const unzoomedX = (imgPt.x - metrics.baseW / 2) * metrics.fitScale;
|
|
6675
|
+
const unzoomedY = (imgPt.y - metrics.baseH / 2) * metrics.fitScale;
|
|
6676
|
+
const canvasX = metrics.centerX + unzoomedX * state.zoom;
|
|
6677
|
+
const canvasY = metrics.centerY + unzoomedY * state.zoom;
|
|
6678
|
+
return { x: Math.round(canvasX), y: Math.round(canvasY) };
|
|
6679
|
+
}
|
|
6680
|
+
function hitTestCrop(canvasPt, state, vpWidth, vpHeight, handleThreshold = 14, cropOverride) {
|
|
6681
|
+
const metrics = getViewportMetrics(state, vpWidth, vpHeight, true, cropOverride);
|
|
6682
|
+
if (!metrics.cropRect) return null;
|
|
6683
|
+
const { x, y, width: w, height: h } = metrics.cropRect;
|
|
6684
|
+
const px = canvasPt.x;
|
|
6685
|
+
const py = canvasPt.y;
|
|
6686
|
+
const dist = (x1, y1, x2, y2) => Math.hypot(x1 - x2, y1 - y2);
|
|
6687
|
+
if (dist(px, py, x, y) <= handleThreshold) return "nw";
|
|
6688
|
+
if (dist(px, py, x + w, y) <= handleThreshold) return "ne";
|
|
6689
|
+
if (dist(px, py, x + w, y + h) <= handleThreshold) return "se";
|
|
6690
|
+
if (dist(px, py, x, y + h) <= handleThreshold) return "sw";
|
|
6691
|
+
const nearX = px >= x - handleThreshold && px <= x + w + handleThreshold;
|
|
6692
|
+
const nearY = py >= y - handleThreshold && py <= y + h + handleThreshold;
|
|
6693
|
+
if (nearX && Math.abs(py - y) <= handleThreshold) return "n";
|
|
6694
|
+
if (nearX && Math.abs(py - (y + h)) <= handleThreshold) return "s";
|
|
6695
|
+
if (nearY && Math.abs(px - x) <= handleThreshold) return "w";
|
|
6696
|
+
if (nearY && Math.abs(px - (x + w)) <= handleThreshold) return "e";
|
|
6697
|
+
if (px >= x && px <= x + w && py >= y && py <= y + h) {
|
|
6698
|
+
return "inside";
|
|
6699
|
+
}
|
|
6700
|
+
return null;
|
|
6701
|
+
}
|
|
6702
|
+
function getCropCursor(handle) {
|
|
6703
|
+
switch (handle) {
|
|
6704
|
+
case "nw":
|
|
6705
|
+
case "se":
|
|
6706
|
+
return "nwse-resize";
|
|
6707
|
+
case "ne":
|
|
6708
|
+
case "sw":
|
|
6709
|
+
return "nesw-resize";
|
|
6710
|
+
case "n":
|
|
6711
|
+
case "s":
|
|
6712
|
+
return "ns-resize";
|
|
6713
|
+
case "e":
|
|
6714
|
+
case "w":
|
|
6715
|
+
return "ew-resize";
|
|
6716
|
+
case "inside":
|
|
6717
|
+
return "move";
|
|
6718
|
+
default:
|
|
6719
|
+
return "crosshair";
|
|
6720
|
+
}
|
|
6721
|
+
}
|
|
6722
|
+
function calculateCropDrag(startCrop, handle, startImgPt, currentImgPt, imgWidth, imgHeight, aspectRatio) {
|
|
6723
|
+
const dx = currentImgPt.x - startImgPt.x;
|
|
6724
|
+
const dy = currentImgPt.y - startImgPt.y;
|
|
6725
|
+
if (handle === "inside") {
|
|
6726
|
+
const maxX = Math.max(0, imgWidth - startCrop.width);
|
|
6727
|
+
const maxY = Math.max(0, imgHeight - startCrop.height);
|
|
6728
|
+
const newX = Math.max(0, Math.min(maxX, startCrop.x + dx));
|
|
6729
|
+
const newY = Math.max(0, Math.min(maxY, startCrop.y + dy));
|
|
6730
|
+
return {
|
|
6731
|
+
x: Math.round(newX),
|
|
6732
|
+
y: Math.round(newY),
|
|
6733
|
+
width: startCrop.width,
|
|
6734
|
+
height: startCrop.height
|
|
6735
|
+
};
|
|
6736
|
+
}
|
|
6737
|
+
let left = startCrop.x;
|
|
6738
|
+
let top = startCrop.y;
|
|
6739
|
+
let right = startCrop.x + startCrop.width;
|
|
6740
|
+
let bottom = startCrop.y + startCrop.height;
|
|
6741
|
+
const minSize = 20;
|
|
6742
|
+
if (handle === "e" || handle === "ne" || handle === "se") {
|
|
6743
|
+
right = Math.min(imgWidth, Math.max(left + minSize, startCrop.x + startCrop.width + dx));
|
|
6744
|
+
}
|
|
6745
|
+
if (handle === "w" || handle === "nw" || handle === "sw") {
|
|
6746
|
+
left = Math.max(0, Math.min(right - minSize, startCrop.x + dx));
|
|
6747
|
+
}
|
|
6748
|
+
if (handle === "s" || handle === "se" || handle === "sw") {
|
|
6749
|
+
bottom = Math.min(imgHeight, Math.max(top + minSize, startCrop.y + startCrop.height + dy));
|
|
6750
|
+
}
|
|
6751
|
+
if (handle === "n" || handle === "nw" || handle === "ne") {
|
|
6752
|
+
top = Math.max(0, Math.min(bottom - minSize, startCrop.y + dy));
|
|
6753
|
+
}
|
|
6754
|
+
if (aspectRatio && aspectRatio > 0) {
|
|
6755
|
+
let currentW = right - left;
|
|
6756
|
+
let currentH = bottom - top;
|
|
6757
|
+
if (handle === "e" || handle === "w") {
|
|
6758
|
+
currentH = Math.min(imgHeight, currentW / aspectRatio);
|
|
6759
|
+
bottom = Math.min(imgHeight, top + currentH);
|
|
6760
|
+
} else if (handle === "n" || handle === "s") {
|
|
6761
|
+
currentW = Math.min(imgWidth, currentH * aspectRatio);
|
|
6762
|
+
right = Math.min(imgWidth, left + currentW);
|
|
6763
|
+
} else {
|
|
6764
|
+
const targetH = currentW / aspectRatio;
|
|
6765
|
+
if (top + targetH <= imgHeight) {
|
|
6766
|
+
bottom = top + targetH;
|
|
6767
|
+
} else {
|
|
6768
|
+
const targetW = currentH * aspectRatio;
|
|
6769
|
+
right = Math.min(imgWidth, left + targetW);
|
|
6770
|
+
}
|
|
6771
|
+
}
|
|
6772
|
+
}
|
|
6773
|
+
return {
|
|
6774
|
+
x: Math.round(left),
|
|
6775
|
+
y: Math.round(top),
|
|
6776
|
+
width: Math.round(Math.max(minSize, right - left)),
|
|
6777
|
+
height: Math.round(Math.max(minSize, bottom - top))
|
|
6778
|
+
};
|
|
6779
|
+
}
|
|
6780
|
+
function getAnnotationBounds(ann) {
|
|
6781
|
+
switch (ann.type) {
|
|
6782
|
+
case "rect":
|
|
6783
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
6784
|
+
case "circle":
|
|
6785
|
+
return {
|
|
6786
|
+
x: ann.x - ann.radiusX,
|
|
6787
|
+
y: ann.y - ann.radiusY,
|
|
6788
|
+
width: ann.radiusX * 2,
|
|
6789
|
+
height: ann.radiusY * 2
|
|
6790
|
+
};
|
|
6791
|
+
case "arrow":
|
|
6792
|
+
case "line": {
|
|
6793
|
+
const minX = Math.min(ann.x, ann.endX);
|
|
6794
|
+
const minY = Math.min(ann.y, ann.endY);
|
|
6795
|
+
return {
|
|
6796
|
+
x: minX,
|
|
6797
|
+
y: minY,
|
|
6798
|
+
width: Math.max(Math.abs(ann.endX - ann.x), 10),
|
|
6799
|
+
height: Math.max(Math.abs(ann.endY - ann.y), 10)
|
|
6800
|
+
};
|
|
6801
|
+
}
|
|
6802
|
+
case "pen": {
|
|
6803
|
+
if (!ann.points.length) return { x: ann.x, y: ann.y, width: 0, height: 0 };
|
|
6804
|
+
let minX = ann.points[0].x;
|
|
6805
|
+
let maxX = ann.points[0].x;
|
|
6806
|
+
let minY = ann.points[0].y;
|
|
6807
|
+
let maxY = ann.points[0].y;
|
|
6808
|
+
for (const pt of ann.points) {
|
|
6809
|
+
if (pt.x < minX) minX = pt.x;
|
|
6810
|
+
if (pt.x > maxX) maxX = pt.x;
|
|
6811
|
+
if (pt.y < minY) minY = pt.y;
|
|
6812
|
+
if (pt.y > maxY) maxY = pt.y;
|
|
6813
|
+
}
|
|
6814
|
+
return { x: minX, y: minY, width: maxX - minX, height: maxY - minY };
|
|
6815
|
+
}
|
|
6816
|
+
case "text":
|
|
6817
|
+
return {
|
|
6818
|
+
x: ann.x,
|
|
6819
|
+
y: ann.y,
|
|
6820
|
+
width: ann.text.length * ann.fontSize * 0.6,
|
|
6821
|
+
height: ann.fontSize * 1.2
|
|
6822
|
+
};
|
|
6823
|
+
case "image":
|
|
6824
|
+
return { x: ann.x, y: ann.y, width: ann.width, height: ann.height };
|
|
6825
|
+
}
|
|
6826
|
+
}
|
|
6827
|
+
function hitTestAnnotation(imgPt, ann, padding = 10) {
|
|
6828
|
+
const b = getAnnotationBounds(ann);
|
|
6829
|
+
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;
|
|
6830
|
+
}
|
|
5451
6831
|
export {
|
|
5452
6832
|
ColorHighlighter,
|
|
6833
|
+
DEFAULT_ADJUSTMENTS,
|
|
6834
|
+
DEFAULT_DEPTH_MASK,
|
|
5453
6835
|
DEFAULT_NODE_HEIGHT,
|
|
5454
6836
|
DEFAULT_NODE_WIDTH,
|
|
5455
6837
|
DEFAULT_PLACEMENT_GAP,
|
|
6838
|
+
DEFAULT_TRANSFORM,
|
|
5456
6839
|
DataSourceBinder,
|
|
5457
6840
|
DependencyGraph,
|
|
5458
6841
|
DirtyTracker,
|
|
@@ -5465,7 +6848,9 @@ export {
|
|
|
5465
6848
|
GradientText,
|
|
5466
6849
|
HistoryManager,
|
|
5467
6850
|
INSTRUCTION_REGEX,
|
|
5468
|
-
|
|
6851
|
+
Image3 as Image,
|
|
6852
|
+
ImageEditorController,
|
|
6853
|
+
ImageEditorRenderer,
|
|
5469
6854
|
LoadingNode,
|
|
5470
6855
|
MARKDOWN_MENTION_SPLIT_REGEX,
|
|
5471
6856
|
MCP_TOOL_EDIT_CANVAS,
|
|
@@ -5483,11 +6868,15 @@ export {
|
|
|
5483
6868
|
TEMPLATE_REGEX,
|
|
5484
6869
|
TemplateSuggestions,
|
|
5485
6870
|
VARIABLE_REGEX,
|
|
6871
|
+
applyAdjustments,
|
|
6872
|
+
applyDepthMask,
|
|
5486
6873
|
blendLayout,
|
|
5487
6874
|
boundsOf,
|
|
6875
|
+
calculateCropDrag,
|
|
5488
6876
|
calculateDagreReflowDisplacement,
|
|
5489
6877
|
calculateDirectionalDisplacement,
|
|
5490
6878
|
canvasHeight,
|
|
6879
|
+
canvasToImagePoint,
|
|
5491
6880
|
centerNodes,
|
|
5492
6881
|
centre,
|
|
5493
6882
|
colNameToIndex,
|
|
@@ -5514,14 +6903,21 @@ export {
|
|
|
5514
6903
|
extractVariableNames,
|
|
5515
6904
|
findHexColors,
|
|
5516
6905
|
flowSide,
|
|
6906
|
+
getAnnotationBounds,
|
|
5517
6907
|
getCenteredNodePosition,
|
|
6908
|
+
getCropCursor,
|
|
5518
6909
|
getEditorMarkdown,
|
|
5519
6910
|
getEditorText,
|
|
5520
6911
|
getLatestVersion,
|
|
5521
6912
|
getLayoutedNodes,
|
|
6913
|
+
getViewportMetrics,
|
|
6914
|
+
globalImageCache,
|
|
5522
6915
|
handleImageInsertion,
|
|
6916
|
+
hitTestAnnotation,
|
|
6917
|
+
hitTestCrop,
|
|
5523
6918
|
htmlToPlainText,
|
|
5524
6919
|
imagePastePluginKey,
|
|
6920
|
+
imageToCanvasPoint,
|
|
5525
6921
|
indexToColName,
|
|
5526
6922
|
insertImageNode,
|
|
5527
6923
|
instructionRegex,
|
|
@@ -5548,6 +6944,7 @@ export {
|
|
|
5548
6944
|
preprocessMarkdownMentions,
|
|
5549
6945
|
realClock,
|
|
5550
6946
|
remapPastedNodes,
|
|
6947
|
+
removeAlpha,
|
|
5551
6948
|
removeImageNode,
|
|
5552
6949
|
repositionNodesTo,
|
|
5553
6950
|
resolveTemplates,
|