@phil65/demoreel 0.0.0-stage → 0.3.1

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.
Files changed (93) hide show
  1. package/LICENSE +21 -0
  2. package/README.md +207 -2
  3. package/THIRD_PARTY_NOTICES.md +31 -0
  4. package/dist/args.d.ts +41 -0
  5. package/dist/args.js +252 -0
  6. package/dist/cli.d.ts +2 -0
  7. package/dist/cli.js +468 -0
  8. package/dist/compose.d.ts +73 -0
  9. package/dist/compose.js +144 -0
  10. package/dist/env.d.ts +38 -0
  11. package/dist/env.js +182 -0
  12. package/dist/idle.d.ts +48 -0
  13. package/dist/idle.js +99 -0
  14. package/dist/index.d.ts +14 -0
  15. package/dist/index.js +13 -0
  16. package/dist/init.d.ts +19 -0
  17. package/dist/init.js +92 -0
  18. package/dist/layout.d.ts +52 -0
  19. package/dist/layout.js +55 -0
  20. package/dist/load.d.ts +2 -0
  21. package/dist/load.js +76 -0
  22. package/dist/marks.d.ts +69 -0
  23. package/dist/marks.js +153 -0
  24. package/dist/motion.d.ts +15 -0
  25. package/dist/motion.js +49 -0
  26. package/dist/openscreen/project.js +793 -0
  27. package/dist/openscreen.d.ts +14 -0
  28. package/dist/openscreen.js +41 -0
  29. package/dist/project.d.ts +143 -0
  30. package/dist/project.js +144 -0
  31. package/dist/record.d.ts +90 -0
  32. package/dist/record.js +189 -0
  33. package/dist/recorder.d.ts +41 -0
  34. package/dist/recorder.js +149 -0
  35. package/dist/render.d.ts +58 -0
  36. package/dist/render.js +139 -0
  37. package/dist/renderer/assets/CanvasPool-CHSHS1-v.js +23 -0
  38. package/dist/renderer/assets/Cursor_App-Starting-zEGhqPJj.svg +8 -0
  39. package/dist/renderer/assets/Cursor_Cross-BuZZnB_U.svg +5 -0
  40. package/dist/renderer/assets/Cursor_Default-DkmrqhKh.svg +5 -0
  41. package/dist/renderer/assets/Cursor_Hand-(Grabbing)-B-Qy-RsV.svg +5 -0
  42. package/dist/renderer/assets/Cursor_Hand-(Open)-DGrW1tFF.svg +5 -0
  43. package/dist/renderer/assets/Cursor_Hand-(Pointing)-Byy2KbtA.svg +5 -0
  44. package/dist/renderer/assets/Cursor_Help-D5NcyFpT.svg +8 -0
  45. package/dist/renderer/assets/Cursor_Move-CerM5f-0.svg +5 -0
  46. package/dist/renderer/assets/Cursor_Not-Allowed-3rttFVa5.svg +7 -0
  47. package/dist/renderer/assets/Cursor_Resize-North-East-South-West-CBd4tbCA.svg +5 -0
  48. package/dist/renderer/assets/Cursor_Resize-North-South-DOxgUIwF.svg +5 -0
  49. package/dist/renderer/assets/Cursor_Resize-North-West-South-East-hzXJFJ2_.svg +5 -0
  50. package/dist/renderer/assets/Cursor_Resize-West-East-rDJgc6AZ.svg +5 -0
  51. package/dist/renderer/assets/Cursor_Text-Cursor-Cn9rFLjY.svg +5 -0
  52. package/dist/renderer/assets/Cursor_Up-Arrow-CYyEN-dA.svg +4 -0
  53. package/dist/renderer/assets/Cursor_Wait-CK8tq-9y.svg +6 -0
  54. package/dist/renderer/assets/RenderTargetSystem-EAUH9iQk.js +404 -0
  55. package/dist/renderer/assets/browserAll-aHn2isHo.js +14 -0
  56. package/dist/renderer/assets/canvasUtils-n5g2f3_X.js +1 -0
  57. package/dist/renderer/assets/getTexelRangeRects-yD6MQ0xy.js +195 -0
  58. package/dist/renderer/assets/getTextureBatchBindGroup-Bwvgbco6.js +1 -0
  59. package/dist/renderer/assets/gif.worker-CjkyQP34.js +3 -0
  60. package/dist/renderer/assets/index-BwP6rH7I.js +556 -0
  61. package/dist/renderer/assets/init-C2Za5NQh.js +83 -0
  62. package/dist/renderer/assets/webworkerAll-JSSmbaR2.js +1 -0
  63. package/dist/renderer/index.html +13 -0
  64. package/dist/renderer/wallpapers/wallpaper1.jpg +0 -0
  65. package/dist/renderer/wallpapers/wallpaper12.jpg +0 -0
  66. package/dist/renderer/wallpapers/wallpaper5.jpg +0 -0
  67. package/dist/renderer/wasm/web-demuxer.wasm +0 -0
  68. package/dist/scene.d.ts +203 -0
  69. package/dist/scene.js +634 -0
  70. package/dist/server.d.ts +12 -0
  71. package/dist/server.js +116 -0
  72. package/dist/sheet.d.ts +37 -0
  73. package/dist/sheet.js +98 -0
  74. package/dist/types.d.ts +103 -0
  75. package/dist/types.js +2 -0
  76. package/dist/zoom.d.ts +57 -0
  77. package/dist/zoom.js +178 -0
  78. package/examples/demo-app/index.html +99 -0
  79. package/examples/local-demo.ts +21 -0
  80. package/licenses/colord.txt +21 -0
  81. package/licenses/earcut.txt +15 -0
  82. package/licenses/eventemitter3.txt +21 -0
  83. package/licenses/gif.js.txt +1 -0
  84. package/licenses/ismobilejs.txt +21 -0
  85. package/licenses/mediabunny.txt +373 -0
  86. package/licenses/motion-dom.txt +21 -0
  87. package/licenses/motion-utils.txt +21 -0
  88. package/licenses/openscreen.txt +21 -0
  89. package/licenses/parse-svg-path.txt +23 -0
  90. package/licenses/pixi-filters.txt +21 -0
  91. package/licenses/pixi.js.txt +21 -0
  92. package/licenses/web-demuxer.txt +21 -0
  93. package/package.json +74 -4
package/dist/load.js ADDED
@@ -0,0 +1,76 @@
1
+ var __rewriteRelativeImportExtension = (this && this.__rewriteRelativeImportExtension) || function (path, preserveJsx) {
2
+ if (typeof path === "string" && /^\.\.?\//.test(path)) {
3
+ return path.replace(/\.(tsx)$|((?:\.d)?)((?:\.[^./]+?)?)\.([cm]?)ts$/i, function (m, tsx, d, ext, cm) {
4
+ return tsx ? preserveJsx ? ".jsx" : ".js" : d && (!ext || !cm) ? m : (d + ext + "." + cm.toLowerCase() + "js");
5
+ });
6
+ }
7
+ return path;
8
+ };
9
+ /**
10
+ * Loads a scenario module (`export default defineScenario({...})`).
11
+ *
12
+ * - `.js` / `.mjs`: plain dynamic import.
13
+ * - `.ts` / `.mts` on Bun or Deno: native.
14
+ * - `.ts` on Node >= 22.18 / 23.6: native type stripping (erasable TypeScript only).
15
+ * - otherwise: `tsx` or `jiti`, if installed next to the scenario or in the cwd.
16
+ */
17
+ import { createRequire } from "node:module";
18
+ import path from "node:path";
19
+ import { pathToFileURL } from "node:url";
20
+ import { DemoreelError } from "./env.js";
21
+ const isTs = (f) => /\.(c|m)?tsx?$/.test(f);
22
+ function resolveFrom(spec, dirs) {
23
+ for (const dir of dirs) {
24
+ try {
25
+ return createRequire(path.join(dir, "noop.js")).resolve(spec);
26
+ }
27
+ catch {
28
+ // next
29
+ }
30
+ }
31
+ return null;
32
+ }
33
+ function pickScenario(mod, file) {
34
+ const candidate = (mod.default ?? mod.scenario ?? mod);
35
+ const s = (typeof candidate.run === "function" ? candidate : candidate.default);
36
+ if (!s || typeof s.run !== "function" || !s.name) {
37
+ throw new DemoreelError(`${file} does not export a scenario`, "export default defineScenario({ name: 'my-demo', async run(scene) { ... } })");
38
+ }
39
+ return s;
40
+ }
41
+ export async function loadScenario(file) {
42
+ const abs = path.resolve(file);
43
+ const url = pathToFileURL(abs).href;
44
+ const versions = process.versions;
45
+ const native = !isTs(abs) || Boolean(versions.bun) || Boolean(versions.deno);
46
+ let firstError = null;
47
+ try {
48
+ return pickScenario(await import(__rewriteRelativeImportExtension(url)), file);
49
+ }
50
+ catch (e) {
51
+ if (native || e instanceof DemoreelError)
52
+ throw e;
53
+ firstError = e;
54
+ }
55
+ // Node without (working) type stripping: fall back to tsx / jiti.
56
+ const dirs = [path.dirname(abs), process.cwd()];
57
+ const tsx = resolveFrom("tsx/esm/api", dirs);
58
+ if (tsx) {
59
+ const api = (await import(__rewriteRelativeImportExtension(pathToFileURL(tsx).href)));
60
+ return pickScenario(await api.tsImport(url, import.meta.url), file);
61
+ }
62
+ const jiti = resolveFrom("jiti", dirs);
63
+ if (jiti) {
64
+ const { createJiti } = (await import(__rewriteRelativeImportExtension(pathToFileURL(jiti).href)));
65
+ return pickScenario({ default: await createJiti(url).import(abs) }, file);
66
+ }
67
+ const code = firstError?.code;
68
+ const msg = firstError?.message ?? "";
69
+ if (/Cannot use import statement outside a module|require is not defined in ES module/.test(msg)) {
70
+ throw new DemoreelError(`cannot load ${file}: Node treats it as CommonJS`, `rename it to ${path.basename(abs).replace(/\.ts$/, ".mts")}, or set "type": "module" in your package.json`);
71
+ }
72
+ const reason = code === "ERR_UNKNOWN_FILE_EXTENSION"
73
+ ? `this Node (${process.version}) cannot import TypeScript`
74
+ : `importing it failed: ${firstError?.message?.split("\n")[0]}`;
75
+ throw new DemoreelError(`cannot load ${file}: ${reason}`, "use Node >= 22.18, Bun (`bunx @phil65/demoreel run ...`), `npm i -D tsx` (or `node --import tsx`), or write the scenario in .js/.mjs");
76
+ }
@@ -0,0 +1,69 @@
1
+ import { type NormRect, type StageLayout } from "./layout.ts";
2
+ import { type LookOptions } from "./project.ts";
3
+ import type { ActionRecord, Caption, SpeedRegion, TrimRegion, Viewport, ZoomRegion } from "./types.ts";
4
+ export interface MarkPoint {
5
+ t: number;
6
+ x: number;
7
+ y: number;
8
+ label?: string | null;
9
+ kind?: string | null;
10
+ duration?: number;
11
+ }
12
+ export interface MarkRegion {
13
+ start: number;
14
+ end: number;
15
+ x?: number | null;
16
+ y?: number | null;
17
+ w?: number | null;
18
+ h?: number | null;
19
+ label?: string | null;
20
+ kind?: string | null;
21
+ }
22
+ export interface MarkSpan extends MarkRegion {
23
+ label: string;
24
+ }
25
+ export interface Marks {
26
+ frame?: {
27
+ width: number;
28
+ height: number;
29
+ } | null;
30
+ points: MarkPoint[];
31
+ regions: MarkRegion[];
32
+ spans: MarkSpan[];
33
+ }
34
+ export interface MarksInput {
35
+ viewport: Viewport;
36
+ durationMs: number;
37
+ actions: ActionRecord[];
38
+ captions: Caption[];
39
+ zoomRegions: ZoomRegion[];
40
+ trimRegions?: TrimRegion[];
41
+ speedRegions?: SpeedRegion[];
42
+ layout: StageLayout;
43
+ look: Pick<LookOptions, "captionPosition">;
44
+ }
45
+ export interface TargetRect {
46
+ action: ActionRecord;
47
+ /** Recording ms. */
48
+ startMs: number;
49
+ endMs: number;
50
+ /** Output-frame rect (clipped), sampled once the cursor has landed. */
51
+ rect: NormRect;
52
+ }
53
+ /** Action target boxes in output-frame coordinates (padding + zoom camera applied). */
54
+ export declare function targetRects(input: Pick<MarksInput, "actions" | "viewport" | "layout" | "zoomRegions">): TargetRect[];
55
+ /**
56
+ * Resolves `captionPosition: "auto"`: bottom, unless the caption would cover a target active
57
+ * while it is shown (with 400 ms slack); then top, if that overlaps less.
58
+ */
59
+ export declare function placeCaptions(captions: Caption[], targets: TargetRect[], position: LookOptions["captionPosition"]): Caption[];
60
+ /** Approximate zoom progress (0-1) of a region at recording time t. */
61
+ export declare function zoomProgress(r: ZoomRegion, t: number): number;
62
+ /** The camera at time t: the dominant zoom region and its progress. */
63
+ export declare function cameraAt(regions: ZoomRegion[], t: number): {
64
+ region: ZoomRegion;
65
+ p: number;
66
+ } | null;
67
+ /** Stage rect -> output-frame rect under the camera at time t (OpenScreen computeZoomTransform). */
68
+ export declare function applyCamera(rect: NormRect, regions: ZoomRegion[], t: number): NormRect;
69
+ export declare function buildMarks(input: MarksInput): Marks;
package/dist/marks.js ADDED
@@ -0,0 +1,153 @@
1
+ /**
2
+ * framesheet marks (schema: github.com/phil65/framesheet/schema/marks.schema.json) for the
3
+ * rendered video: clicks as points, action targets / highlights / zooms as regions, captions as
4
+ * spans. Times are seconds of the *output* (after trim/speed regions); coordinates are
5
+ * fractions of the output frame, i.e. mapped through the padding layout and the zoom camera,
6
+ * so `framesheet inspect out.mp4 --marks marks.json` draws them where they appear.
7
+ */
8
+ import { sourceToOutputMs } from "./idle.js";
9
+ import { clipUnit, DEPTH_SCALES, boxToStage, clampFocus, toStage } from "./layout.js";
10
+ import { captionRect } from "./project.js";
11
+ import { normalizeDepth } from "./zoom.js";
12
+ /** Action target boxes in output-frame coordinates (padding + zoom camera applied). */
13
+ export function targetRects(input) {
14
+ const out = [];
15
+ for (const a of input.actions) {
16
+ if (!a.box || a.kind === "zoom")
17
+ continue;
18
+ // Sample the camera once the cursor has landed (zoom is then (nearly) complete).
19
+ const t = Math.min(a.endMs, a.arriveMs + 300);
20
+ const rect = clipUnit(applyCamera(boxToStage(input.layout, input.viewport, a.box), input.zoomRegions, t));
21
+ if (rect)
22
+ out.push({ action: a, startMs: a.kind === "highlight" ? a.startMs : a.arriveMs, endMs: a.endMs, rect });
23
+ }
24
+ return out;
25
+ }
26
+ const overlapArea = (a, b) => Math.max(0, Math.min(a.x + a.w, b.x + b.w) - Math.max(a.x, b.x)) * Math.max(0, Math.min(a.y + a.h, b.y + b.h) - Math.max(a.y, b.y));
27
+ /**
28
+ * Resolves `captionPosition: "auto"`: bottom, unless the caption would cover a target active
29
+ * while it is shown (with 400 ms slack); then top, if that overlaps less.
30
+ */
31
+ export function placeCaptions(captions, targets, position) {
32
+ return captions.map((c) => {
33
+ if (position !== "auto")
34
+ return { ...c, position };
35
+ const active = targets.filter((t) => t.endMs >= c.startMs - 400 && t.startMs <= c.endMs + 400);
36
+ const cost = (p) => {
37
+ const r = captionRect(p);
38
+ const box = { x: r.x / 100, y: r.y / 100, w: r.width / 100, h: r.height / 100 };
39
+ return active.reduce((n, t) => n + overlapArea(t.rect, box), 0);
40
+ };
41
+ const bottom = cost("bottom");
42
+ return { ...c, position: bottom > 0 && cost("top") < bottom ? "top" : "bottom" };
43
+ });
44
+ }
45
+ const ZOOM_IN_BEFORE_MS = 1022;
46
+ const ZOOM_IN_AFTER_MS = 500;
47
+ const ZOOM_OUT_MS = 1016;
48
+ const smooth = (x) => {
49
+ const t = Math.min(1, Math.max(0, x));
50
+ return t * t * (3 - 2 * t);
51
+ };
52
+ /** Approximate zoom progress (0-1) of a region at recording time t. */
53
+ export function zoomProgress(r, t) {
54
+ const inStart = r.startMs - ZOOM_IN_BEFORE_MS;
55
+ if (t <= inStart || t >= r.endMs + ZOOM_OUT_MS)
56
+ return 0;
57
+ if (t < r.startMs + ZOOM_IN_AFTER_MS)
58
+ return smooth((t - inStart) / (ZOOM_IN_BEFORE_MS + ZOOM_IN_AFTER_MS));
59
+ if (t <= r.endMs)
60
+ return 1;
61
+ return 1 - smooth((t - r.endMs) / ZOOM_OUT_MS);
62
+ }
63
+ /** The camera at time t: the dominant zoom region and its progress. */
64
+ export function cameraAt(regions, t) {
65
+ let best = null;
66
+ for (const r of regions) {
67
+ const p = zoomProgress(r, t);
68
+ if (p > 0 && (!best || p > best.p))
69
+ best = { region: r, p };
70
+ }
71
+ return best;
72
+ }
73
+ /** Stage rect -> output-frame rect under the camera at time t (OpenScreen computeZoomTransform). */
74
+ export function applyCamera(rect, regions, t) {
75
+ const cam = cameraAt(regions, t);
76
+ if (!cam)
77
+ return rect;
78
+ const s = DEPTH_SCALES[normalizeDepth(cam.region.depth)];
79
+ const f = clampFocus(cam.region.focus, s);
80
+ const scale = 1 + (s - 1) * cam.p;
81
+ const ox = (0.5 - f.cx * s) * cam.p;
82
+ const oy = (0.5 - f.cy * s) * cam.p;
83
+ return { x: rect.x * scale + ox, y: rect.y * scale + oy, w: rect.w * scale, h: rect.h * scale };
84
+ }
85
+ const r4 = (v) => Math.round(v * 10000) / 10000;
86
+ const r3 = (v) => Math.round(v * 1000) / 1000;
87
+ function describe(a) {
88
+ const what = a.label ?? (a.text ? `"${a.text}"` : undefined);
89
+ return what ? `${a.kind} ${what}` : a.kind;
90
+ }
91
+ export function buildMarks(input) {
92
+ const { viewport: vp, layout, zoomRegions: zr } = input;
93
+ const out = (ms) => r3(Math.max(0, sourceToOutputMs(ms, input.trimRegions, input.speedRegions)) / 1000);
94
+ const endS = out(input.durationMs);
95
+ const box = (rect) => (rect ? { x: r4(rect.x), y: r4(rect.y), w: r4(rect.w), h: r4(rect.h) } : null);
96
+ const marks = { points: [], regions: [], spans: [] };
97
+ for (const t of targetRects(input)) {
98
+ const start = out(t.startMs);
99
+ marks.regions.push({
100
+ start,
101
+ end: Math.min(endS, Math.max(start, out(t.endMs))),
102
+ ...box(t.rect),
103
+ label: describe(t.action),
104
+ kind: t.action.kind === "highlight" ? "highlight" : "target",
105
+ });
106
+ }
107
+ for (const a of input.actions) {
108
+ if (a.point && (a.kind === "click" || a.kind === "select" || a.kind === "type")) {
109
+ const t = a.kind === "type" ? a.arriveMs : a.endMs;
110
+ const p = applyCamera({ ...toStage(layout, vp, a.point), w: 0, h: 0 }, zr, t);
111
+ if (p.x >= 0 && p.x <= 1 && p.y >= 0 && p.y <= 1) {
112
+ marks.points.push({
113
+ t: Math.min(endS, out(t)),
114
+ x: r4(p.x),
115
+ y: r4(p.y),
116
+ label: describe(a),
117
+ kind: a.kind,
118
+ ...(a.kind === "type" ? { duration: r3(Math.max(0, out(a.endMs) - out(a.arriveMs))) } : {}),
119
+ });
120
+ }
121
+ }
122
+ }
123
+ for (const z of zr) {
124
+ const start = out(z.startMs);
125
+ marks.regions.push({
126
+ start,
127
+ end: Math.max(start, Math.min(endS, out(z.endMs))),
128
+ label: `zoom ${DEPTH_SCALES[normalizeDepth(z.depth)]}x (${z.source ?? "auto"})`,
129
+ kind: "zoom",
130
+ });
131
+ }
132
+ for (const s of input.speedRegions ?? []) {
133
+ marks.regions.push({ start: out(s.startMs), end: out(s.endMs), label: `${s.speed}x`, kind: "speed" });
134
+ }
135
+ for (const cap of input.captions) {
136
+ const c = captionRect(cap.position ?? input.look.captionPosition);
137
+ const start = out(cap.startMs);
138
+ marks.spans.push({
139
+ start,
140
+ end: Math.max(start, Math.min(endS, out(cap.endMs))),
141
+ label: cap.text,
142
+ kind: "caption",
143
+ x: c.x / 100,
144
+ y: c.y / 100,
145
+ w: c.width / 100,
146
+ h: c.height / 100,
147
+ });
148
+ }
149
+ marks.points.sort((a, b) => a.t - b.t);
150
+ marks.regions.sort((a, b) => a.start - b.start);
151
+ marks.spans.sort((a, b) => a.start - b.start);
152
+ return marks;
153
+ }
@@ -0,0 +1,15 @@
1
+ /** Human-like cursor paths: eased cubic Bezier with a slight arc, duration from distance (Fitts-ish). */
2
+ export interface Point {
3
+ x: number;
4
+ y: number;
5
+ }
6
+ export declare function easeInOutCubic(t: number): number;
7
+ /** Ease that accelerates quickly and settles slowly, which reads as "aimed" movement. */
8
+ export declare function easeOutQuint(t: number): number;
9
+ export declare function easeAimed(t: number): number;
10
+ /** Movement duration in ms for a given distance and target size. */
11
+ export declare function travelDuration(distance: number, targetSize?: number, speed?: number): number;
12
+ /** Deterministic pseudo-random so re-renders of a scenario produce the same motion. */
13
+ export declare function mulberry32(seed: number): () => number;
14
+ export declare function makePath(from: Point, to: Point, rand: () => number): (t: number) => Point;
15
+ export declare const sleep: (ms: number) => Promise<void>;
package/dist/motion.js ADDED
@@ -0,0 +1,49 @@
1
+ /** Human-like cursor paths: eased cubic Bezier with a slight arc, duration from distance (Fitts-ish). */
2
+ export function easeInOutCubic(t) {
3
+ return t < 0.5 ? 4 * t * t * t : 1 - (-2 * t + 2) ** 3 / 2;
4
+ }
5
+ /** Ease that accelerates quickly and settles slowly, which reads as "aimed" movement. */
6
+ export function easeOutQuint(t) {
7
+ return 1 - (1 - t) ** 5;
8
+ }
9
+ export function easeAimed(t) {
10
+ // blend: smooth start (cubic in-out) with a long settle (quint out)
11
+ return 0.45 * easeInOutCubic(t) + 0.55 * easeOutQuint(t);
12
+ }
13
+ /** Movement duration in ms for a given distance and target size. */
14
+ export function travelDuration(distance, targetSize = 40, speed = 1) {
15
+ if (distance < 2)
16
+ return 0;
17
+ const fitts = 190 + 120 * Math.log2(distance / Math.max(targetSize, 8) + 1);
18
+ return Math.round(Math.min(950, Math.max(240, fitts)) / speed);
19
+ }
20
+ /** Deterministic pseudo-random so re-renders of a scenario produce the same motion. */
21
+ export function mulberry32(seed) {
22
+ let a = seed >>> 0;
23
+ return () => {
24
+ a = (a + 0x6d2b79f5) >>> 0;
25
+ let t = a;
26
+ t = Math.imul(t ^ (t >>> 15), t | 1);
27
+ t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
28
+ return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
29
+ };
30
+ }
31
+ export function makePath(from, to, rand) {
32
+ const dx = to.x - from.x;
33
+ const dy = to.y - from.y;
34
+ const dist = Math.hypot(dx, dy);
35
+ // perpendicular unit vector
36
+ const nx = dist > 0 ? -dy / dist : 0;
37
+ const ny = dist > 0 ? dx / dist : 0;
38
+ const arc = dist * (0.06 + rand() * 0.08) * (rand() < 0.5 ? -1 : 1);
39
+ const c1 = { x: from.x + dx * 0.3 + nx * arc, y: from.y + dy * 0.3 + ny * arc };
40
+ const c2 = { x: from.x + dx * 0.75 + nx * arc * 0.4, y: from.y + dy * 0.75 + ny * arc * 0.4 };
41
+ return (t) => {
42
+ const u = 1 - t;
43
+ return {
44
+ x: u * u * u * from.x + 3 * u * u * t * c1.x + 3 * u * t * t * c2.x + t * t * t * to.x,
45
+ y: u * u * u * from.y + 3 * u * u * t * c1.y + 3 * u * t * t * c2.y + t * t * t * to.y,
46
+ };
47
+ };
48
+ }
49
+ export const sleep = (ms) => (ms <= 0 ? Promise.resolve() : new Promise((r) => setTimeout(r, ms)));