@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.
Files changed (225) hide show
  1. package/LICENSE +202 -0
  2. package/NOTICE +5 -0
  3. package/README.md +5 -2
  4. package/package.json +70 -4
  5. package/src/build.d.ts +18 -0
  6. package/src/build.js +73 -0
  7. package/src/host.d.ts +37 -0
  8. package/src/host.js +76 -0
  9. package/src/index.d.ts +11 -0
  10. package/src/index.js +6 -0
  11. package/src/layers.d.ts +30 -0
  12. package/src/layers.js +56 -0
  13. package/src/offline/audio.d.ts +19 -0
  14. package/src/offline/audio.js +20 -0
  15. package/src/offline/data.d.ts +26 -0
  16. package/src/offline/data.js +71 -0
  17. package/src/offline/main.d.ts +9 -0
  18. package/src/offline/main.js +52 -0
  19. package/src/offline/player.d.ts +23 -0
  20. package/src/offline/player.js +157 -0
  21. package/src/protocol/clip-guards.d.ts +5 -0
  22. package/src/protocol/clip-guards.js +22 -0
  23. package/src/protocol/guards.d.ts +6 -0
  24. package/src/protocol/guards.js +257 -0
  25. package/src/protocol/layer-guards.d.ts +7 -0
  26. package/src/protocol/layer-guards.js +100 -0
  27. package/src/protocol/layers.d.ts +135 -0
  28. package/src/protocol/layers.js +1 -0
  29. package/src/protocol/types.d.ts +340 -0
  30. package/src/protocol/types.js +7 -0
  31. package/src/protocol.d.ts +2 -0
  32. package/src/protocol.js +2 -0
  33. package/src/raster.d.ts +4 -0
  34. package/src/raster.js +2 -0
  35. package/src/runtime/assets.d.ts +19 -0
  36. package/src/runtime/assets.js +63 -0
  37. package/src/runtime/caption-box.d.ts +3 -0
  38. package/src/runtime/caption-box.js +16 -0
  39. package/src/runtime/captions.d.ts +31 -0
  40. package/src/runtime/captions.js +115 -0
  41. package/src/runtime/clip-captions.d.ts +9 -0
  42. package/src/runtime/clip-captions.js +27 -0
  43. package/src/runtime/clips.d.ts +83 -0
  44. package/src/runtime/clips.js +259 -0
  45. package/src/runtime/console.d.ts +13 -0
  46. package/src/runtime/console.js +99 -0
  47. package/src/runtime/cut.d.ts +7 -0
  48. package/src/runtime/cut.js +63 -0
  49. package/src/runtime/determinism.d.ts +12 -0
  50. package/src/runtime/determinism.js +47 -0
  51. package/src/runtime/film.d.ts +43 -0
  52. package/src/runtime/film.js +31 -0
  53. package/src/runtime/focus.d.ts +8 -0
  54. package/src/runtime/focus.js +71 -0
  55. package/src/runtime/fonts.d.ts +22 -0
  56. package/src/runtime/fonts.js +109 -0
  57. package/src/runtime/frame.d.ts +9 -0
  58. package/src/runtime/frame.js +140 -0
  59. package/src/runtime/fx/backdrop.d.ts +60 -0
  60. package/src/runtime/fx/backdrop.js +264 -0
  61. package/src/runtime/fx/color.d.ts +19 -0
  62. package/src/runtime/fx/color.js +84 -0
  63. package/src/runtime/fx/css-color.d.ts +6 -0
  64. package/src/runtime/fx/css-color.js +44 -0
  65. package/src/runtime/fx/curve.d.ts +12 -0
  66. package/src/runtime/fx/curve.js +34 -0
  67. package/src/runtime/fx/gl.d.ts +29 -0
  68. package/src/runtime/fx/gl.js +207 -0
  69. package/src/runtime/fx/glsl.d.ts +35 -0
  70. package/src/runtime/fx/glsl.js +113 -0
  71. package/src/runtime/fx/index.d.ts +23 -0
  72. package/src/runtime/fx/index.js +151 -0
  73. package/src/runtime/fx/key.d.ts +11 -0
  74. package/src/runtime/fx/key.js +21 -0
  75. package/src/runtime/fx/numbers.d.ts +14 -0
  76. package/src/runtime/fx/numbers.js +57 -0
  77. package/src/runtime/fx/pace.d.ts +17 -0
  78. package/src/runtime/fx/pace.js +26 -0
  79. package/src/runtime/fx/paint.d.ts +28 -0
  80. package/src/runtime/fx/paint.js +150 -0
  81. package/src/runtime/guard.d.ts +4 -0
  82. package/src/runtime/guard.js +141 -0
  83. package/src/runtime/hand-titles/index.d.ts +9 -0
  84. package/src/runtime/hand-titles/index.js +73 -0
  85. package/src/runtime/hand-titles/layer.d.ts +20 -0
  86. package/src/runtime/hand-titles/layer.js +118 -0
  87. package/src/runtime/hand-titles/place.d.ts +27 -0
  88. package/src/runtime/hand-titles/place.js +45 -0
  89. package/src/runtime/held-store.d.ts +35 -0
  90. package/src/runtime/held-store.js +63 -0
  91. package/src/runtime/held.d.ts +19 -0
  92. package/src/runtime/held.js +225 -0
  93. package/src/runtime/kit/camera.d.ts +59 -0
  94. package/src/runtime/kit/camera.js +81 -0
  95. package/src/runtime/kit/chip.d.ts +25 -0
  96. package/src/runtime/kit/chip.js +75 -0
  97. package/src/runtime/kit/count.d.ts +37 -0
  98. package/src/runtime/kit/count.js +100 -0
  99. package/src/runtime/kit/cursor.d.ts +46 -0
  100. package/src/runtime/kit/cursor.js +108 -0
  101. package/src/runtime/kit/dom.d.ts +14 -0
  102. package/src/runtime/kit/dom.js +46 -0
  103. package/src/runtime/kit/geometry.d.ts +24 -0
  104. package/src/runtime/kit/geometry.js +56 -0
  105. package/src/runtime/kit/index.d.ts +113 -0
  106. package/src/runtime/kit/index.js +157 -0
  107. package/src/runtime/kit/mark-strokes.d.ts +65 -0
  108. package/src/runtime/kit/mark-strokes.js +226 -0
  109. package/src/runtime/kit/marks.d.ts +21 -0
  110. package/src/runtime/kit/marks.js +165 -0
  111. package/src/runtime/kit/math.d.ts +23 -0
  112. package/src/runtime/kit/math.js +35 -0
  113. package/src/runtime/kit/paper.d.ts +7 -0
  114. package/src/runtime/kit/paper.js +27 -0
  115. package/src/runtime/kit/random.d.ts +8 -0
  116. package/src/runtime/kit/random.js +32 -0
  117. package/src/runtime/kit/text-fx.d.ts +18 -0
  118. package/src/runtime/kit/text-fx.js +55 -0
  119. package/src/runtime/kit/typing.d.ts +21 -0
  120. package/src/runtime/kit/typing.js +42 -0
  121. package/src/runtime/laid/layer.d.ts +44 -0
  122. package/src/runtime/laid/layer.js +370 -0
  123. package/src/runtime/layers/describe.d.ts +41 -0
  124. package/src/runtime/layers/describe.js +327 -0
  125. package/src/runtime/layers/item.d.ts +123 -0
  126. package/src/runtime/layers/item.js +150 -0
  127. package/src/runtime/layers/parts.d.ts +53 -0
  128. package/src/runtime/layers/parts.js +158 -0
  129. package/src/runtime/layers/rows.d.ts +4 -0
  130. package/src/runtime/layers/rows.js +233 -0
  131. package/src/runtime/layers/scan.d.ts +24 -0
  132. package/src/runtime/layers/scan.js +204 -0
  133. package/src/runtime/layers/walk.d.ts +16 -0
  134. package/src/runtime/layers/walk.js +337 -0
  135. package/src/runtime/load.d.ts +10 -0
  136. package/src/runtime/load.js +267 -0
  137. package/src/runtime/main.d.ts +1 -0
  138. package/src/runtime/main.js +90 -0
  139. package/src/runtime/modules.d.ts +23 -0
  140. package/src/runtime/modules.js +53 -0
  141. package/src/runtime/port.d.ts +8 -0
  142. package/src/runtime/port.js +46 -0
  143. package/src/runtime/questions.d.ts +24 -0
  144. package/src/runtime/questions.js +162 -0
  145. package/src/runtime/recut.d.ts +81 -0
  146. package/src/runtime/recut.js +325 -0
  147. package/src/runtime/render.d.ts +80 -0
  148. package/src/runtime/render.js +358 -0
  149. package/src/runtime/retouch/ask.d.ts +17 -0
  150. package/src/runtime/retouch/ask.js +171 -0
  151. package/src/runtime/retouch/entry.d.ts +77 -0
  152. package/src/runtime/retouch/entry.js +1 -0
  153. package/src/runtime/retouch/judge.d.ts +25 -0
  154. package/src/runtime/retouch/judge.js +209 -0
  155. package/src/runtime/retouch/look.d.ts +39 -0
  156. package/src/runtime/retouch/look.js +135 -0
  157. package/src/runtime/retouch/own-styles.d.ts +19 -0
  158. package/src/runtime/retouch/own-styles.js +141 -0
  159. package/src/runtime/retouch/parts.d.ts +22 -0
  160. package/src/runtime/retouch/parts.js +166 -0
  161. package/src/runtime/retouch/retouch.d.ts +37 -0
  162. package/src/runtime/retouch/retouch.js +238 -0
  163. package/src/runtime/retouch/seat.d.ts +40 -0
  164. package/src/runtime/retouch/seat.js +228 -0
  165. package/src/runtime/retouch/state.d.ts +37 -0
  166. package/src/runtime/retouch/state.js +153 -0
  167. package/src/runtime/retouch/type-style.d.ts +19 -0
  168. package/src/runtime/retouch/type-style.js +111 -0
  169. package/src/runtime/scan.d.ts +2 -0
  170. package/src/runtime/scan.js +49 -0
  171. package/src/runtime/scenes/card.d.ts +12 -0
  172. package/src/runtime/scenes/card.js +56 -0
  173. package/src/runtime/scenes/install.d.ts +69 -0
  174. package/src/runtime/scenes/install.js +528 -0
  175. package/src/runtime/scenes/read.d.ts +27 -0
  176. package/src/runtime/scenes/read.js +73 -0
  177. package/src/runtime/scenes/scoped.d.ts +13 -0
  178. package/src/runtime/scenes/scoped.js +46 -0
  179. package/src/runtime/score.d.ts +32 -0
  180. package/src/runtime/score.js +262 -0
  181. package/src/runtime/sketch/pen.d.ts +3 -0
  182. package/src/runtime/sketch/pen.js +371 -0
  183. package/src/runtime/sketch/strokes.d.ts +41 -0
  184. package/src/runtime/sketch/strokes.js +187 -0
  185. package/src/runtime/snapshot/draw.d.ts +14 -0
  186. package/src/runtime/snapshot/draw.js +125 -0
  187. package/src/runtime/snapshot/hold-first-draw.d.ts +12 -0
  188. package/src/runtime/snapshot/hold-first-draw.js +128 -0
  189. package/src/runtime/snapshot/lift.d.ts +41 -0
  190. package/src/runtime/snapshot/lift.js +161 -0
  191. package/src/runtime/snapshot/plate.d.ts +20 -0
  192. package/src/runtime/snapshot/plate.js +79 -0
  193. package/src/runtime/snapshot/serialize.d.ts +51 -0
  194. package/src/runtime/snapshot/serialize.js +286 -0
  195. package/src/runtime/snapshot/styles.d.ts +8 -0
  196. package/src/runtime/snapshot/styles.js +109 -0
  197. package/src/runtime/snapshot/xml.d.ts +6 -0
  198. package/src/runtime/snapshot/xml.js +110 -0
  199. package/src/runtime/stage-size.d.ts +7 -0
  200. package/src/runtime/stage-size.js +3 -0
  201. package/src/runtime/stage.d.ts +27 -0
  202. package/src/runtime/stage.js +154 -0
  203. package/src/runtime/stretch.d.ts +13 -0
  204. package/src/runtime/stretch.js +16 -0
  205. package/src/runtime/texts.d.ts +24 -0
  206. package/src/runtime/texts.js +211 -0
  207. package/src/runtime/video-sync.d.ts +51 -0
  208. package/src/runtime/video-sync.js +218 -0
  209. package/src/stills.d.ts +90 -0
  210. package/src/stills.js +329 -0
  211. package/src/tools/film.d.ts +13 -0
  212. package/src/tools/film.js +116 -0
  213. package/src/tools/levels.d.ts +8 -0
  214. package/src/tools/levels.js +28 -0
  215. package/src/tools/main.d.ts +1 -0
  216. package/src/tools/main.js +223 -0
  217. package/src/tools/score.d.ts +4 -0
  218. package/src/tools/score.js +35 -0
  219. package/src/tools/types.d.ts +84 -0
  220. package/src/tools/types.js +2 -0
  221. package/src/tools.d.ts +6 -0
  222. package/src/tools.js +4 -0
  223. package/src/virtual.d.ts +11 -0
  224. package/src/vite.d.ts +8 -0
  225. package/src/vite.js +43 -0
@@ -0,0 +1,371 @@
1
+ import { stageParts } from "../stage.js";
2
+ import { stageSize } from "../stage-size.js";
3
+ import { sketchBox } from "./strokes.js";
4
+ /*
5
+ * A rough scene's outlines redrawn as hand-drawn strokes. It goes by what is drawn, a border or a flat colour, since
6
+ * the class names are the film's own. If the pen itself fails, the scene loses its sketch and plays on.
7
+ */
8
+ const SVG = 'http://www.w3.org/2000/svg';
9
+ const SIDES = ['Top', 'Right', 'Bottom', 'Left'];
10
+ const CORNERS = ['TopLeft', 'TopRight', 'BottomRight', 'BottomLeft'];
11
+ const LEFT_ALONE = /^(?:IMG|CANVAS|VIDEO|AUDIO|IFRAME|OBJECT|EMBED|INPUT|TEXTAREA|SELECT|BUTTON|PROGRESS|METER|DETAILS|HR|BR|STYLE|TABLE|TD|TH|TR)$/;
12
+ const KITS_OWN = '.film-cursor,.film-chip';
13
+ /** A scene of more elements is left clean, since each is read at every frame. */
14
+ const MOST = 300;
15
+ const DRAWABLE = /^(?:solid|dashed|dotted)$/;
16
+ const CLIPS = /^(?:hidden|clip|scroll|auto)$/;
17
+ const OUT_OF_FLOW = /^(?:absolute|fixed)$/;
18
+ /*
19
+ * data-sketch on an element says what of its own is put aside while it is sketched. edge: its border's colour. fill:
20
+ * its background's. flow: it is position: static, and is made relative so the strokes can be placed by it. label:
21
+ * where its ::after is placed. shape: nothing; no rule reads it.
22
+ */
23
+ const RULES = '[data-sketch-on] [data-sketch~=edge]{border-color:transparent!important}' +
24
+ '[data-sketch-on] [data-sketch~=fill]{background-color:transparent!important}[data-sketch-on] [data-sketch~=flow]{position:relative!important}' +
25
+ '[data-sketch-on] [data-sketch~=label]::after{left:50%!important;top:50%!important;right:auto!important;bottom:auto!important;transform:translate(-50%,-50%)!important;width:max-content!important;min-width:min-content!important;max-width:100%!important;margin:0!important;translate:none!important;rotate:none!important;scale:none!important;text-align:center!important}';
26
+ // Inline, so that a film's plain rules for svg, path or "every child" do not reach these properties.
27
+ const SVG_CSS = ';width:1px;height:1px;margin:0;padding:0;border:0;outline:0;box-sizing:content-box;display:block;visibility:inherit;opacity:1;transform:none;filter:none;clip-path:none;mix-blend-mode:normal;z-index:auto;overflow:visible;pointer-events:none';
28
+ const PATH_CSS = ';transform:none;visibility:inherit;stroke-opacity:1;stroke-dashoffset:0;stroke-linecap:round;stroke-linejoin:round;opacity:';
29
+ const n1 = (n) => String(Math.round(n * 10) / 10);
30
+ const isClear = (c) => !c || c === 'transparent' || /^rgba\(.*,\s*0\)$/.test(c) || /\/\s*0\)$/.test(c);
31
+ const isSet = (v) => /\(/.test(v || '');
32
+ const toPx = (v, of) => (v && /%$/.test(v) ? (parseFloat(v) / 100) * of : parseFloat(v ?? '') || 0);
33
+ const isPens = (n) => n.namespaceURI === SVG && n.hasAttribute('data-sketch-pen');
34
+ function ink(colour, width, kind, opacity) {
35
+ const w = Number(width);
36
+ const dash = kind === 'dashed' ? w * 3.5 + ' ' + w * 3 : kind === 'dotted' ? '0.1 ' + w * 2.6 : 'none';
37
+ return 'fill:none;stroke:' + colour + ';stroke-width:' + width + 'px;stroke-dasharray:' + dash + PATH_CSS + opacity;
38
+ }
39
+ /** Remembered for the length of one drawing. */
40
+ const positions = new Map();
41
+ function position(n, pseudo) {
42
+ let m = positions.get(n);
43
+ if (!m)
44
+ positions.set(n, (m = new Map()));
45
+ const k = pseudo ?? 'self';
46
+ let p = m.get(k);
47
+ if (p === undefined)
48
+ m.set(k, (p = getComputedStyle(n, pseudo).position));
49
+ return p;
50
+ }
51
+ /** Does el hold anything placed absolutely or fixed, a ::before or ::after too, that an ancestor of el positions? Made relative, el would position it instead. */
52
+ function anchors(el) {
53
+ const all = el.firstElementChild ? Array.from(el.querySelectorAll('*')) : [];
54
+ const out = (n, pseudo) => OUT_OF_FLOW.test(position(n, pseudo));
55
+ for (let i = -1; i < all.length; i++) {
56
+ const n = i < 0 ? el : all[i];
57
+ if (!n)
58
+ continue;
59
+ const inSvg = n.namespaceURI === SVG;
60
+ if (inSvg && (isPens(n) || (n.parentNode instanceof Element && n.parentNode.namespaceURI === SVG)))
61
+ continue;
62
+ const self = i >= 0 && out(n, null);
63
+ if (!self && (inSvg || !(out(n, '::before') || out(n, '::after'))))
64
+ continue;
65
+ let p = self ? n.parentElement : n;
66
+ while (p && p !== el && position(p, null) === 'static')
67
+ p = p.parentElement;
68
+ if (p === el)
69
+ return true;
70
+ }
71
+ return false;
72
+ }
73
+ /** What to draw for an element, read from the film's own styles; null leaves it as the scene drew it. */
74
+ function plan(s, u) {
75
+ const el = s.el;
76
+ const cs = s.cs;
77
+ const w = el.offsetWidth;
78
+ const h = el.offsetHeight;
79
+ const thin = Math.min(w, h);
80
+ const share = (w * h) / (stageSize.width * stageSize.height);
81
+ let other = false;
82
+ const raw = [0, 0, 0, 0];
83
+ SIDES.forEach((side, i) => {
84
+ raw[i] = parseFloat(cs.getPropertyValue('border-' + side.toLowerCase() + '-width')) || 0;
85
+ });
86
+ const sides = SIDES.map((side, i) => {
87
+ const kind = cs.getPropertyValue('border-' + side.toLowerCase() + '-style');
88
+ const color = cs.getPropertyValue('border-' + side.toLowerCase() + '-color');
89
+ const wide = raw[i] ?? 0;
90
+ if (!wide || !kind || kind === 'none' || kind === 'hidden' || isClear(color))
91
+ return null;
92
+ if (!DRAWABLE.test(kind))
93
+ other = true;
94
+ return { wide, color, kind };
95
+ });
96
+ const on = sides.filter((x) => x !== null);
97
+ const first = on[0];
98
+ const flat = !isClear(cs.backgroundColor);
99
+ const empty = !(el.textContent ?? '').trim() && Array.from(el.children).every(isPens);
100
+ if (!on.length && !(flat && empty))
101
+ return null;
102
+ // The pen runs along and past the border, where an element that clips would cut it off.
103
+ const mask = cs.getPropertyValue('mask-image') || cs.getPropertyValue('-webkit-mask-image');
104
+ if (!w || !h || other || CLIPS.test(cs.overflowX) || CLIPS.test(cs.overflowY) || isSet(cs.clipPath) || isSet(mask) || /paint|content|strict/.test(cs.contain) || isSet(cs.borderImageSource))
105
+ return null;
106
+ // The strokes are a positioned child, which an inline box or a table's part cannot hold.
107
+ if (/^(?:inline|contents|table)/.test(cs.display) && !/^inline-(?:block|flex|grid)$/.test(cs.display))
108
+ return null;
109
+ const whole = on.length === 4;
110
+ const alike = on.every((x) => !!first && x.wide === first.wide && x.color === first.color && x.kind === first.kind);
111
+ const radii = CORNERS.map((c) => cs.getPropertyValue('border-' + c.replace(/([a-z])([A-Z])/, '$1-$2').toLowerCase() + '-radius'));
112
+ const r = (radii[0] ?? '').split(' ');
113
+ const rx = toPx(r[0], w);
114
+ const ry = toPx(r[1] || r[0], h);
115
+ const curved = radii.some((v) => {
116
+ const a = v.split(' ');
117
+ const x = toPx(a[0], w);
118
+ const y = toPx(a[1] || a[0], h);
119
+ return Math.min(x, y) * Math.min(1, x ? w / (2 * x) : 1, y ? h / (2 * y) : 1) >= 0.3 * thin;
120
+ });
121
+ const meet = on.length > 0 && (raw[1] + raw[3] + 1 >= w || raw[0] + raw[2] + 1 >= h);
122
+ // A shape: empty, and filled by its background or by four like borders that meet. An empty flat colour over 29% of the stage is the background.
123
+ const shape = empty && (on.length ? meet && whole && alike : share < 0.29);
124
+ if (!on.length && !shape)
125
+ return null;
126
+ // Borders that meet at a corner, clear ones too, make a triangle; one border on an element of no thickness is a line.
127
+ if (meet && !shape && sides.some((x, i) => x && (raw[(i + 1) % 4] || raw[(i + 3) % 4])))
128
+ return null;
129
+ // One line goes round a curve: not round an arch, a ring whose sides differ, or corners that differ.
130
+ if (curved && (!radii.every((v) => v === radii[0]) || (!shape && !(whole && alike))))
131
+ return null;
132
+ const inFlow = cs.position === 'static';
133
+ if (inFlow && anchors(el))
134
+ return null;
135
+ const hatch = shape || !whole || !empty || flat || isSet(cs.backgroundImage) || share >= 0.29 ? 0 : share > 0.077 ? 2 : 1;
136
+ let skip = null;
137
+ const pad = 8 * u;
138
+ const bar = shape && thin <= 8 * u;
139
+ let label = false;
140
+ if (empty && !bar && (hatch || el.hasAttribute('data-label'))) {
141
+ const after = (s.after ??= getComputedStyle(el, '::after'));
142
+ let lx = parseFloat(after.left) + raw[3];
143
+ let ly = parseFloat(after.top) + raw[0];
144
+ const lw = parseFloat(after.width);
145
+ const lh = parseFloat(after.height);
146
+ const room = w - raw[1] - raw[3];
147
+ const moved = /^matrix\(1, 0, 0, 1, (-?[\d.e-]+), (-?[\d.e-]+)\)$/.exec(after.transform || '');
148
+ if (moved) {
149
+ lx += parseFloat(moved[1] ?? '0');
150
+ ly += parseFloat(moved[2] ?? '0');
151
+ }
152
+ // A placeholder's label goes in the middle: at a corner it would cross a circle's line. One wholly outside is a caption, and stays.
153
+ const mx = parseFloat(after.marginLeft) || 0;
154
+ const my = parseFloat(after.marginTop) || 0;
155
+ label = el.hasAttribute('data-label') && after.position === 'absolute' && lx + mx < w - raw[1] && ly + my < h - raw[2] && lx + mx + lw > raw[3] && ly + my + lh > raw[0];
156
+ if (label) {
157
+ lx = raw[3] + (room - lw) / 2;
158
+ ly = raw[0] + (h - raw[0] - raw[2] - lh) / 2;
159
+ }
160
+ if (hatch && lw > 0 && lh > 0 && !Number.isNaN(lx) && !Number.isNaN(ly))
161
+ skip = [lx - pad, ly - pad, lx + lw + pad, ly + lh + pad];
162
+ }
163
+ return {
164
+ w,
165
+ h,
166
+ left: raw[3],
167
+ top: raw[0],
168
+ fill: first ? first.color : cs.backgroundColor,
169
+ sides: shape ? null : sides,
170
+ mark: (shape ? (bar ? 'fill edge' : 'shape') : 'edge') + (inFlow ? ' flow' : '') + (label ? ' label' : ''),
171
+ o: {
172
+ u,
173
+ sides: shape ? [2, 2, 2, 2] : [sides[0]?.wide ?? 0, sides[1]?.wide ?? 0, sides[2]?.wide ?? 0, sides[3]?.wide ?? 0],
174
+ onEdge: shape,
175
+ bar,
176
+ broken: !shape && !!first && first.kind !== 'solid',
177
+ rx,
178
+ ry,
179
+ hatch,
180
+ skip,
181
+ },
182
+ };
183
+ }
184
+ let sheet = null;
185
+ /** The pen for one rough scene: a call that sketches the scene as it stands, made after every render, since a scene may show a state by its borders. */
186
+ export const sketchScene = (box, id) => {
187
+ let known = new WeakMap();
188
+ const owner = new WeakMap();
189
+ let seed = 7;
190
+ let dead = false;
191
+ let said = false;
192
+ for (let c = 0; c < id.length; c++)
193
+ seed = (seed * 31 + id.charCodeAt(c)) % 2147483646;
194
+ const off = () => {
195
+ box.removeAttribute('data-sketch-on');
196
+ for (const n of Array.from(box.querySelectorAll('svg[data-sketch-pen]')))
197
+ n.remove();
198
+ for (const n of Array.from(box.querySelectorAll('[data-sketch]')))
199
+ n.removeAttribute('data-sketch');
200
+ known = new WeakMap();
201
+ };
202
+ const say = (why) => {
203
+ if (!said)
204
+ console.warn('rough scene ' + id + ' is shown without its sketch (' + why + '); nothing in the film needs changing');
205
+ said = true;
206
+ };
207
+ const root = { path: seed, kids: 0, lone: 0 };
208
+ const draw = () => {
209
+ if (!box.parentNode)
210
+ return;
211
+ const u = Math.sqrt(stageSize.width * stageSize.height) / 960;
212
+ const all = Array.from(box.querySelectorAll('*'));
213
+ const seen = [];
214
+ const strays = [];
215
+ const todo = [];
216
+ let fresh = [];
217
+ root.kids = root.lone = 0;
218
+ const keyOf = (s) => {
219
+ const d = plan(s, u);
220
+ return { s, d, key: d ? JSON.stringify([s.path, d]) : '' };
221
+ };
222
+ // Not kit.css, which places by the canvas: a scene may have taken the canvas into its box.
223
+ const stage = stageParts()?.stage;
224
+ if (stage && (!sheet || !sheet.parentNode)) {
225
+ sheet = document.createElement('style');
226
+ sheet.textContent = RULES;
227
+ stage.insertBefore(sheet, stage.firstChild);
228
+ }
229
+ // Read with the rules off, so that what is read is the film's own; then written, all at once.
230
+ box.removeAttribute('data-sketch-on');
231
+ positions.clear();
232
+ for (const el of all) {
233
+ // cloneNode and innerHTML copy the pen's strokes and marks.
234
+ if (el.namespaceURI === SVG) {
235
+ if (isPens(el) && owner.get(el) !== el.parentNode)
236
+ strays.push(el);
237
+ continue;
238
+ }
239
+ if (!(el instanceof HTMLElement))
240
+ continue;
241
+ let s = known.get(el);
242
+ if (!s) {
243
+ s = { el, svg: null, paths: [], key: '', path: 0, kids: 0, lone: 0, after: null, cs: getComputedStyle(el) };
244
+ known.set(el, s);
245
+ el.removeAttribute('data-sketch');
246
+ }
247
+ /*
248
+ * Seeded by where the element sits among its parent's elements, all the way down: only an earlier sibling that
249
+ * comes or goes moves them. What is left alone has a count of its own, so the canvas moving in and out of a
250
+ * scene's box does not change what is sketched beside it.
251
+ */
252
+ const parent = el.parentNode instanceof Element ? known.get(el.parentNode) : undefined;
253
+ const up = parent ?? root;
254
+ const alone = LEFT_ALONE.test(el.tagName) || !!el.closest(KITS_OWN);
255
+ s.path = (up.path * 131 + (alone ? 65537 * ++up.lone : ++up.kids)) % 2147483646;
256
+ s.kids = s.lone = 0;
257
+ if (!alone && seen.push(s) > MOST)
258
+ break;
259
+ }
260
+ // For this frame alone: a frame follows from t, never from the frames before it.
261
+ if (seen.length > MOST) {
262
+ off();
263
+ say('over ' + MOST + ' elements');
264
+ return;
265
+ }
266
+ for (const s of seen) {
267
+ const t = keyOf(s);
268
+ if (t.key !== s.key || (t.d && s.svg?.parentNode !== s.el))
269
+ todo.push(t);
270
+ }
271
+ box.setAttribute('data-sketch-on', '');
272
+ for (const n of strays)
273
+ n.remove();
274
+ for (const t of todo) {
275
+ const s = t.s;
276
+ const d = t.d;
277
+ if (!d) {
278
+ s.el.removeAttribute('data-sketch');
279
+ s.svg?.remove();
280
+ s.key = '';
281
+ continue;
282
+ }
283
+ let svg = s.svg;
284
+ if (!svg) {
285
+ svg = document.createElementNS(SVG, 'svg');
286
+ svg.setAttribute('data-sketch-pen', '');
287
+ svg.setAttribute('aria-hidden', 'true');
288
+ owner.set(svg, s.el);
289
+ s.svg = svg;
290
+ }
291
+ if (svg.parentNode !== s.el) {
292
+ s.el.appendChild(svg);
293
+ fresh.push(t);
294
+ }
295
+ if (t.key === s.key)
296
+ continue;
297
+ s.key = t.key;
298
+ const k = sketchBox(s.path + 1, d.w, d.h, d.o);
299
+ const parts = [
300
+ [k.wash, 'fill:' + d.fill + ';stroke:none' + PATH_CSS + 0.05],
301
+ [k.hatch, ink(d.fill, k.fine, 'solid', 0.36)],
302
+ [k.cross, ink(d.fill, k.fine, 'solid', 0.2)],
303
+ ];
304
+ const soft = new Map();
305
+ const faint = new Map();
306
+ const firm = new Map();
307
+ const add = (to, css, path) => {
308
+ to.set(css, (to.get(css) ?? '') + path);
309
+ };
310
+ k.lines.forEach((l, i) => {
311
+ if (!l)
312
+ return;
313
+ const side = d.sides?.[i] ?? { color: d.fill, kind: 'solid' };
314
+ const wide = (part) => n1(Number(l.width) * (d.o.bar ? 1 : 0.66) * part);
315
+ // Each pass thinner and fainter than the one over it; a bar's first is as thick as the bar.
316
+ add(firm, ink(side.color, wide(1), side.kind, 0.88), l.main);
317
+ add(faint, ink(side.color, wide(0.74), side.kind, 0.5), l.again);
318
+ if (l.third)
319
+ add(soft, ink(side.color, wide(0.55), side.kind, 0.32), l.third);
320
+ });
321
+ for (const g of [soft, faint, firm])
322
+ for (const [css, path] of g)
323
+ parts.push([path, css]);
324
+ s.el.setAttribute('data-sketch', d.mark);
325
+ svg.setAttribute('style', 'position:absolute;left:' + -d.left + 'px;top:' + -d.top + 'px' + SVG_CSS);
326
+ while (s.paths.length < parts.length)
327
+ s.paths.push(svg.appendChild(document.createElementNS(SVG, 'path')));
328
+ while (s.paths.length > parts.length)
329
+ s.paths.pop()?.remove();
330
+ s.paths.forEach((p, i) => {
331
+ p.setAttribute('d', parts[i]?.[0] ?? '');
332
+ p.setAttribute('style', parts[i]?.[1] ?? '');
333
+ });
334
+ }
335
+ if (!fresh.length) {
336
+ positions.clear();
337
+ return;
338
+ }
339
+ // The strokes are a child, which :empty and its like can tell. Where the film's styles ask for something else now, they come off again in this drawing.
340
+ box.removeAttribute('data-sketch-on');
341
+ positions.clear();
342
+ fresh = fresh.filter((t) => keyOf(t.s).key !== t.key);
343
+ box.setAttribute('data-sketch-on', '');
344
+ for (const t of fresh) {
345
+ t.s.el.removeAttribute('data-sketch');
346
+ t.s.svg?.remove();
347
+ t.s.key = '';
348
+ }
349
+ positions.clear();
350
+ };
351
+ return () => {
352
+ if (dead)
353
+ return;
354
+ try {
355
+ draw();
356
+ }
357
+ catch (error) {
358
+ dead = true;
359
+ let why = 'error';
360
+ try {
361
+ off();
362
+ why = String(error instanceof Error ? error.message : error).slice(0, 200);
363
+ }
364
+ catch {
365
+ // The reason could not be read.
366
+ }
367
+ said = false;
368
+ say(why);
369
+ }
370
+ };
371
+ };
@@ -0,0 +1,41 @@
1
+ export type Sides = [number, number, number, number];
2
+ export interface SketchBoxOptions {
3
+ /** The square root of the stage's area over that of 1280 by 720. */
4
+ u: number;
5
+ /** The border's width at top, right, bottom, left; 0 for a side not drawn. */
6
+ sides: Sides;
7
+ /** Drawn on the box's edge, not down the border's middle. */
8
+ onEdge: boolean;
9
+ /** One stroke along the longer side. */
10
+ bar: boolean;
11
+ /** Dashed or dotted. */
12
+ broken: boolean;
13
+ /** The corner as CSS states it. */
14
+ rx: number;
15
+ ry: number;
16
+ /** 0: none; 1: close; 2: wide. */
17
+ hatch: 0 | 1 | 2;
18
+ /** [x0, y0, x1, y1] that the hatching keeps off. */
19
+ skip: Sides | null;
20
+ }
21
+ /** One line as three pencil passes, each straying further than the last, as path data. */
22
+ export interface SketchStroke {
23
+ main: string;
24
+ again: string;
25
+ third: string;
26
+ width: string;
27
+ }
28
+ export interface SketchBox {
29
+ wash: string;
30
+ hatch: string;
31
+ cross: string;
32
+ /** The hatching's pen width. */
33
+ fine: string;
34
+ /** Top, right, bottom, left; or the one line of a bar or a round box first. */
35
+ lines: [SketchStroke | null, SketchStroke | null, SketchStroke | null, SketchStroke | null];
36
+ }
37
+ /**
38
+ * The strokes for one element of w by h px, as path data: a pure function of its arguments, so that a frame follows
39
+ * from t alone.
40
+ */
41
+ export declare function sketchBox(seed: number, w: number, h: number, o: SketchBoxOptions): SketchBox;
@@ -0,0 +1,187 @@
1
+ const n1 = (n) => String(Math.round(n * 10) / 10);
2
+ const n0 = (n) => String(Math.round(n));
3
+ function through(p) {
4
+ const first = p[0] ?? [0, 0];
5
+ const last = p[p.length - 1] ?? first;
6
+ let d = 'M' + n1(first[0]) + ' ' + n1(first[1]);
7
+ for (let k = 1; k < p.length - 1; k++) {
8
+ const a = p[k] ?? first;
9
+ const b = p[k + 1] ?? last;
10
+ d += 'Q' + n1(a[0]) + ' ' + n1(a[1]) + ' ' + n1((a[0] + b[0]) / 2) + ' ' + n1((a[1] + b[1]) / 2);
11
+ }
12
+ return d + 'L' + n1(last[0]) + ' ' + n1(last[1]);
13
+ }
14
+ /**
15
+ * The strokes for one element of w by h px, as path data: a pure function of its arguments, so that a frame follows
16
+ * from t alone.
17
+ */
18
+ export function sketchBox(seed, w, h, o) {
19
+ const u = o.u;
20
+ const step = 110 * u;
21
+ const thin = Math.min(w, h);
22
+ const b = o.sides;
23
+ // Each line has random numbers of its own, drawn in an order its length does not change: as a box grows, what is drawn stays put.
24
+ const stream = (k) => {
25
+ // Mixed first: the generator is linear, and would give neighbouring seeds the same numbers a constant apart.
26
+ let m = seed + k * 104729;
27
+ m ^= m >>> 16;
28
+ m = Math.imul(m, 0x45d9f3b);
29
+ m ^= m >>> 16;
30
+ let s = ((m >>> 1) % 2147483646) + 1;
31
+ return () => (s = (s * 16807) % 2147483647) / 2147483647;
32
+ };
33
+ const line = (roll, x0, y0, x1, y1, amp) => {
34
+ const len = Math.hypot(x1 - x0, y1 - y0) || 1;
35
+ const ux = (x1 - x0) / len;
36
+ const uy = (y1 - y0) / len;
37
+ const over = Math.min(37 * u, len * 0.11, thin * 0.16);
38
+ const a = over * (0.3 + roll());
39
+ const all = len + a + over * (0.3 + roll());
40
+ const far = amp * Math.min(1, all / step);
41
+ const p = [];
42
+ let off = roll() * 2 - 1;
43
+ const put = (s, f) => {
44
+ p.push([x0 + ux * (s - a) - uy * f * far, y0 + uy * (s - a) + ux * f * far]);
45
+ };
46
+ for (let s = 0;; s += step) {
47
+ const next = roll() * 2 - 1;
48
+ put(s, off);
49
+ if (!(s + step < all) || p.length > 400) {
50
+ put(all, off + ((next - off) * (all - s)) / step);
51
+ return through(p);
52
+ }
53
+ off = next;
54
+ }
55
+ };
56
+ // On one box a wider border is never a thinner pen: what chance adds is the box's own, and under 1 px.
57
+ const base = (3 + Math.max(0, Math.min(1, thin / u / 100 - 1.5))) * u;
58
+ const chance = stream(0)() * Math.min(0.5 * u, 0.8);
59
+ const pen = (bw) => Math.max(2 * u, base + bw - 2) + chance;
60
+ // Pencil: three thin passes; the third is spared on a small element, where it would only thicken the line.
61
+ const stroke = (k, bw, draw) => ({
62
+ main: draw(stream(1 + k), 1),
63
+ again: draw(stream(11 + k), 1.5),
64
+ third: Math.max(w, h) > step ? draw(stream(31 + k), 2.1) : '',
65
+ width: n1(pen(bw)),
66
+ });
67
+ const out = { wash: '', hatch: '', cross: '', fine: n1(1.5 * u), lines: [null, null, null, null] };
68
+ if (o.bar) {
69
+ // With its round ends the stroke covers what the element covers: a dot where the sides are equal.
70
+ out.lines[0] = stroke(0, thin - base + 2, (roll, k) => (w >= h ? line(roll, h / 2, h / 2, w - h / 2, h / 2, 3 * u * k) : line(roll, w / 2, w / 2, w / 2, h - w / 2, 3 * u * k)));
71
+ return out;
72
+ }
73
+ const mid = o.onEdge ? 0 : 0.5;
74
+ const x0 = b[3] * mid;
75
+ const y0 = b[0] * mid;
76
+ const x1 = w - b[1] * mid;
77
+ const y1 = h - b[2] * mid;
78
+ const whole = !!(b[0] && b[1] && b[2] && b[3]);
79
+ // As CSS does, radii too large for the box are shrunk by one factor.
80
+ const fit = Math.min(1, o.rx ? w / (2 * o.rx) : 1, o.ry ? h / (2 * o.ry) : 1);
81
+ const round = whole && Math.min(o.rx, o.ry) * fit >= 0.3 * thin;
82
+ const rx = round ? Math.max(0, o.rx * fit - x0) : 0;
83
+ const ry = round ? Math.max(0, o.ry * fit - y0) : 0;
84
+ if (round)
85
+ out.lines[0] = stroke(0, b[0], (roll, k) => ring(roll, k));
86
+ else {
87
+ const edges = [
88
+ [x0, y0, x1, y0],
89
+ [x1, y0, x1, y1],
90
+ [x1, y1, x0, y1],
91
+ [x0, y1, x0, y0],
92
+ ];
93
+ edges.forEach((e, k) => {
94
+ if (b[k])
95
+ out.lines[k] = stroke(k, b[k] ?? 0, (roll, more) => line(roll, e[0], e[1], e[2], e[3], 4.5 * u * more));
96
+ });
97
+ }
98
+ function ring(roll, k) {
99
+ // Each point: x, y, the outward normal, and a weight, 0 on a corner and 1 mid-way along a straight run.
100
+ const p = [];
101
+ const q = [];
102
+ const starts = [];
103
+ const full = 4.5 * u * k * 0.8;
104
+ const amp = Math.min(full, (full * Math.min(rx, ry)) / (96 * u));
105
+ const corners = [
106
+ [x1 - rx, y0 + ry, -90, 0, 1],
107
+ [x1 - rx, y1 - ry, 0, -1, 0],
108
+ [x0 + rx, y1 - ry, 90, 0, -1],
109
+ [x0 + rx, y0 + ry, 180, 1, 0],
110
+ ];
111
+ corners.forEach((corner, j) => {
112
+ starts.push(p.length + 3);
113
+ for (let i = 0; i <= 6; i++) {
114
+ const t = ((corner[2] + 15 * i) * Math.PI) / 180;
115
+ p.push([corner[0] + rx * Math.cos(t), corner[1] + ry * Math.sin(t), Math.cos(t), Math.sin(t), 0]);
116
+ }
117
+ const run = j % 2 ? x1 - x0 - 2 * rx : y1 - y0 - 2 * ry;
118
+ const n = Math.min(Math.floor(run / step), 400);
119
+ const end = p[p.length - 1];
120
+ if (end)
121
+ for (let m = 1; m <= n; m++)
122
+ p.push([end[0] + (corner[3] * run * m) / (n + 1), end[1] + (corner[4] * run * m) / (n + 1), end[2], end[3], Math.sin((Math.PI * m) / (n + 1))]);
123
+ });
124
+ // A dashed ring stops where it began: dashes laid over dashes would show out of step.
125
+ const from = starts[Math.floor(roll() * 4)] ?? 0;
126
+ const p1 = roll() * 6.3;
127
+ const p2 = roll() * 6.3;
128
+ const p3 = roll() * 6.3;
129
+ const last = p.length + (o.broken ? 0 : 3);
130
+ let was = null;
131
+ for (let i = 0; i <= last; i++) {
132
+ // A slow swell and a drift, so that the end misses the beginning.
133
+ const at = p[(from + i) % p.length];
134
+ if (!at)
135
+ continue;
136
+ const t = ((from + i) * 2 * Math.PI) / p.length;
137
+ const off = (amp + at[4] * (full - amp)) * (0.55 * Math.sin(t + p1) + 0.35 * Math.sin(2 * t + p2) + 0.2 * Math.sin(3 * t + p3)) + amp * 0.9 * (i / last - 0.5);
138
+ // With no straight run between two corners, one ends on the point the next begins with.
139
+ if (!was || Math.hypot(at[0] - was[0], at[1] - was[1]) > 0.5)
140
+ q.push([at[0] + at[2] * off, at[1] + at[3] * off]);
141
+ was = at;
142
+ }
143
+ return through(q);
144
+ }
145
+ if (!whole || !o.hatch)
146
+ return out;
147
+ const arc = (x, y) => (round ? 'A' + n1(rx) + ' ' + n1(ry) + ' 0 0 1 ' + n1(x) + ' ' + n1(y) : '');
148
+ out.wash =
149
+ 'M' + n1(x0 + rx) + ' ' + n1(y0) + 'H' + n1(x1 - rx) + arc(x1, y0 + ry) + 'V' + n1(y1 - ry) + arc(x1 - rx, y1) + 'H' + n1(x0 + rx) + arc(x0, y1 - ry) + 'V' + n1(y0 + ry) + arc(x0 + rx, y0) + 'Z';
150
+ const skip = o.skip;
151
+ const inside = (x, y) => {
152
+ if (x < x0 || x > x1 || y < y0 || y > y1 || (skip && x > skip[0] && x < skip[2] && y > skip[1] && y < skip[3]))
153
+ return false;
154
+ const ex = x < x0 + rx ? x0 + rx - x : x > x1 - rx ? x - (x1 - rx) : 0;
155
+ const ey = y < y0 + ry ? y0 + ry - y : y > y1 - ry ? y - (y1 - ry) : 0;
156
+ return !ex || !ey || (ex * ex) / (rx * rx) + (ey * ey) / (ry * ry) <= 1;
157
+ };
158
+ // Shading is two layers a few degrees apart, the second more open, and spared in a small box.
159
+ const layer = (k, lean, wide) => {
160
+ let d = '';
161
+ const roll = stream(k);
162
+ const turn = ((38 + lean + roll() * 10) * Math.PI) / 180;
163
+ const dx = -Math.sin(turn);
164
+ const dy = Math.cos(turn);
165
+ const gap = ((o.hatch === 2 ? 20 : 12) * wide * u) / dy;
166
+ const inch = 3 * u;
167
+ for (let px = gap * (0.3 + roll() * 0.5); px < w - (h * dx) / dy && d.length < 6000; px += gap * (0.7 + roll() * 0.6)) {
168
+ const trim = roll() * 2 * inch;
169
+ let from = -1;
170
+ for (let a = 0; a <= h / dy + inch; a += inch) {
171
+ const on = a <= h / dy && inside(px + dx * a, dy * a);
172
+ if (on && from < 0)
173
+ from = a;
174
+ if (!on && from >= 0) {
175
+ if (a - inch - from > 4 * inch)
176
+ d += 'M' + n0(px + dx * (from + trim)) + ' ' + n0(dy * (from + trim)) + 'L' + n0(px + dx * (a - inch)) + ' ' + n0(dy * (a - inch));
177
+ from = -1;
178
+ }
179
+ }
180
+ }
181
+ return d;
182
+ };
183
+ out.hatch = layer(21, 0, 1);
184
+ if (thin > step)
185
+ out.cross = layer(22, 7, 1.7);
186
+ return out;
187
+ }
@@ -0,0 +1,14 @@
1
+ /**
2
+ * How a snapshot's SVG picture is drawn at w × h. Shared by the stage (plates) and an export's raster frame.
3
+ * Resolves with a canvas or an ImageBitmap; given a canvas to draw in, always with an ImageBitmap.
4
+ */
5
+ export type SnapshotSource = HTMLImageElement | HTMLCanvasElement | ImageBitmap;
6
+ export type SnapshotSurface = HTMLCanvasElement | ImageBitmap;
7
+ /** The catch sees a refusal, or a given canvas that is lost or cannot be copied: it draws in a new one. */
8
+ export declare function drawSnapshot(picture: SnapshotSource, w: number, h: number, into?: HTMLCanvasElement | null): Promise<SnapshotSurface>;
9
+ /**
10
+ * Ends an error message: why the text does not parse as XML, in this browser's parser's words (120 characters at
11
+ * most), or '' when it parses, the parser throws, or the text is so long that asking would be slow. Firefox puts
12
+ * the page's address among its words; it is left out.
13
+ */
14
+ export declare function whyNoDecode(svg: string): string;