@ggraphic/editor 0.1.0-alpha.6 → 0.1.0-alpha.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.
@@ -1,5 +1,4 @@
1
- import Konva from 'konva';
2
- import type { ImageAdjustments } from './userImageUtils.js';
1
+ import type Konva from 'konva';
3
2
  import type { Mirror, Rect } from './coverFit.js';
4
3
  /**
5
4
  * Konva-Name des abgedunkelten ganzen Fotos im Zuschneide-Modus (Hintergrund
@@ -53,11 +52,3 @@ export declare function visibleContainerRect(container: HTMLElement): Rect | nul
53
52
  * Ohne sichtbares Restbild wird `canvas` ausgeblendet.
54
53
  */
55
54
  export declare function paintCropGhosts(stage: Konva.Stage, canvas: HTMLCanvasElement, visible?: Rect | null): void;
56
- /**
57
- * Die Bildquelle des Restbilds mit dem Look des Fotos („Bearbeiten"-Filter,
58
- * Einfärbung), einmal auf eine verkleinerte Kopie gerechnet statt per Cache
59
- * auf dem ganzen, oft vielfach gezoomten Restbild. Spiegeln bleibt außen vor:
60
- * das Restbild spiegelt über seine Skalierung. `displayedWidth` ist die Breite
61
- * des ganzen Fotos auf der Bühne; der Weichzeichner-Radius gilt dort.
62
- */
63
- export declare function cropGhostSource(image: HTMLImageElement, adjustments: ImageAdjustments | null, displayedWidth: number, color?: string | null): CanvasImageSource;
@@ -1,6 +1,5 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-only
2
- import Konva from 'konva';
3
- import { getActiveImageFilters, imageFilterAttrs } from './imageFilters.js';
2
+ import { LOOK_IMAGE_ATTR } from './lookBitmap.js';
4
3
  /**
5
4
  * Konva-Name des abgedunkelten ganzen Fotos im Zuschneide-Modus (Hintergrund
6
5
  * und eigene Bilder). Die Bühne endet an der Seite; was darüber hinausragt,
@@ -159,58 +158,11 @@ export function paintCropGhosts(stage, canvas, visible = null) {
159
158
  ctx.setTransform(...toOverlay);
160
159
  ctx.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
161
160
  ctx.globalAlpha = node.getAbsoluteOpacity();
162
- ctx.drawImage(node.image(), 0, 0, width, height);
161
+ // The look bitmap, when the photo has one (`useLookBitmap`); the mirror is in the matrix.
162
+ const source = node.getAttr(LOOK_IMAGE_ATTR) ?? node.image();
163
+ ctx.drawImage(source, 0, 0, width, height);
163
164
  }
164
165
  ctx.setTransform(...toOverlay);
165
166
  ctx.globalAlpha = 1;
166
167
  ctx.clearRect(0, 0, stage.width(), stage.height());
167
168
  }
168
- // Längste Kante der gefilterten Restbild-Vorlage; abgedunkelt braucht es keine volle Auflösung.
169
- const GHOST_SOURCE_SIDE = 1024;
170
- /**
171
- * Die Bildquelle des Restbilds mit dem Look des Fotos („Bearbeiten"-Filter,
172
- * Einfärbung), einmal auf eine verkleinerte Kopie gerechnet statt per Cache
173
- * auf dem ganzen, oft vielfach gezoomten Restbild. Spiegeln bleibt außen vor:
174
- * das Restbild spiegelt über seine Skalierung. `displayedWidth` ist die Breite
175
- * des ganzen Fotos auf der Bühne; der Weichzeichner-Radius gilt dort.
176
- */
177
- export function cropGhostSource(image, adjustments, displayedWidth, color = null) {
178
- const look = adjustments ? { ...adjustments, flipX: false, flipY: false } : null;
179
- const filters = [
180
- ...(color ? [Konva.Filters.RGB] : []),
181
- ...(look ? getActiveImageFilters(look) : []),
182
- ];
183
- const natural = { width: image.naturalWidth, height: image.naturalHeight };
184
- if (filters.length === 0 || !natural.width || !natural.height)
185
- return image;
186
- const scale = Math.min(1, GHOST_SOURCE_SIDE / Math.max(natural.width, natural.height));
187
- const width = Math.max(1, Math.round(natural.width * scale));
188
- const height = Math.max(1, Math.round(natural.height * scale));
189
- const canvas = document.createElement('canvas');
190
- canvas.width = width;
191
- canvas.height = height;
192
- const ctx = canvas.getContext('2d');
193
- if (!ctx)
194
- return image;
195
- ctx.drawImage(image, 0, 0, width, height);
196
- // Die Filter lesen ihre Werte vom Knoten (`this`), wie in Konvas Cache.
197
- const attrs = new Konva.Image({
198
- image,
199
- ...(look ? imageFilterAttrs(look) : {}),
200
- blurRadius: ((look?.blur ?? 0) * width) / Math.max(1, displayedWidth),
201
- ...(color
202
- ? {
203
- red: parseInt(color.slice(1, 3), 16),
204
- green: parseInt(color.slice(3, 5), 16),
205
- blue: parseInt(color.slice(5, 7), 16),
206
- }
207
- : {}),
208
- });
209
- const data = ctx.getImageData(0, 0, width, height);
210
- for (const filter of filters)
211
- if (typeof filter === 'function')
212
- filter.call(attrs, data);
213
- ctx.putImageData(data, 0, 0);
214
- attrs.destroy();
215
- return canvas;
216
- }
@@ -1,16 +1,11 @@
1
- import Konva from 'konva';
2
1
  /**
3
2
  * On a canvas drawn only to be captured once (a gallery or page preview): the output pixels per
4
3
  * page pixel of that capture. Null in the editor.
5
4
  *
6
- * Konva runs an image's filters over its cache, and `cache()` without a ratio sizes that cache at
7
- * page size times the device ratio: a portrait photo is filtered over 2160 × 2700 pixels for a
8
- * preview drawn 400 px wide. Measured at 4x CPU throttling, 01.11.2026: a duotone photo held one
9
- * frame for 308 ms. A preview caches at what it draws or what it captures, whichever is larger,
10
- * so a full-size capture (the render gate) stays sharp. The editor keeps the full cache, so
11
- * zooming in stays sharp.
5
+ * A photo's look bitmap (`lookBitmap`) is sized from it: the editor filters at the device ratio,
6
+ * a preview at what it draws or captures, whichever is larger, so a full-size capture (the
7
+ * render gate) stays sharp. Filtering a preview at page size held one frame for 308 ms (a
8
+ * duotone photo at 4x CPU throttling, 01.11.2026).
12
9
  */
13
10
  export declare const PreviewRenderContext: import("react").Context<number | null>;
14
11
  export declare function usePreviewCaptureRatio(): number | null;
15
- /** Caches a filtered node; on a preview at the resolution it is drawn or captured at. */
16
- export declare function cacheFiltered(node: Konva.Node, captureRatio: number | null): void;
@@ -1,31 +1,15 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-only
2
- import Konva from 'konva';
3
2
  import { createContext, useContext } from 'react';
4
3
  /**
5
4
  * On a canvas drawn only to be captured once (a gallery or page preview): the output pixels per
6
5
  * page pixel of that capture. Null in the editor.
7
6
  *
8
- * Konva runs an image's filters over its cache, and `cache()` without a ratio sizes that cache at
9
- * page size times the device ratio: a portrait photo is filtered over 2160 × 2700 pixels for a
10
- * preview drawn 400 px wide. Measured at 4x CPU throttling, 01.11.2026: a duotone photo held one
11
- * frame for 308 ms. A preview caches at what it draws or what it captures, whichever is larger,
12
- * so a full-size capture (the render gate) stays sharp. The editor keeps the full cache, so
13
- * zooming in stays sharp.
7
+ * A photo's look bitmap (`lookBitmap`) is sized from it: the editor filters at the device ratio,
8
+ * a preview at what it draws or captures, whichever is larger, so a full-size capture (the
9
+ * render gate) stays sharp. Filtering a preview at page size held one frame for 308 ms (a
10
+ * duotone photo at 4x CPU throttling, 01.11.2026).
14
11
  */
15
12
  export const PreviewRenderContext = createContext(null);
16
13
  export function usePreviewCaptureRatio() {
17
14
  return useContext(PreviewRenderContext);
18
15
  }
19
- /** Caches a filtered node; on a preview at the resolution it is drawn or captured at. */
20
- export function cacheFiltered(node, captureRatio) {
21
- if (captureRatio === null) {
22
- node.cache();
23
- return;
24
- }
25
- const absolute = node.getAbsoluteScale().x;
26
- // The stage carries the display scale; what remains is the node's scale within the page.
27
- const withinPage = absolute / (node.getStage()?.scaleX() || 1);
28
- node.cache({
29
- pixelRatio: Math.max(absolute * Konva.pixelRatio, withinPage * captureRatio),
30
- });
31
- }
@@ -0,0 +1,75 @@
1
+ /**
2
+ * The look of a photo ("Bearbeiten" adjustments, the background tint) as a bitmap made once per
3
+ * look, not as Konva filters run while drawing.
4
+ *
5
+ * A Konva filter runs over the node's cache, and the cache holds the drawn crop: every pan, zoom
6
+ * or crop-frame drag cleared it and ran every filter again, one full pass per pointer move (#197).
7
+ * Here the colour filters run once over the whole photo (`lookBitmap`), and the node draws that
8
+ * bitmap with its ordinary crop (`lookSceneFunc`); moving the crop costs a `drawImage`.
9
+ *
10
+ * Two parts of the look depend on the drawn frame, not the photo, and stay with the drawing:
11
+ * the flip mirrors the drawn frame (as the filter mirrored the cache, which the crop geometry
12
+ * relies on) and the vignette darkens towards the frame's edges. The node keeps the photo as its
13
+ * `image` and its crop in photo pixels, so geometry, hit area and print DPI read what they read.
14
+ */
15
+ import Konva from 'konva';
16
+ import type { ImageAdjustments } from './userImageUtils.js';
17
+ /** Node attr holding the look bitmap the node draws instead of its `image`. */
18
+ export declare const LOOK_IMAGE_ATTR = "lookImage";
19
+ /**
20
+ * Largest look bitmap, in pixels: iOS WebKit silently drops a canvas above 16 777 216 px. A photo
21
+ * larger than that at the resolution asked for is filtered smaller.
22
+ */
23
+ export declare const LOOK_MAX_AREA = 16777216;
24
+ /** Size of the quick bitmap shown while a look changes fast (a slider drag). */
25
+ export declare const LOOK_QUICK_AREA = 1500000;
26
+ /** What the colour filters read, as a key; null when the look has no colour filter. */
27
+ export declare function lookKey(adjustments: ImageAdjustments | null, color: string | null): string | null;
28
+ /** Whether the node must draw through `lookSceneFunc`: a colour filter, a flip or a vignette. */
29
+ export declare function drawsLook(adjustments: ImageAdjustments | null, color: string | null): boolean;
30
+ /** Node attrs `lookSceneFunc` reads: the flip and the vignette. */
31
+ export declare function lookDrawAttrs(adjustments: ImageAdjustments | null): {
32
+ flipX: boolean;
33
+ flipY: boolean;
34
+ vignette: number;
35
+ };
36
+ /**
37
+ * Device pixels per node unit the node is drawn at. The editor matches what Konva's own cache
38
+ * held (the device ratio, whatever the stage zoom); a preview draws at what it draws or captures,
39
+ * whichever is larger, so a full-size capture (the render gate) stays sharp.
40
+ */
41
+ export declare function lookPixelRatio(node: Konva.Node, captureRatio: number | null): number;
42
+ /**
43
+ * Size of the bitmap of a photo drawn `photoWidth` node units wide at `pixelRatio`: never larger
44
+ * than the photo, nor than `maxArea`.
45
+ */
46
+ export declare function lookBitmapSize(natural: {
47
+ width: number;
48
+ height: number;
49
+ }, photoWidth: number, pixelRatio: number, maxArea?: number): {
50
+ width: number;
51
+ height: number;
52
+ };
53
+ /** An already built bitmap of this look at least `width` wide, if there is one. */
54
+ export declare function builtLookBitmap(image: HTMLImageElement, key: string, width: number): HTMLCanvasElement | null;
55
+ /**
56
+ * The photo with its colour filters applied, at least `size` (an equal or larger bitmap of the
57
+ * same look is reused), or the photo itself when the look has no colour filter. The blur radius
58
+ * is in node units, as on the node: `photoWidth` is how wide the whole photo is drawn.
59
+ */
60
+ export declare function lookBitmap(image: HTMLImageElement, adjustments: ImageAdjustments | null, color: string | null, photoWidth: number, size: {
61
+ width: number;
62
+ height: number;
63
+ }): CanvasImageSource;
64
+ /**
65
+ * Darkens `ctx`'s pixels towards the edges of `width` × `height` by `strength`, the curve of the
66
+ * former vignette filter: k = 1 − strength·smoothstep(t), t = (r − 0.35) / 0.65, r the distance
67
+ * from the middle with the corners at 1. Black at alpha 1 − k drawn `source-atop` multiplies each
68
+ * pixel's colour by k and keeps its alpha, so a cut-out stays cut out.
69
+ */
70
+ export declare function paintVignette(ctx: CanvasRenderingContext2D, width: number, height: number, strength: number): void;
71
+ /**
72
+ * Scene function of a photo with a look, and of a straightened one: the look bitmap with the
73
+ * node's crop, the flip, and the vignette over the drawn frame.
74
+ */
75
+ export declare function lookSceneFunc(context: Konva.Context, shape: Konva.Shape): void;
@@ -0,0 +1,267 @@
1
+ // SPDX-License-Identifier: AGPL-3.0-only
2
+ /**
3
+ * The look of a photo ("Bearbeiten" adjustments, the background tint) as a bitmap made once per
4
+ * look, not as Konva filters run while drawing.
5
+ *
6
+ * A Konva filter runs over the node's cache, and the cache holds the drawn crop: every pan, zoom
7
+ * or crop-frame drag cleared it and ran every filter again, one full pass per pointer move (#197).
8
+ * Here the colour filters run once over the whole photo (`lookBitmap`), and the node draws that
9
+ * bitmap with its ordinary crop (`lookSceneFunc`); moving the crop costs a `drawImage`.
10
+ *
11
+ * Two parts of the look depend on the drawn frame, not the photo, and stay with the drawing:
12
+ * the flip mirrors the drawn frame (as the filter mirrored the cache, which the crop geometry
13
+ * relies on) and the vignette darkens towards the frame's edges. The node keeps the photo as its
14
+ * `image` and its crop in photo pixels, so geometry, hit area and print DPI read what they read.
15
+ */
16
+ import Konva from 'konva';
17
+ import { cornersOf, hasCorners, traceRoundedRect } from './cornerRadius.js';
18
+ import { getActiveImageFilters, imageFilterAttrs } from './imageFilters.js';
19
+ import { STRAIGHT_DRAW_ATTR } from './straighten.js';
20
+ /** Node attr holding the look bitmap the node draws instead of its `image`. */
21
+ export const LOOK_IMAGE_ATTR = 'lookImage';
22
+ /**
23
+ * Largest look bitmap, in pixels: iOS WebKit silently drops a canvas above 16 777 216 px. A photo
24
+ * larger than that at the resolution asked for is filtered smaller.
25
+ */
26
+ export const LOOK_MAX_AREA = 16_777_216;
27
+ /** Size of the quick bitmap shown while a look changes fast (a slider drag). */
28
+ export const LOOK_QUICK_AREA = 1_500_000;
29
+ /** The look without the frame-bound flip and vignette. */
30
+ function colourLook(adjustments) {
31
+ return adjustments ? { ...adjustments, flipX: false, flipY: false, vignette: 0 } : null;
32
+ }
33
+ /** The look's colour filters: everything but the flip and the vignette. */
34
+ function colourFilters(adjustments, color) {
35
+ const look = colourLook(adjustments);
36
+ return [...(color ? [Konva.Filters.RGB] : []), ...(look ? getActiveImageFilters(look) : [])];
37
+ }
38
+ /** What the colour filters read, as a key; null when the look has no colour filter. */
39
+ export function lookKey(adjustments, color) {
40
+ if (colourFilters(adjustments, color).length === 0)
41
+ return null;
42
+ const look = colourLook(adjustments);
43
+ return JSON.stringify([
44
+ color,
45
+ look && imageFilterAttrs(look),
46
+ // The switches put a filter in the list instead of setting an attr.
47
+ look && [!!look.grayscale, !!look.sepia, !!look.invert],
48
+ ]);
49
+ }
50
+ /** Whether the node must draw through `lookSceneFunc`: a colour filter, a flip or a vignette. */
51
+ export function drawsLook(adjustments, color) {
52
+ return (lookKey(adjustments, color) !== null ||
53
+ !!adjustments?.flipX ||
54
+ !!adjustments?.flipY ||
55
+ (adjustments?.vignette ?? 0) > 0);
56
+ }
57
+ /** Node attrs `lookSceneFunc` reads: the flip and the vignette. */
58
+ export function lookDrawAttrs(adjustments) {
59
+ return {
60
+ flipX: !!adjustments?.flipX,
61
+ flipY: !!adjustments?.flipY,
62
+ vignette: adjustments?.vignette ?? 0,
63
+ };
64
+ }
65
+ /**
66
+ * Device pixels per node unit the node is drawn at. The editor matches what Konva's own cache
67
+ * held (the device ratio, whatever the stage zoom); a preview draws at what it draws or captures,
68
+ * whichever is larger, so a full-size capture (the render gate) stays sharp.
69
+ */
70
+ export function lookPixelRatio(node, captureRatio) {
71
+ if (captureRatio === null)
72
+ return Konva.pixelRatio;
73
+ const absolute = Math.abs(node.getAbsoluteScale().x);
74
+ // The stage carries the display scale; what remains is the node's scale within the page.
75
+ const withinPage = absolute / (node.getStage()?.scaleX() || 1);
76
+ return Math.max(absolute * Konva.pixelRatio, withinPage * captureRatio);
77
+ }
78
+ /**
79
+ * Size of the bitmap of a photo drawn `photoWidth` node units wide at `pixelRatio`: never larger
80
+ * than the photo, nor than `maxArea`.
81
+ */
82
+ export function lookBitmapSize(natural, photoWidth, pixelRatio, maxArea = LOOK_MAX_AREA) {
83
+ const wanted = (photoWidth * pixelRatio) / natural.width;
84
+ const areaLimit = Math.sqrt(maxArea / (natural.width * natural.height));
85
+ const scale = Math.min(1, wanted, areaLimit);
86
+ return {
87
+ width: Math.max(1, Math.round(natural.width * scale)),
88
+ height: Math.max(1, Math.round(natural.height * scale)),
89
+ };
90
+ }
91
+ // Per photo the looks built last; the background, a framed photo and its dimmed rest share them.
92
+ const built = new WeakMap();
93
+ const KEPT_PER_IMAGE = 2;
94
+ /** An already built bitmap of this look at least `width` wide, if there is one. */
95
+ export function builtLookBitmap(image, key, width) {
96
+ return built.get(image)?.find((e) => e.key === key && e.bitmap.width >= width)?.bitmap ?? null;
97
+ }
98
+ /**
99
+ * The photo with its colour filters applied, at least `size` (an equal or larger bitmap of the
100
+ * same look is reused), or the photo itself when the look has no colour filter. The blur radius
101
+ * is in node units, as on the node: `photoWidth` is how wide the whole photo is drawn.
102
+ */
103
+ export function lookBitmap(image, adjustments, color, photoWidth, size) {
104
+ const key = lookKey(adjustments, color);
105
+ if (key === null || !image.naturalWidth || !image.naturalHeight || photoWidth <= 0)
106
+ return image;
107
+ const hit = builtLookBitmap(image, key, size.width);
108
+ if (hit)
109
+ return hit;
110
+ const canvas = document.createElement('canvas');
111
+ canvas.width = size.width;
112
+ canvas.height = size.height;
113
+ const ctx = canvas.getContext('2d', { willReadFrequently: true });
114
+ if (!ctx)
115
+ return image;
116
+ ctx.drawImage(image, 0, 0, size.width, size.height);
117
+ // The filters read their values off a node (`this`), as in Konva's cache.
118
+ const look = colourLook(adjustments);
119
+ const attrs = new Konva.Image({
120
+ image,
121
+ ...(look ? imageFilterAttrs(look) : {}),
122
+ ...(color
123
+ ? {
124
+ red: parseInt(color.slice(1, 3), 16),
125
+ green: parseInt(color.slice(3, 5), 16),
126
+ blue: parseInt(color.slice(5, 7), 16),
127
+ }
128
+ : {}),
129
+ });
130
+ const data = ctx.getImageData(0, 0, size.width, size.height);
131
+ // Konva scales the blur radius by the pixels per node unit it filters at.
132
+ const ratio = size.width / photoWidth;
133
+ for (const filter of colourFilters(adjustments, color)) {
134
+ if (typeof filter === 'function')
135
+ filter.call(attrs, data, ratio);
136
+ }
137
+ ctx.putImageData(data, 0, 0);
138
+ attrs.destroy();
139
+ const kept = (built.get(image) ?? []).filter((e) => e.key !== key);
140
+ built.set(image, [{ key, bitmap: canvas }, ...kept].slice(0, KEPT_PER_IMAGE));
141
+ return canvas;
142
+ }
143
+ /** The photo as Konva's image would draw it, from the look bitmap, flipped as the look says. */
144
+ function drawPhoto(ctx, shape, width, height) {
145
+ const photo = shape.getAttr('image');
146
+ if (!photo)
147
+ return;
148
+ const source = shape.getAttr(LOOK_IMAGE_ATTR) ?? photo;
149
+ const flipX = !!shape.getAttr('flipX');
150
+ const flipY = !!shape.getAttr('flipY');
151
+ ctx.save();
152
+ if (flipX || flipY) {
153
+ // The flip mirrors the drawn frame, corners and all, as the flip filter mirrored the cache.
154
+ ctx.translate(flipX ? width : 0, flipY ? height : 0);
155
+ ctx.scale(flipX ? -1 : 1, flipY ? -1 : 1);
156
+ }
157
+ const draw = shape.getAttr(STRAIGHT_DRAW_ATTR);
158
+ if (draw) {
159
+ // Begradigt: das ganze Foto gedreht im Rahmen, beschnitten auf den Rahmen.
160
+ const radius = shape.getAttr('cornerRadius');
161
+ ctx.beginPath();
162
+ if (hasCorners(radius))
163
+ traceRoundedRect(ctx, width, height, cornersOf(radius, width, height));
164
+ else
165
+ ctx.rect(0, 0, width, height);
166
+ ctx.closePath();
167
+ ctx.clip();
168
+ ctx.translate(draw.x, draw.y);
169
+ ctx.rotate((draw.rotation * Math.PI) / 180);
170
+ ctx.drawImage(source, -draw.width / 2, -draw.height / 2, draw.width, draw.height);
171
+ ctx.restore();
172
+ return;
173
+ }
174
+ // As Konva.Image draws itself (no fill or stroke: photos carry neither).
175
+ const cornerRadius = shape.cornerRadius();
176
+ if (cornerRadius) {
177
+ ctx.beginPath();
178
+ Konva.Util.drawRoundedRectPath(ctx, width, height, cornerRadius);
179
+ ctx.closePath();
180
+ ctx.clip();
181
+ }
182
+ const cropWidth = shape.getAttr('cropWidth');
183
+ const cropHeight = shape.getAttr('cropHeight');
184
+ if (cropWidth && cropHeight) {
185
+ // The crop is in photo pixels; the bitmap may be smaller.
186
+ const s = Number(source.width) / (photo.naturalWidth || photo.width);
187
+ const cropX = shape.getAttr('cropX') ?? 0;
188
+ const cropY = shape.getAttr('cropY') ?? 0;
189
+ ctx.drawImage(source, cropX * s, cropY * s, cropWidth * s, cropHeight * s, 0, 0, width, height);
190
+ }
191
+ else {
192
+ ctx.drawImage(source, 0, 0, width, height);
193
+ }
194
+ ctx.restore();
195
+ }
196
+ /**
197
+ * Darkens `ctx`'s pixels towards the edges of `width` × `height` by `strength`, the curve of the
198
+ * former vignette filter: k = 1 − strength·smoothstep(t), t = (r − 0.35) / 0.65, r the distance
199
+ * from the middle with the corners at 1. Black at alpha 1 − k drawn `source-atop` multiplies each
200
+ * pixel's colour by k and keeps its alpha, so a cut-out stays cut out.
201
+ */
202
+ export function paintVignette(ctx, width, height, strength) {
203
+ // r = |(dx, dy)| / √2 with dx, dy ∈ [−1, 1]: in a box scaled to 2 × 2 a circle of radius √2.
204
+ ctx.save();
205
+ ctx.translate(width / 2, height / 2);
206
+ ctx.scale(width / 2, height / 2);
207
+ const gradient = ctx.createRadialGradient(0, 0, 0.35 * Math.SQRT2, 0, 0, Math.SQRT2);
208
+ const STOPS = 32;
209
+ for (let i = 0; i <= STOPS; i++) {
210
+ const t = i / STOPS;
211
+ gradient.addColorStop(t, `rgba(0,0,0,${strength * t * t * (3 - 2 * t)})`);
212
+ }
213
+ ctx.globalCompositeOperation = 'source-atop';
214
+ ctx.fillStyle = gradient;
215
+ ctx.fillRect(-1, -1, 2, 2);
216
+ ctx.restore();
217
+ }
218
+ // One reusable canvas per node for the vignette. A recording context gets a fresh one: a
219
+ // recorded `drawImage` keeps the canvas it was handed and reads it later.
220
+ const scratch = new WeakMap();
221
+ function vignetteCanvas(shape, reuse, width, height) {
222
+ let canvas = reuse ? scratch.get(shape) : undefined;
223
+ if (!canvas) {
224
+ canvas = document.createElement('canvas');
225
+ if (reuse)
226
+ scratch.set(shape, canvas);
227
+ }
228
+ if (canvas.width !== width || canvas.height !== height) {
229
+ canvas.width = width;
230
+ canvas.height = height;
231
+ }
232
+ return canvas;
233
+ }
234
+ // Longest side of the vignette canvas: the overlay limit, below what iOS WebKit drops.
235
+ const MAX_VIGNETTE_SIDE = 8192;
236
+ /**
237
+ * Scene function of a photo with a look, and of a straightened one: the look bitmap with the
238
+ * node's crop, the flip, and the vignette over the drawn frame.
239
+ */
240
+ export function lookSceneFunc(context, shape) {
241
+ const width = shape.width();
242
+ const height = shape.height();
243
+ const vignette = Math.max(0, Math.min(1, Number(shape.getAttr('vignette')) || 0));
244
+ if (!vignette || width <= 0 || height <= 0) {
245
+ drawPhoto(context, shape, width, height);
246
+ return;
247
+ }
248
+ // The vignette may only darken the photo's own pixels: drawn apart, then put on the canvas.
249
+ const scale = Math.abs(shape.getAbsoluteScale().x) * context.canvas.getPixelRatio();
250
+ const pixelWidth = Math.max(1, Math.min(MAX_VIGNETTE_SIDE, Math.ceil(width * scale)));
251
+ const pixelHeight = Math.max(1, Math.min(MAX_VIGNETTE_SIDE, Math.ceil(height * scale)));
252
+ const native = typeof CanvasRenderingContext2D !== 'undefined' &&
253
+ context._context instanceof CanvasRenderingContext2D;
254
+ const canvas = vignetteCanvas(shape, native, pixelWidth, pixelHeight);
255
+ const ctx = canvas.getContext('2d');
256
+ if (!ctx) {
257
+ drawPhoto(context, shape, width, height);
258
+ return;
259
+ }
260
+ ctx.setTransform(1, 0, 0, 1, 0, 0);
261
+ ctx.globalCompositeOperation = 'source-over';
262
+ ctx.clearRect(0, 0, pixelWidth, pixelHeight);
263
+ ctx.scale(pixelWidth / width, pixelHeight / height);
264
+ drawPhoto(ctx, shape, width, height);
265
+ paintVignette(ctx, width, height, vignette);
266
+ context.drawImage(canvas, 0, 0, width, height);
267
+ }
@@ -1,5 +1,4 @@
1
1
  import { type Mirror, type Rect, type Size } from './coverFit.js';
2
- import type Konva from 'konva';
3
2
  export declare const STRAIGHTEN_LIMIT = 45;
4
3
  interface Point {
5
4
  x: number;
@@ -42,8 +41,6 @@ export interface StraightDraw {
42
41
  /** Der Flip-Filter spiegelt danach den Knoten; daher hier Lage gespiegelt, Winkel je Achse umgekehrt. */
43
42
  export declare function straightDrawOf(photo: Rect, deg: number, box: Rect, mirror: Mirror | null): StraightDraw;
44
43
  export declare const STRAIGHT_DRAW_ATTR = "straightDraw";
45
- /** Konvas `crop` schneidet nur achsparallel aus; das gedrehte Foto zeichnet diese Funktion. */
46
- export declare function straightSceneFunc(context: Konva.Context, shape: Konva.Shape): void;
47
44
  /** Trägt nur den Maßstab: `printImageInfo` liest `cropWidth` für die Druck-DPI. */
48
45
  export declare function straightScaleCrop(natural: Size, photo: Rect, box: Rect): Rect;
49
46
  /** Ein neues Hintergrundfoto beginnt ungedreht; 0 statt Löschen hält ein gedreht verschobenes Fenster auf Deckung. */
@@ -1,6 +1,5 @@
1
1
  // SPDX-License-Identifier: AGPL-3.0-only
2
2
  // Begradigen. Gedrehte Fotos sind Rechtecke ihrer ungedrehten Lage, gedreht um die Mitte (Grad, wie Konva).
3
- import { cornersOf, hasCorners, traceRoundedRect } from './cornerRadius.js';
4
3
  import { coverPhotoRect, framedPhotoRect, panOfFramedPhoto, } from './coverFit.js';
5
4
  export const STRAIGHTEN_LIMIT = 45;
6
5
  export function clampStraighten(value) {
@@ -211,30 +210,6 @@ export function straightDrawOf(photo, deg, box, mirror) {
211
210
  }
212
211
  // Ziehen setzt das Attribut direkt, ohne React-Zustand.
213
212
  export const STRAIGHT_DRAW_ATTR = 'straightDraw';
214
- /** Konvas `crop` schneidet nur achsparallel aus; das gedrehte Foto zeichnet diese Funktion. */
215
- export function straightSceneFunc(context, shape) {
216
- const image = shape.getAttr('image');
217
- const draw = shape.getAttr(STRAIGHT_DRAW_ATTR);
218
- if (!image || !draw)
219
- return;
220
- const width = shape.width();
221
- const height = shape.height();
222
- const radius = shape.getAttr('cornerRadius');
223
- context.save();
224
- context.beginPath();
225
- if (hasCorners(radius)) {
226
- traceRoundedRect(context, width, height, cornersOf(radius, width, height));
227
- }
228
- else {
229
- context.rect(0, 0, width, height);
230
- }
231
- context.closePath();
232
- context.clip();
233
- context.translate(draw.x, draw.y);
234
- context.rotate(radians(draw.rotation));
235
- context.drawImage(image, -draw.width / 2, -draw.height / 2, draw.width, draw.height);
236
- context.restore();
237
- }
238
213
  /** Trägt nur den Maßstab: `printImageInfo` liest `cropWidth` für die Druck-DPI. */
239
214
  export function straightScaleCrop(natural, photo, box) {
240
215
  const sx = natural.width / photo.width;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ggraphic/editor",
3
- "version": "0.1.0-alpha.6",
3
+ "version": "0.1.0-alpha.8",
4
4
  "license": "AGPL-3.0-only",
5
5
  "repository": {
6
6
  "type": "git",
@@ -1,6 +0,0 @@
1
- import type Konva from 'konva';
2
- /**
3
- * Re-caches a filtered node once per frame at most. A pan changes the crop on
4
- * every pointer move, and each `cache()` re-runs every filter over the bitmap.
5
- */
6
- export declare function recacheNextFrame(node: Konva.Node): void;
@@ -1,15 +0,0 @@
1
- const pending = new WeakSet();
2
- /**
3
- * Re-caches a filtered node once per frame at most. A pan changes the crop on
4
- * every pointer move, and each `cache()` re-runs every filter over the bitmap.
5
- */
6
- export function recacheNextFrame(node) {
7
- if (!node.isCached() || pending.has(node))
8
- return;
9
- pending.add(node);
10
- requestAnimationFrame(() => {
11
- pending.delete(node);
12
- if (node.isCached())
13
- node.cache();
14
- });
15
- }