@openartifactsai/motion-stage 0.0.0-stage → 0.23.0-rc.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/LICENSE +202 -0
- package/NOTICE +5 -0
- package/README.md +5 -2
- package/package.json +70 -4
- package/src/build.d.ts +18 -0
- package/src/build.js +73 -0
- package/src/host.d.ts +37 -0
- package/src/host.js +76 -0
- package/src/index.d.ts +11 -0
- package/src/index.js +6 -0
- package/src/layers.d.ts +30 -0
- package/src/layers.js +56 -0
- package/src/offline/audio.d.ts +19 -0
- package/src/offline/audio.js +20 -0
- package/src/offline/data.d.ts +26 -0
- package/src/offline/data.js +71 -0
- package/src/offline/main.d.ts +9 -0
- package/src/offline/main.js +52 -0
- package/src/offline/player.d.ts +23 -0
- package/src/offline/player.js +157 -0
- package/src/protocol/clip-guards.d.ts +5 -0
- package/src/protocol/clip-guards.js +22 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +257 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +100 -0
- package/src/protocol/layers.d.ts +135 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +340 -0
- package/src/protocol/types.js +7 -0
- package/src/protocol.d.ts +2 -0
- package/src/protocol.js +2 -0
- package/src/raster.d.ts +4 -0
- package/src/raster.js +2 -0
- package/src/runtime/assets.d.ts +19 -0
- package/src/runtime/assets.js +63 -0
- package/src/runtime/caption-box.d.ts +3 -0
- package/src/runtime/caption-box.js +16 -0
- package/src/runtime/captions.d.ts +31 -0
- package/src/runtime/captions.js +115 -0
- package/src/runtime/clip-captions.d.ts +9 -0
- package/src/runtime/clip-captions.js +27 -0
- package/src/runtime/clips.d.ts +83 -0
- package/src/runtime/clips.js +259 -0
- package/src/runtime/console.d.ts +13 -0
- package/src/runtime/console.js +99 -0
- package/src/runtime/cut.d.ts +7 -0
- package/src/runtime/cut.js +63 -0
- package/src/runtime/determinism.d.ts +12 -0
- package/src/runtime/determinism.js +47 -0
- package/src/runtime/film.d.ts +43 -0
- package/src/runtime/film.js +31 -0
- package/src/runtime/focus.d.ts +8 -0
- package/src/runtime/focus.js +71 -0
- package/src/runtime/fonts.d.ts +22 -0
- package/src/runtime/fonts.js +109 -0
- package/src/runtime/frame.d.ts +9 -0
- package/src/runtime/frame.js +140 -0
- package/src/runtime/fx/backdrop.d.ts +60 -0
- package/src/runtime/fx/backdrop.js +264 -0
- package/src/runtime/fx/color.d.ts +19 -0
- package/src/runtime/fx/color.js +84 -0
- package/src/runtime/fx/css-color.d.ts +6 -0
- package/src/runtime/fx/css-color.js +44 -0
- package/src/runtime/fx/curve.d.ts +12 -0
- package/src/runtime/fx/curve.js +34 -0
- package/src/runtime/fx/gl.d.ts +29 -0
- package/src/runtime/fx/gl.js +207 -0
- package/src/runtime/fx/glsl.d.ts +35 -0
- package/src/runtime/fx/glsl.js +113 -0
- package/src/runtime/fx/index.d.ts +23 -0
- package/src/runtime/fx/index.js +151 -0
- package/src/runtime/fx/key.d.ts +11 -0
- package/src/runtime/fx/key.js +21 -0
- package/src/runtime/fx/numbers.d.ts +14 -0
- package/src/runtime/fx/numbers.js +57 -0
- package/src/runtime/fx/pace.d.ts +17 -0
- package/src/runtime/fx/pace.js +26 -0
- package/src/runtime/fx/paint.d.ts +28 -0
- package/src/runtime/fx/paint.js +150 -0
- package/src/runtime/guard.d.ts +4 -0
- package/src/runtime/guard.js +141 -0
- package/src/runtime/hand-titles/index.d.ts +9 -0
- package/src/runtime/hand-titles/index.js +73 -0
- package/src/runtime/hand-titles/layer.d.ts +20 -0
- package/src/runtime/hand-titles/layer.js +118 -0
- package/src/runtime/hand-titles/place.d.ts +27 -0
- package/src/runtime/hand-titles/place.js +45 -0
- package/src/runtime/held-store.d.ts +35 -0
- package/src/runtime/held-store.js +63 -0
- package/src/runtime/held.d.ts +19 -0
- package/src/runtime/held.js +225 -0
- package/src/runtime/kit/camera.d.ts +59 -0
- package/src/runtime/kit/camera.js +81 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -0
- package/src/runtime/kit/count.d.ts +37 -0
- package/src/runtime/kit/count.js +100 -0
- package/src/runtime/kit/cursor.d.ts +46 -0
- package/src/runtime/kit/cursor.js +108 -0
- package/src/runtime/kit/dom.d.ts +14 -0
- package/src/runtime/kit/dom.js +46 -0
- package/src/runtime/kit/geometry.d.ts +24 -0
- package/src/runtime/kit/geometry.js +56 -0
- package/src/runtime/kit/index.d.ts +113 -0
- package/src/runtime/kit/index.js +157 -0
- package/src/runtime/kit/mark-strokes.d.ts +65 -0
- package/src/runtime/kit/mark-strokes.js +226 -0
- package/src/runtime/kit/marks.d.ts +21 -0
- package/src/runtime/kit/marks.js +165 -0
- package/src/runtime/kit/math.d.ts +23 -0
- package/src/runtime/kit/math.js +35 -0
- package/src/runtime/kit/paper.d.ts +7 -0
- package/src/runtime/kit/paper.js +27 -0
- package/src/runtime/kit/random.d.ts +8 -0
- package/src/runtime/kit/random.js +32 -0
- package/src/runtime/kit/text-fx.d.ts +18 -0
- package/src/runtime/kit/text-fx.js +55 -0
- package/src/runtime/kit/typing.d.ts +21 -0
- package/src/runtime/kit/typing.js +42 -0
- package/src/runtime/laid/layer.d.ts +44 -0
- package/src/runtime/laid/layer.js +370 -0
- package/src/runtime/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +123 -0
- package/src/runtime/layers/item.js +150 -0
- package/src/runtime/layers/parts.d.ts +53 -0
- package/src/runtime/layers/parts.js +158 -0
- package/src/runtime/layers/rows.d.ts +4 -0
- package/src/runtime/layers/rows.js +233 -0
- package/src/runtime/layers/scan.d.ts +24 -0
- package/src/runtime/layers/scan.js +204 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +337 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +267 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +90 -0
- package/src/runtime/modules.d.ts +23 -0
- package/src/runtime/modules.js +53 -0
- package/src/runtime/port.d.ts +8 -0
- package/src/runtime/port.js +46 -0
- package/src/runtime/questions.d.ts +24 -0
- package/src/runtime/questions.js +162 -0
- package/src/runtime/recut.d.ts +81 -0
- package/src/runtime/recut.js +325 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +358 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +171 -0
- package/src/runtime/retouch/entry.d.ts +77 -0
- package/src/runtime/retouch/entry.js +1 -0
- package/src/runtime/retouch/judge.d.ts +25 -0
- package/src/runtime/retouch/judge.js +209 -0
- package/src/runtime/retouch/look.d.ts +39 -0
- package/src/runtime/retouch/look.js +135 -0
- package/src/runtime/retouch/own-styles.d.ts +19 -0
- package/src/runtime/retouch/own-styles.js +141 -0
- package/src/runtime/retouch/parts.d.ts +22 -0
- package/src/runtime/retouch/parts.js +166 -0
- package/src/runtime/retouch/retouch.d.ts +37 -0
- package/src/runtime/retouch/retouch.js +238 -0
- package/src/runtime/retouch/seat.d.ts +40 -0
- package/src/runtime/retouch/seat.js +228 -0
- package/src/runtime/retouch/state.d.ts +37 -0
- package/src/runtime/retouch/state.js +153 -0
- package/src/runtime/retouch/type-style.d.ts +19 -0
- package/src/runtime/retouch/type-style.js +111 -0
- package/src/runtime/scan.d.ts +2 -0
- package/src/runtime/scan.js +49 -0
- package/src/runtime/scenes/card.d.ts +12 -0
- package/src/runtime/scenes/card.js +56 -0
- package/src/runtime/scenes/install.d.ts +69 -0
- package/src/runtime/scenes/install.js +528 -0
- package/src/runtime/scenes/read.d.ts +27 -0
- package/src/runtime/scenes/read.js +73 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +46 -0
- package/src/runtime/score.d.ts +32 -0
- package/src/runtime/score.js +262 -0
- package/src/runtime/sketch/pen.d.ts +3 -0
- package/src/runtime/sketch/pen.js +371 -0
- package/src/runtime/sketch/strokes.d.ts +41 -0
- package/src/runtime/sketch/strokes.js +187 -0
- package/src/runtime/snapshot/draw.d.ts +14 -0
- package/src/runtime/snapshot/draw.js +125 -0
- package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
- package/src/runtime/snapshot/hold-first-draw.js +128 -0
- package/src/runtime/snapshot/lift.d.ts +41 -0
- package/src/runtime/snapshot/lift.js +161 -0
- package/src/runtime/snapshot/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +51 -0
- package/src/runtime/snapshot/serialize.js +286 -0
- package/src/runtime/snapshot/styles.d.ts +8 -0
- package/src/runtime/snapshot/styles.js +109 -0
- package/src/runtime/snapshot/xml.d.ts +6 -0
- package/src/runtime/snapshot/xml.js +110 -0
- package/src/runtime/stage-size.d.ts +7 -0
- package/src/runtime/stage-size.js +3 -0
- package/src/runtime/stage.d.ts +27 -0
- package/src/runtime/stage.js +154 -0
- package/src/runtime/stretch.d.ts +13 -0
- package/src/runtime/stretch.js +16 -0
- package/src/runtime/texts.d.ts +24 -0
- package/src/runtime/texts.js +211 -0
- package/src/runtime/video-sync.d.ts +51 -0
- package/src/runtime/video-sync.js +218 -0
- package/src/stills.d.ts +90 -0
- package/src/stills.js +329 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/levels.d.ts +8 -0
- package/src/tools/levels.js +28 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +223 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +35 -0
- package/src/tools/types.d.ts +84 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +6 -0
- package/src/tools.js +4 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
import { filmTime, realNow } from "./determinism.js";
|
|
2
|
+
import { fxPaintAt } from "./fx/index.js";
|
|
3
|
+
import { film, filmAt } from "./film.js";
|
|
4
|
+
import { heldForPull, heldPulled } from "./held.js";
|
|
5
|
+
import { say } from "./port.js";
|
|
6
|
+
import { RenderStuck, afterSample, holdStage, nextTask, releaseStage, renderAt, renderState, rendersSettle, syncClips, takeTurn, unsettledNow } from "./render.js";
|
|
7
|
+
import { scanStage } from "./scan.js";
|
|
8
|
+
import { liftableBackdrop, liftedPicture } from "./snapshot/lift.js";
|
|
9
|
+
import { PLAIN_INK, forcedColorsActive, inOwnColors, serializeForRaster, svgForRaster } from "./snapshot/serialize.js";
|
|
10
|
+
import { builtStage } from "./stage.js";
|
|
11
|
+
import { stageSize } from "./stage-size.js";
|
|
12
|
+
import { clipsBehind, warnOnce } from "./video-sync.js";
|
|
13
|
+
/** A pull that only inspects the picture: past this, the page could not use it anyway. */
|
|
14
|
+
export const SMALL_SNAPSHOT_MAX_CHARS = 1000000;
|
|
15
|
+
/** With the pull's own render (up to 10 s) and its clips, a pull fits the host's 15 s for a frame. */
|
|
16
|
+
const REDRAW_BUDGET_MS = 12000;
|
|
17
|
+
const MOST_PASSES = 3;
|
|
18
|
+
function snapshot(ask, scale, t) {
|
|
19
|
+
scanStage();
|
|
20
|
+
fxPaintAt(scale);
|
|
21
|
+
const parts = builtStage();
|
|
22
|
+
const { width, height } = stageSize;
|
|
23
|
+
// The snapshot lacks the stage root, so under forced colors it is given the root's text color.
|
|
24
|
+
const { ink, xml, under } = inOwnColors(() => {
|
|
25
|
+
// An export may take the backdrop out and draw it under the snapshot (see snapshot/lift.ts).
|
|
26
|
+
const backdrop = ask.lift === true ? liftableBackdrop(parts.stage, width, height) : null;
|
|
27
|
+
const lifted = backdrop === null ? null : liftedPicture(backdrop, parts.stage, width, Math.max(1, Math.round(width * scale)), Math.max(1, Math.round(height * scale)));
|
|
28
|
+
return {
|
|
29
|
+
under: lifted,
|
|
30
|
+
ink: forcedColorsActive() ? getComputedStyle(document.documentElement).color || PLAIN_INK : undefined,
|
|
31
|
+
xml: serializeForRaster(parts.stage, {
|
|
32
|
+
width,
|
|
33
|
+
height,
|
|
34
|
+
mediaScale: ask.smallMedia ? scale : 1,
|
|
35
|
+
fx: ask.readFx ? null : parts.fxWatch,
|
|
36
|
+
smoothAt: ask.smooth ? scale : 0,
|
|
37
|
+
warn: warnOnce,
|
|
38
|
+
lift: lifted === null ? null : backdrop,
|
|
39
|
+
}),
|
|
40
|
+
};
|
|
41
|
+
});
|
|
42
|
+
const made = svgForRaster(xml, width, height, scale, ink);
|
|
43
|
+
if (ask.smallMedia && made.svg.length > SMALL_SNAPSHOT_MAX_CHARS) {
|
|
44
|
+
under?.close();
|
|
45
|
+
throw new Error('the stage is too large to inspect: its snapshot is ' + made.svg.length + ' characters');
|
|
46
|
+
}
|
|
47
|
+
return { type: 'frame', seq: ask.seq, t, svg: made.svg, pw: made.pw, ph: made.ph, ...(under === null ? {} : { under }) };
|
|
48
|
+
}
|
|
49
|
+
const failure = (error) => (error instanceof Error ? error : new Error(String(error)));
|
|
50
|
+
/** Draws the asked moment and answers with its snapshot, after any live render still painting has settled. */
|
|
51
|
+
export function pullFrame(ask) {
|
|
52
|
+
const scale = ask.scale > 0 && ask.scale <= 4 ? ask.scale : 1;
|
|
53
|
+
takeTurn(async () => {
|
|
54
|
+
if (!film.loaded) {
|
|
55
|
+
say({ type: 'frame-error', seq: ask.seq, hold: true, env: true, message: 'the film is not loaded' });
|
|
56
|
+
return;
|
|
57
|
+
}
|
|
58
|
+
const began = realNow();
|
|
59
|
+
holdStage();
|
|
60
|
+
let rendered = false;
|
|
61
|
+
try {
|
|
62
|
+
const wait = rendersSettle();
|
|
63
|
+
if (wait)
|
|
64
|
+
await wait;
|
|
65
|
+
// A scene coming on here comes on over a picture of the scene before, drawn at this frame's own scale.
|
|
66
|
+
await heldForPull(filmAt(ask.t).T, scale, ask);
|
|
67
|
+
/*
|
|
68
|
+
* A render a wait gave up on may settle while this pull draws and paint its own moment over this one: then
|
|
69
|
+
* this moment is drawn again, twice at most. Only the renders pending as the pull begins count.
|
|
70
|
+
*/
|
|
71
|
+
const before = unsettledNow();
|
|
72
|
+
let kept = null;
|
|
73
|
+
let late = true;
|
|
74
|
+
let drawnAt = filmTime();
|
|
75
|
+
for (let passes = 0; late && passes < MOST_PASSES; passes++) {
|
|
76
|
+
const still = before.filter((entry) => !entry.done);
|
|
77
|
+
const passBegan = realNow();
|
|
78
|
+
let awaited = false;
|
|
79
|
+
try {
|
|
80
|
+
rendered = false;
|
|
81
|
+
const sampled = renderState.gavePromise;
|
|
82
|
+
let drawn = renderAt(ask.t);
|
|
83
|
+
if (!renderState.ok)
|
|
84
|
+
throw failure(renderState.error);
|
|
85
|
+
if (drawn) {
|
|
86
|
+
awaited = true;
|
|
87
|
+
await drawn;
|
|
88
|
+
}
|
|
89
|
+
// A sample this draw began goes first, and this moment is drawn again: nothing else draws meanwhile, since this pull holds the stage.
|
|
90
|
+
const again = afterSample(sampled, ask.t);
|
|
91
|
+
if (again) {
|
|
92
|
+
awaited = true;
|
|
93
|
+
drawn = (await again).q;
|
|
94
|
+
if (!renderState.ok)
|
|
95
|
+
throw failure(renderState.error);
|
|
96
|
+
if (drawn)
|
|
97
|
+
await drawn;
|
|
98
|
+
}
|
|
99
|
+
rendered = true;
|
|
100
|
+
drawnAt = filmTime();
|
|
101
|
+
// A small frame waits at most SMALL_CLIP_WAIT_MS for a clip, and says when one was still on its way.
|
|
102
|
+
const clips = syncClips('frame', ask.smallMedia);
|
|
103
|
+
if (clips) {
|
|
104
|
+
awaited = true;
|
|
105
|
+
await clips;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
// A redraw that fails answers with the pass before; one that never settled may still paint, so it fails the pull.
|
|
110
|
+
if (kept === null || error instanceof RenderStuck)
|
|
111
|
+
throw error;
|
|
112
|
+
break;
|
|
113
|
+
}
|
|
114
|
+
// A render is marked done a few promise steps after it paints.
|
|
115
|
+
if (still.length && awaited)
|
|
116
|
+
await nextTask();
|
|
117
|
+
late = awaited && still.some((entry) => entry.done) && 2 * realNow() - began - passBegan < REDRAW_BUDGET_MS;
|
|
118
|
+
kept = late ? snapshot(ask, scale, drawnAt) : null;
|
|
119
|
+
}
|
|
120
|
+
const out = kept ?? snapshot(ask, scale, drawnAt);
|
|
121
|
+
const clipLate = ask.smallMedia && clipsBehind();
|
|
122
|
+
say({ ...out, ms: Math.round(realNow() - began), ...(clipLate ? { clipLate } : {}) });
|
|
123
|
+
}
|
|
124
|
+
catch (error) {
|
|
125
|
+
const fatal = error instanceof RenderStuck;
|
|
126
|
+
const message = String(error instanceof Error ? error.message : error).slice(0, 300);
|
|
127
|
+
say({
|
|
128
|
+
type: 'frame-error',
|
|
129
|
+
seq: ask.seq,
|
|
130
|
+
hold: !fatal,
|
|
131
|
+
env: fatal && error.inBackground,
|
|
132
|
+
message: rendered || fatal ? message : 'render(' + filmTime().toFixed(3) + ') threw: ' + message,
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
finally {
|
|
136
|
+
heldPulled();
|
|
137
|
+
releaseStage();
|
|
138
|
+
}
|
|
139
|
+
});
|
|
140
|
+
}
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import type { GlPainter } from './gl.js';
|
|
2
|
+
import type { KeyContext } from './key.js';
|
|
3
|
+
import type { Frame } from './paint.js';
|
|
4
|
+
export type PaintDraw = (c: CanvasRenderingContext2D, data: unknown, f: Frame) => void;
|
|
5
|
+
export type ThingDraw = (c: CanvasRenderingContext2D, o: Record<string, unknown>, f: Frame) => void;
|
|
6
|
+
/** What the backdrops need of the stage around them. */
|
|
7
|
+
export interface FxHost {
|
|
8
|
+
frame(): number;
|
|
9
|
+
filmTime(): number;
|
|
10
|
+
reseed(seed: number): void;
|
|
11
|
+
keys: Omit<KeyContext, 'frame'>;
|
|
12
|
+
/** null: no WebGL here, and shader steps draw nothing. */
|
|
13
|
+
gl: GlPainter | null;
|
|
14
|
+
/** Each fault is told once. */
|
|
15
|
+
tell(message: string): void;
|
|
16
|
+
}
|
|
17
|
+
/** How a backdrop is painted this time. */
|
|
18
|
+
export interface PaintMode {
|
|
19
|
+
pw: number;
|
|
20
|
+
ph: number;
|
|
21
|
+
/** Shaders show the picture they drew last, if they have one. */
|
|
22
|
+
hold: boolean;
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* One scene's painted picture: a canvas under everything else in its box. Calls in render only record steps; the
|
|
26
|
+
* stage paints them afterwards, at the size the picture is shown or exported at.
|
|
27
|
+
*/
|
|
28
|
+
export declare class Backdrop {
|
|
29
|
+
readonly canvas: HTMLCanvasElement;
|
|
30
|
+
readonly label: string;
|
|
31
|
+
private readonly context;
|
|
32
|
+
private readonly holder;
|
|
33
|
+
private readonly standIns;
|
|
34
|
+
private steps;
|
|
35
|
+
private askedIn;
|
|
36
|
+
private drawn;
|
|
37
|
+
private broken;
|
|
38
|
+
private readonly host;
|
|
39
|
+
readonly W: number;
|
|
40
|
+
readonly H: number;
|
|
41
|
+
constructor(host: FxHost, W: number, H: number, into: HTMLElement, options: {
|
|
42
|
+
flat?: unknown;
|
|
43
|
+
label?: unknown;
|
|
44
|
+
class?: unknown;
|
|
45
|
+
}, ground: string);
|
|
46
|
+
private record;
|
|
47
|
+
/** Every stand-in out of sight until its thing is drawn this frame. */
|
|
48
|
+
hideThings(): void;
|
|
49
|
+
paintStep(draw: unknown, options: unknown): (data?: unknown) => void;
|
|
50
|
+
thingStep(name: unknown, options: unknown, draw: unknown): (props?: Record<string, unknown>) => void;
|
|
51
|
+
shaderStep(body: unknown): (values?: Record<string, unknown>) => void;
|
|
52
|
+
/** Leaves the canvas one clear pixel: its flat color shows. */
|
|
53
|
+
private blank;
|
|
54
|
+
/** Paints the steps recorded this frame, unless they and the size are what is on the canvas already. */
|
|
55
|
+
paint(mode: PaintMode): void;
|
|
56
|
+
private fail;
|
|
57
|
+
private uniformValues;
|
|
58
|
+
private drawThing;
|
|
59
|
+
private drawShader;
|
|
60
|
+
}
|
|
@@ -0,0 +1,264 @@
|
|
|
1
|
+
import { toHex, toHexAlpha } from "./color.js";
|
|
2
|
+
import { readColor } from "./css-color.js";
|
|
3
|
+
import { stepKey } from "./key.js";
|
|
4
|
+
import { mix32 } from "./numbers.js";
|
|
5
|
+
function lookOf(standIn) {
|
|
6
|
+
const cs = getComputedStyle(standIn);
|
|
7
|
+
const scale = parseFloat(cs.getPropertyValue('scale'));
|
|
8
|
+
const laid = (standIn.getAttribute('data-film-laid') ?? '').split(' ');
|
|
9
|
+
return {
|
|
10
|
+
hidden: cs.visibility === 'hidden',
|
|
11
|
+
size: Number.isFinite(scale) && scale >= 0 ? scale : 1,
|
|
12
|
+
fill: laid.includes('fill') ? toHexAlpha(readColor(cs.backgroundColor)) : null,
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
const finite = (v, otherwise) => (typeof v === 'number' && Number.isFinite(v) ? v : otherwise);
|
|
16
|
+
const slug = (name) => name.toLowerCase().replace(/[^a-z0-9]+/g, '-').replace(/^-|-$/g, '') || 'thing';
|
|
17
|
+
let shaderIds = 0;
|
|
18
|
+
/**
|
|
19
|
+
* One scene's painted picture: a canvas under everything else in its box. Calls in render only record steps; the
|
|
20
|
+
* stage paints them afterwards, at the size the picture is shown or exported at.
|
|
21
|
+
*/
|
|
22
|
+
export class Backdrop {
|
|
23
|
+
canvas;
|
|
24
|
+
label;
|
|
25
|
+
context;
|
|
26
|
+
holder;
|
|
27
|
+
standIns = new Map();
|
|
28
|
+
steps = [];
|
|
29
|
+
askedIn = -1;
|
|
30
|
+
drawn = '';
|
|
31
|
+
broken = false;
|
|
32
|
+
host;
|
|
33
|
+
W;
|
|
34
|
+
H;
|
|
35
|
+
constructor(host, W, H, into, options, ground) {
|
|
36
|
+
this.host = host;
|
|
37
|
+
this.W = W;
|
|
38
|
+
this.H = H;
|
|
39
|
+
this.label = typeof options.label === 'string' && options.label.trim() ? options.label.trim() : 'Backdrop';
|
|
40
|
+
const canvas = document.createElement('canvas');
|
|
41
|
+
canvas.width = 1;
|
|
42
|
+
canvas.height = 1;
|
|
43
|
+
canvas.setAttribute('data-film-backdrop', '');
|
|
44
|
+
canvas.setAttribute('data-label', this.label);
|
|
45
|
+
if (typeof options.class === 'string' && options.class.trim())
|
|
46
|
+
canvas.className = options.class.trim();
|
|
47
|
+
const flat = typeof options.flat === 'string' && options.flat.trim() ? options.flat.trim() : ground;
|
|
48
|
+
canvas.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;display:block;z-index:-1;pointer-events:none';
|
|
49
|
+
canvas.style.background = flat;
|
|
50
|
+
if (!canvas.style.background)
|
|
51
|
+
canvas.style.background = ground;
|
|
52
|
+
const context = canvas.getContext('2d');
|
|
53
|
+
if (!context)
|
|
54
|
+
throw new Error('this browser gives no 2d canvas');
|
|
55
|
+
const holder = document.createElement('div');
|
|
56
|
+
holder.setAttribute('data-film-things', '');
|
|
57
|
+
holder.style.cssText = 'position:absolute;left:0;top:0;width:100%;height:100%;pointer-events:none';
|
|
58
|
+
into.style.isolation = 'isolate';
|
|
59
|
+
into.insertBefore(holder, into.firstChild);
|
|
60
|
+
into.insertBefore(canvas, holder);
|
|
61
|
+
this.canvas = canvas;
|
|
62
|
+
this.context = context;
|
|
63
|
+
this.holder = holder;
|
|
64
|
+
}
|
|
65
|
+
record(step) {
|
|
66
|
+
const frame = this.host.frame();
|
|
67
|
+
if (this.askedIn !== frame) {
|
|
68
|
+
this.askedIn = frame;
|
|
69
|
+
this.steps = [];
|
|
70
|
+
}
|
|
71
|
+
this.steps.push(step);
|
|
72
|
+
}
|
|
73
|
+
/** Every stand-in out of sight until its thing is drawn this frame. */
|
|
74
|
+
hideThings() {
|
|
75
|
+
for (const standIn of this.standIns.values())
|
|
76
|
+
standIn.style.display = 'none';
|
|
77
|
+
}
|
|
78
|
+
paintStep(draw, options) {
|
|
79
|
+
if (typeof draw !== 'function')
|
|
80
|
+
throw new TypeError('bg.paint takes a function: (c, d, f) => …');
|
|
81
|
+
const still = !!options && typeof options === 'object' && Reflect.get(options, 'still') === true;
|
|
82
|
+
const paint = (c, data, f) => void Reflect.apply(draw, undefined, [c, data, f]);
|
|
83
|
+
return (data) => this.record({ kind: 'paint', draw: paint, data, still });
|
|
84
|
+
}
|
|
85
|
+
thingStep(name, options, draw) {
|
|
86
|
+
if (typeof name !== 'string' || !name)
|
|
87
|
+
throw new TypeError('bg.thing takes a name');
|
|
88
|
+
const fn = typeof draw === 'function' ? draw : typeof options === 'function' ? options : null;
|
|
89
|
+
if (!fn)
|
|
90
|
+
throw new TypeError('bg.thing takes a draw: (c, o, f) => …');
|
|
91
|
+
const label = options && typeof options === 'object' && typeof Reflect.get(options, 'label') === 'string' ? String(Reflect.get(options, 'label')) : name;
|
|
92
|
+
let standIn = this.standIns.get(name);
|
|
93
|
+
if (!standIn) {
|
|
94
|
+
standIn = document.createElement('i');
|
|
95
|
+
standIn.className = 'film-thing thing-' + slug(name);
|
|
96
|
+
standIn.setAttribute('data-film-thing', name);
|
|
97
|
+
standIn.style.cssText = 'position:absolute;display:none;opacity:0;pointer-events:auto;box-sizing:border-box';
|
|
98
|
+
this.holder.appendChild(standIn);
|
|
99
|
+
this.standIns.set(name, standIn);
|
|
100
|
+
}
|
|
101
|
+
standIn.setAttribute('data-label', label);
|
|
102
|
+
const own = standIn;
|
|
103
|
+
const thing = (c, o, f) => void Reflect.apply(fn, undefined, [c, o, f]);
|
|
104
|
+
return (given) => {
|
|
105
|
+
const props = { ...given, x: finite(given?.x, this.W / 2), y: finite(given?.y, this.H / 2), w: finite(given?.w, 100), h: finite(given?.h, 100), show: finite(given?.show, 1), turn: finite(given?.turn, 0), color: given?.color ?? '#808080' };
|
|
106
|
+
if (!(props.show > 0))
|
|
107
|
+
return;
|
|
108
|
+
const s = own.style;
|
|
109
|
+
s.left = ((props.x - props.w / 2) / this.W) * 100 + '%';
|
|
110
|
+
s.top = ((props.y - props.h / 2) / this.H) * 100 + '%';
|
|
111
|
+
s.width = (props.w / this.W) * 100 + '%';
|
|
112
|
+
s.height = (props.h / this.H) * 100 + '%';
|
|
113
|
+
s.transform = props.turn ? 'rotate(' + props.turn + 'deg)' : '';
|
|
114
|
+
s.backgroundColor = typeof props.color === 'string' ? props.color : '';
|
|
115
|
+
s.display = '';
|
|
116
|
+
this.record({ kind: 'thing', draw: thing, props, standIn: own });
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
shaderStep(body) {
|
|
120
|
+
if (typeof body !== 'string')
|
|
121
|
+
throw new TypeError('bg.shader takes the GLSL of a fragment shader as text');
|
|
122
|
+
const gl = this.host.gl;
|
|
123
|
+
if (!gl)
|
|
124
|
+
return () => undefined;
|
|
125
|
+
gl.compile(body);
|
|
126
|
+
const memo = { id: ++shaderIds, given: new Map(), held: null };
|
|
127
|
+
return (values) => {
|
|
128
|
+
if (values && typeof values === 'object')
|
|
129
|
+
for (const [name, value] of Object.entries(values))
|
|
130
|
+
memo.given.set(name, value);
|
|
131
|
+
this.record({ kind: 'shader', body, values: new Map(memo.given), memo });
|
|
132
|
+
};
|
|
133
|
+
}
|
|
134
|
+
/** Leaves the canvas one clear pixel: its flat color shows. */
|
|
135
|
+
blank() {
|
|
136
|
+
if (this.canvas.width !== 1 || this.canvas.height !== 1 || this.drawn !== '') {
|
|
137
|
+
this.canvas.width = 1;
|
|
138
|
+
this.canvas.height = 1;
|
|
139
|
+
}
|
|
140
|
+
this.drawn = '';
|
|
141
|
+
}
|
|
142
|
+
/** Paints the steps recorded this frame, unless they and the size are what is on the canvas already. */
|
|
143
|
+
paint(mode) {
|
|
144
|
+
if (this.broken || this.askedIn !== this.host.frame()) {
|
|
145
|
+
this.blank();
|
|
146
|
+
return;
|
|
147
|
+
}
|
|
148
|
+
const T = this.host.filmTime();
|
|
149
|
+
const at = (still) => (still ? '' : '@' + T);
|
|
150
|
+
const keys = { ...this.host.keys, frame: this.host.frame() };
|
|
151
|
+
const looks = new Map();
|
|
152
|
+
const shaderValues = new Map();
|
|
153
|
+
let key = mode.pw + 'x' + mode.ph;
|
|
154
|
+
try {
|
|
155
|
+
for (const step of this.steps) {
|
|
156
|
+
if (step.kind === 'paint')
|
|
157
|
+
key += '|p' + stepKey(step.data, keys) + at(step.still);
|
|
158
|
+
else if (step.kind === 'thing') {
|
|
159
|
+
const look = lookOf(step.standIn);
|
|
160
|
+
looks.set(step, look);
|
|
161
|
+
key += '|t' + stepKey(step.props, keys) + JSON.stringify(look) + at(false);
|
|
162
|
+
}
|
|
163
|
+
else {
|
|
164
|
+
const values = this.uniformValues(step.values);
|
|
165
|
+
shaderValues.set(step, values);
|
|
166
|
+
key += '|s' + step.memo.id + (mode.hold && step.memo.held ? ':held' : JSON.stringify([...values]));
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
catch (error) {
|
|
171
|
+
this.fail(error);
|
|
172
|
+
return;
|
|
173
|
+
}
|
|
174
|
+
if (key === this.drawn)
|
|
175
|
+
return;
|
|
176
|
+
const c = this.context;
|
|
177
|
+
this.canvas.width = mode.pw;
|
|
178
|
+
this.canvas.height = mode.ph;
|
|
179
|
+
const px = mode.pw / this.W;
|
|
180
|
+
const f = { W: this.W, H: this.H, pw: mode.pw, ph: mode.ph, px, T };
|
|
181
|
+
try {
|
|
182
|
+
this.host.reseed(mix32(Math.round(T * 1000)) ^ mix32(this.label.length + this.steps.length));
|
|
183
|
+
const under = readColor(getComputedStyle(this.canvas).backgroundColor);
|
|
184
|
+
if (under[3] >= 1) {
|
|
185
|
+
c.fillStyle = toHex(under);
|
|
186
|
+
c.fillRect(0, 0, mode.pw, mode.ph);
|
|
187
|
+
}
|
|
188
|
+
for (const step of this.steps) {
|
|
189
|
+
c.save();
|
|
190
|
+
c.setTransform(px, 0, 0, px, 0, 0);
|
|
191
|
+
if (step.kind === 'paint')
|
|
192
|
+
step.draw(c, step.data, step.still ? { ...f, T: 0 } : f);
|
|
193
|
+
else if (step.kind === 'thing')
|
|
194
|
+
this.drawThing(c, step.draw, step.props, looks.get(step), f);
|
|
195
|
+
else
|
|
196
|
+
this.drawShader(c, step.body, shaderValues.get(step) ?? new Map(), step.memo, mode, f);
|
|
197
|
+
c.restore();
|
|
198
|
+
}
|
|
199
|
+
this.drawn = key;
|
|
200
|
+
}
|
|
201
|
+
catch (error) {
|
|
202
|
+
this.fail(error);
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
fail(error) {
|
|
206
|
+
this.host.tell('FX.backdrop "' + this.label + '" shows its flat color: ' + String(error instanceof Error ? error.message : error));
|
|
207
|
+
this.broken = true;
|
|
208
|
+
this.blank();
|
|
209
|
+
}
|
|
210
|
+
uniformValues(given) {
|
|
211
|
+
const out = new Map();
|
|
212
|
+
for (const [name, value] of given) {
|
|
213
|
+
if (typeof value === 'string') {
|
|
214
|
+
const color = readColor(value, this.canvas);
|
|
215
|
+
out.set(name, [color[0], color[1], color[2]]);
|
|
216
|
+
}
|
|
217
|
+
else if (typeof value === 'number' || typeof value === 'boolean') {
|
|
218
|
+
if (typeof value === 'number' && !Number.isFinite(value))
|
|
219
|
+
throw new Error('uniform "' + name + '" is not a number');
|
|
220
|
+
out.set(name, value);
|
|
221
|
+
}
|
|
222
|
+
else if (Array.isArray(value) || (ArrayBuffer.isView(value) && !(value instanceof DataView))) {
|
|
223
|
+
const items = Array.isArray(value) ? value : value;
|
|
224
|
+
const list = Array.from(items, (v) => (typeof v === 'number' ? v : NaN));
|
|
225
|
+
if (!list.every(Number.isFinite))
|
|
226
|
+
throw new Error('uniform "' + name + '" is not a number');
|
|
227
|
+
out.set(name, list);
|
|
228
|
+
}
|
|
229
|
+
else
|
|
230
|
+
throw new Error('uniform "' + name + '" is not a number');
|
|
231
|
+
}
|
|
232
|
+
return out;
|
|
233
|
+
}
|
|
234
|
+
drawThing(c, draw, props, look, f) {
|
|
235
|
+
if (look?.hidden)
|
|
236
|
+
return;
|
|
237
|
+
const size = look?.size ?? 1;
|
|
238
|
+
c.translate(props.x, props.y);
|
|
239
|
+
if (props.turn)
|
|
240
|
+
c.rotate((props.turn * Math.PI) / 180);
|
|
241
|
+
if (size !== 1)
|
|
242
|
+
c.scale(size, size);
|
|
243
|
+
const color = look?.fill ?? toHex(readColor(props.color));
|
|
244
|
+
draw(c, { ...props, color }, { ...f, px: f.px * size });
|
|
245
|
+
}
|
|
246
|
+
drawShader(c, body, values, memo, mode, f) {
|
|
247
|
+
const gl = this.host.gl;
|
|
248
|
+
if (!gl)
|
|
249
|
+
return;
|
|
250
|
+
if (mode.hold && memo.held) {
|
|
251
|
+
c.drawImage(memo.held, 0, 0, f.W, f.H);
|
|
252
|
+
return;
|
|
253
|
+
}
|
|
254
|
+
const picture = gl.draw(body, f.pw, f.ph, f.W, f.H, values);
|
|
255
|
+
c.drawImage(picture, 0, 0, f.W, f.H);
|
|
256
|
+
if (!mode.hold)
|
|
257
|
+
return;
|
|
258
|
+
const held = memo.held ?? document.createElement('canvas');
|
|
259
|
+
held.width = f.pw;
|
|
260
|
+
held.height = f.ph;
|
|
261
|
+
held.getContext('2d')?.drawImage(picture, 0, 0);
|
|
262
|
+
memo.held = held;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/** Colors as [r, g, b] or [r, g, b, a], each 0..1, gamma-encoded sRGB. */
|
|
2
|
+
export type Rgb = readonly [number, number, number];
|
|
3
|
+
export type Rgba = readonly [number, number, number, number];
|
|
4
|
+
/** `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`; null for anything else. */
|
|
5
|
+
export declare function parseHex(text: string): Rgba | null;
|
|
6
|
+
/** `#rrggbb`, alpha dropped. */
|
|
7
|
+
export declare const toHex: (c: Rgb | Rgba) => string;
|
|
8
|
+
/** `#rrggbb`, or `#rrggbbaa` when the color is see-through. */
|
|
9
|
+
export declare const toHexAlpha: (c: Rgba) => string;
|
|
10
|
+
/** `rgb(…)` / `rgba(…)` with numbers 0..255 (or percentages) and an optional alpha; null for any other form. */
|
|
11
|
+
export declare function parseRgbFunction(text: string): Rgba | null;
|
|
12
|
+
export declare function toOklab(c: Rgb): Rgb;
|
|
13
|
+
export declare function fromOklab(lab: Rgb): Rgb;
|
|
14
|
+
/** `k` of the way from a to b (0 gives a, 1 gives b), blended in OKLab so the middle keeps its brightness. */
|
|
15
|
+
export declare function mixRgb(a: Rgb, b: Rgb, k: number): Rgb;
|
|
16
|
+
/** −1 is black, 0 the color itself, 1 white. */
|
|
17
|
+
export declare const shadeRgb: (c: Rgb, k: number) => Rgb;
|
|
18
|
+
/** A canvas fill style for the color at alpha `a`. */
|
|
19
|
+
export declare const rgbaText: (c: Rgb, a: number) => string;
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
const unit = (v) => (v < 0 ? 0 : v > 1 ? 1 : v);
|
|
2
|
+
const HEX = /^#([0-9a-f]{3,4}|[0-9a-f]{6}|[0-9a-f]{8})$/i;
|
|
3
|
+
/** `#rgb`, `#rgba`, `#rrggbb` or `#rrggbbaa`; null for anything else. */
|
|
4
|
+
export function parseHex(text) {
|
|
5
|
+
const found = HEX.exec(text.trim());
|
|
6
|
+
if (!found)
|
|
7
|
+
return null;
|
|
8
|
+
let digits = found[1] ?? '';
|
|
9
|
+
if (digits.length <= 4)
|
|
10
|
+
digits = digits.replace(/./g, (d) => d + d);
|
|
11
|
+
const byte = (at) => parseInt(digits.slice(at, at + 2), 16) / 255;
|
|
12
|
+
return [byte(0), byte(2), byte(4), digits.length === 8 ? byte(6) : 1];
|
|
13
|
+
}
|
|
14
|
+
const twoDigits = (v) => Math.round(unit(v) * 255).toString(16).padStart(2, '0');
|
|
15
|
+
/** `#rrggbb`, alpha dropped. */
|
|
16
|
+
export const toHex = (c) => '#' + twoDigits(c[0]) + twoDigits(c[1]) + twoDigits(c[2]);
|
|
17
|
+
/** `#rrggbb`, or `#rrggbbaa` when the color is see-through. */
|
|
18
|
+
export const toHexAlpha = (c) => toHex(c) + (c[3] < 1 ? twoDigits(c[3]) : '');
|
|
19
|
+
/** `rgb(…)` / `rgba(…)` with numbers 0..255 (or percentages) and an optional alpha; null for any other form. */
|
|
20
|
+
export function parseRgbFunction(text) {
|
|
21
|
+
const found = /^rgba?\(\s*([^)]*)\)$/i.exec(text.trim());
|
|
22
|
+
if (!found)
|
|
23
|
+
return null;
|
|
24
|
+
const parts = (found[1] ?? '').split(/[\s,/]+/).filter(Boolean);
|
|
25
|
+
if (parts.length < 3 || parts.length > 4)
|
|
26
|
+
return null;
|
|
27
|
+
const channel = (part, max) => (part.endsWith('%') ? parseFloat(part) / 100 : parseFloat(part) / max);
|
|
28
|
+
const values = parts.map((part, i) => channel(part, i < 3 ? 255 : 1));
|
|
29
|
+
if (!values.every(Number.isFinite))
|
|
30
|
+
return null;
|
|
31
|
+
return [unit(values[0] ?? 0), unit(values[1] ?? 0), unit(values[2] ?? 0), parts.length === 4 ? unit(values[3] ?? 1) : 1];
|
|
32
|
+
}
|
|
33
|
+
const toLinear = (v) => (v <= 0.04045 ? v / 12.92 : ((v + 0.055) / 1.055) ** 2.4);
|
|
34
|
+
const toGamma = (v) => (v <= 0.0031308 ? v * 12.92 : 1.055 * v ** (1 / 2.4) - 0.055);
|
|
35
|
+
const times = (m, v) => {
|
|
36
|
+
const row = (r) => r[0] * v[0] + r[1] * v[1] + r[2] * v[2];
|
|
37
|
+
return [row(m[0]), row(m[1]), row(m[2])];
|
|
38
|
+
};
|
|
39
|
+
// The OKLab transform (Björn Ottosson, 2020): linear sRGB to cone response, then cone response to Lab.
|
|
40
|
+
const RGB_TO_LMS = [
|
|
41
|
+
[0.4122214708, 0.5363325363, 0.0514459929],
|
|
42
|
+
[0.2119034982, 0.6806995451, 0.1073969566],
|
|
43
|
+
[0.0883024619, 0.2817188376, 0.6299787005],
|
|
44
|
+
];
|
|
45
|
+
const LMS_TO_LAB = [
|
|
46
|
+
[0.2104542553, 0.793617785, -0.0040720468],
|
|
47
|
+
[1.9779984951, -2.428592205, 0.4505937099],
|
|
48
|
+
[0.0259040371, 0.7827717662, -0.808675766],
|
|
49
|
+
];
|
|
50
|
+
const LAB_TO_LMS = [
|
|
51
|
+
[1, 0.3963377774, 0.2158037573],
|
|
52
|
+
[1, -0.1055613458, -0.0638541728],
|
|
53
|
+
[1, -0.0894841775, -1.291485548],
|
|
54
|
+
];
|
|
55
|
+
const LMS_TO_RGB = [
|
|
56
|
+
[4.0767416621, -3.3077115913, 0.2309699292],
|
|
57
|
+
[-1.2684380046, 2.6097574011, -0.3413193965],
|
|
58
|
+
[-0.0041960863, -0.7034186147, 1.707614701],
|
|
59
|
+
];
|
|
60
|
+
export function toOklab(c) {
|
|
61
|
+
const lms = times(RGB_TO_LMS, [toLinear(c[0]), toLinear(c[1]), toLinear(c[2])]);
|
|
62
|
+
return times(LMS_TO_LAB, [Math.cbrt(lms[0]), Math.cbrt(lms[1]), Math.cbrt(lms[2])]);
|
|
63
|
+
}
|
|
64
|
+
export function fromOklab(lab) {
|
|
65
|
+
const cone = times(LAB_TO_LMS, lab);
|
|
66
|
+
const linear = times(LMS_TO_RGB, [cone[0] ** 3, cone[1] ** 3, cone[2] ** 3]);
|
|
67
|
+
return [unit(toGamma(Math.max(0, linear[0]))), unit(toGamma(Math.max(0, linear[1]))), unit(toGamma(Math.max(0, linear[2])))];
|
|
68
|
+
}
|
|
69
|
+
/** `k` of the way from a to b (0 gives a, 1 gives b), blended in OKLab so the middle keeps its brightness. */
|
|
70
|
+
export function mixRgb(a, b, k) {
|
|
71
|
+
if (k === 0)
|
|
72
|
+
return a;
|
|
73
|
+
if (k === 1)
|
|
74
|
+
return b;
|
|
75
|
+
const from = toOklab(a);
|
|
76
|
+
const to = toOklab(b);
|
|
77
|
+
return fromOklab([from[0] + (to[0] - from[0]) * k, from[1] + (to[1] - from[1]) * k, from[2] + (to[2] - from[2]) * k]);
|
|
78
|
+
}
|
|
79
|
+
const BLACK = [0, 0, 0];
|
|
80
|
+
const WHITE = [1, 1, 1];
|
|
81
|
+
/** −1 is black, 0 the color itself, 1 white. */
|
|
82
|
+
export const shadeRgb = (c, k) => mixRgb(c, k < 0 ? BLACK : WHITE, Math.min(1, Math.abs(k)));
|
|
83
|
+
/** A canvas fill style for the color at alpha `a`. */
|
|
84
|
+
export const rgbaText = (c, a) => 'rgba(' + Math.round(unit(c[0]) * 255) + ',' + Math.round(unit(c[1]) * 255) + ',' + Math.round(unit(c[2]) * 255) + ',' + (Number.isFinite(a) ? unit(a) : 1) + ')';
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import type { Rgba } from './color.js';
|
|
2
|
+
/**
|
|
3
|
+
* Any CSS color (a `var()`, a name, `color-mix()`…) as it stands now for `where`, or else for the stage, whose
|
|
4
|
+
* variables are the film's palette. What the browser cannot read is black.
|
|
5
|
+
*/
|
|
6
|
+
export declare function readColor(css: unknown, where?: HTMLElement | null): Rgba;
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { stageElement } from "../stage-size.js";
|
|
2
|
+
import { parseHex, parseRgbFunction } from "./color.js";
|
|
3
|
+
const known = new Map();
|
|
4
|
+
const MOST_KNOWN = 1000;
|
|
5
|
+
let pixel = null;
|
|
6
|
+
/** A computed color the browser gave in a form other than rgb(): drawn on one pixel and read back. */
|
|
7
|
+
function byPixel(said) {
|
|
8
|
+
pixel ??= Object.assign(document.createElement('canvas'), { width: 1, height: 1 }).getContext('2d', { willReadFrequently: true });
|
|
9
|
+
if (!pixel)
|
|
10
|
+
return [0, 0, 0, 1];
|
|
11
|
+
pixel.globalCompositeOperation = 'copy';
|
|
12
|
+
pixel.fillStyle = said;
|
|
13
|
+
pixel.fillRect(0, 0, 1, 1);
|
|
14
|
+
const [r = 0, g = 0, b = 0, a = 1] = Array.from(pixel.getImageData(0, 0, 1, 1).data, (v) => v / 255);
|
|
15
|
+
return [r, g, b, a];
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Any CSS color (a `var()`, a name, `color-mix()`…) as it stands now for `where`, or else for the stage, whose
|
|
19
|
+
* variables are the film's palette. What the browser cannot read is black.
|
|
20
|
+
*/
|
|
21
|
+
export function readColor(css, where) {
|
|
22
|
+
const text = String(css).trim();
|
|
23
|
+
const plain = parseHex(text) ?? parseRgbFunction(text);
|
|
24
|
+
if (plain)
|
|
25
|
+
return plain;
|
|
26
|
+
const host = where ?? stageElement() ?? document.body;
|
|
27
|
+
const probe = where ? where : host.appendChild(document.createElement('i'));
|
|
28
|
+
const before = probe.style.color;
|
|
29
|
+
probe.style.color = '';
|
|
30
|
+
probe.style.color = text;
|
|
31
|
+
const said = probe.style.color === '' ? 'rgb(0, 0, 0)' : getComputedStyle(probe).color;
|
|
32
|
+
if (where)
|
|
33
|
+
probe.style.color = before;
|
|
34
|
+
else
|
|
35
|
+
probe.remove();
|
|
36
|
+
let found = known.get(said);
|
|
37
|
+
if (!found) {
|
|
38
|
+
found = parseRgbFunction(said) ?? byPixel(said);
|
|
39
|
+
if (known.size >= MOST_KNOWN)
|
|
40
|
+
known.clear();
|
|
41
|
+
known.set(said, found);
|
|
42
|
+
}
|
|
43
|
+
return found;
|
|
44
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/** What a smooth curve is drawn into: a 2D context after beginPath, or a Path2D. */
|
|
2
|
+
export interface CurveTarget {
|
|
3
|
+
moveTo(x: number, y: number): void;
|
|
4
|
+
bezierCurveTo(ax: number, ay: number, bx: number, by: number, x: number, y: number): void;
|
|
5
|
+
closePath(): void;
|
|
6
|
+
}
|
|
7
|
+
export type Point = readonly [number, number];
|
|
8
|
+
/**
|
|
9
|
+
* A curve through every point (a Catmull-Rom spline, laid as cubic Béziers). Closed, it runs on from the last point
|
|
10
|
+
* back to the first; open, its ends leave and reach their points straight on.
|
|
11
|
+
*/
|
|
12
|
+
export declare function curve(target: CurveTarget, points: readonly unknown[], close?: boolean): void;
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
function pointAt(points, i) {
|
|
2
|
+
const p = points[i];
|
|
3
|
+
if (!Array.isArray(p))
|
|
4
|
+
return [0, 0];
|
|
5
|
+
const x = Number(p[0]);
|
|
6
|
+
const y = Number(p[1]);
|
|
7
|
+
return [Number.isFinite(x) ? x : 0, Number.isFinite(y) ? y : 0];
|
|
8
|
+
}
|
|
9
|
+
/**
|
|
10
|
+
* A curve through every point (a Catmull-Rom spline, laid as cubic Béziers). Closed, it runs on from the last point
|
|
11
|
+
* back to the first; open, its ends leave and reach their points straight on.
|
|
12
|
+
*/
|
|
13
|
+
export function curve(target, points, close = false) {
|
|
14
|
+
const n = points.length;
|
|
15
|
+
if (n === 0)
|
|
16
|
+
return;
|
|
17
|
+
const at = (i) => pointAt(points, close ? ((i % n) + n) % n : Math.max(0, Math.min(n - 1, i)));
|
|
18
|
+
const first = at(0);
|
|
19
|
+
target.moveTo(first[0], first[1]);
|
|
20
|
+
const lean = (i) => {
|
|
21
|
+
const back = at(i - 1);
|
|
22
|
+
const ahead = at(i + 1);
|
|
23
|
+
return [(ahead[0] - back[0]) / 6, (ahead[1] - back[1]) / 6];
|
|
24
|
+
};
|
|
25
|
+
for (let i = 1; i < (close ? n + 1 : n); i++) {
|
|
26
|
+
const [ax, ay] = at(i - 1);
|
|
27
|
+
const [bx, by] = at(i);
|
|
28
|
+
const [lx, ly] = lean(i - 1);
|
|
29
|
+
const [mx, my] = lean(i);
|
|
30
|
+
target.bezierCurveTo(ax + lx, ay + ly, bx - mx, by - my, bx, by);
|
|
31
|
+
}
|
|
32
|
+
if (close)
|
|
33
|
+
target.closePath();
|
|
34
|
+
}
|