@overtone-art/canvas-editor-core 0.2.6 → 0.2.8
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/LICENSE +21 -0
- package/dist/chunk-ORZZ6MGQ.mjs +228 -0
- package/dist/chunk-ORZZ6MGQ.mjs.map +1 -0
- package/dist/index.d.mts +297 -225
- package/dist/index.d.ts +297 -225
- package/dist/index.global.js +506 -0
- package/dist/index.global.js.map +1 -0
- package/dist/index.js +1719 -99
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1501 -94
- package/dist/index.mjs.map +1 -1
- package/dist/node.d.mts +86 -0
- package/dist/node.d.ts +86 -0
- package/dist/node.js +814 -0
- package/dist/node.js.map +1 -0
- package/dist/node.mjs +675 -0
- package/dist/node.mjs.map +1 -0
- package/dist/types-D60CfxL9.d.mts +461 -0
- package/dist/types-D60CfxL9.d.ts +461 -0
- package/package.json +49 -2
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Ilia Dzhiubanskii
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,228 @@
|
|
|
1
|
+
// src/displacement.ts
|
|
2
|
+
var CHANNEL_INDEX = {
|
|
3
|
+
red: 0,
|
|
4
|
+
green: 1,
|
|
5
|
+
blue: 2,
|
|
6
|
+
alpha: 3
|
|
7
|
+
};
|
|
8
|
+
function finiteScale(value, fallback, label) {
|
|
9
|
+
const resolved = value ?? fallback;
|
|
10
|
+
if (!Number.isFinite(resolved)) throw new Error(`${label} must be finite`);
|
|
11
|
+
return resolved;
|
|
12
|
+
}
|
|
13
|
+
function sample(source, width, height, x, y, channel) {
|
|
14
|
+
const clampedX = Math.max(0, Math.min(width - 1, x));
|
|
15
|
+
const clampedY = Math.max(0, Math.min(height - 1, y));
|
|
16
|
+
const x0 = Math.floor(clampedX);
|
|
17
|
+
const y0 = Math.floor(clampedY);
|
|
18
|
+
const x1 = Math.min(width - 1, x0 + 1);
|
|
19
|
+
const y1 = Math.min(height - 1, y0 + 1);
|
|
20
|
+
const tx = clampedX - x0;
|
|
21
|
+
const ty = clampedY - y0;
|
|
22
|
+
const top = source[(y0 * width + x0) * 4 + channel] * (1 - tx) + source[(y0 * width + x1) * 4 + channel] * tx;
|
|
23
|
+
const bottom = source[(y1 * width + x0) * 4 + channel] * (1 - tx) + source[(y1 * width + x1) * 4 + channel] * tx;
|
|
24
|
+
return top * (1 - ty) + bottom * ty;
|
|
25
|
+
}
|
|
26
|
+
function displaceRgba(source, map, width, height, options) {
|
|
27
|
+
if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) {
|
|
28
|
+
throw new Error("Displacement dimensions must be positive integers");
|
|
29
|
+
}
|
|
30
|
+
const expectedLength = width * height * 4;
|
|
31
|
+
if (source.length !== expectedLength || map.length !== expectedLength) {
|
|
32
|
+
throw new Error("Displacement source and map must match the requested dimensions");
|
|
33
|
+
}
|
|
34
|
+
const scaleX = finiteScale(options.scaleX, 10, "Displacement scaleX");
|
|
35
|
+
const scaleY = finiteScale(options.scaleY, 10, "Displacement scaleY");
|
|
36
|
+
const channelX = CHANNEL_INDEX[options.channelX ?? "red"];
|
|
37
|
+
const channelY = CHANNEL_INDEX[options.channelY ?? "green"];
|
|
38
|
+
const output = new Uint8ClampedArray(expectedLength);
|
|
39
|
+
for (let y = 0; y < height; y += 1) {
|
|
40
|
+
for (let x = 0; x < width; x += 1) {
|
|
41
|
+
const offset = (y * width + x) * 4;
|
|
42
|
+
const sourceX = x + (map[offset + channelX] - 128) / 127 * scaleX;
|
|
43
|
+
const sourceY = y + (map[offset + channelY] - 128) / 127 * scaleY;
|
|
44
|
+
for (let channel = 0; channel < 4; channel += 1) {
|
|
45
|
+
output[offset + channel] = Math.round(
|
|
46
|
+
sample(source, width, height, sourceX, sourceY, channel)
|
|
47
|
+
);
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
return output;
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
// src/export.ts
|
|
55
|
+
import { StaticCanvas } from "fabric";
|
|
56
|
+
function computePrintAreaClip(area, scaleX, scaleY, targetWidth, targetHeight) {
|
|
57
|
+
const left = Math.max(0, Math.min(targetWidth, area.left * scaleX));
|
|
58
|
+
const top = Math.max(0, Math.min(targetHeight, area.top * scaleY));
|
|
59
|
+
const right = Math.max(left, Math.min(targetWidth, (area.left + area.width) * scaleX));
|
|
60
|
+
const bottom = Math.max(top, Math.min(targetHeight, (area.top + area.height) * scaleY));
|
|
61
|
+
return { left, top, width: right - left, height: bottom - top };
|
|
62
|
+
}
|
|
63
|
+
function computeCoverPlacement(sourceWidth, sourceHeight, targetWidth, targetHeight) {
|
|
64
|
+
if (sourceWidth <= 0 || sourceHeight <= 0 || targetWidth <= 0 || targetHeight <= 0) {
|
|
65
|
+
throw new Error("Cover dimensions must be positive");
|
|
66
|
+
}
|
|
67
|
+
const scale = Math.max(targetWidth / sourceWidth, targetHeight / sourceHeight);
|
|
68
|
+
const width = sourceWidth * scale;
|
|
69
|
+
const height = sourceHeight * scale;
|
|
70
|
+
return {
|
|
71
|
+
left: (targetWidth - width) / 2,
|
|
72
|
+
top: (targetHeight - height) / 2,
|
|
73
|
+
width,
|
|
74
|
+
height
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function canvasElementToBlob(output, format, quality) {
|
|
78
|
+
const mime = format === "jpeg" ? "image/jpeg" : `image/${format}`;
|
|
79
|
+
return new Promise((resolve, reject) => {
|
|
80
|
+
output.toBlob(
|
|
81
|
+
(blob) => blob ? resolve(blob) : reject(new Error(`Failed to export ${format}`)),
|
|
82
|
+
mime,
|
|
83
|
+
quality
|
|
84
|
+
);
|
|
85
|
+
});
|
|
86
|
+
}
|
|
87
|
+
async function exportPNG(canvas, options = {}) {
|
|
88
|
+
const { multiplier = 1, format = "png", quality = 1 } = options;
|
|
89
|
+
const output = canvas.toCanvasElement(multiplier);
|
|
90
|
+
return canvasElementToBlob(output, format, quality);
|
|
91
|
+
}
|
|
92
|
+
async function exportIsolatedPNG(source, objects, options = {}) {
|
|
93
|
+
const element = source.lowerCanvasEl.ownerDocument.createElement("canvas");
|
|
94
|
+
const canvas = new StaticCanvas(element, {
|
|
95
|
+
width: options.width ?? source.getWidth(),
|
|
96
|
+
height: options.height ?? source.getHeight(),
|
|
97
|
+
backgroundColor: options.backgroundColor || void 0
|
|
98
|
+
});
|
|
99
|
+
try {
|
|
100
|
+
const clones = options.cloneObjects === false ? objects : await Promise.all(objects.map((object) => object.clone()));
|
|
101
|
+
if (clones.length) canvas.add(...clones);
|
|
102
|
+
if (options.backgroundImage) canvas.backgroundImage = await options.backgroundImage.clone();
|
|
103
|
+
canvas.requestRenderAll();
|
|
104
|
+
return await exportPNG(canvas, options);
|
|
105
|
+
} finally {
|
|
106
|
+
canvas.dispose();
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
async function exportMockup(canvas, mockup, options = {}) {
|
|
110
|
+
const { multiplier = 1, format = "png", quality = 1 } = options;
|
|
111
|
+
const design = canvas.toCanvasElement(multiplier);
|
|
112
|
+
const output = design.ownerDocument.createElement("canvas");
|
|
113
|
+
output.width = design.width;
|
|
114
|
+
output.height = design.height;
|
|
115
|
+
const context = output.getContext("2d");
|
|
116
|
+
if (!context) throw new Error("2D canvas context is unavailable");
|
|
117
|
+
const loadImage = (url) => new Promise((resolve, reject) => {
|
|
118
|
+
const element = new Image();
|
|
119
|
+
element.crossOrigin = "anonymous";
|
|
120
|
+
element.onload = () => resolve(element);
|
|
121
|
+
element.onerror = () => reject(new Error(`Failed to load mockup image: ${url}`));
|
|
122
|
+
element.src = url;
|
|
123
|
+
});
|
|
124
|
+
const drawCover = (image, targetContext = context) => {
|
|
125
|
+
const placement = computeCoverPlacement(
|
|
126
|
+
image.naturalWidth || image.width,
|
|
127
|
+
image.naturalHeight || image.height,
|
|
128
|
+
output.width,
|
|
129
|
+
output.height
|
|
130
|
+
);
|
|
131
|
+
targetContext.drawImage(
|
|
132
|
+
image,
|
|
133
|
+
placement.left,
|
|
134
|
+
placement.top,
|
|
135
|
+
placement.width,
|
|
136
|
+
placement.height
|
|
137
|
+
);
|
|
138
|
+
};
|
|
139
|
+
const scratch = [design];
|
|
140
|
+
try {
|
|
141
|
+
drawCover(await loadImage(mockup.image));
|
|
142
|
+
let compositedDesign = design;
|
|
143
|
+
if (mockup.displacement) {
|
|
144
|
+
const sourceContext = design.getContext("2d");
|
|
145
|
+
if (!sourceContext) throw new Error("2D design context is unavailable");
|
|
146
|
+
const mapCanvas = design.ownerDocument.createElement("canvas");
|
|
147
|
+
scratch.push(mapCanvas);
|
|
148
|
+
mapCanvas.width = design.width;
|
|
149
|
+
mapCanvas.height = design.height;
|
|
150
|
+
const mapContext = mapCanvas.getContext("2d");
|
|
151
|
+
if (!mapContext) throw new Error("2D displacement-map context is unavailable");
|
|
152
|
+
drawCover(await loadImage(mockup.displacement.image), mapContext);
|
|
153
|
+
const warped = design.ownerDocument.createElement("canvas");
|
|
154
|
+
scratch.push(warped);
|
|
155
|
+
warped.width = design.width;
|
|
156
|
+
warped.height = design.height;
|
|
157
|
+
const warpedContext = warped.getContext("2d");
|
|
158
|
+
if (!warpedContext) throw new Error("2D displaced-design context is unavailable");
|
|
159
|
+
let sourcePixels;
|
|
160
|
+
let mapPixels;
|
|
161
|
+
try {
|
|
162
|
+
sourcePixels = sourceContext.getImageData(0, 0, design.width, design.height).data;
|
|
163
|
+
mapPixels = mapContext.getImageData(0, 0, design.width, design.height).data;
|
|
164
|
+
} catch (error) {
|
|
165
|
+
throw new Error("Failed to apply mockup displacement map; verify image CORS access", {
|
|
166
|
+
cause: error
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
const pixels = displaceRgba(sourcePixels, mapPixels, design.width, design.height, {
|
|
170
|
+
...mockup.displacement,
|
|
171
|
+
scaleX: (mockup.displacement.scaleX ?? 10) * multiplier,
|
|
172
|
+
scaleY: (mockup.displacement.scaleY ?? 10) * multiplier
|
|
173
|
+
});
|
|
174
|
+
const imageData = warpedContext.createImageData(design.width, design.height);
|
|
175
|
+
imageData.data.set(pixels);
|
|
176
|
+
warpedContext.putImageData(imageData, 0, 0);
|
|
177
|
+
compositedDesign = warped;
|
|
178
|
+
}
|
|
179
|
+
context.save();
|
|
180
|
+
if (mockup.printArea && mockup.clipToPrintArea !== false) {
|
|
181
|
+
const clip = computePrintAreaClip(
|
|
182
|
+
mockup.printArea,
|
|
183
|
+
output.width / canvas.getWidth(),
|
|
184
|
+
output.height / canvas.getHeight(),
|
|
185
|
+
output.width,
|
|
186
|
+
output.height
|
|
187
|
+
);
|
|
188
|
+
context.beginPath();
|
|
189
|
+
context.rect(clip.left, clip.top, clip.width, clip.height);
|
|
190
|
+
context.clip();
|
|
191
|
+
}
|
|
192
|
+
context.globalAlpha = Math.max(0, Math.min(1, mockup.designOpacity ?? 1));
|
|
193
|
+
context.globalCompositeOperation = !mockup.designBlendMode || mockup.designBlendMode === "normal" ? "source-over" : mockup.designBlendMode;
|
|
194
|
+
context.drawImage(compositedDesign, 0, 0);
|
|
195
|
+
context.restore();
|
|
196
|
+
if (mockup.overlay) {
|
|
197
|
+
context.save();
|
|
198
|
+
context.globalAlpha = Math.max(0, Math.min(1, mockup.overlay.opacity ?? 1));
|
|
199
|
+
context.globalCompositeOperation = mockup.overlay.blendMode === "normal" ? "source-over" : mockup.overlay.blendMode ?? "multiply";
|
|
200
|
+
drawCover(await loadImage(mockup.overlay.image));
|
|
201
|
+
context.restore();
|
|
202
|
+
}
|
|
203
|
+
return await canvasElementToBlob(output, format, quality);
|
|
204
|
+
} finally {
|
|
205
|
+
for (const element of scratch) {
|
|
206
|
+
element.width = 0;
|
|
207
|
+
element.height = 0;
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
function exportSVG(canvas) {
|
|
212
|
+
return canvas.toSVG();
|
|
213
|
+
}
|
|
214
|
+
function exportDataURL(canvas, format = "png", multiplier = 1) {
|
|
215
|
+
return canvas.toDataURL({ format, multiplier });
|
|
216
|
+
}
|
|
217
|
+
|
|
218
|
+
export {
|
|
219
|
+
displaceRgba,
|
|
220
|
+
computePrintAreaClip,
|
|
221
|
+
computeCoverPlacement,
|
|
222
|
+
exportPNG,
|
|
223
|
+
exportIsolatedPNG,
|
|
224
|
+
exportMockup,
|
|
225
|
+
exportSVG,
|
|
226
|
+
exportDataURL
|
|
227
|
+
};
|
|
228
|
+
//# sourceMappingURL=chunk-ORZZ6MGQ.mjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../src/displacement.ts","../src/export.ts"],"sourcesContent":["import type { MockupDisplacement, MockupDisplacementChannel } from './types';\n\nconst CHANNEL_INDEX: Record<MockupDisplacementChannel, number> = {\n red: 0,\n green: 1,\n blue: 2,\n alpha: 3,\n};\n\nfunction finiteScale(value: number | undefined, fallback: number, label: string): number {\n const resolved = value ?? fallback;\n if (!Number.isFinite(resolved)) throw new Error(`${label} must be finite`);\n return resolved;\n}\n\nfunction sample(\n source: Uint8ClampedArray,\n width: number,\n height: number,\n x: number,\n y: number,\n channel: number,\n): number {\n const clampedX = Math.max(0, Math.min(width - 1, x));\n const clampedY = Math.max(0, Math.min(height - 1, y));\n const x0 = Math.floor(clampedX);\n const y0 = Math.floor(clampedY);\n const x1 = Math.min(width - 1, x0 + 1);\n const y1 = Math.min(height - 1, y0 + 1);\n const tx = clampedX - x0;\n const ty = clampedY - y0;\n const top =\n source[(y0 * width + x0) * 4 + channel] * (1 - tx) +\n source[(y0 * width + x1) * 4 + channel] * tx;\n const bottom =\n source[(y1 * width + x0) * 4 + channel] * (1 - tx) +\n source[(y1 * width + x1) * 4 + channel] * tx;\n return top * (1 - ty) + bottom * ty;\n}\n\n/**\n * Warp RGBA pixels with an equally sized channel map. A channel value of 128\n * is neutral; 0 and 255 move by the configured negative/positive maximum.\n */\nexport function displaceRgba(\n source: Uint8ClampedArray,\n map: Uint8ClampedArray,\n width: number,\n height: number,\n options: Omit<MockupDisplacement, 'image'>,\n): Uint8ClampedArray {\n if (!Number.isInteger(width) || !Number.isInteger(height) || width <= 0 || height <= 0) {\n throw new Error('Displacement dimensions must be positive integers');\n }\n const expectedLength = width * height * 4;\n if (source.length !== expectedLength || map.length !== expectedLength) {\n throw new Error('Displacement source and map must match the requested dimensions');\n }\n\n const scaleX = finiteScale(options.scaleX, 10, 'Displacement scaleX');\n const scaleY = finiteScale(options.scaleY, 10, 'Displacement scaleY');\n const channelX = CHANNEL_INDEX[options.channelX ?? 'red'];\n const channelY = CHANNEL_INDEX[options.channelY ?? 'green'];\n const output = new Uint8ClampedArray(expectedLength);\n\n for (let y = 0; y < height; y += 1) {\n for (let x = 0; x < width; x += 1) {\n const offset = (y * width + x) * 4;\n const sourceX = x + ((map[offset + channelX] - 128) / 127) * scaleX;\n const sourceY = y + ((map[offset + channelY] - 128) / 127) * scaleY;\n for (let channel = 0; channel < 4; channel += 1) {\n output[offset + channel] = Math.round(\n sample(source, width, height, sourceX, sourceY, channel),\n );\n }\n }\n }\n return output;\n}\n","import { StaticCanvas } from 'fabric';\nimport type { Canvas, FabricObject, ImageFormat } from 'fabric';\nimport type { MockupConfig, MockupPrintArea } from './types';\nimport { displaceRgba } from './displacement';\n\nexport interface PngExportOptions {\n multiplier?: number;\n format?: ImageFormat;\n quality?: number;\n}\n\nexport interface CoverPlacement {\n left: number;\n top: number;\n width: number;\n height: number;\n}\n\nexport function computePrintAreaClip(\n area: MockupPrintArea,\n scaleX: number,\n scaleY: number,\n targetWidth: number,\n targetHeight: number,\n): MockupPrintArea {\n const left = Math.max(0, Math.min(targetWidth, area.left * scaleX));\n const top = Math.max(0, Math.min(targetHeight, area.top * scaleY));\n const right = Math.max(left, Math.min(targetWidth, (area.left + area.width) * scaleX));\n const bottom = Math.max(top, Math.min(targetHeight, (area.top + area.height) * scaleY));\n return { left, top, width: right - left, height: bottom - top };\n}\n\n/** Object-fit: cover geometry, exported for deterministic preview/composite tests. */\nexport function computeCoverPlacement(\n sourceWidth: number,\n sourceHeight: number,\n targetWidth: number,\n targetHeight: number,\n): CoverPlacement {\n if (sourceWidth <= 0 || sourceHeight <= 0 || targetWidth <= 0 || targetHeight <= 0) {\n throw new Error('Cover dimensions must be positive');\n }\n const scale = Math.max(targetWidth / sourceWidth, targetHeight / sourceHeight);\n const width = sourceWidth * scale;\n const height = sourceHeight * scale;\n return {\n left: (targetWidth - width) / 2,\n top: (targetHeight - height) / 2,\n width,\n height,\n };\n}\n\nfunction canvasElementToBlob(\n output: HTMLCanvasElement,\n format: ImageFormat,\n quality: number,\n): Promise<Blob> {\n const mime = format === 'jpeg' ? 'image/jpeg' : `image/${format}`;\n return new Promise<Blob>((resolve, reject) => {\n output.toBlob(\n (blob) => (blob ? resolve(blob) : reject(new Error(`Failed to export ${format}`))),\n mime,\n quality,\n );\n });\n}\n\nexport async function exportPNG(canvas: Canvas, options: PngExportOptions = {}): Promise<Blob> {\n const { multiplier = 1, format = 'png' as ImageFormat, quality = 1 } = options;\n const output = canvas.toCanvasElement(multiplier);\n return canvasElementToBlob(output, format, quality);\n}\n\n/** Render cloned objects without mutating the live editor canvas. */\nexport async function exportIsolatedPNG(\n source: Canvas,\n objects: FabricObject[],\n options: PngExportOptions & {\n width?: number;\n height?: number;\n backgroundColor?: string;\n backgroundImage?: FabricObject | null;\n cloneObjects?: boolean;\n } = {},\n): Promise<Blob> {\n const element = source.lowerCanvasEl.ownerDocument.createElement('canvas');\n const canvas = new StaticCanvas(element, {\n width: options.width ?? source.getWidth(),\n height: options.height ?? source.getHeight(),\n backgroundColor: options.backgroundColor || undefined,\n });\n try {\n const clones =\n options.cloneObjects === false\n ? objects\n : await Promise.all(objects.map((object) => object.clone()));\n if (clones.length) canvas.add(...clones);\n if (options.backgroundImage) canvas.backgroundImage = await options.backgroundImage.clone();\n canvas.requestRenderAll();\n // Awaited, not returned: `finally` would otherwise dispose the canvas while\n // the export is still reading from it.\n return await exportPNG(canvas as unknown as Canvas, options);\n } finally {\n canvas.dispose();\n }\n}\n\n/** Rasterize the browser mockup preview together with the transparent design. */\nexport async function exportMockup(\n canvas: Canvas,\n mockup: MockupConfig,\n options: PngExportOptions = {},\n): Promise<Blob> {\n const { multiplier = 1, format = 'png' as ImageFormat, quality = 1 } = options;\n const design = canvas.toCanvasElement(multiplier);\n const output = design.ownerDocument.createElement('canvas');\n output.width = design.width;\n output.height = design.height;\n const context = output.getContext('2d');\n if (!context) throw new Error('2D canvas context is unavailable');\n\n const loadImage = (url: string) =>\n new Promise<HTMLImageElement>((resolve, reject) => {\n const element = new Image();\n element.crossOrigin = 'anonymous';\n element.onload = () => resolve(element);\n element.onerror = () => reject(new Error(`Failed to load mockup image: ${url}`));\n element.src = url;\n });\n const drawCover = (\n image: HTMLImageElement,\n targetContext: CanvasRenderingContext2D = context,\n ) => {\n const placement = computeCoverPlacement(\n image.naturalWidth || image.width,\n image.naturalHeight || image.height,\n output.width,\n output.height,\n );\n targetContext.drawImage(\n image,\n placement.left,\n placement.top,\n placement.width,\n placement.height,\n );\n };\n\n // Full-size scratch buffers, released explicitly in the finally below: a\n // detached canvas element can hold its backing store well past its last\n // reference, and a 4K mockup allocates three of them per export.\n const scratch: HTMLCanvasElement[] = [design];\n try {\n drawCover(await loadImage(mockup.image));\n let compositedDesign: CanvasImageSource = design;\n if (mockup.displacement) {\n const sourceContext = design.getContext('2d');\n if (!sourceContext) throw new Error('2D design context is unavailable');\n const mapCanvas = design.ownerDocument.createElement('canvas');\n scratch.push(mapCanvas);\n mapCanvas.width = design.width;\n mapCanvas.height = design.height;\n const mapContext = mapCanvas.getContext('2d');\n if (!mapContext) throw new Error('2D displacement-map context is unavailable');\n drawCover(await loadImage(mockup.displacement.image), mapContext);\n\n const warped = design.ownerDocument.createElement('canvas');\n scratch.push(warped);\n warped.width = design.width;\n warped.height = design.height;\n const warpedContext = warped.getContext('2d');\n if (!warpedContext) throw new Error('2D displaced-design context is unavailable');\n let sourcePixels: Uint8ClampedArray;\n let mapPixels: Uint8ClampedArray;\n try {\n sourcePixels = sourceContext.getImageData(0, 0, design.width, design.height).data;\n mapPixels = mapContext.getImageData(0, 0, design.width, design.height).data;\n } catch (error) {\n throw new Error('Failed to apply mockup displacement map; verify image CORS access', {\n cause: error,\n });\n }\n const pixels = displaceRgba(sourcePixels, mapPixels, design.width, design.height, {\n ...mockup.displacement,\n scaleX: (mockup.displacement.scaleX ?? 10) * multiplier,\n scaleY: (mockup.displacement.scaleY ?? 10) * multiplier,\n });\n const imageData = warpedContext.createImageData(design.width, design.height);\n imageData.data.set(pixels);\n warpedContext.putImageData(imageData, 0, 0);\n compositedDesign = warped;\n }\n context.save();\n if (mockup.printArea && mockup.clipToPrintArea !== false) {\n const clip = computePrintAreaClip(\n mockup.printArea,\n output.width / canvas.getWidth(),\n output.height / canvas.getHeight(),\n output.width,\n output.height,\n );\n context.beginPath();\n context.rect(clip.left, clip.top, clip.width, clip.height);\n context.clip();\n }\n context.globalAlpha = Math.max(0, Math.min(1, mockup.designOpacity ?? 1));\n context.globalCompositeOperation =\n !mockup.designBlendMode || mockup.designBlendMode === 'normal'\n ? 'source-over'\n : mockup.designBlendMode;\n context.drawImage(compositedDesign, 0, 0);\n context.restore();\n\n if (mockup.overlay) {\n context.save();\n context.globalAlpha = Math.max(0, Math.min(1, mockup.overlay.opacity ?? 1));\n context.globalCompositeOperation =\n mockup.overlay.blendMode === 'normal'\n ? 'source-over'\n : (mockup.overlay.blendMode ?? 'multiply');\n drawCover(await loadImage(mockup.overlay.image));\n context.restore();\n }\n return await canvasElementToBlob(output, format, quality);\n } finally {\n for (const element of scratch) {\n element.width = 0;\n element.height = 0;\n }\n }\n}\n\nexport function exportSVG(canvas: Canvas): string {\n return canvas.toSVG();\n}\n\nexport function exportDataURL(canvas: Canvas, format: ImageFormat = 'png', multiplier = 1): string {\n return canvas.toDataURL({ format, multiplier });\n}\n"],"mappings":";AAEA,IAAM,gBAA2D;AAAA,EAC/D,KAAK;AAAA,EACL,OAAO;AAAA,EACP,MAAM;AAAA,EACN,OAAO;AACT;AAEA,SAAS,YAAY,OAA2B,UAAkB,OAAuB;AACvF,QAAM,WAAW,SAAS;AAC1B,MAAI,CAAC,OAAO,SAAS,QAAQ,EAAG,OAAM,IAAI,MAAM,GAAG,KAAK,iBAAiB;AACzE,SAAO;AACT;AAEA,SAAS,OACP,QACA,OACA,QACA,GACA,GACA,SACQ;AACR,QAAM,WAAW,KAAK,IAAI,GAAG,KAAK,IAAI,QAAQ,GAAG,CAAC,CAAC;AACnD,QAAM,WAAW,KAAK,IAAI,GAAG,KAAK,IAAI,SAAS,GAAG,CAAC,CAAC;AACpD,QAAM,KAAK,KAAK,MAAM,QAAQ;AAC9B,QA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