@open-artifact/motion-stage 0.0.0-stage → 0.12.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 +71 -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 +155 -0
- package/src/protocol/guards.d.ts +6 -0
- package/src/protocol/guards.js +208 -0
- package/src/protocol/layer-guards.d.ts +7 -0
- package/src/protocol/layer-guards.js +97 -0
- package/src/protocol/layers.d.ts +123 -0
- package/src/protocol/layers.js +1 -0
- package/src/protocol/types.d.ts +261 -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/captions.d.ts +26 -0
- package/src/runtime/captions.js +103 -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 +40 -0
- package/src/runtime/film.js +29 -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 +123 -0
- package/src/runtime/kit/camera.d.ts +40 -0
- package/src/runtime/kit/camera.js +56 -0
- package/src/runtime/kit/chip.d.ts +25 -0
- package/src/runtime/kit/chip.js +75 -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 +22 -0
- package/src/runtime/kit/geometry.js +46 -0
- package/src/runtime/kit/index.d.ts +95 -0
- package/src/runtime/kit/index.js +149 -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/layers/describe.d.ts +41 -0
- package/src/runtime/layers/describe.js +327 -0
- package/src/runtime/layers/item.d.ts +121 -0
- package/src/runtime/layers/item.js +149 -0
- package/src/runtime/layers/parts.d.ts +50 -0
- package/src/runtime/layers/parts.js +155 -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 +202 -0
- package/src/runtime/layers/walk.d.ts +16 -0
- package/src/runtime/layers/walk.js +333 -0
- package/src/runtime/load.d.ts +10 -0
- package/src/runtime/load.js +228 -0
- package/src/runtime/main.d.ts +1 -0
- package/src/runtime/main.js +69 -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 +35 -0
- package/src/runtime/recut.js +158 -0
- package/src/runtime/render.d.ts +80 -0
- package/src/runtime/render.js +342 -0
- package/src/runtime/retouch/ask.d.ts +17 -0
- package/src/runtime/retouch/ask.js +156 -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/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 +47 -0
- package/src/runtime/scenes/install.js +387 -0
- package/src/runtime/scenes/read.d.ts +15 -0
- package/src/runtime/scenes/read.js +57 -0
- package/src/runtime/scenes/scoped.d.ts +13 -0
- package/src/runtime/scenes/scoped.js +40 -0
- package/src/runtime/score.d.ts +26 -0
- package/src/runtime/score.js +221 -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/plate.d.ts +20 -0
- package/src/runtime/snapshot/plate.js +79 -0
- package/src/runtime/snapshot/serialize.d.ts +49 -0
- package/src/runtime/snapshot/serialize.js +271 -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 +210 -0
- package/src/runtime/video-sync.d.ts +26 -0
- package/src/runtime/video-sync.js +126 -0
- package/src/stills.d.ts +41 -0
- package/src/stills.js +253 -0
- package/src/tools/film.d.ts +13 -0
- package/src/tools/film.js +116 -0
- package/src/tools/main.d.ts +1 -0
- package/src/tools/main.js +216 -0
- package/src/tools/score.d.ts +4 -0
- package/src/tools/score.js +29 -0
- package/src/tools/types.d.ts +69 -0
- package/src/tools/types.js +2 -0
- package/src/tools.d.ts +5 -0
- package/src/tools.js +3 -0
- package/src/virtual.d.ts +11 -0
- package/src/vite.d.ts +8 -0
- package/src/vite.js +43 -0
|
@@ -0,0 +1,165 @@
|
|
|
1
|
+
import { clamp, eio, seg } from "./math.js";
|
|
2
|
+
import { BOIL_FPS, MARK_COLOR, markStrokes, strokeBox, strokeLength, strokePath } from "./mark-strokes.js";
|
|
3
|
+
import { vnoise } from "./random.js";
|
|
4
|
+
import { stageSize } from "../stage-size.js";
|
|
5
|
+
const SVG = 'http://www.w3.org/2000/svg';
|
|
6
|
+
// Kept apart from the Marks object, whose properties a film may overwrite.
|
|
7
|
+
const FILTERS = new WeakMap();
|
|
8
|
+
let filterCount = 0;
|
|
9
|
+
/**
|
|
10
|
+
* A browser filters a filter's whole region, so each stroke gets its own copy of the stage-wide `#mk-rough`,
|
|
11
|
+
* only as large as the stroke (half the pen and 12 px for the displacement). The noise at a point depends on
|
|
12
|
+
* where it sits in the layer, so a stroke looks as it would under the one big filter.
|
|
13
|
+
*/
|
|
14
|
+
function strokeFilter(marks, path, points, width) {
|
|
15
|
+
const box = strokeBox(points);
|
|
16
|
+
const more = width / 2 + 12;
|
|
17
|
+
const own = FILTERS.get(marks);
|
|
18
|
+
if (!own || !(width >= 0) || !isFinite(more + box[0] + box[1] + box[2] + box[3]))
|
|
19
|
+
return 'url(#mk-rough)';
|
|
20
|
+
let filter = own.copies.get(path);
|
|
21
|
+
if (!filter) {
|
|
22
|
+
// A film may take a mark's nodes away itself, without clear().
|
|
23
|
+
for (const [stroke, copy] of own.copies)
|
|
24
|
+
if (!stroke.isConnected && !marks.svg.contains(stroke)) {
|
|
25
|
+
copy.remove();
|
|
26
|
+
own.copies.delete(stroke);
|
|
27
|
+
}
|
|
28
|
+
const copy = own.pattern.cloneNode(true);
|
|
29
|
+
if (!(copy instanceof Element))
|
|
30
|
+
return 'url(#mk-rough)';
|
|
31
|
+
copy.id = 'mk-rough-' + ++filterCount;
|
|
32
|
+
copy.setAttribute('filterUnits', 'userSpaceOnUse');
|
|
33
|
+
marks.svg.appendChild(copy);
|
|
34
|
+
own.copies.set(path, copy);
|
|
35
|
+
filter = copy;
|
|
36
|
+
}
|
|
37
|
+
const x = Math.floor(box[0] - more);
|
|
38
|
+
const y = Math.floor(box[1] - more);
|
|
39
|
+
const region = [x, y, Math.ceil(box[2] + more) - x, Math.ceil(box[3] + more) - y];
|
|
40
|
+
['x', 'y', 'width', 'height'].forEach((name, i) => {
|
|
41
|
+
const value = String(region[i]);
|
|
42
|
+
if (filter.getAttribute(name) !== value)
|
|
43
|
+
filter.setAttribute(name, value);
|
|
44
|
+
});
|
|
45
|
+
return 'url(#' + filter.id + ')';
|
|
46
|
+
}
|
|
47
|
+
/** Hand-drawn marks on an SVG layer: they draw on, boil, and erase. */
|
|
48
|
+
export class Marks {
|
|
49
|
+
svg;
|
|
50
|
+
items = [];
|
|
51
|
+
nodes = new Map();
|
|
52
|
+
constructor(svg) {
|
|
53
|
+
this.svg = svg;
|
|
54
|
+
svg.setAttribute('fill', 'none');
|
|
55
|
+
svg.setAttribute('stroke-linecap', 'round');
|
|
56
|
+
svg.setAttribute('stroke-linejoin', 'round');
|
|
57
|
+
// Strokes copy this element rather than look it up by id later: svg may not be in the document yet.
|
|
58
|
+
let pattern = document.getElementById('mk-rough');
|
|
59
|
+
if (!pattern) {
|
|
60
|
+
const defs = document.createElementNS(SVG, 'defs');
|
|
61
|
+
defs.innerHTML =
|
|
62
|
+
'<filter id="mk-rough" filterUnits="userSpaceOnUse" x="-400" y="-400" width="2720" height="1880"><feTurbulence type="fractalNoise" baseFrequency="0.9" numOctaves="1" seed="3" result="n"/><feDisplacementMap in="SourceGraphic" in2="n" scale="2.2" xChannelSelector="R" yChannelSelector="G"/></filter>';
|
|
63
|
+
const filter = defs.firstElementChild;
|
|
64
|
+
// Marks in a camera's space draw past the edges of a small stage, so the filter never covers less than at 1920×1080.
|
|
65
|
+
filter?.setAttribute('width', String(Math.max(1920, stageSize.width) + 800));
|
|
66
|
+
filter?.setAttribute('height', String(Math.max(1080, stageSize.height) + 800));
|
|
67
|
+
svg.appendChild(defs);
|
|
68
|
+
pattern = filter;
|
|
69
|
+
}
|
|
70
|
+
if (pattern)
|
|
71
|
+
FILTERS.set(this, { pattern, copies: new Map() });
|
|
72
|
+
}
|
|
73
|
+
/** Geometry as numbers, or `geom: () => ({x, y, w, h})` read every frame. Getters on `mark` stay getters. */
|
|
74
|
+
add(mark) {
|
|
75
|
+
const item = { dur: 0.45, color: MARK_COLOR, width: 10, seed: 1, outDur: 0.3 };
|
|
76
|
+
Object.defineProperties(item, Object.getOwnPropertyDescriptors(mark));
|
|
77
|
+
this.items.push(item);
|
|
78
|
+
return this;
|
|
79
|
+
}
|
|
80
|
+
get(id) {
|
|
81
|
+
return this.items.find((mark) => mark.id === id);
|
|
82
|
+
}
|
|
83
|
+
clear() {
|
|
84
|
+
const copies = FILTERS.get(this)?.copies;
|
|
85
|
+
this.items = [];
|
|
86
|
+
for (const node of this.nodes.values())
|
|
87
|
+
node.remove();
|
|
88
|
+
if (copies)
|
|
89
|
+
for (const copy of copies.values())
|
|
90
|
+
copy.remove();
|
|
91
|
+
this.nodes.clear();
|
|
92
|
+
copies?.clear();
|
|
93
|
+
}
|
|
94
|
+
strokes(mark, boil) {
|
|
95
|
+
return markStrokes(mark, boil);
|
|
96
|
+
}
|
|
97
|
+
static toPath(points) {
|
|
98
|
+
return strokePath(points);
|
|
99
|
+
}
|
|
100
|
+
render(t) {
|
|
101
|
+
const boil = Math.floor(t * BOIL_FPS);
|
|
102
|
+
for (const mark of this.items) {
|
|
103
|
+
const gone = mark.out !== undefined && t >= mark.out + mark.outDur;
|
|
104
|
+
const on = t >= mark.t0 && !gone && !(mark.until !== undefined && t >= mark.until);
|
|
105
|
+
let group = this.nodes.get(mark);
|
|
106
|
+
if (!on) {
|
|
107
|
+
if (group)
|
|
108
|
+
group.style.display = 'none';
|
|
109
|
+
continue;
|
|
110
|
+
}
|
|
111
|
+
if (!group) {
|
|
112
|
+
group = document.createElementNS(SVG, 'g');
|
|
113
|
+
// Layers passes over the kit's marks: they are not parts of the film's drawing.
|
|
114
|
+
group.setAttribute('data-film-mark', '');
|
|
115
|
+
this.svg.appendChild(group);
|
|
116
|
+
this.nodes.set(mark, group);
|
|
117
|
+
}
|
|
118
|
+
group.style.display = '';
|
|
119
|
+
this.drawMark(mark, group, t, boil);
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
drawMark(mark, group, t, boil) {
|
|
123
|
+
const strokes = this.strokes(mark, mark.still ? 0 : boil);
|
|
124
|
+
while (group.childNodes.length < strokes.length)
|
|
125
|
+
group.appendChild(document.createElementNS(SVG, 'path'));
|
|
126
|
+
const lengths = strokes.map(strokeLength);
|
|
127
|
+
const total = lengths.reduce((a, b) => a + b, 0) || 1;
|
|
128
|
+
const drawOn = mark.ease ?? ((x) => 1 - Math.pow(1 - x, 2.4));
|
|
129
|
+
const drawn = drawOn(seg(t, mark.t0, mark.t0 + mark.dur)) * total;
|
|
130
|
+
const erased = mark.out !== undefined ? eio(seg(t, mark.out, mark.out + mark.outDur)) * total : 0;
|
|
131
|
+
const weight = mark.still ? 1 : 1 + 0.04 * vnoise(mark.seed + 77)(boil * 1.7);
|
|
132
|
+
let before = 0;
|
|
133
|
+
for (let i = 0; i < group.childNodes.length; i++) {
|
|
134
|
+
const path = group.childNodes[i];
|
|
135
|
+
if (!(path instanceof SVGPathElement))
|
|
136
|
+
continue;
|
|
137
|
+
const stroke = strokes[i];
|
|
138
|
+
if (!stroke) {
|
|
139
|
+
path.style.display = 'none';
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
path.style.display = '';
|
|
143
|
+
const length = lengths[i] ?? 0;
|
|
144
|
+
const from = clamp(erased - before, 0, length);
|
|
145
|
+
const to = clamp(drawn - before, 0, length);
|
|
146
|
+
before += length;
|
|
147
|
+
path.setAttribute('d', strokePath(stroke));
|
|
148
|
+
path.setAttribute('stroke', mark.color);
|
|
149
|
+
path.setAttribute('stroke-width', (mark.width * weight).toFixed(2));
|
|
150
|
+
if (mark.opacity !== undefined)
|
|
151
|
+
path.setAttribute('opacity', String(mark.opacity));
|
|
152
|
+
if (mark.rough !== false)
|
|
153
|
+
path.setAttribute('filter', strokeFilter(this, path, stroke, mark.width * weight));
|
|
154
|
+
else {
|
|
155
|
+
const copy = FILTERS.get(this)?.copies.get(path);
|
|
156
|
+
if (copy && path.getAttribute('filter') === 'url(#' + copy.id + ')')
|
|
157
|
+
path.removeAttribute('filter');
|
|
158
|
+
}
|
|
159
|
+
const shown = Math.max(0, to - from);
|
|
160
|
+
path.setAttribute('stroke-dasharray', `${shown.toFixed(1)} ${(length + 50).toFixed(1)}`);
|
|
161
|
+
path.setAttribute('stroke-dashoffset', (-from).toFixed(1));
|
|
162
|
+
path.style.opacity = String(shown < 0.5 ? 0 : mark.opacity !== undefined ? mark.opacity : 1);
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
}
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
export declare const clamp01: (x: number) => number;
|
|
2
|
+
export declare const clamp: (x: number, low: number, high: number) => number;
|
|
3
|
+
/** 0 to 1 across `a` to `b`. */
|
|
4
|
+
export declare const seg: (t: number, a: number, b: number) => number;
|
|
5
|
+
export declare const lerp: (a: number, b: number, p: number) => number;
|
|
6
|
+
/** Ease-out cubic. */
|
|
7
|
+
export declare const eo: (x: number) => number;
|
|
8
|
+
/** Ease-in cubic. */
|
|
9
|
+
export declare const ei: (x: number) => number;
|
|
10
|
+
export declare const eio: (x: number) => number;
|
|
11
|
+
/** Ease-out quart. */
|
|
12
|
+
export declare const eoq: (x: number) => number;
|
|
13
|
+
/** Ease-out expo: a whip. */
|
|
14
|
+
export declare const eox: (x: number) => number;
|
|
15
|
+
/** Back-out: overshoots, then settles. */
|
|
16
|
+
export declare const eob: (x: number, overshoot?: number) => number;
|
|
17
|
+
export declare const spring: (x: number, damp?: number, freq?: number) => number;
|
|
18
|
+
export declare const step: (t: number, a: number) => 0 | 1;
|
|
19
|
+
export declare const pulse: (t: number, a: number, b: number) => 0 | 1;
|
|
20
|
+
export declare const smoothstep: (a: number, b: number, x: number) => number;
|
|
21
|
+
/** A scale that swells and shrinks gently around 1. */
|
|
22
|
+
export declare const breathe: (t: number, amount?: number, period?: number) => number;
|
|
23
|
+
export type Ease = (x: number) => number;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
export const clamp01 = (x) => (x < 0 ? 0 : x > 1 ? 1 : x);
|
|
2
|
+
export const clamp = (x, low, high) => (x < low ? low : x > high ? high : x);
|
|
3
|
+
/** 0 to 1 across `a` to `b`. */
|
|
4
|
+
export const seg = (t, a, b) => clamp01((t - a) / (b - a));
|
|
5
|
+
export const lerp = (a, b, p) => a + (b - a) * p;
|
|
6
|
+
/** Ease-out cubic. */
|
|
7
|
+
export const eo = (x) => 1 - Math.pow(1 - clamp01(x), 3);
|
|
8
|
+
/** Ease-in cubic. */
|
|
9
|
+
export const ei = (x) => Math.pow(clamp01(x), 3);
|
|
10
|
+
export const eio = (x) => {
|
|
11
|
+
const p = clamp01(x);
|
|
12
|
+
return p < 0.5 ? 4 * p * p * p : 1 - Math.pow(-2 * p + 2, 3) / 2;
|
|
13
|
+
};
|
|
14
|
+
/** Ease-out quart. */
|
|
15
|
+
export const eoq = (x) => 1 - Math.pow(1 - clamp01(x), 4);
|
|
16
|
+
/** Ease-out expo: a whip. */
|
|
17
|
+
export const eox = (x) => 1 - Math.pow(2, -10 * clamp01(x));
|
|
18
|
+
/** Back-out: overshoots, then settles. */
|
|
19
|
+
export const eob = (x, overshoot = 1.4) => {
|
|
20
|
+
const p = clamp01(x);
|
|
21
|
+
const c3 = overshoot + 1;
|
|
22
|
+
return 1 + c3 * Math.pow(p - 1, 3) + overshoot * Math.pow(p - 1, 2);
|
|
23
|
+
};
|
|
24
|
+
export const spring = (x, damp = 6, freq = 2.2) => {
|
|
25
|
+
const p = clamp01(x);
|
|
26
|
+
return 1 - Math.exp(-damp * p) * Math.cos(freq * Math.PI * 2 * p) * (1 - p);
|
|
27
|
+
};
|
|
28
|
+
export const step = (t, a) => (t >= a ? 1 : 0);
|
|
29
|
+
export const pulse = (t, a, b) => (t >= a && t < b ? 1 : 0);
|
|
30
|
+
export const smoothstep = (a, b, x) => {
|
|
31
|
+
const p = clamp01((x - a) / (b - a));
|
|
32
|
+
return p * p * (3 - 2 * p);
|
|
33
|
+
};
|
|
34
|
+
/** A scale that swells and shrinks gently around 1. */
|
|
35
|
+
export const breathe = (t, amount = 0.012, period = 3.2) => 1 + amount * Math.sin((t / period) * Math.PI * 2);
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
export type Point = [number, number];
|
|
2
|
+
/** A unit polygon (radius about 1) with straight edges at uneven angles, like a shape cut from paper. */
|
|
3
|
+
export declare function cutPaper(seed?: number, sides?: number, irregularity?: number): Point[];
|
|
4
|
+
/** Morphs polygon `from` toward `to`; `to` repeats when it has fewer points. */
|
|
5
|
+
export declare const polyLerp: (from: Point[], to: Point[], p: number) => Point[];
|
|
6
|
+
/** An SVG `points` list: the polygon scaled, turned by `rotation` radians and centred on (cx, cy). */
|
|
7
|
+
export declare function polyStr(points: Point[], cx: number, cy: number, sx: number, sy: number, rotation?: number): string;
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { lerp } from "./math.js";
|
|
2
|
+
import { rng } from "./random.js";
|
|
3
|
+
/** A unit polygon (radius about 1) with straight edges at uneven angles, like a shape cut from paper. */
|
|
4
|
+
export function cutPaper(seed = 1, sides = 7, irregularity = 0.28) {
|
|
5
|
+
const next = rng(seed * 977 + 3);
|
|
6
|
+
const points = [];
|
|
7
|
+
let angle = next() * Math.PI * 2;
|
|
8
|
+
for (let i = 0; i < sides; i++) {
|
|
9
|
+
angle += ((Math.PI * 2) / sides) * (0.7 + 0.6 * next());
|
|
10
|
+
const radius = 1 - irregularity * next();
|
|
11
|
+
points.push([Math.cos(angle) * radius, Math.sin(angle) * radius]);
|
|
12
|
+
}
|
|
13
|
+
return points;
|
|
14
|
+
}
|
|
15
|
+
/** Morphs polygon `from` toward `to`; `to` repeats when it has fewer points. */
|
|
16
|
+
export const polyLerp = (from, to, p) => from.map((a, i) => {
|
|
17
|
+
const b = to[i % to.length] ?? a;
|
|
18
|
+
return [lerp(a[0], b[0], p), lerp(a[1], b[1], p)];
|
|
19
|
+
});
|
|
20
|
+
/** An SVG `points` list: the polygon scaled, turned by `rotation` radians and centred on (cx, cy). */
|
|
21
|
+
export function polyStr(points, cx, cy, sx, sy, rotation = 0) {
|
|
22
|
+
const cos = Math.cos(rotation);
|
|
23
|
+
const sin = Math.sin(rotation);
|
|
24
|
+
return points
|
|
25
|
+
.map(([x, y]) => `${(cx + (x * cos - y * sin) * sx).toFixed(1)},${(cy + (x * sin + y * cos) * sy).toFixed(1)}`)
|
|
26
|
+
.join(' ');
|
|
27
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
/** A seeded linear congruential generator: the same seed gives the same numbers in 0..1, in every browser. */
|
|
2
|
+
export declare function rng(seed: number): () => number;
|
|
3
|
+
/** Smooth 1-D value noise in -1..1. */
|
|
4
|
+
export declare function vnoise(seed: number): (position: number) => number;
|
|
5
|
+
/** The seed `Math.random` restarts from at film time `ms` (whole milliseconds), so every frame draws the same numbers. */
|
|
6
|
+
export declare const frameSeed: (ms: number) => number;
|
|
7
|
+
/** FNV-1a of a scene id, mixed with the moment: a scene's own random numbers at that moment. */
|
|
8
|
+
export declare function sceneSeed(id: string, ms: number): number;
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** A seeded linear congruential generator: the same seed gives the same numbers in 0..1, in every browser. */
|
|
2
|
+
export function rng(seed) {
|
|
3
|
+
let state = seed >>> 0;
|
|
4
|
+
return () => {
|
|
5
|
+
state = (Math.imul(state, 1664525) + 1013904223) >>> 0;
|
|
6
|
+
return state / 4294967296;
|
|
7
|
+
};
|
|
8
|
+
}
|
|
9
|
+
/** Smooth 1-D value noise in -1..1. */
|
|
10
|
+
export function vnoise(seed) {
|
|
11
|
+
const next = rng(seed);
|
|
12
|
+
const table = Array.from({ length: 256 }, () => next() * 2 - 1);
|
|
13
|
+
return (position) => {
|
|
14
|
+
const whole = Math.floor(position);
|
|
15
|
+
const fraction = position - whole;
|
|
16
|
+
const a = table[whole & 255] ?? 0;
|
|
17
|
+
const b = table[(whole + 1) & 255] ?? 0;
|
|
18
|
+
const u = fraction * fraction * (3 - 2 * fraction);
|
|
19
|
+
return a + (b - a) * u;
|
|
20
|
+
};
|
|
21
|
+
}
|
|
22
|
+
/** The seed `Math.random` restarts from at film time `ms` (whole milliseconds), so every frame draws the same numbers. */
|
|
23
|
+
export const frameSeed = (ms) => ((Math.round(ms) + 1) * 2654435761) >>> 0;
|
|
24
|
+
/** FNV-1a of a scene id, mixed with the moment: a scene's own random numbers at that moment. */
|
|
25
|
+
export function sceneSeed(id, ms) {
|
|
26
|
+
let hash = 2166136261;
|
|
27
|
+
for (let i = 0; i < id.length; i++) {
|
|
28
|
+
hash ^= id.charCodeAt(i);
|
|
29
|
+
hash = Math.imul(hash, 16777619);
|
|
30
|
+
}
|
|
31
|
+
return (hash ^ Math.imul(Math.round(ms) + 1, 2654435761)) >>> 0;
|
|
32
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export interface TextFxOptions {
|
|
2
|
+
stagger?: number;
|
|
3
|
+
dur?: number;
|
|
4
|
+
dx?: number;
|
|
5
|
+
}
|
|
6
|
+
/** Splits an element's text into one inline-block span per word, once. */
|
|
7
|
+
declare function split(el: Element): HTMLSpanElement[];
|
|
8
|
+
/** Words drift in from the right one after another; returns when the last one has landed. */
|
|
9
|
+
declare function fadeFromRight(el: Element, t: number, start: number, options?: TextFxOptions): number;
|
|
10
|
+
declare function fadeOutLeft(el: Element, t: number, start: number, options?: TextFxOptions): void;
|
|
11
|
+
declare function inOut(el: Element, t: number, tIn: number, tOut?: number, options?: TextFxOptions): void;
|
|
12
|
+
export declare const TextFx: {
|
|
13
|
+
split: typeof split;
|
|
14
|
+
fadeFromRight: typeof fadeFromRight;
|
|
15
|
+
fadeOutLeft: typeof fadeOutLeft;
|
|
16
|
+
inOut: typeof inOut;
|
|
17
|
+
};
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import { ei, eo, seg } from "./math.js";
|
|
2
|
+
const WORDS = new WeakMap();
|
|
3
|
+
/** Splits an element's text into one inline-block span per word, once. */
|
|
4
|
+
function split(el) {
|
|
5
|
+
const known = WORDS.get(el);
|
|
6
|
+
if (known)
|
|
7
|
+
return known;
|
|
8
|
+
const text = el.textContent ?? '';
|
|
9
|
+
el.textContent = '';
|
|
10
|
+
const spans = [];
|
|
11
|
+
for (const word of text.split(/(\s+)/)) {
|
|
12
|
+
if (/^\s+$/.test(word)) {
|
|
13
|
+
el.appendChild(document.createTextNode(word));
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
if (!word)
|
|
17
|
+
continue;
|
|
18
|
+
const span = document.createElement('span');
|
|
19
|
+
span.textContent = word;
|
|
20
|
+
span.style.display = 'inline-block';
|
|
21
|
+
span.style.willChange = 'opacity, transform';
|
|
22
|
+
el.appendChild(span);
|
|
23
|
+
spans.push(span);
|
|
24
|
+
}
|
|
25
|
+
WORDS.set(el, spans);
|
|
26
|
+
return spans;
|
|
27
|
+
}
|
|
28
|
+
/** Words drift in from the right one after another; returns when the last one has landed. */
|
|
29
|
+
function fadeFromRight(el, t, start, options = {}) {
|
|
30
|
+
const { stagger = 0.11, dur = 0.5, dx = 26 } = options;
|
|
31
|
+
const words = split(el);
|
|
32
|
+
words.forEach((span, i) => {
|
|
33
|
+
const p = eo(seg(t, start + i * stagger, start + i * stagger + dur));
|
|
34
|
+
span.style.opacity = String(p);
|
|
35
|
+
span.style.transform = `translateX(${((1 - p) * dx).toFixed(1)}px)`;
|
|
36
|
+
});
|
|
37
|
+
return start + (words.length - 1) * stagger + dur;
|
|
38
|
+
}
|
|
39
|
+
function fadeOutLeft(el, t, start, options = {}) {
|
|
40
|
+
const { stagger = 0.07, dur = 0.35, dx = 22 } = options;
|
|
41
|
+
split(el).forEach((span, i) => {
|
|
42
|
+
const p = ei(seg(t, start + i * stagger, start + i * stagger + dur));
|
|
43
|
+
if (t < start)
|
|
44
|
+
return;
|
|
45
|
+
span.style.opacity = String(1 - p);
|
|
46
|
+
span.style.transform = `translateX(${(-p * dx).toFixed(1)}px)`;
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
function inOut(el, t, tIn, tOut, options) {
|
|
50
|
+
if (tOut !== undefined && t >= tOut)
|
|
51
|
+
fadeOutLeft(el, t, tOut, options);
|
|
52
|
+
else
|
|
53
|
+
fadeFromRight(el, t, tIn, options);
|
|
54
|
+
}
|
|
55
|
+
export const TextFx = { split, fadeFromRight, fadeOutLeft, inOut };
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The first `count` UTF-16 units of `text`, or one fewer where unit `count` would split a surrogate pair
|
|
3
|
+
* (most emoji): half a pair alone draws as U+FFFD.
|
|
4
|
+
*/
|
|
5
|
+
export declare const uncut: (text: string, count: number) => string;
|
|
6
|
+
export interface Typist {
|
|
7
|
+
text: string;
|
|
8
|
+
/** Film seconds at which each character lands. */
|
|
9
|
+
times: number[];
|
|
10
|
+
end: number;
|
|
11
|
+
count(now: number): number;
|
|
12
|
+
at(now: number): string;
|
|
13
|
+
}
|
|
14
|
+
/**
|
|
15
|
+
* A natural typing schedule for `text` from `start` at about `cps` characters a second.
|
|
16
|
+
* Every time also goes to `heard`, so a score can put a key click on each.
|
|
17
|
+
*/
|
|
18
|
+
export declare function typist(text: string, start: number, cps: number, seed?: number, heard?: number[]): Typist;
|
|
19
|
+
/** The part of `text` shown at `t` when it streams in chunks from `start`, like a model's reply. */
|
|
20
|
+
export declare function streamed(text: string, t: number, start: number, cps?: number, chunk?: number): string;
|
|
21
|
+
export declare const esc: (text: string) => string;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import { rng } from "./random.js";
|
|
2
|
+
/**
|
|
3
|
+
* The first `count` UTF-16 units of `text`, or one fewer where unit `count` would split a surrogate pair
|
|
4
|
+
* (most emoji): half a pair alone draws as U+FFFD.
|
|
5
|
+
*/
|
|
6
|
+
export const uncut = (text, count) => text.slice(0, count > 0 && count < text.length && (text.charCodeAt(count - 1) & 0xfc00) === 0xd800 ? count - 1 : count);
|
|
7
|
+
/**
|
|
8
|
+
* A natural typing schedule for `text` from `start` at about `cps` characters a second.
|
|
9
|
+
* Every time also goes to `heard`, so a score can put a key click on each.
|
|
10
|
+
*/
|
|
11
|
+
export function typist(text, start, cps, seed = 7, heard) {
|
|
12
|
+
const next = rng(seed);
|
|
13
|
+
const times = [];
|
|
14
|
+
let t = start;
|
|
15
|
+
for (let i = 0; i < text.length; i++) {
|
|
16
|
+
let gap = (1 / cps) * (0.55 + 0.9 * next());
|
|
17
|
+
if (text[i] === ' ')
|
|
18
|
+
gap += 0.035;
|
|
19
|
+
if (text[i - 1] === '.' || text[i - 1] === '!')
|
|
20
|
+
gap += 0.16;
|
|
21
|
+
t += gap;
|
|
22
|
+
times.push(t);
|
|
23
|
+
}
|
|
24
|
+
if (heard)
|
|
25
|
+
for (const time of times)
|
|
26
|
+
heard.push(time);
|
|
27
|
+
const count = (now) => {
|
|
28
|
+
let typed = 0;
|
|
29
|
+
while (typed < times.length && (times[typed] ?? Infinity) <= now)
|
|
30
|
+
typed++;
|
|
31
|
+
return typed;
|
|
32
|
+
};
|
|
33
|
+
return { text, times, end: t, count, at: (now) => uncut(text, count(now)) };
|
|
34
|
+
}
|
|
35
|
+
/** The part of `text` shown at `t` when it streams in chunks from `start`, like a model's reply. */
|
|
36
|
+
export function streamed(text, t, start, cps = 95, chunk = 7) {
|
|
37
|
+
if (t < start)
|
|
38
|
+
return '';
|
|
39
|
+
const shown = Math.floor(((t - start) * cps) / chunk) * chunk;
|
|
40
|
+
return uncut(text, Math.min(text.length, Math.max(0, shown)));
|
|
41
|
+
}
|
|
42
|
+
export const esc = (text) => text.replace(/&/g, '&').replace(/</g, '<');
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
import type { LayerLook } from '../../protocol.ts';
|
|
2
|
+
import type { ReadMemo } from '../retouch/entry.ts';
|
|
3
|
+
import type { Item, Layer } from './item.ts';
|
|
4
|
+
/** What a reading of one stretch knows, for describing its rows. */
|
|
5
|
+
export interface Stretch {
|
|
6
|
+
W: number;
|
|
7
|
+
H: number;
|
|
8
|
+
from: number;
|
|
9
|
+
to: number;
|
|
10
|
+
/** The looks kept, in order, and the played moment of each. */
|
|
11
|
+
n: number;
|
|
12
|
+
times: number[];
|
|
13
|
+
/** The stretch's own name for ids: a scene's id, or 'film'. */
|
|
14
|
+
stretch: string;
|
|
15
|
+
prefix: string | null;
|
|
16
|
+
}
|
|
17
|
+
export interface LookSys {
|
|
18
|
+
look(el: Element): LayerLook | null;
|
|
19
|
+
kin(el: Element, cls: string, memo: ReadMemo): string | null;
|
|
20
|
+
/** What a hide on a g inside an svg would also be found by; null: none would hold (and in a film with no scenes). */
|
|
21
|
+
parts?(el: Element, memo: ReadMemo): string | null;
|
|
22
|
+
}
|
|
23
|
+
/** stays: a person's change is laid on it, so it is never looked through: its row leads to the controls that made the change. */
|
|
24
|
+
export declare const stays: (it: Item) => boolean;
|
|
25
|
+
export declare function beneath(it: Item): boolean;
|
|
26
|
+
export declare function inOrder(a: Item, b: Item): number;
|
|
27
|
+
/** A card's first line is its words, not the number in its corner. */
|
|
28
|
+
export declare function lead(said: string[]): string[];
|
|
29
|
+
/** The measures of one reading: what is as large as the stage, what grew by a hand-set Scale, and each row's description. */
|
|
30
|
+
export declare function measures(s: Stretch, inked: (it: Item) => Item[]): {
|
|
31
|
+
isFull: (it: Item) => boolean;
|
|
32
|
+
swelled: (it: Item) => boolean;
|
|
33
|
+
grown: (it: Item) => boolean;
|
|
34
|
+
describe: (it: Item, parts: Item[]) => Layer;
|
|
35
|
+
};
|
|
36
|
+
/**
|
|
37
|
+
* How the film itself draws a layer now, for what the page can change by itself: only of one thing, whose place tells
|
|
38
|
+
* it from all others. What the film itself changes in the course of the stretch is left out: a value laid over it
|
|
39
|
+
* would stop that.
|
|
40
|
+
*/
|
|
41
|
+
export declare function lookOf(l: Layer, sys: LookSys, memo: ReadMemo, used: ReadonlySet<string>): LayerLook | null;
|