@snowcone-app/loaders 0.0.0-stage → 0.3.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/CHANGELOG.md +15 -0
- package/README.md +61 -2
- package/dist/beam/index.cjs +438 -0
- package/dist/beam/index.cjs.map +1 -0
- package/dist/beam/index.d.cts +137 -0
- package/dist/beam/index.d.ts +137 -0
- package/dist/beam/index.js +426 -0
- package/dist/beam/index.js.map +1 -0
- package/dist/chunk-2TO7VCSK.js +238 -0
- package/dist/chunk-2TO7VCSK.js.map +1 -0
- package/dist/chunk-6IWUTOBS.js +16 -0
- package/dist/chunk-6IWUTOBS.js.map +1 -0
- package/dist/chunk-7N24M2SH.cjs +259 -0
- package/dist/chunk-7N24M2SH.cjs.map +1 -0
- package/dist/chunk-NCXRWPGN.cjs +19 -0
- package/dist/chunk-NCXRWPGN.cjs.map +1 -0
- package/dist/index.cjs +16 -0
- package/dist/index.cjs.map +1 -0
- package/dist/index.d.cts +63 -0
- package/dist/index.d.ts +63 -0
- package/dist/index.js +3 -0
- package/dist/index.js.map +1 -0
- package/dist/mask.cjs +89 -0
- package/dist/mask.cjs.map +1 -0
- package/dist/mask.d.cts +90 -0
- package/dist/mask.d.ts +90 -0
- package/dist/mask.js +4 -0
- package/dist/mask.js.map +1 -0
- package/package.json +84 -3
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
# @snowcone-app/loaders
|
|
2
|
+
|
|
3
|
+
## 0.3.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#6729](https://github.com/snowcone-app/snowcone-monorepo/pull/6729) [`b7abcb7`](https://github.com/snowcone-app/snowcone-monorepo/commit/b7abcb7efca4e2fd671fdb0d5483516b5452c02f) Thanks [@kevinsproles](https://github.com/kevinsproles)! - The beam on light products and big screens: on a light surface (a white cup) the beam is a coloured line painted on, not light added (white plus light stays white, so it only showed where it crossed the letters and the rules above and below them). The learned area only grows, at most every 0.6 s, and never moves during the last pass. A picture swapped under it (a colour switch) starts fresh instead of learning the whole body. A new frame is read only when one arrives, and the canvas is capped at 2.5 MP (a 5K screen asked for 15 MP every frame). New stamps: `data-sc-loader-area`, `data-sc-loader-surface`.
|
|
8
|
+
|
|
9
|
+
## 0.2.0
|
|
10
|
+
|
|
11
|
+
### Minor Changes
|
|
12
|
+
|
|
13
|
+
- [#6723](https://github.com/snowcone-app/snowcone-monorepo/pull/6723) [`e6e1425`](https://github.com/snowcone-app/snowcone-monorepo/commit/e6e1425062b5284fc9f05e452d529c7141d6994b) Thanks [@kevinsproles](https://github.com/kevinsproles)! - New package `@snowcone-app/loaders`: loaders for live product pictures, one import per loader (a site downloads only the loaders it imports). The first is the beam (`@snowcone-app/loaders/beam`): a soft line of light gliding over the part of the picture that changes while its next render is on the way, then one last pass once it has caught up. It learns which part changes by comparing frames, and keeps that per mockup id.
|
|
14
|
+
|
|
15
|
+
`HeroProductImage` now draws the beam as its re-render overlay, in place of the rainbow.
|
package/README.md
CHANGED
|
@@ -1,3 +1,62 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @snowcone-app/loaders
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Loaders for live product pictures: what you draw over a mockup while its next render is on the way.
|
|
4
|
+
|
|
5
|
+
Each loader is its own import. A site downloads only the loaders it imports, never the whole set:
|
|
6
|
+
|
|
7
|
+
```tsx
|
|
8
|
+
import { BeamLoader } from "@snowcone-app/loaders/beam";
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## The beam
|
|
12
|
+
|
|
13
|
+
A soft line of light glides over the part of the picture that is changing, then makes one last pass across and fades
|
|
14
|
+
once the picture has caught up.
|
|
15
|
+
|
|
16
|
+
```tsx
|
|
17
|
+
<div style={{ position: "relative" }}>
|
|
18
|
+
<img ref={imgRef} src={mockupUrl} crossOrigin="anonymous" style={{ width: "100%", objectFit: "cover" }} />
|
|
19
|
+
{showLoader && (
|
|
20
|
+
<BeamLoader
|
|
21
|
+
visible={isRendering} // false = caught up: last pass, then onExited
|
|
22
|
+
onExited={() => setShowLoader(false)}
|
|
23
|
+
picture={() => imgRef.current} // the picture on screen now
|
|
24
|
+
maskKey={mockupId} // what it learns is kept per scene
|
|
25
|
+
fit="cover" // the <img>'s object-fit
|
|
26
|
+
/>
|
|
27
|
+
)}
|
|
28
|
+
</div>
|
|
29
|
+
```
|
|
30
|
+
|
|
31
|
+
It finds the part that changes by itself. It compares the picture from before the edit with each new frame, and
|
|
32
|
+
what differs (the print, the coloured body) is where it stays. What it learns is kept per `maskKey` in memory and in
|
|
33
|
+
localStorage (about 1 KB a scene, at most 40 scenes), so the next edit of the same picture starts on the right part at
|
|
34
|
+
once. Until it knows, it covers the whole picture. Reading the picture needs it to be same-origin or served with CORS
|
|
35
|
+
(`crossOrigin="anonymous"`); otherwise the beam just covers the whole picture.
|
|
36
|
+
|
|
37
|
+
Already know the area? Pass `mask` (an image whose alpha is the area, in the picture's own pixels) and nothing is
|
|
38
|
+
learned.
|
|
39
|
+
|
|
40
|
+
| Prop | Default | |
|
|
41
|
+
|---|---|---|
|
|
42
|
+
| `look` | `"beam"` | `"beam"`: light only. `"soft"`: a faint haze and a trail, painted on. `"prism"`: thin rainbow fringes trail the beam. |
|
|
43
|
+
| `tone` | `"auto"` | The light's colour. `"auto"` takes a light version of the changing part's colour; `"cool"`, `"warm"`, or `"r,g,b"`. |
|
|
44
|
+
|
|
45
|
+
On a light surface (a white cup) light added to white can't be seen, so once the area is known the beam becomes a
|
|
46
|
+
coloured line painted on. The learned area only grows during a load, at most every 0.6 s, so the beam never wanders.
|
|
47
|
+
|
|
48
|
+
With `prefers-reduced-motion` the beam doesn't move: a still, soft glow covers the area instead.
|
|
49
|
+
|
|
50
|
+
Machine-readable state (assert on these, not on pixels): `data-sc-loader="beam"`, `data-sc-loader-phase`
|
|
51
|
+
(`loading` | `finishing`), `data-sc-loader-mask` (`whole` | `learned` | `given`), `data-sc-loader-area` (the area in
|
|
52
|
+
picture space, `x,y,w,h` from 0 to 1) and `data-sc-loader-surface` (`light` | `dark`).
|
|
53
|
+
|
|
54
|
+
Not on React? `BeamEngine` is the same beam without React. Call `draw(ctx, width, height, place, now)` on each
|
|
55
|
+
animation frame, `finish(now)` when caught up, and `setRegion(...)` to give it an area.
|
|
56
|
+
|
|
57
|
+
## Writing a loader
|
|
58
|
+
|
|
59
|
+
A loader is a component that takes `LoaderProps` (from `@snowcone-app/loaders`): `visible`, `onExited`, `picture`,
|
|
60
|
+
`maskKey`, `fit`, `mask`. It should never take pointer events, always play an exit when `visible` turns false, and turn
|
|
61
|
+
back (not restart) if `visible` comes back before its exit ends. `@snowcone-app/loaders/mask` has the frame-comparison
|
|
62
|
+
helpers the beam uses.
|
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
'use strict';
|
|
2
|
+
|
|
3
|
+
var chunk7N24M2SH_cjs = require('../chunk-7N24M2SH.cjs');
|
|
4
|
+
var chunkNCXRWPGN_cjs = require('../chunk-NCXRWPGN.cjs');
|
|
5
|
+
var react = require('react');
|
|
6
|
+
var jsxRuntime = require('react/jsx-runtime');
|
|
7
|
+
|
|
8
|
+
// src/beam/engine.ts
|
|
9
|
+
var BEAM_FADE_IN_MS = 450;
|
|
10
|
+
var BEAM_SWEEP_MS = 1150;
|
|
11
|
+
var BEAM_PASS_MS = 380;
|
|
12
|
+
var BEAM_TAIL_MS = 150;
|
|
13
|
+
var BEAM_RUSH_MAX_MS = 190;
|
|
14
|
+
var BEAM_FINISH_MS = BEAM_RUSH_MAX_MS + BEAM_PASS_MS + BEAM_TAIL_MS;
|
|
15
|
+
var REGION_FADE_MS = 300;
|
|
16
|
+
var TONES = { cool: "190,222,255", warm: "255,150,60" };
|
|
17
|
+
var ON_LIGHT = { glow: "70,140,255", core: "40,110,240" };
|
|
18
|
+
var FRINGES = ["255,70,70", "255,170,40", "250,240,80", "70,220,110", "70,150,255", "160,90,255"];
|
|
19
|
+
var clamp = (v, a = 0, b = 1) => Math.max(a, Math.min(b, v));
|
|
20
|
+
var ease = (p) => 0.5 - 0.5 * Math.cos(Math.PI * clamp(p));
|
|
21
|
+
var BeamMotion = class {
|
|
22
|
+
t0;
|
|
23
|
+
ph0 = 0;
|
|
24
|
+
/** When the beam started (its fade-in runs from here). */
|
|
25
|
+
bornAt;
|
|
26
|
+
fin = null;
|
|
27
|
+
constructor(now) {
|
|
28
|
+
this.t0 = now;
|
|
29
|
+
this.bornAt = now;
|
|
30
|
+
}
|
|
31
|
+
get finishing() {
|
|
32
|
+
return this.fin !== null;
|
|
33
|
+
}
|
|
34
|
+
loading(now) {
|
|
35
|
+
const ph = (((now - this.t0) / BEAM_SWEEP_MS + this.ph0) % 2 + 2) % 2;
|
|
36
|
+
return ph < 1 ? { s: ease(ph), dir: 1 } : { s: ease(2 - ph), dir: -1 };
|
|
37
|
+
}
|
|
38
|
+
fadeIn(now) {
|
|
39
|
+
const p = clamp((now - this.bornAt) / BEAM_FADE_IN_MS);
|
|
40
|
+
return p * p;
|
|
41
|
+
}
|
|
42
|
+
at(now) {
|
|
43
|
+
if (!this.fin) return { ...this.loading(now), alpha: this.fadeIn(now), finishing: false, done: false };
|
|
44
|
+
const { at, from, end, rush, alpha } = this.fin;
|
|
45
|
+
const t = now - at;
|
|
46
|
+
if (t >= rush + BEAM_PASS_MS + BEAM_TAIL_MS) return { s: 1 - end, dir: end === 0 ? 1 : -1, alpha: 0, finishing: true, done: true };
|
|
47
|
+
if (t < rush) {
|
|
48
|
+
const p2 = t / rush;
|
|
49
|
+
return { s: from + (end - from) * (1 - (1 - p2) * (1 - p2)), dir: end > from ? 1 : -1, alpha, finishing: true, done: false };
|
|
50
|
+
}
|
|
51
|
+
const p = (t - rush) / BEAM_PASS_MS;
|
|
52
|
+
const s = p >= 1 ? 1 - end : end + (1 - 2 * end) * ease(p);
|
|
53
|
+
const fadeFrom = rush + BEAM_PASS_MS * 0.6, fadeLen = BEAM_PASS_MS * 0.4 + BEAM_TAIL_MS;
|
|
54
|
+
const a = t <= fadeFrom ? alpha : alpha * (1 - clamp((t - fadeFrom) / fadeLen));
|
|
55
|
+
return { s, dir: end === 0 ? 1 : -1, alpha: a, finishing: true, done: false };
|
|
56
|
+
}
|
|
57
|
+
/** The picture has caught up: dash to the nearer edge, one pass across, fade. Repeated calls change nothing. */
|
|
58
|
+
finish(now) {
|
|
59
|
+
if (this.fin) return;
|
|
60
|
+
const cur = this.at(now);
|
|
61
|
+
const end = cur.s < 0.5 ? 0 : 1;
|
|
62
|
+
this.fin = { at: now, from: cur.s, end, rush: 60 + (BEAM_RUSH_MAX_MS - 60) * 2 * Math.abs(cur.s - end), alpha: cur.alpha };
|
|
63
|
+
}
|
|
64
|
+
/** Behind again before the exit ended: carry on gliding from where the beam is, the way it was going. */
|
|
65
|
+
resume(now) {
|
|
66
|
+
if (!this.fin) return;
|
|
67
|
+
const cur = this.at(now);
|
|
68
|
+
this.fin = null;
|
|
69
|
+
const ph = Math.acos(clamp(1 - 2 * cur.s, -1, 1)) / Math.PI;
|
|
70
|
+
this.ph0 = cur.dir > 0 ? ph : 2 - ph;
|
|
71
|
+
this.t0 = now;
|
|
72
|
+
}
|
|
73
|
+
};
|
|
74
|
+
var WHOLE = { mask: null, box: null };
|
|
75
|
+
var BeamEngine = class {
|
|
76
|
+
motion;
|
|
77
|
+
look;
|
|
78
|
+
tone;
|
|
79
|
+
reduced;
|
|
80
|
+
prev = WHOLE;
|
|
81
|
+
next = WHOLE;
|
|
82
|
+
regionAt = -Infinity;
|
|
83
|
+
maskLayer = null;
|
|
84
|
+
constructor(opts, now) {
|
|
85
|
+
this.motion = new BeamMotion(now);
|
|
86
|
+
this.look = opts.look ?? "beam";
|
|
87
|
+
this.tone = opts.tone ?? "auto";
|
|
88
|
+
this.reduced = !!opts.reducedMotion;
|
|
89
|
+
}
|
|
90
|
+
/** On a light surface the beam is painted (a coloured line); on a dark one it is light. Only once the area is known:
|
|
91
|
+
* over the whole frame (the first edit of a scene) a painted line would cross the background too, so it stays light. */
|
|
92
|
+
get onLight() {
|
|
93
|
+
return !!this.next.mask && this.next.surface === "light";
|
|
94
|
+
}
|
|
95
|
+
/** How the canvas should blend over the picture: light only adds (`screen`), the soft laser and any beam on a
|
|
96
|
+
* light surface paint on. */
|
|
97
|
+
get blendMode() {
|
|
98
|
+
return this.look === "soft" || this.onLight ? "normal" : "screen";
|
|
99
|
+
}
|
|
100
|
+
/** A new area to stay on; the old one fades into it over {@link REGION_FADE_MS}. While the beam is still fading in
|
|
101
|
+
* from the whole frame, it takes the area at once: no glimpse of a beam across the whole picture first. */
|
|
102
|
+
setRegion(region, now) {
|
|
103
|
+
const next = region ?? WHOLE;
|
|
104
|
+
if (!this.next.mask && now - this.motion.bornAt < BEAM_FADE_IN_MS) {
|
|
105
|
+
this.prev = this.next = next;
|
|
106
|
+
this.regionAt = -Infinity;
|
|
107
|
+
return;
|
|
108
|
+
}
|
|
109
|
+
this.prev = this.mixWeight(now) >= 1 ? this.next : this.prev;
|
|
110
|
+
this.next = next;
|
|
111
|
+
this.regionAt = now;
|
|
112
|
+
}
|
|
113
|
+
finish(now) {
|
|
114
|
+
this.motion.finish(now);
|
|
115
|
+
}
|
|
116
|
+
resume(now) {
|
|
117
|
+
this.motion.resume(now);
|
|
118
|
+
}
|
|
119
|
+
mixWeight(now) {
|
|
120
|
+
return clamp((now - this.regionAt) / REGION_FADE_MS);
|
|
121
|
+
}
|
|
122
|
+
rgb() {
|
|
123
|
+
if (this.tone === "cool" || this.tone === "warm") return TONES[this.tone];
|
|
124
|
+
if (this.tone !== "auto") return this.tone;
|
|
125
|
+
if (this.onLight) return ON_LIGHT.glow;
|
|
126
|
+
return this.next.tone ?? this.prev.tone ?? TONES.cool;
|
|
127
|
+
}
|
|
128
|
+
/** The beam's area in frame pixels: the region's box over the picture, or the picture's visible part. */
|
|
129
|
+
area(region, place, W, H) {
|
|
130
|
+
if (!region.box) {
|
|
131
|
+
const x0 = Math.max(0, place.x), y0 = Math.max(0, place.y);
|
|
132
|
+
return { x: x0, y: y0, w: Math.min(W, place.x + place.w) - x0, h: Math.min(H, place.y + place.h) - y0 };
|
|
133
|
+
}
|
|
134
|
+
const b = region.box;
|
|
135
|
+
return { x: place.x + b.x * place.w, y: place.y + b.y * place.h, w: b.w * place.w, h: b.h * place.h };
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Draws one frame. `place` is where the picture lands in the frame (`fitRect`), `dpr` the device pixel ratio.
|
|
139
|
+
* Returns `true` once the exit is over (the canvas is cleared).
|
|
140
|
+
*/
|
|
141
|
+
draw(ctx, W, H, place, now, dpr = 1) {
|
|
142
|
+
const pos = this.motion.at(now);
|
|
143
|
+
ctx.globalCompositeOperation = "source-over";
|
|
144
|
+
ctx.globalAlpha = 1;
|
|
145
|
+
ctx.clearRect(0, 0, W, H);
|
|
146
|
+
if (pos.done) return true;
|
|
147
|
+
if (pos.alpha <= 1e-3) return false;
|
|
148
|
+
const t = this.mixWeight(now);
|
|
149
|
+
const a0 = this.area(this.prev, place, W, H), a1 = this.area(this.next, place, W, H);
|
|
150
|
+
const A = { x: a0.x + (a1.x - a0.x) * t, y: a0.y + (a1.y - a0.y) * t, w: a0.w + (a1.w - a0.w) * t, h: a0.h + (a1.h - a0.h) * t };
|
|
151
|
+
const rgb = this.rgb();
|
|
152
|
+
const pad = 10 * dpr, top = A.y - pad, tall = A.h + 2 * pad;
|
|
153
|
+
ctx.globalAlpha = pos.alpha;
|
|
154
|
+
if (this.reduced) {
|
|
155
|
+
ctx.fillStyle = `rgba(${rgb},0.22)`;
|
|
156
|
+
ctx.fillRect(0, 0, W, H);
|
|
157
|
+
} else {
|
|
158
|
+
const x = A.x + pos.s * A.w, back = -pos.dir;
|
|
159
|
+
const light = this.look !== "soft";
|
|
160
|
+
const painted = this.onLight;
|
|
161
|
+
if (this.look === "soft") {
|
|
162
|
+
ctx.fillStyle = "rgba(255,255,255,0.10)";
|
|
163
|
+
ctx.fillRect(0, 0, W, H);
|
|
164
|
+
const T = A.w * 0.22, g2 = ctx.createLinearGradient(x + back * T, 0, x, 0);
|
|
165
|
+
g2.addColorStop(0, `rgba(${rgb},0)`);
|
|
166
|
+
g2.addColorStop(1, `rgba(${rgb},0.22)`);
|
|
167
|
+
ctx.fillStyle = g2;
|
|
168
|
+
ctx.fillRect(Math.min(x, x + back * T), top, T, tall);
|
|
169
|
+
}
|
|
170
|
+
if (this.look === "prism") {
|
|
171
|
+
FRINGES.forEach((c, i) => {
|
|
172
|
+
ctx.fillStyle = `rgba(${c},${0.55 - i * 0.04})`;
|
|
173
|
+
ctx.fillRect(x + back * (6 + i * 7) * dpr * (pos.finishing ? 1.5 : 1) - 2.5 * dpr, top, 5 * dpr, tall);
|
|
174
|
+
});
|
|
175
|
+
}
|
|
176
|
+
const gw = (light ? 18 : 26) * dpr;
|
|
177
|
+
const g = ctx.createLinearGradient(x - gw, 0, x + gw, 0);
|
|
178
|
+
g.addColorStop(0, `rgba(${rgb},0)`);
|
|
179
|
+
g.addColorStop(0.5, `rgba(${rgb},${painted ? 0.45 : light ? 0.85 : 0.75})`);
|
|
180
|
+
g.addColorStop(1, `rgba(${rgb},0)`);
|
|
181
|
+
ctx.fillStyle = g;
|
|
182
|
+
ctx.fillRect(x - gw, top, 2 * gw, tall);
|
|
183
|
+
ctx.fillStyle = painted ? `rgba(${ON_LIGHT.core},0.85)` : "rgba(255,255,255,0.95)";
|
|
184
|
+
ctx.fillRect(x - dpr, top, 2 * dpr, tall);
|
|
185
|
+
ctx.fillStyle = painted ? `rgba(${ON_LIGHT.core},0.7)` : "rgba(255,245,225,0.9)";
|
|
186
|
+
const sparks = light ? 10 : 14;
|
|
187
|
+
for (let i = 0; i < sparks; i++) {
|
|
188
|
+
ctx.globalAlpha = pos.alpha * Math.random();
|
|
189
|
+
ctx.fillRect(x + (Math.random() - 0.5) * 16 * dpr, A.y + Math.random() * A.h, 1.6 * dpr, 1.6 * dpr);
|
|
190
|
+
}
|
|
191
|
+
ctx.globalAlpha = pos.alpha;
|
|
192
|
+
}
|
|
193
|
+
const layer = this.maskOf(W, H, place, t);
|
|
194
|
+
if (layer) {
|
|
195
|
+
ctx.globalAlpha = 1;
|
|
196
|
+
ctx.globalCompositeOperation = "destination-in";
|
|
197
|
+
ctx.drawImage(layer, 0, 0);
|
|
198
|
+
ctx.globalCompositeOperation = "source-over";
|
|
199
|
+
}
|
|
200
|
+
return false;
|
|
201
|
+
}
|
|
202
|
+
maskOf(W, H, place, t) {
|
|
203
|
+
if (!this.prev.mask && !this.next.mask) return null;
|
|
204
|
+
if (typeof document === "undefined") return null;
|
|
205
|
+
this.maskLayer ??= document.createElement("canvas");
|
|
206
|
+
const m = this.maskLayer;
|
|
207
|
+
if (m.width !== W || m.height !== H) {
|
|
208
|
+
m.width = W;
|
|
209
|
+
m.height = H;
|
|
210
|
+
}
|
|
211
|
+
const x = m.getContext("2d");
|
|
212
|
+
if (!x) return null;
|
|
213
|
+
x.globalAlpha = 1;
|
|
214
|
+
x.clearRect(0, 0, W, H);
|
|
215
|
+
for (const [r, a] of [[this.prev, 1 - t], [this.next, t]]) {
|
|
216
|
+
if (a <= 0) continue;
|
|
217
|
+
x.globalAlpha = a;
|
|
218
|
+
if (r.mask) x.drawImage(r.mask, place.x, place.y, place.w, place.h);
|
|
219
|
+
else x.fillRect(0, 0, W, H);
|
|
220
|
+
}
|
|
221
|
+
x.globalAlpha = 1;
|
|
222
|
+
return m;
|
|
223
|
+
}
|
|
224
|
+
};
|
|
225
|
+
var LEARN_EVERY_MS = 250;
|
|
226
|
+
var REGION_STEADY_MS = 600;
|
|
227
|
+
var REGION_MIN_MOVE = 0.02;
|
|
228
|
+
var LIGHT_SURFACE = 0.62;
|
|
229
|
+
var MAX_CANVAS_PIXELS = 25e5;
|
|
230
|
+
function lightTone([r, g, b]) {
|
|
231
|
+
return [r, g, b].map((v) => Math.round(v + (255 - v) * 0.6)).join(",");
|
|
232
|
+
}
|
|
233
|
+
function BeamLoader({
|
|
234
|
+
visible,
|
|
235
|
+
onExited,
|
|
236
|
+
picture,
|
|
237
|
+
maskKey,
|
|
238
|
+
fit = "cover",
|
|
239
|
+
mask,
|
|
240
|
+
look = "beam",
|
|
241
|
+
tone = "auto",
|
|
242
|
+
className,
|
|
243
|
+
style
|
|
244
|
+
}) {
|
|
245
|
+
const canvasRef = react.useRef(null);
|
|
246
|
+
const engineRef = react.useRef(null);
|
|
247
|
+
const learnNowRef = react.useRef(() => {
|
|
248
|
+
});
|
|
249
|
+
const [phase, setPhase] = react.useState("loading");
|
|
250
|
+
const [maskSource, setMaskSource] = react.useState(mask ? "given" : "whole");
|
|
251
|
+
const live = react.useRef({ picture, maskKey, fit, onExited });
|
|
252
|
+
live.current = { picture, maskKey, fit, onExited };
|
|
253
|
+
const [area, setArea] = react.useState(void 0);
|
|
254
|
+
const [surface, setSurface] = react.useState(void 0);
|
|
255
|
+
react.useEffect(() => {
|
|
256
|
+
const now = () => typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
257
|
+
const reduced = typeof matchMedia === "function" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
258
|
+
const engine = new BeamEngine({ look, tone, reducedMotion: reduced }, now());
|
|
259
|
+
engineRef.current = engine;
|
|
260
|
+
const pic = () => live.current.picture?.() ?? null;
|
|
261
|
+
const L = {
|
|
262
|
+
key: live.current.maskKey,
|
|
263
|
+
base: null,
|
|
264
|
+
bits: null,
|
|
265
|
+
natural: null,
|
|
266
|
+
lastSrc: "",
|
|
267
|
+
lastSnap: null,
|
|
268
|
+
shown: null,
|
|
269
|
+
shownAt: -Infinity
|
|
270
|
+
};
|
|
271
|
+
const srcOf = (el) => el && typeof HTMLImageElement !== "undefined" && el instanceof HTMLImageElement ? el.currentSrc || el.src : "";
|
|
272
|
+
const look_ = () => {
|
|
273
|
+
const el = pic();
|
|
274
|
+
if (!el) return null;
|
|
275
|
+
const n = chunkNCXRWPGN_cjs.naturalSize(el);
|
|
276
|
+
if (n.w > 0) L.natural = n;
|
|
277
|
+
const src = srcOf(el);
|
|
278
|
+
if (src && src === L.lastSrc && L.lastSnap) return L.lastSnap;
|
|
279
|
+
const snap = chunk7N24M2SH_cjs.snapshotPicture(el);
|
|
280
|
+
if (snap) {
|
|
281
|
+
L.lastSrc = src;
|
|
282
|
+
L.lastSnap = snap;
|
|
283
|
+
}
|
|
284
|
+
return snap;
|
|
285
|
+
};
|
|
286
|
+
const surfaceOf = (s, alpha) => s ? chunk7N24M2SH_cjs.meanLuma(s, alpha) > LIGHT_SURFACE ? "light" : "dark" : void 0;
|
|
287
|
+
const apply = (bits, w, h, from, source, force = false) => {
|
|
288
|
+
const alpha = chunk7N24M2SH_cjs.softAlpha(bits, w, h, { grow: 4, blur: 6 });
|
|
289
|
+
const box = chunk7N24M2SH_cjs.alphaBox(alpha, w, h);
|
|
290
|
+
if (!box) return;
|
|
291
|
+
const t = now();
|
|
292
|
+
if (!force) {
|
|
293
|
+
if (engine.motion.finishing) return;
|
|
294
|
+
const p = L.shown;
|
|
295
|
+
if (p) {
|
|
296
|
+
const moved = Math.max(p.x - box.x, p.y - box.y, box.x + box.w - p.x - p.w, box.y + box.h - p.y - p.h);
|
|
297
|
+
if (moved < REGION_MIN_MOVE || t - L.shownAt < REGION_STEADY_MS) return;
|
|
298
|
+
}
|
|
299
|
+
}
|
|
300
|
+
const colour = from ? chunk7N24M2SH_cjs.meanColour(from, bits) : null;
|
|
301
|
+
const surf = surfaceOf(from, alpha);
|
|
302
|
+
engine.setRegion({ mask: chunk7N24M2SH_cjs.alphaCanvas(alpha, w, h), box, tone: colour ? lightTone(colour) : void 0, surface: surf }, t);
|
|
303
|
+
L.shown = box;
|
|
304
|
+
L.shownAt = t;
|
|
305
|
+
setMaskSource(source);
|
|
306
|
+
setArea([box.x, box.y, box.w, box.h].map((v) => v.toFixed(3)).join(","));
|
|
307
|
+
setSurface(surf);
|
|
308
|
+
};
|
|
309
|
+
const startOn = (snap) => {
|
|
310
|
+
L.key = live.current.maskKey;
|
|
311
|
+
L.base = snap;
|
|
312
|
+
L.bits = null;
|
|
313
|
+
L.shown = null;
|
|
314
|
+
const known = snap && L.key ? chunk7N24M2SH_cjs.recallMask(L.key, snap.w, snap.h) : null;
|
|
315
|
+
if (known && snap) {
|
|
316
|
+
L.bits = known;
|
|
317
|
+
apply(known, snap.w, snap.h, snap, "learned", true);
|
|
318
|
+
} else {
|
|
319
|
+
const surf = surfaceOf(snap);
|
|
320
|
+
engine.setRegion({ mask: null, box: null, surface: surf }, now());
|
|
321
|
+
setMaskSource("whole");
|
|
322
|
+
setArea(void 0);
|
|
323
|
+
setSurface(surf);
|
|
324
|
+
}
|
|
325
|
+
};
|
|
326
|
+
if (mask) {
|
|
327
|
+
const s = chunk7N24M2SH_cjs.snapshotPicture(mask);
|
|
328
|
+
if (s) {
|
|
329
|
+
const bits = new Uint8Array(s.w * s.h);
|
|
330
|
+
for (let i = 0; i < bits.length; i++) bits[i] = s.rgba[i * 4 + 3] > 127 ? 1 : 0;
|
|
331
|
+
apply(bits, s.w, s.h, look_(), "given", true);
|
|
332
|
+
}
|
|
333
|
+
} else {
|
|
334
|
+
startOn(look_());
|
|
335
|
+
}
|
|
336
|
+
const learn = () => {
|
|
337
|
+
if (mask) return;
|
|
338
|
+
const cur = look_();
|
|
339
|
+
if (!cur) return;
|
|
340
|
+
if (live.current.maskKey !== L.key) return startOn(cur);
|
|
341
|
+
if (!L.base) return startOn(cur);
|
|
342
|
+
const bits = chunk7N24M2SH_cjs.changedBits(L.base, cur);
|
|
343
|
+
if (!bits || chunk7N24M2SH_cjs.coverageOf(bits) > chunk7N24M2SH_cjs.UNRELIABLE_COVERAGE) {
|
|
344
|
+
L.base = cur;
|
|
345
|
+
return;
|
|
346
|
+
}
|
|
347
|
+
if (!chunk7N24M2SH_cjs.isLearnable(bits)) return;
|
|
348
|
+
const key = L.key;
|
|
349
|
+
const all = key ? chunk7N24M2SH_cjs.rememberMask(key, cur.w, cur.h, bits) : L.bits ? chunk7N24M2SH_cjs.orBits(L.bits, bits) : bits;
|
|
350
|
+
L.bits = all;
|
|
351
|
+
apply(all, cur.w, cur.h, L.base, "learned");
|
|
352
|
+
};
|
|
353
|
+
const learnTimer = setInterval(learn, LEARN_EVERY_MS);
|
|
354
|
+
let raf = 0;
|
|
355
|
+
const draw = () => {
|
|
356
|
+
const c = canvasRef.current;
|
|
357
|
+
const ctx = c?.getContext("2d");
|
|
358
|
+
if (!c || !ctx) return;
|
|
359
|
+
raf = requestAnimationFrame(draw);
|
|
360
|
+
const dpr = Math.min(2, typeof window !== "undefined" && window.devicePixelRatio || 1);
|
|
361
|
+
const css = Math.max(1, c.clientWidth * c.clientHeight);
|
|
362
|
+
const scale = Math.min(dpr, Math.sqrt(MAX_CANVAS_PIXELS / css));
|
|
363
|
+
const W = Math.max(1, Math.round(c.clientWidth * scale)), H = Math.max(1, Math.round(c.clientHeight * scale));
|
|
364
|
+
if (c.width !== W || c.height !== H) {
|
|
365
|
+
c.width = W;
|
|
366
|
+
c.height = H;
|
|
367
|
+
}
|
|
368
|
+
const n = L.natural;
|
|
369
|
+
const place = n ? chunkNCXRWPGN_cjs.fitRect(n.w, n.h, W, H, live.current.fit) : { x: 0, y: 0, w: W, h: H };
|
|
370
|
+
engine.draw(ctx, W, H, place, now(), scale);
|
|
371
|
+
if (c.style.mixBlendMode !== engine.blendMode) c.style.mixBlendMode = engine.blendMode;
|
|
372
|
+
};
|
|
373
|
+
if (typeof requestAnimationFrame === "function") raf = requestAnimationFrame(draw);
|
|
374
|
+
learnNowRef.current = learn;
|
|
375
|
+
return () => {
|
|
376
|
+
clearInterval(learnTimer);
|
|
377
|
+
if (typeof cancelAnimationFrame === "function") cancelAnimationFrame(raf);
|
|
378
|
+
engineRef.current = null;
|
|
379
|
+
learnNowRef.current = () => {
|
|
380
|
+
};
|
|
381
|
+
};
|
|
382
|
+
}, []);
|
|
383
|
+
react.useEffect(() => {
|
|
384
|
+
const engine = engineRef.current;
|
|
385
|
+
if (!engine) return;
|
|
386
|
+
const now = typeof performance !== "undefined" ? performance.now() : Date.now();
|
|
387
|
+
if (!visible) {
|
|
388
|
+
learnNowRef.current();
|
|
389
|
+
engine.finish(now);
|
|
390
|
+
setPhase("finishing");
|
|
391
|
+
const t = setTimeout(() => live.current.onExited?.(), BEAM_FINISH_MS);
|
|
392
|
+
return () => clearTimeout(t);
|
|
393
|
+
}
|
|
394
|
+
if (engine.motion.finishing) {
|
|
395
|
+
engine.resume(now);
|
|
396
|
+
setPhase("loading");
|
|
397
|
+
}
|
|
398
|
+
return void 0;
|
|
399
|
+
}, [visible]);
|
|
400
|
+
return /* @__PURE__ */ jsxRuntime.jsx(
|
|
401
|
+
"canvas",
|
|
402
|
+
{
|
|
403
|
+
ref: canvasRef,
|
|
404
|
+
"aria-hidden": "true",
|
|
405
|
+
"data-sc-loader": "beam",
|
|
406
|
+
"data-sc-loader-look": look,
|
|
407
|
+
"data-sc-loader-phase": phase,
|
|
408
|
+
"data-sc-loader-mask": maskSource,
|
|
409
|
+
"data-sc-loader-area": area,
|
|
410
|
+
"data-sc-loader-surface": surface,
|
|
411
|
+
className,
|
|
412
|
+
style: {
|
|
413
|
+
position: "absolute",
|
|
414
|
+
inset: 0,
|
|
415
|
+
width: "100%",
|
|
416
|
+
height: "100%",
|
|
417
|
+
pointerEvents: "none",
|
|
418
|
+
// the starting blend; the draw loop keeps it in step with the beam (painted on a learned light surface)
|
|
419
|
+
mixBlendMode: look === "soft" ? "normal" : "screen",
|
|
420
|
+
...style
|
|
421
|
+
}
|
|
422
|
+
}
|
|
423
|
+
);
|
|
424
|
+
}
|
|
425
|
+
|
|
426
|
+
exports.BEAM_FADE_IN_MS = BEAM_FADE_IN_MS;
|
|
427
|
+
exports.BEAM_FINISH_MS = BEAM_FINISH_MS;
|
|
428
|
+
exports.BEAM_PASS_MS = BEAM_PASS_MS;
|
|
429
|
+
exports.BEAM_RUSH_MAX_MS = BEAM_RUSH_MAX_MS;
|
|
430
|
+
exports.BEAM_SWEEP_MS = BEAM_SWEEP_MS;
|
|
431
|
+
exports.BEAM_TAIL_MS = BEAM_TAIL_MS;
|
|
432
|
+
exports.BeamEngine = BeamEngine;
|
|
433
|
+
exports.BeamLoader = BeamLoader;
|
|
434
|
+
exports.BeamMotion = BeamMotion;
|
|
435
|
+
exports.LEARN_EVERY_MS = LEARN_EVERY_MS;
|
|
436
|
+
exports.REGION_FADE_MS = REGION_FADE_MS;
|
|
437
|
+
//# sourceMappingURL=index.cjs.map
|
|
438
|
+
//# sourceMappingURL=index.cjs.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/beam/engine.ts","../../src/beam/BeamLoader.tsx"],"names":["p","g","useRef","useState","useEffect","naturalSize","snapshotPicture","meanLuma","softAlpha","alphaBox","meanColour","alphaCanvas","recallMask","changedBits","coverageOf","UNRELIABLE_COVERAGE","isLearnable","rememberMask","orBits","fitRect","jsx"],"mappings":";;;;;;;;AAUO,IAAM,eAAA,GAAkB;AAExB,IAAM,aAAA,GAAgB;AAEtB,IAAM,YAAA,GAAe;AAErB,IAAM,YAAA,GAAe;AAErB,IAAM,gBAAA,GAAmB;AAEzB,IAAM,cAAA,GAAiB,mBAAmB,YAAA,GAAe;AAEzD,IAAM,cAAA,GAAiB;AAQ9B,IAAM,KAAA,GAAQ,EAAE,IAAA,EAAM,aAAA,EAAe,MAAM,YAAA,EAAa;AAExD,IAAM,QAAA,GAAW,EAAE,IAAA,EAAM,YAAA,EAAc,MAAM,YAAA,EAAa;AAC1D,IAAM,UAAU,CAAC,WAAA,EAAa,cAAc,YAAA,EAAc,YAAA,EAAc,cAAc,YAAY,CAAA;AAElG,IAAM,KAAA,GAAQ,CAAC,CAAA,EAAW,CAAA,GAAI,GAAG,CAAA,GAAI,CAAA,KAAM,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,IAAA,CAAK,GAAA,CAAI,CAAA,EAAG,CAAC,CAAC,CAAA;AACrE,IAAM,IAAA,GAAO,CAAC,CAAA,KAAc,GAAA,GAAM,GAAA,GAAM,IAAA,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P,MAAA,EAAQ,MAAA;AAAA,QACR,aAAA,EAAe,MAAA;AAAA;AAAA,QAEf,YAAA,EAAc,IAAA,KAAS,MAAA,GAAS,QAAA,GAAW,QAAA;AAAA,QAC3C,GAAG;AAAA;AACL;AAAA,GACF;AAEJ","file":"index.cjs","sourcesContent":["/**\n * The beam, framework-free: a soft line of light that glides back and forth over the part of the picture that is\n * changing while a render is on the way, then makes one last pass across and fades once the picture has caught up.\n *\n * `BeamMotion` is the timing (pure: give it a clock, read the beam's place and opacity). `BeamEngine` draws it into\n * a 2D canvas, cut to a soft mask of the changing part (or the whole frame when none is known yet).\n */\nimport type { Rect } from \"../index\";\n\n/** The beam fades in over this long. */\nexport const BEAM_FADE_IN_MS = 450;\n/** One sweep from one side to the other while loading. */\nexport const BEAM_SWEEP_MS = 1150;\n/** The last pass, once the picture has caught up. */\nexport const BEAM_PASS_MS = 380;\n/** The fade after the last pass. */\nexport const BEAM_TAIL_MS = 150;\n/** The longest dash to the nearer edge before the last pass (from the middle). */\nexport const BEAM_RUSH_MAX_MS = 190;\n/** Caught up → gone, at most: the dash, the last pass, the tail. A host's fallback timer should wait this long. */\nexport const BEAM_FINISH_MS = BEAM_RUSH_MAX_MS + BEAM_PASS_MS + BEAM_TAIL_MS;\n/** A newly learned area replaces the old one over this long (never a jump). */\nexport const REGION_FADE_MS = 300;\n\n/** The looks from the 1 Dec prototype: `beam` = \"Just the beam\" (light only, the default); `soft` = \"Soft laser\"\n * (a faint haze and a trail, painted on); `prism` = the beam with thin rainbow fringes trailing it. */\nexport type BeamLook = \"beam\" | \"soft\" | \"prism\";\n/** The light's colour: `auto` = a light version of the colour of the part that changes (cool white until known). */\nexport type BeamTone = \"auto\" | \"cool\" | \"warm\" | `${number},${number},${number}`;\n\nconst TONES = { cool: \"190,222,255\", warm: \"255,150,60\" } as const;\n/** On a light surface light can't show (white + light = white): the beam is a coloured laser painted on instead. */\nconst ON_LIGHT = { glow: \"70,140,255\", core: \"40,110,240\" } as const;\nconst FRINGES = [\"255,70,70\", \"255,170,40\", \"250,240,80\", \"70,220,110\", \"70,150,255\", \"160,90,255\"];\n\nconst clamp = (v: number, a = 0, b = 1) => Math.max(a, Math.min(b, v));\nconst ease = (p: number) => 0.5 - 0.5 * Math.cos(Math.PI * clamp(p));\n\nexport interface BeamPosition {\n /** Where the beam is across the area, 0 (left edge) to 1 (right edge). */\n s: number;\n /** Which way it moves. */\n dir: 1 | -1;\n /** Overall opacity, 0–1. */\n alpha: number;\n finishing: boolean;\n /** The exit is over: nothing to draw, the host may unmount. */\n done: boolean;\n}\n\n/** The beam's timing. Times are milliseconds on any monotonic clock (`performance.now()`). */\nexport class BeamMotion {\n private t0: number;\n private ph0 = 0;\n /** When the beam started (its fade-in runs from here). */\n readonly bornAt: number;\n private fin: { at: number; from: number; end: 0 | 1; rush: number; alpha: number } | null = null;\n\n constructor(now: number) {\n this.t0 = now;\n this.bornAt = now;\n }\n\n get finishing(): boolean {\n return this.fin !== null;\n }\n\n private loading(now: number): { s: number; dir: 1 | -1 } {\n const ph = (((now - this.t0) / BEAM_SWEEP_MS + this.ph0) % 2 + 2) % 2;\n return ph < 1 ? { s: ease(ph), dir: 1 } : { s: ease(2 - ph), dir: -1 };\n }\n\n private fadeIn(now: number): number {\n const p = clamp((now - this.bornAt) / BEAM_FADE_IN_MS);\n return p * p;\n }\n\n at(now: number): BeamPosition {\n if (!this.fin) return { ...this.loading(now), alpha: this.fadeIn(now), finishing: false, done: false };\n const { at, from, end, rush, alpha } = this.fin;\n const t = now - at;\n if (t >= rush + BEAM_PASS_MS + BEAM_TAIL_MS) return { s: 1 - end, dir: end === 0 ? 1 : -1, alpha: 0, finishing: true, done: true };\n if (t < rush) {\n const p = t / rush;\n return { s: from + (end - from) * (1 - (1 - p) * (1 - p)), dir: end > from ? 1 : -1, alpha, finishing: true, done: false };\n }\n const p = (t - rush) / BEAM_PASS_MS;\n const s = p >= 1 ? 1 - end : end + (1 - 2 * end) * ease(p);\n // full through most of the pass, then gone by the end of the tail\n const fadeFrom = rush + BEAM_PASS_MS * 0.6, fadeLen = BEAM_PASS_MS * 0.4 + BEAM_TAIL_MS;\n const a = t <= fadeFrom ? alpha : alpha * (1 - clamp((t - fadeFrom) / fadeLen));\n return { s, dir: end === 0 ? 1 : -1, alpha: a, finishing: true, done: false };\n }\n\n /** The picture has caught up: dash to the nearer edge, one pass across, fade. Repeated calls change nothing. */\n finish(now: number): void {\n if (this.fin) return;\n const cur = this.at(now);\n const end: 0 | 1 = cur.s < 0.5 ? 0 : 1;\n this.fin = { at: now, from: cur.s, end, rush: 60 + (BEAM_RUSH_MAX_MS - 60) * 2 * Math.abs(cur.s - end), alpha: cur.alpha };\n }\n\n /** Behind again before the exit ended: carry on gliding from where the beam is, the way it was going. */\n resume(now: number): void {\n if (!this.fin) return;\n const cur = this.at(now);\n this.fin = null;\n const ph = Math.acos(clamp(1 - 2 * cur.s, -1, 1)) / Math.PI; // ease(ph) = s\n this.ph0 = cur.dir > 0 ? ph : 2 - ph;\n this.t0 = now;\n }\n}\n\n/** The part of the picture the beam stays on. */\nexport interface BeamRegion {\n /** Alpha mask in picture space (alpha = changes), drawn over the picture's rect; `null` = the whole frame. */\n mask: CanvasImageSource | null;\n /** The mask's box in picture space (0–1); `null` with no mask. */\n box: { x: number; y: number; w: number; h: number } | null;\n /** The colour of the changing part, `\"r,g,b\"`, for `tone: \"auto\"`. */\n tone?: string;\n /** How light the surface under the area is: `light` (a white cup) gets a painted coloured beam, `dark` light only. */\n surface?: \"light\" | \"dark\";\n}\n\nconst WHOLE: BeamRegion = { mask: null, box: null };\n\nexport interface BeamEngineOptions {\n look?: BeamLook;\n tone?: BeamTone;\n /** `prefers-reduced-motion`: no gliding line, a still soft glow over the area instead. */\n reducedMotion?: boolean;\n}\n\n/** Draws the beam into a 2D canvas the size of the frame. */\nexport class BeamEngine {\n readonly motion: BeamMotion;\n readonly look: BeamLook;\n private readonly tone: BeamTone;\n private readonly reduced: boolean;\n private prev: BeamRegion = WHOLE;\n private next: BeamRegion = WHOLE;\n private regionAt = -Infinity;\n private maskLayer: HTMLCanvasElement | null = null;\n\n constructor(opts: BeamEngineOptions, now: number) {\n this.motion = new BeamMotion(now);\n this.look = opts.look ?? \"beam\";\n this.tone = opts.tone ?? \"auto\";\n this.reduced = !!opts.reducedMotion;\n }\n\n /** On a light surface the beam is painted (a coloured line); on a dark one it is light. Only once the area is known:\n * over the whole frame (the first edit of a scene) a painted line would cross the background too, so it stays light. */\n get onLight(): boolean {\n return !!this.next.mask && this.next.surface === \"light\";\n }\n\n /** How the canvas should blend over the picture: light only adds (`screen`), the soft laser and any beam on a\n * light surface paint on. */\n get blendMode(): \"screen\" | \"normal\" {\n return this.look === \"soft\" || this.onLight ? \"normal\" : \"screen\";\n }\n\n /** A new area to stay on; the old one fades into it over {@link REGION_FADE_MS}. While the beam is still fading in\n * from the whole frame, it takes the area at once: no glimpse of a beam across the whole picture first. */\n setRegion(region: BeamRegion | null, now: number): void {\n const next = region ?? WHOLE;\n if (!this.next.mask && now - this.motion.bornAt < BEAM_FADE_IN_MS) {\n this.prev = this.next = next;\n this.regionAt = -Infinity;\n return;\n }\n this.prev = this.mixWeight(now) >= 1 ? this.next : this.prev;\n this.next = next;\n this.regionAt = now;\n }\n\n finish(now: number): void { this.motion.finish(now); }\n resume(now: number): void { this.motion.resume(now); }\n\n private mixWeight(now: number): number {\n return clamp((now - this.regionAt) / REGION_FADE_MS);\n }\n\n private rgb(): string {\n if (this.tone === \"cool\" || this.tone === \"warm\") return TONES[this.tone];\n if (this.tone !== \"auto\") return this.tone;\n if (this.onLight) return ON_LIGHT.glow;\n return this.next.tone ?? this.prev.tone ?? TONES.cool;\n }\n\n /** The beam's area in frame pixels: the region's box over the picture, or the picture's visible part. */\n private area(region: BeamRegion, place: Rect, W: number, H: number): Rect {\n if (!region.box) {\n const x0 = Math.max(0, place.x), y0 = Math.max(0, place.y);\n return { x: x0, y: y0, w: Math.min(W, place.x + place.w) - x0, h: Math.min(H, place.y + place.h) - y0 };\n }\n const b = region.box;\n return { x: place.x + b.x * place.w, y: place.y + b.y * place.h, w: b.w * place.w, h: b.h * place.h };\n }\n\n /**\n * Draws one frame. `place` is where the picture lands in the frame (`fitRect`), `dpr` the device pixel ratio.\n * Returns `true` once the exit is over (the canvas is cleared).\n */\n draw(ctx: CanvasRenderingContext2D, W: number, H: number, place: Rect, now: number, dpr = 1): boolean {\n const pos = this.motion.at(now);\n ctx.globalCompositeOperation = \"source-over\";\n ctx.globalAlpha = 1;\n ctx.clearRect(0, 0, W, H);\n if (pos.done) return true;\n if (pos.alpha <= 0.001) return false;\n\n const t = this.mixWeight(now);\n const a0 = this.area(this.prev, place, W, H), a1 = this.area(this.next, place, W, H);\n const A: Rect = { x: a0.x + (a1.x - a0.x) * t, y: a0.y + (a1.y - a0.y) * t, w: a0.w + (a1.w - a0.w) * t, h: a0.h + (a1.h - a0.h) * t };\n const rgb = this.rgb();\n const pad = 10 * dpr, top = A.y - pad, tall = A.h + 2 * pad;\n\n ctx.globalAlpha = pos.alpha;\n if (this.reduced) {\n ctx.fillStyle = `rgba(${rgb},0.22)`;\n ctx.fillRect(0, 0, W, H);\n } else {\n const x = A.x + pos.s * A.w, back = -pos.dir;\n const light = this.look !== \"soft\";\n const painted = this.onLight;\n if (this.look === \"soft\") {\n ctx.fillStyle = \"rgba(255,255,255,0.10)\";\n ctx.fillRect(0, 0, W, H);\n const T = A.w * 0.22, g = ctx.createLinearGradient(x + back * T, 0, x, 0);\n g.addColorStop(0, `rgba(${rgb},0)`);\n g.addColorStop(1, `rgba(${rgb},0.22)`);\n ctx.fillStyle = g;\n ctx.fillRect(Math.min(x, x + back * T), top, T, tall);\n }\n if (this.look === \"prism\") {\n FRINGES.forEach((c, i) => {\n ctx.fillStyle = `rgba(${c},${0.55 - i * 0.04})`;\n ctx.fillRect(x + back * (6 + i * 7) * dpr * (pos.finishing ? 1.5 : 1) - 2.5 * dpr, top, 5 * dpr, tall);\n });\n }\n const gw = (light ? 18 : 26) * dpr;\n const g = ctx.createLinearGradient(x - gw, 0, x + gw, 0);\n g.addColorStop(0, `rgba(${rgb},0)`);\n g.addColorStop(0.5, `rgba(${rgb},${painted ? 0.45 : light ? 0.85 : 0.75})`);\n g.addColorStop(1, `rgba(${rgb},0)`);\n ctx.fillStyle = g;\n ctx.fillRect(x - gw, top, 2 * gw, tall);\n ctx.fillStyle = painted ? `rgba(${ON_LIGHT.core},0.85)` : \"rgba(255,255,255,0.95)\";\n ctx.fillRect(x - dpr, top, 2 * dpr, tall);\n ctx.fillStyle = painted ? `rgba(${ON_LIGHT.core},0.7)` : \"rgba(255,245,225,0.9)\";\n const sparks = light ? 10 : 14;\n for (let i = 0; i < sparks; i++) {\n ctx.globalAlpha = pos.alpha * Math.random();\n ctx.fillRect(x + (Math.random() - 0.5) * 16 * dpr, A.y + Math.random() * A.h, 1.6 * dpr, 1.6 * dpr);\n }\n ctx.globalAlpha = pos.alpha;\n }\n\n // cut to the area: the old region fading into the new one\n const layer = this.maskOf(W, H, place, t);\n if (layer) {\n ctx.globalAlpha = 1;\n ctx.globalCompositeOperation = \"destination-in\";\n ctx.drawImage(layer, 0, 0);\n ctx.globalCompositeOperation = \"source-over\";\n }\n return false;\n }\n\n private maskOf(W: number, H: number, place: Rect, t: number): HTMLCanvasElement | null {\n if (!this.prev.mask && !this.next.mask) return null; // the whole frame: nothing to cut\n if (typeof document === \"undefined\") return null;\n this.maskLayer ??= document.createElement(\"canvas\");\n const m = this.maskLayer;\n if (m.width !== W || m.height !== H) { m.width = W; m.height = H; }\n const x = m.getContext(\"2d\");\n if (!x) return null;\n x.globalAlpha = 1;\n x.clearRect(0, 0, W, H);\n for (const [r, a] of [[this.prev, 1 - t], [this.next, t]] as const) {\n if (a <= 0) continue;\n x.globalAlpha = a;\n if (r.mask) x.drawImage(r.mask, place.x, place.y, place.w, place.h);\n else x.fillRect(0, 0, W, H);\n }\n x.globalAlpha = 1;\n return m;\n }\n}\n","\"use client\";\n\nimport { useEffect, useRef, useState } from \"react\";\nimport { fitRect, naturalSize, type LoaderMaskSource, type LoaderPhase, type LoaderProps } from \"../index\";\nimport {\n alphaBox,\n alphaCanvas,\n changedBits,\n coverageOf,\n isLearnable,\n meanLuma,\n meanColour,\n orBits,\n recallMask,\n rememberMask,\n snapshotPicture,\n softAlpha,\n type Snapshot,\n UNRELIABLE_COVERAGE,\n} from \"../mask\";\nimport { BEAM_FINISH_MS, BeamEngine, type BeamLook, type BeamTone } from \"./engine\";\n\n/** How often the picture is compared with its first look while loading. */\nexport const LEARN_EVERY_MS = 250;\n/** A learned area moves on screen at most this often... */\nexport const REGION_STEADY_MS = 600;\n/** ...and only when an edge moves out by more than this share of the picture (an area only grows within an episode). */\nexport const REGION_MIN_MOVE = 0.02;\n/** A surface brighter than this (0–1) is light: the beam is painted on it, not added as light. */\nexport const LIGHT_SURFACE = 0.62;\n/** The beam's canvas never has more pixels than this (it is soft: CSS scales it up). */\nexport const MAX_CANVAS_PIXELS = 2_500_000;\n\nexport interface BeamLoaderProps extends LoaderProps {\n /** `\"beam\"` (default): light only. `\"soft\"`: a faint haze and a trail, painted on. `\"prism\"`: rainbow fringes. */\n look?: BeamLook;\n /** The light's colour. `\"auto\"` (default): a light version of the colour of the part that changes. */\n tone?: BeamTone;\n}\n\n/** A light version of a colour: the beam reads as light on that material (warm on wood, mint on a green cup). */\nfunction lightTone([r, g, b]: [number, number, number]): string {\n return [r, g, b].map((v) => Math.round(v + (255 - v) * 0.6)).join(\",\");\n}\n\n/**\n * The beam loader: a soft line of light gliding over the part of the picture that is changing, while its next\n * render is on the way; one last pass across and a fade once it has caught up.\n *\n * Fills its positioned parent (put it over the picture's `<img>`). It finds the part that changes by itself: give it\n * `picture` (the `<img>` on screen) and a `maskKey` (the mockup id) and it compares frames, remembering the area per\n * scene, so the next edit starts on the right part at once. Until it knows, it covers the whole picture.\n *\n * Machine-readable state: `data-sc-loader=\"beam\"`, `data-sc-loader-phase` (`loading` | `finishing`),\n * `data-sc-loader-mask` (`whole` | `learned` | `given`).\n */\nexport function BeamLoader({\n visible,\n onExited,\n picture,\n maskKey,\n fit = \"cover\",\n mask,\n look = \"beam\",\n tone = \"auto\",\n className,\n style,\n}: BeamLoaderProps) {\n const canvasRef = useRef<HTMLCanvasElement | null>(null);\n const engineRef = useRef<BeamEngine | null>(null);\n // one last comparison when the picture has caught up (set by the mount effect)\n const learnNowRef = useRef<() => void>(() => {});\n const [phase, setPhase] = useState<LoaderPhase>(\"loading\");\n const [maskSource, setMaskSource] = useState<LoaderMaskSource>(mask ? \"given\" : \"whole\");\n\n // the latest props, for the loops below (they are set up once)\n const live = useRef({ picture, maskKey, fit, onExited });\n live.current = { picture, maskKey, fit, onExited };\n\n // the area on screen, stamped for agents: data-sc-loader-area (picture space) and data-sc-loader-surface\n const [area, setArea] = useState<string | undefined>(undefined);\n const [surface, setSurface] = useState<\"light\" | \"dark\" | undefined>(undefined);\n\n useEffect(() => {\n const now = () => (typeof performance !== \"undefined\" ? performance.now() : Date.now());\n const reduced = typeof matchMedia === \"function\" && matchMedia(\"(prefers-reduced-motion: reduce)\").matches;\n const engine = new BeamEngine({ look, tone, reducedMotion: reduced }, now());\n engineRef.current = engine;\n const pic = () => live.current.picture?.() ?? null;\n\n // What this episode knows. `base` is the first look the frames are compared with; `shown` the area on screen.\n const L = {\n key: live.current.maskKey,\n base: null as Snapshot | null,\n bits: null as Uint8Array | null,\n natural: null as { w: number; h: number } | null,\n lastSrc: \"\",\n lastSnap: null as Snapshot | null,\n shown: null as { x: number; y: number; w: number; h: number } | null,\n shownAt: -Infinity,\n };\n const srcOf = (el: ReturnType<typeof pic>) =>\n el && typeof HTMLImageElement !== \"undefined\" && el instanceof HTMLImageElement ? el.currentSrc || el.src : \"\";\n // A new snapshot only when a new picture is on screen (a frame arrived): reading a 5K frame is not free.\n const look_ = () => {\n const el = pic();\n if (!el) return null;\n const n = naturalSize(el);\n if (n.w > 0) L.natural = n;\n const src = srcOf(el);\n if (src && src === L.lastSrc && L.lastSnap) return L.lastSnap;\n const snap = snapshotPicture(el);\n if (snap) { L.lastSrc = src; L.lastSnap = snap; }\n return snap;\n };\n const surfaceOf = (s: Snapshot | null, alpha?: Float32Array | null) => (s ? (meanLuma(s, alpha) > LIGHT_SURFACE ? \"light\" : \"dark\") : undefined);\n\n /** Shows a learned area. Steady on purpose: a small change, or one sooner than REGION_STEADY_MS after the last,\n * keeps the area on screen as it is (it moving under the beam read as the beam fading out and back elsewhere). */\n const apply = (bits: Uint8Array, w: number, h: number, from: Snapshot | null, source: LoaderMaskSource, force = false) => {\n const alpha = softAlpha(bits, w, h, { grow: 4, blur: 6 });\n const box = alphaBox(alpha, w, h);\n if (!box) return;\n const t = now();\n if (!force) {\n if (engine.motion.finishing) return;\n const p = L.shown;\n if (p) {\n // only outward: the area grows as more of the picture is seen changing, never twitches back in\n const moved = Math.max(p.x - box.x, p.y - box.y, box.x + box.w - p.x - p.w, box.y + box.h - p.y - p.h);\n if (moved < REGION_MIN_MOVE || t - L.shownAt < REGION_STEADY_MS) return;\n }\n }\n const colour = from ? meanColour(from, bits) : null;\n const surf = surfaceOf(from, alpha);\n engine.setRegion({ mask: alphaCanvas(alpha, w, h), box, tone: colour ? lightTone(colour) : undefined, surface: surf }, t);\n L.shown = box;\n L.shownAt = t;\n setMaskSource(source);\n setArea([box.x, box.y, box.w, box.h].map((v) => v.toFixed(3)).join(\",\"));\n setSurface(surf);\n };\n /** Starts over on what is known about the scene now on screen (mount, or a colour switch swapped the picture). */\n const startOn = (snap: Snapshot | null) => {\n L.key = live.current.maskKey;\n L.base = snap;\n L.bits = null;\n L.shown = null;\n const known = snap && L.key ? recallMask(L.key, snap.w, snap.h) : null;\n if (known && snap) {\n L.bits = known;\n apply(known, snap.w, snap.h, snap, \"learned\", true);\n } else {\n const surf = surfaceOf(snap);\n engine.setRegion({ mask: null, box: null, surface: surf }, now());\n setMaskSource(\"whole\");\n setArea(undefined);\n setSurface(surf);\n }\n };\n\n if (mask) {\n // a given mask: its alpha is the area\n const s = snapshotPicture(mask);\n if (s) {\n const bits = new Uint8Array(s.w * s.h);\n for (let i = 0; i < bits.length; i++) bits[i] = s.rgba[i * 4 + 3] > 127 ? 1 : 0;\n apply(bits, s.w, s.h, look_(), \"given\", true);\n }\n } else {\n startOn(look_());\n }\n\n // Compare what is on screen now with the first look: the difference is the part that changes.\n const learn = () => {\n if (mask) return;\n const cur = look_();\n if (!cur) return;\n if (live.current.maskKey !== L.key) return startOn(cur); // another scene (a colour switch): never learn across\n if (!L.base) return startOn(cur); // the picture was not readable at the start: begin now, with what is known\n const bits = changedBits(L.base, cur);\n if (!bits || coverageOf(bits) > UNRELIABLE_COVERAGE) {\n // another picture, not an edit of the first look (the store picture vs the first live frame, encoded, sized or\n // cropped differently): compare later frames with this one instead\n L.base = cur;\n return;\n }\n if (!isLearnable(bits)) return;\n const key = L.key;\n const all = key ? rememberMask(key, cur.w, cur.h, bits) : L.bits ? orBits(L.bits, bits) : bits;\n L.bits = all;\n apply(all, cur.w, cur.h, L.base, \"learned\");\n };\n const learnTimer = setInterval(learn, LEARN_EVERY_MS);\n\n let raf = 0;\n const draw = () => {\n const c = canvasRef.current;\n const ctx = c?.getContext(\"2d\");\n if (!c || !ctx) return; // no 2D canvas here (a test DOM): nothing to draw, and no point asking again\n raf = requestAnimationFrame(draw);\n // device pixels, capped: the beam is soft, so a 5K frame needs no 15-megapixel canvas redrawn every frame\n const dpr = Math.min(2, (typeof window !== \"undefined\" && window.devicePixelRatio) || 1);\n const css = Math.max(1, c.clientWidth * c.clientHeight);\n const scale = Math.min(dpr, Math.sqrt(MAX_CANVAS_PIXELS / css));\n const W = Math.max(1, Math.round(c.clientWidth * scale)), H = Math.max(1, Math.round(c.clientHeight * scale));\n if (c.width !== W || c.height !== H) { c.width = W; c.height = H; }\n const n = L.natural;\n const place = n ? fitRect(n.w, n.h, W, H, live.current.fit) : { x: 0, y: 0, w: W, h: H };\n engine.draw(ctx, W, H, place, now(), scale);\n if (c.style.mixBlendMode !== engine.blendMode) c.style.mixBlendMode = engine.blendMode;\n };\n if (typeof requestAnimationFrame === \"function\") raf = requestAnimationFrame(draw);\n\n learnNowRef.current = learn;\n return () => {\n clearInterval(learnTimer);\n if (typeof cancelAnimationFrame === \"function\") cancelAnimationFrame(raf);\n engineRef.current = null;\n learnNowRef.current = () => {};\n };\n // set up once per mount: a loader lives for one render-in-flight episode\n // eslint-disable-next-line react-hooks/exhaustive-deps\n }, []);\n\n // caught up → the last pass, then onExited (a timer, so a hidden tab without animation frames still exits)\n useEffect(() => {\n const engine = engineRef.current;\n if (!engine) return;\n const now = typeof performance !== \"undefined\" ? performance.now() : Date.now();\n if (!visible) {\n learnNowRef.current();\n engine.finish(now);\n setPhase(\"finishing\");\n const t = setTimeout(() => live.current.onExited?.(), BEAM_FINISH_MS);\n return () => clearTimeout(t);\n }\n if (engine.motion.finishing) {\n engine.resume(now);\n setPhase(\"loading\");\n }\n return undefined;\n }, [visible]);\n\n return (\n <canvas\n ref={canvasRef}\n aria-hidden=\"true\"\n data-sc-loader=\"beam\"\n data-sc-loader-look={look}\n data-sc-loader-phase={phase}\n data-sc-loader-mask={maskSource}\n data-sc-loader-area={area}\n data-sc-loader-surface={surface}\n className={className}\n style={{\n position: \"absolute\",\n inset: 0,\n width: \"100%\",\n height: \"100%\",\n pointerEvents: \"none\",\n // the starting blend; the draw loop keeps it in step with the beam (painted on a learned light surface)\n mixBlendMode: look === \"soft\" ? \"normal\" : \"screen\",\n ...style,\n }}\n />\n );\n}\n"]}
|