@celljs/ta-motion 0.0.0-stage → 4.41.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/README.en.md +11 -0
- package/README.md +10 -2
- package/extension/index.mjs +105 -0
- package/package.json +59 -4
- package/web/index.js +2233 -0
package/web/index.js
ADDED
|
@@ -0,0 +1,2233 @@
|
|
|
1
|
+
// ../ta-protocol/esm/common/language.js
|
|
2
|
+
var TaLanguageUtil;
|
|
3
|
+
(function(TaLanguageUtil2) {
|
|
4
|
+
TaLanguageUtil2.ALL = ["zh-CN", "zh-TW", "en-US", "ja-JP"];
|
|
5
|
+
TaLanguageUtil2.NAMES = { "zh-CN": "简体中文", "zh-TW": "繁體中文", "en-US": "English", "ja-JP": "日本語" };
|
|
6
|
+
function of(tag) {
|
|
7
|
+
const said = (tag !== null && tag !== undefined ? tag : "").toLowerCase().replace(/_/g, "-");
|
|
8
|
+
if (/^zh(\b|-)/.test(said)) {
|
|
9
|
+
return /-hant\b/.test(said) || !/-hans\b/.test(said) && /-(tw|hk|mo)\b/.test(said) ? "zh-TW" : "zh-CN";
|
|
10
|
+
}
|
|
11
|
+
return /^ja(\b|-)/.test(said) ? "ja-JP" : "en-US";
|
|
12
|
+
}
|
|
13
|
+
TaLanguageUtil2.of = of;
|
|
14
|
+
function server(env = typeof process === "undefined" ? {} : process.env) {
|
|
15
|
+
return of(env.TA_LANGUAGE || env.LC_ALL || env.LC_MESSAGES || env.LANG);
|
|
16
|
+
}
|
|
17
|
+
TaLanguageUtil2.server = server;
|
|
18
|
+
function pick(language, words) {
|
|
19
|
+
return words[of(language)];
|
|
20
|
+
}
|
|
21
|
+
TaLanguageUtil2.pick = pick;
|
|
22
|
+
function say(words) {
|
|
23
|
+
return words[server()];
|
|
24
|
+
}
|
|
25
|
+
TaLanguageUtil2.say = say;
|
|
26
|
+
function chinese(language) {
|
|
27
|
+
return of(language).startsWith("zh");
|
|
28
|
+
}
|
|
29
|
+
TaLanguageUtil2.chinese = chinese;
|
|
30
|
+
function cjk(language) {
|
|
31
|
+
return of(language) !== "en-US";
|
|
32
|
+
}
|
|
33
|
+
TaLanguageUtil2.cjk = cjk;
|
|
34
|
+
function list(language, items) {
|
|
35
|
+
return items.join(cjk(language) ? "、" : ", ");
|
|
36
|
+
}
|
|
37
|
+
TaLanguageUtil2.list = list;
|
|
38
|
+
})(TaLanguageUtil || (TaLanguageUtil = {}));
|
|
39
|
+
|
|
40
|
+
// web-src/i18n.ts
|
|
41
|
+
var EN = {
|
|
42
|
+
making: "Making the animation…",
|
|
43
|
+
scenesSoFar: "{count} scenes so far",
|
|
44
|
+
failed: "This animation cannot play: {reason}",
|
|
45
|
+
play: "Play",
|
|
46
|
+
pause: "Pause",
|
|
47
|
+
replay: "Play again",
|
|
48
|
+
export: "Export as MP4 video",
|
|
49
|
+
exporting: "Exporting the video {percent}%",
|
|
50
|
+
exported: "Saved as {name}",
|
|
51
|
+
exportFailed: "The video was not exported: {reason}",
|
|
52
|
+
unsupported: "This device cannot make a video: export it in Ta on your computer.",
|
|
53
|
+
edit: "Edit",
|
|
54
|
+
done: "Done",
|
|
55
|
+
scene: "Scene {n}",
|
|
56
|
+
seconds: "Length (s)",
|
|
57
|
+
caption: "Subtitle",
|
|
58
|
+
words: "Words",
|
|
59
|
+
value: "Value",
|
|
60
|
+
label: "Label",
|
|
61
|
+
values: "Values, by commas",
|
|
62
|
+
labels: "Labels, by commas",
|
|
63
|
+
stale: "A newer version came first: it is shown now.",
|
|
64
|
+
untitled: "Animation",
|
|
65
|
+
kinds: {
|
|
66
|
+
text: "Words",
|
|
67
|
+
number: "Number",
|
|
68
|
+
bars: "Bar chart",
|
|
69
|
+
line: "Line chart",
|
|
70
|
+
pie: "Pie chart",
|
|
71
|
+
list: "List",
|
|
72
|
+
shape: "Shape",
|
|
73
|
+
image: "Picture"
|
|
74
|
+
}
|
|
75
|
+
};
|
|
76
|
+
var ZH = {
|
|
77
|
+
making: "正在制作动画…",
|
|
78
|
+
scenesSoFar: "已写好 {count} 个场景",
|
|
79
|
+
failed: "这段动画放不出来:{reason}",
|
|
80
|
+
play: "播放",
|
|
81
|
+
pause: "暂停",
|
|
82
|
+
replay: "重播",
|
|
83
|
+
export: "导出为 MP4 视频",
|
|
84
|
+
exporting: "正在导出视频 {percent}%",
|
|
85
|
+
exported: "已存为 {name}",
|
|
86
|
+
exportFailed: "视频没导出来:{reason}",
|
|
87
|
+
unsupported: "这台设备做不了视频,请在电脑上的 Ta 里导出。",
|
|
88
|
+
edit: "修改",
|
|
89
|
+
done: "完成",
|
|
90
|
+
scene: "场景 {n}",
|
|
91
|
+
seconds: "时长(秒)",
|
|
92
|
+
caption: "字幕",
|
|
93
|
+
words: "文字",
|
|
94
|
+
value: "数值",
|
|
95
|
+
label: "名称",
|
|
96
|
+
values: "数值,用逗号隔开",
|
|
97
|
+
labels: "名称,用逗号隔开",
|
|
98
|
+
stale: "已有更新的版本,现在显示的是它。",
|
|
99
|
+
untitled: "动画",
|
|
100
|
+
kinds: {
|
|
101
|
+
text: "文字",
|
|
102
|
+
number: "数字",
|
|
103
|
+
bars: "柱状图",
|
|
104
|
+
line: "折线图",
|
|
105
|
+
pie: "饼图",
|
|
106
|
+
list: "列表",
|
|
107
|
+
shape: "形状",
|
|
108
|
+
image: "图片"
|
|
109
|
+
}
|
|
110
|
+
};
|
|
111
|
+
var ZH_TW = {
|
|
112
|
+
making: "正在製作動畫…",
|
|
113
|
+
scenesSoFar: "已寫好 {count} 個場景",
|
|
114
|
+
failed: "這段動畫放不出來:{reason}",
|
|
115
|
+
play: "播放",
|
|
116
|
+
pause: "暫停",
|
|
117
|
+
replay: "重播",
|
|
118
|
+
export: "匯出為 MP4 影片",
|
|
119
|
+
exporting: "正在匯出影片 {percent}%",
|
|
120
|
+
exported: "已存為 {name}",
|
|
121
|
+
exportFailed: "影片沒匯出來:{reason}",
|
|
122
|
+
unsupported: "這台裝置做不了影片,請在電腦上的 Ta 裡匯出。",
|
|
123
|
+
edit: "修改",
|
|
124
|
+
done: "完成",
|
|
125
|
+
scene: "場景 {n}",
|
|
126
|
+
seconds: "時長(秒)",
|
|
127
|
+
caption: "字幕",
|
|
128
|
+
words: "文字",
|
|
129
|
+
value: "數值",
|
|
130
|
+
label: "名稱",
|
|
131
|
+
values: "數值,用逗號隔開",
|
|
132
|
+
labels: "名稱,用逗號隔開",
|
|
133
|
+
stale: "已有更新的版本,現在顯示的是它。",
|
|
134
|
+
untitled: "動畫",
|
|
135
|
+
kinds: {
|
|
136
|
+
text: "文字",
|
|
137
|
+
number: "數字",
|
|
138
|
+
bars: "長條圖",
|
|
139
|
+
line: "折線圖",
|
|
140
|
+
pie: "圓餅圖",
|
|
141
|
+
list: "清單",
|
|
142
|
+
shape: "形狀",
|
|
143
|
+
image: "圖片"
|
|
144
|
+
}
|
|
145
|
+
};
|
|
146
|
+
var JA = {
|
|
147
|
+
making: "アニメーションを作成中…",
|
|
148
|
+
scenesSoFar: "{count} シーンまで作成済み",
|
|
149
|
+
failed: "このアニメーションは再生できません:{reason}",
|
|
150
|
+
play: "再生",
|
|
151
|
+
pause: "一時停止",
|
|
152
|
+
replay: "もう一度再生",
|
|
153
|
+
export: "MP4 動画として書き出す",
|
|
154
|
+
exporting: "動画を書き出し中 {percent}%",
|
|
155
|
+
exported: "{name} として保存しました",
|
|
156
|
+
exportFailed: "動画を書き出せませんでした:{reason}",
|
|
157
|
+
unsupported: "この端末では動画を作れません。パソコンの Ta で書き出してください。",
|
|
158
|
+
edit: "編集",
|
|
159
|
+
done: "完了",
|
|
160
|
+
scene: "シーン {n}",
|
|
161
|
+
seconds: "長さ(秒)",
|
|
162
|
+
caption: "字幕",
|
|
163
|
+
words: "文字",
|
|
164
|
+
value: "数値",
|
|
165
|
+
label: "名前",
|
|
166
|
+
values: "数値(カンマ区切り)",
|
|
167
|
+
labels: "名前(カンマ区切り)",
|
|
168
|
+
stale: "新しい版が先に届いたため、そちらを表示しています。",
|
|
169
|
+
untitled: "アニメーション",
|
|
170
|
+
kinds: {
|
|
171
|
+
text: "文字",
|
|
172
|
+
number: "数値",
|
|
173
|
+
bars: "棒グラフ",
|
|
174
|
+
line: "折れ線グラフ",
|
|
175
|
+
pie: "円グラフ",
|
|
176
|
+
list: "リスト",
|
|
177
|
+
shape: "図形",
|
|
178
|
+
image: "画像"
|
|
179
|
+
}
|
|
180
|
+
};
|
|
181
|
+
var WORDS = { "zh-CN": ZH, "zh-TW": ZH_TW, "en-US": EN, "ja-JP": JA };
|
|
182
|
+
var MotionWords;
|
|
183
|
+
((MotionWords) => {
|
|
184
|
+
function of(language) {
|
|
185
|
+
return TaLanguageUtil.pick(language, WORDS);
|
|
186
|
+
}
|
|
187
|
+
MotionWords.of = of;
|
|
188
|
+
function fill(text, values) {
|
|
189
|
+
return text.replace(/\{(\w+)\}/g, (all, name) => (name in values) ? String(values[name]) : all);
|
|
190
|
+
}
|
|
191
|
+
MotionWords.fill = fill;
|
|
192
|
+
})(MotionWords ||= {});
|
|
193
|
+
|
|
194
|
+
// web-src/motion-draw.ts
|
|
195
|
+
var THEMES = {
|
|
196
|
+
dark: {
|
|
197
|
+
background: "#0b1020",
|
|
198
|
+
text: "#f3f5f9",
|
|
199
|
+
muted: "rgba(243, 245, 249, 0.62)",
|
|
200
|
+
grid: "rgba(255, 255, 255, 0.12)",
|
|
201
|
+
glow: 0.22,
|
|
202
|
+
pill: "rgba(0, 0, 0, 0.5)"
|
|
203
|
+
},
|
|
204
|
+
light: {
|
|
205
|
+
background: "#f6f7fb",
|
|
206
|
+
text: "#0f172a",
|
|
207
|
+
muted: "rgba(15, 23, 42, 0.58)",
|
|
208
|
+
grid: "rgba(15, 23, 42, 0.12)",
|
|
209
|
+
glow: 0.14,
|
|
210
|
+
pill: "rgba(255, 255, 255, 0.82)"
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
var COLOURS = ["#22c55e", "#f59e0b", "#ef4444", "#a855f7", "#06b6d4", "#ec4899", "#84cc16"];
|
|
214
|
+
var FONT = '"PingFang SC", "Hiragino Sans GB", "Noto Sans CJK SC", "Microsoft YaHei", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif';
|
|
215
|
+
var TEXT_SIZES = { hero: 0.16, title: 0.085, subtitle: 0.058, body: 0.048, caption: 0.034 };
|
|
216
|
+
var TRANSITION = 0.6;
|
|
217
|
+
var ENTER = 0.6;
|
|
218
|
+
var clamp01 = (value) => Math.min(1, Math.max(0, value));
|
|
219
|
+
var MotionEase;
|
|
220
|
+
((MotionEase) => {
|
|
221
|
+
MotionEase.out = (p) => 1 - (1 - p) ** 3;
|
|
222
|
+
MotionEase.outQuart = (p) => 1 - (1 - p) ** 4;
|
|
223
|
+
MotionEase.back = (p) => {
|
|
224
|
+
const c = 1.70158;
|
|
225
|
+
return 1 + (c + 1) * (p - 1) ** 3 + c * (p - 1) ** 2;
|
|
226
|
+
};
|
|
227
|
+
MotionEase.inOut = (p) => p < 0.5 ? 4 * p ** 3 : 1 - (-2 * p + 2) ** 3 / 2;
|
|
228
|
+
})(MotionEase ||= {});
|
|
229
|
+
var MotionLayout;
|
|
230
|
+
((MotionLayout) => {
|
|
231
|
+
const WIDE = /[\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef\u3000-\u303f]/;
|
|
232
|
+
function wrap(text, max, measure) {
|
|
233
|
+
const lines = [];
|
|
234
|
+
for (const paragraph of text.split(`
|
|
235
|
+
`)) {
|
|
236
|
+
const tokens = paragraph.split(/(\s+)/).flatMap((token) => WIDE.test(token) ? [
|
|
237
|
+
...token.matchAll(/[\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef\u3000-\u303f]|[^\u2e80-\u9fff\uac00-\ud7af\uf900-\ufaff\uff00-\uffef\u3000-\u303f]+/g)
|
|
238
|
+
].map((match) => match[0]) : [token]);
|
|
239
|
+
let line = "";
|
|
240
|
+
for (const token of tokens) {
|
|
241
|
+
const next = line + token;
|
|
242
|
+
if (line && measure(next.trimEnd()) > max) {
|
|
243
|
+
lines.push(line.trimEnd());
|
|
244
|
+
line = token.trimStart();
|
|
245
|
+
} else {
|
|
246
|
+
line = next;
|
|
247
|
+
}
|
|
248
|
+
}
|
|
249
|
+
lines.push(line.trimEnd());
|
|
250
|
+
}
|
|
251
|
+
return lines;
|
|
252
|
+
}
|
|
253
|
+
MotionLayout.wrap = wrap;
|
|
254
|
+
function format(value, decimals) {
|
|
255
|
+
return value.toLocaleString("en-US", { minimumFractionDigits: decimals, maximumFractionDigits: decimals });
|
|
256
|
+
}
|
|
257
|
+
MotionLayout.format = format;
|
|
258
|
+
function short(value) {
|
|
259
|
+
const size = Math.abs(value);
|
|
260
|
+
const cut = (n, unit) => `${Number((value / n).toFixed(n === 1 ? 2 : 1))}${unit}`;
|
|
261
|
+
return size >= 1e9 ? cut(1e9, "B") : size >= 1e6 ? cut(1e6, "M") : size >= 1e4 ? cut(1000, "k") : cut(1, "");
|
|
262
|
+
}
|
|
263
|
+
MotionLayout.short = short;
|
|
264
|
+
})(MotionLayout ||= {});
|
|
265
|
+
|
|
266
|
+
class MotionCanvas {
|
|
267
|
+
context;
|
|
268
|
+
motion;
|
|
269
|
+
pictures;
|
|
270
|
+
palette;
|
|
271
|
+
width = 0;
|
|
272
|
+
height = 0;
|
|
273
|
+
unit = 0;
|
|
274
|
+
constructor(context, motion, pictures = () => {
|
|
275
|
+
return;
|
|
276
|
+
}) {
|
|
277
|
+
this.context = context;
|
|
278
|
+
this.motion = motion;
|
|
279
|
+
this.pictures = pictures;
|
|
280
|
+
this.palette = THEMES[motion.theme];
|
|
281
|
+
}
|
|
282
|
+
fill(fill) {
|
|
283
|
+
if (typeof fill === "string") {
|
|
284
|
+
return fill;
|
|
285
|
+
}
|
|
286
|
+
const gradient = this.context.createLinearGradient(0, 0, this.width, this.height);
|
|
287
|
+
gradient.addColorStop(0, fill[0]);
|
|
288
|
+
gradient.addColorStop(1, fill[1]);
|
|
289
|
+
return gradient;
|
|
290
|
+
}
|
|
291
|
+
font(size, bold, scale = 1) {
|
|
292
|
+
const px = Math.max(8, TEXT_SIZES[size] * this.unit * scale);
|
|
293
|
+
return { px, font: `${bold ? 700 : 500} ${px.toFixed(1)}px ${FONT}` };
|
|
294
|
+
}
|
|
295
|
+
wrap(text, max) {
|
|
296
|
+
return MotionLayout.wrap(text, max, (run) => this.context.measureText(run).width);
|
|
297
|
+
}
|
|
298
|
+
colour(index, own) {
|
|
299
|
+
return own ?? (index === 0 ? this.motion.accent : COLOURS[(index - 1) % COLOURS.length]);
|
|
300
|
+
}
|
|
301
|
+
rounded(x, y, w, h, r) {
|
|
302
|
+
const ctx = this.context;
|
|
303
|
+
const radius = Math.max(0, Math.min(r, Math.abs(w) / 2, Math.abs(h) / 2));
|
|
304
|
+
ctx.beginPath();
|
|
305
|
+
if (w <= 0 || h <= 0) {
|
|
306
|
+
return;
|
|
307
|
+
}
|
|
308
|
+
ctx.moveTo(x + radius, y);
|
|
309
|
+
ctx.arcTo(x + w, y, x + w, y + h, radius);
|
|
310
|
+
ctx.arcTo(x + w, y + h, x, y + h, radius);
|
|
311
|
+
ctx.arcTo(x, y + h, x, y, radius);
|
|
312
|
+
ctx.arcTo(x, y, x + w, y, radius);
|
|
313
|
+
ctx.closePath();
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
// web-src/motion-parts.ts
|
|
318
|
+
class MotionParts extends MotionCanvas {
|
|
319
|
+
text(part, w, shown) {
|
|
320
|
+
const ctx = this.context;
|
|
321
|
+
const { px, font } = this.font(part.size, part.bold);
|
|
322
|
+
ctx.font = font;
|
|
323
|
+
ctx.fillStyle = part.color ?? (part.size === "subtitle" || part.size === "caption" ? this.palette.muted : this.palette.text);
|
|
324
|
+
ctx.textBaseline = "middle";
|
|
325
|
+
ctx.textAlign = part.align;
|
|
326
|
+
const x = part.align === "left" ? -w / 2 : part.align === "right" ? w / 2 : 0;
|
|
327
|
+
const lines = this.wrap(part.text, w);
|
|
328
|
+
let left = Math.round([...part.text].length * shown);
|
|
329
|
+
const height = px * 1.22;
|
|
330
|
+
let y = -((lines.length - 1) * height) / 2;
|
|
331
|
+
for (const line of lines) {
|
|
332
|
+
const chars = [...line];
|
|
333
|
+
const visible = left >= chars.length ? line : chars.slice(0, Math.max(0, left)).join("");
|
|
334
|
+
left -= chars.length;
|
|
335
|
+
if (visible) {
|
|
336
|
+
ctx.fillText(visible, x, y);
|
|
337
|
+
}
|
|
338
|
+
y += height;
|
|
339
|
+
}
|
|
340
|
+
}
|
|
341
|
+
number(part, w, h, grow) {
|
|
342
|
+
const ctx = this.context;
|
|
343
|
+
const value = part.from + (part.value - part.from) * MotionEase.outQuart(grow);
|
|
344
|
+
const { px, font } = this.font(part.size, true);
|
|
345
|
+
const label = part.label ? this.font("body", false) : undefined;
|
|
346
|
+
const top = -h / 2;
|
|
347
|
+
ctx.textBaseline = "middle";
|
|
348
|
+
ctx.textAlign = part.align;
|
|
349
|
+
const x = part.align === "left" ? -w / 2 : part.align === "right" ? w / 2 : 0;
|
|
350
|
+
ctx.font = font;
|
|
351
|
+
ctx.fillStyle = part.color ?? this.motion.accent;
|
|
352
|
+
ctx.fillText(`${part.prefix}${MotionLayout.format(value, part.decimals)}${part.suffix}`, x, top + px * 0.55);
|
|
353
|
+
if (label && part.label) {
|
|
354
|
+
ctx.font = label.font;
|
|
355
|
+
ctx.fillStyle = this.palette.muted;
|
|
356
|
+
ctx.fillText(part.label, x, top + px * 1.1 + label.px * 0.8);
|
|
357
|
+
}
|
|
358
|
+
}
|
|
359
|
+
bars(part, w, h, since) {
|
|
360
|
+
const ctx = this.context;
|
|
361
|
+
const count = part.items.length;
|
|
362
|
+
const step = Math.min(0.15, part.dur / (count * 2));
|
|
363
|
+
const growOf = (index) => part.enter === "none" ? 1 : MotionEase.out(clamp01((since - index * step) / Math.max(0.05, part.dur - step * (count - 1))));
|
|
364
|
+
const values = part.items.map((item) => item.value);
|
|
365
|
+
const max = Math.max(0, ...values);
|
|
366
|
+
const min = Math.min(0, ...values);
|
|
367
|
+
const span = max - min || 1;
|
|
368
|
+
const value = this.font("body", true, 0.85);
|
|
369
|
+
const label = this.font("caption", false);
|
|
370
|
+
const unit = part.unit ? ` ${part.unit}` : "";
|
|
371
|
+
const radius = this.unit * 0.008;
|
|
372
|
+
if (part.horizontal) {
|
|
373
|
+
const labels = w * 0.3;
|
|
374
|
+
const row = h / count;
|
|
375
|
+
const bar2 = Math.min(row * 0.62, this.unit * 0.07);
|
|
376
|
+
const room = w - labels - value.px * 3.2;
|
|
377
|
+
part.items.forEach((item, index) => {
|
|
378
|
+
const g = growOf(index);
|
|
379
|
+
const y = -h / 2 + row * (index + 0.5);
|
|
380
|
+
ctx.font = label.font;
|
|
381
|
+
ctx.fillStyle = this.palette.muted;
|
|
382
|
+
ctx.textAlign = "right";
|
|
383
|
+
ctx.textBaseline = "middle";
|
|
384
|
+
ctx.fillText(item.label, -w / 2 + labels - label.px * 0.6, y);
|
|
385
|
+
const length = Math.max(0, Math.max(0, item.value) / (max || 1) * room * g);
|
|
386
|
+
ctx.fillStyle = this.colour(0, item.color ?? part.color);
|
|
387
|
+
this.rounded(-w / 2 + labels, y - bar2 / 2, length, bar2, radius);
|
|
388
|
+
ctx.fill();
|
|
389
|
+
if (g > 0) {
|
|
390
|
+
ctx.font = value.font;
|
|
391
|
+
ctx.fillStyle = this.palette.text;
|
|
392
|
+
ctx.textAlign = "left";
|
|
393
|
+
ctx.fillText(`${MotionLayout.short(item.value * g)}${unit}`, -w / 2 + labels + length + label.px * 0.5, y);
|
|
394
|
+
}
|
|
395
|
+
});
|
|
396
|
+
return;
|
|
397
|
+
}
|
|
398
|
+
const top = -h / 2 + value.px * 1.5;
|
|
399
|
+
const bottom = h / 2 - label.px * 1.8;
|
|
400
|
+
const plot = bottom - top;
|
|
401
|
+
const base = top + plot * max / span;
|
|
402
|
+
const slot = w / count;
|
|
403
|
+
const bar = Math.min(slot * 0.62, this.unit * 0.12);
|
|
404
|
+
ctx.strokeStyle = this.palette.grid;
|
|
405
|
+
ctx.lineWidth = Math.max(1, this.unit * 0.002);
|
|
406
|
+
ctx.beginPath();
|
|
407
|
+
ctx.moveTo(-w / 2, base);
|
|
408
|
+
ctx.lineTo(w / 2, base);
|
|
409
|
+
ctx.stroke();
|
|
410
|
+
part.items.forEach((item, index) => {
|
|
411
|
+
const g = growOf(index);
|
|
412
|
+
const x = -w / 2 + slot * (index + 0.5);
|
|
413
|
+
const length = Math.abs(item.value) / span * plot * g;
|
|
414
|
+
ctx.fillStyle = this.colour(0, item.color ?? part.color);
|
|
415
|
+
if (item.value >= 0) {
|
|
416
|
+
this.rounded(x - bar / 2, base - length, bar, length, radius);
|
|
417
|
+
} else {
|
|
418
|
+
this.rounded(x - bar / 2, base, bar, length, radius);
|
|
419
|
+
}
|
|
420
|
+
ctx.fill();
|
|
421
|
+
ctx.textAlign = "center";
|
|
422
|
+
ctx.textBaseline = "middle";
|
|
423
|
+
ctx.font = label.font;
|
|
424
|
+
ctx.fillStyle = this.palette.muted;
|
|
425
|
+
ctx.fillText(item.label, x, bottom + label.px * 0.95, slot * 0.98);
|
|
426
|
+
if (g > 0) {
|
|
427
|
+
ctx.font = value.font;
|
|
428
|
+
ctx.fillStyle = this.palette.text;
|
|
429
|
+
ctx.fillText(`${MotionLayout.short(item.value * g)}${unit}`, x, item.value >= 0 ? base - length - value.px * 0.75 : base + length + value.px * 0.75, slot);
|
|
430
|
+
}
|
|
431
|
+
});
|
|
432
|
+
}
|
|
433
|
+
line(part, w, h, grow) {
|
|
434
|
+
const ctx = this.context;
|
|
435
|
+
const label = this.font("caption", false);
|
|
436
|
+
const value = this.font("body", true, 0.8);
|
|
437
|
+
const legend = part.series.some((series) => series.label) ? label.px * 1.8 : 0;
|
|
438
|
+
const top = -h / 2 + legend + value.px * 1.2;
|
|
439
|
+
const bottom = h / 2 - (part.labels.length ? label.px * 1.8 : 0);
|
|
440
|
+
const all = part.series.flatMap((series) => series.values);
|
|
441
|
+
let min = Math.min(...all);
|
|
442
|
+
let max = Math.max(...all);
|
|
443
|
+
const pad = (max - min || Math.abs(max) || 1) * 0.08;
|
|
444
|
+
min -= pad;
|
|
445
|
+
max += pad;
|
|
446
|
+
const size = Math.max(...part.series.map((series) => series.values.length));
|
|
447
|
+
const x = (index) => -w / 2 + (size > 1 ? index / (size - 1) * w : w / 2);
|
|
448
|
+
const y = (v) => bottom - (v - min) / (max - min) * (bottom - top);
|
|
449
|
+
ctx.strokeStyle = this.palette.grid;
|
|
450
|
+
ctx.lineWidth = Math.max(1, this.unit * 0.0015);
|
|
451
|
+
for (let row = 0;row < 3; row++) {
|
|
452
|
+
const at = top + (bottom - top) * row / 2;
|
|
453
|
+
ctx.beginPath();
|
|
454
|
+
ctx.moveTo(-w / 2, at);
|
|
455
|
+
ctx.lineTo(w / 2, at);
|
|
456
|
+
ctx.stroke();
|
|
457
|
+
}
|
|
458
|
+
const head = MotionEase.inOut(grow) * (size - 1);
|
|
459
|
+
part.series.forEach((series, index) => {
|
|
460
|
+
const colour = this.colour(index, series.color ?? (index === 0 ? part.color : undefined));
|
|
461
|
+
const points = [];
|
|
462
|
+
for (let i = 0;i < series.values.length && i <= Math.ceil(head); i++) {
|
|
463
|
+
if (i <= head) {
|
|
464
|
+
points.push([x(i), y(series.values[i])]);
|
|
465
|
+
} else {
|
|
466
|
+
const f = head - Math.floor(head);
|
|
467
|
+
const a = series.values[i - 1];
|
|
468
|
+
const b = series.values[i];
|
|
469
|
+
points.push([x(i - 1) + (x(i) - x(i - 1)) * f, y(a + (b - a) * f)]);
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
if (points.length < 1) {
|
|
473
|
+
return;
|
|
474
|
+
}
|
|
475
|
+
if (index === 0 && points.length > 1) {
|
|
476
|
+
const area = ctx.createLinearGradient(0, top, 0, bottom);
|
|
477
|
+
area.addColorStop(0, colour);
|
|
478
|
+
area.addColorStop(1, "rgba(0, 0, 0, 0)");
|
|
479
|
+
ctx.save();
|
|
480
|
+
ctx.globalAlpha *= 0.22;
|
|
481
|
+
ctx.fillStyle = area;
|
|
482
|
+
ctx.beginPath();
|
|
483
|
+
ctx.moveTo(points[0][0], bottom);
|
|
484
|
+
for (const [px, py] of points) {
|
|
485
|
+
ctx.lineTo(px, py);
|
|
486
|
+
}
|
|
487
|
+
ctx.lineTo(points[points.length - 1][0], bottom);
|
|
488
|
+
ctx.closePath();
|
|
489
|
+
ctx.fill();
|
|
490
|
+
ctx.restore();
|
|
491
|
+
}
|
|
492
|
+
ctx.strokeStyle = colour;
|
|
493
|
+
ctx.lineWidth = this.unit * 0.007;
|
|
494
|
+
ctx.lineJoin = "round";
|
|
495
|
+
ctx.lineCap = "round";
|
|
496
|
+
ctx.beginPath();
|
|
497
|
+
for (const [i, [px, py]] of points.entries()) {
|
|
498
|
+
if (i) {
|
|
499
|
+
ctx.lineTo(px, py);
|
|
500
|
+
} else {
|
|
501
|
+
ctx.moveTo(px, py);
|
|
502
|
+
}
|
|
503
|
+
}
|
|
504
|
+
ctx.stroke();
|
|
505
|
+
const [hx, hy] = points[points.length - 1];
|
|
506
|
+
ctx.fillStyle = colour;
|
|
507
|
+
ctx.beginPath();
|
|
508
|
+
ctx.arc(hx, hy, this.unit * 0.011, 0, Math.PI * 2);
|
|
509
|
+
ctx.fill();
|
|
510
|
+
const at = Math.min(series.values.length - 1, head);
|
|
511
|
+
const lower = Math.floor(at);
|
|
512
|
+
const current = series.values[lower] + ((series.values[Math.min(lower + 1, series.values.length - 1)] ?? 0) - series.values[lower]) * (at - lower);
|
|
513
|
+
ctx.font = value.font;
|
|
514
|
+
ctx.fillStyle = this.palette.text;
|
|
515
|
+
ctx.textAlign = hx > w / 2 - value.px * 2 ? "right" : "center";
|
|
516
|
+
ctx.textBaseline = "middle";
|
|
517
|
+
ctx.fillText(`${MotionLayout.short(current)}${part.unit ? ` ${part.unit}` : ""}`, hx, hy - value.px * 0.9);
|
|
518
|
+
});
|
|
519
|
+
ctx.font = label.font;
|
|
520
|
+
ctx.fillStyle = this.palette.muted;
|
|
521
|
+
ctx.textBaseline = "middle";
|
|
522
|
+
const labels = part.labels;
|
|
523
|
+
if (labels.length) {
|
|
524
|
+
const count = labels.length;
|
|
525
|
+
const shown = count <= 5 ? labels.map((_, index) => index) : [0, Math.round((count - 1) / 4), Math.round((count - 1) / 2), Math.round((count - 1) * 3 / 4), count - 1];
|
|
526
|
+
for (const index of new Set(shown)) {
|
|
527
|
+
const where = count === size ? x(index) : -w / 2 + (count > 1 ? index / (count - 1) * w : w / 2);
|
|
528
|
+
ctx.textAlign = index === 0 ? "left" : index === count - 1 ? "right" : "center";
|
|
529
|
+
ctx.fillText(labels[index], where, bottom + label.px * 0.95);
|
|
530
|
+
}
|
|
531
|
+
}
|
|
532
|
+
if (legend) {
|
|
533
|
+
let lx = -w / 2;
|
|
534
|
+
ctx.textAlign = "left";
|
|
535
|
+
part.series.forEach((series, index) => {
|
|
536
|
+
if (!series.label) {
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
ctx.fillStyle = this.colour(index, series.color ?? (index === 0 ? part.color : undefined));
|
|
540
|
+
ctx.beginPath();
|
|
541
|
+
ctx.arc(lx + label.px * 0.4, -h / 2 + label.px * 0.6, label.px * 0.32, 0, Math.PI * 2);
|
|
542
|
+
ctx.fill();
|
|
543
|
+
ctx.fillStyle = this.palette.muted;
|
|
544
|
+
ctx.fillText(series.label, lx + label.px, -h / 2 + label.px * 0.6);
|
|
545
|
+
lx += label.px * 1.6 + ctx.measureText(series.label).width;
|
|
546
|
+
});
|
|
547
|
+
}
|
|
548
|
+
}
|
|
549
|
+
pie(part, w, h, grow) {
|
|
550
|
+
const ctx = this.context;
|
|
551
|
+
const total = part.items.reduce((sum, item) => sum + Math.max(0, item.value), 0) || 1;
|
|
552
|
+
const radius = Math.min(h, w * 0.5) / 2;
|
|
553
|
+
const inner = part.donut ? radius * 0.58 : 0;
|
|
554
|
+
const cx = -w / 2 + radius;
|
|
555
|
+
const sweep = MotionEase.inOut(grow) * Math.PI * 2;
|
|
556
|
+
let start = -Math.PI / 2;
|
|
557
|
+
part.items.forEach((item, index) => {
|
|
558
|
+
const angle = Math.max(0, item.value) / total * Math.PI * 2;
|
|
559
|
+
const end = Math.min(start + angle, -Math.PI / 2 + sweep);
|
|
560
|
+
if (end > start) {
|
|
561
|
+
ctx.fillStyle = this.colour(index, item.color);
|
|
562
|
+
ctx.beginPath();
|
|
563
|
+
ctx.arc(cx, 0, radius, start, end);
|
|
564
|
+
if (inner) {
|
|
565
|
+
ctx.arc(cx, 0, inner, end, start, true);
|
|
566
|
+
} else {
|
|
567
|
+
ctx.lineTo(cx, 0);
|
|
568
|
+
}
|
|
569
|
+
ctx.closePath();
|
|
570
|
+
ctx.fill();
|
|
571
|
+
}
|
|
572
|
+
start += angle;
|
|
573
|
+
});
|
|
574
|
+
const label = this.font("body", false, 0.9);
|
|
575
|
+
const row = label.px * 1.7;
|
|
576
|
+
const left = cx + radius + label.px * 1.4;
|
|
577
|
+
let y = -((part.items.length - 1) * row) / 2;
|
|
578
|
+
ctx.textBaseline = "middle";
|
|
579
|
+
part.items.forEach((item, index) => {
|
|
580
|
+
ctx.fillStyle = this.colour(index, item.color);
|
|
581
|
+
ctx.beginPath();
|
|
582
|
+
ctx.arc(left, y, label.px * 0.34, 0, Math.PI * 2);
|
|
583
|
+
ctx.fill();
|
|
584
|
+
ctx.font = label.font;
|
|
585
|
+
ctx.fillStyle = this.palette.text;
|
|
586
|
+
ctx.textAlign = "left";
|
|
587
|
+
ctx.fillText(item.label, left + label.px * 0.8, y, Math.max(1, w / 2 - left - label.px * 4.4));
|
|
588
|
+
ctx.fillStyle = this.palette.muted;
|
|
589
|
+
ctx.textAlign = "right";
|
|
590
|
+
ctx.fillText(`${Math.round(Math.max(0, item.value) / total * 100 * Math.min(1, grow * 1.5))}%`, w / 2, y);
|
|
591
|
+
y += row;
|
|
592
|
+
});
|
|
593
|
+
}
|
|
594
|
+
list(part, w, since) {
|
|
595
|
+
const ctx = this.context;
|
|
596
|
+
const { px, font } = this.font(part.size, false);
|
|
597
|
+
ctx.font = font;
|
|
598
|
+
const marker = px * 1.8;
|
|
599
|
+
const rows = part.items.map((entry) => this.wrap(entry, w - marker));
|
|
600
|
+
const height = rows.reduce((sum, lines) => sum + lines.length * px * 1.3, 0) + (rows.length - 1) * px * 0.55;
|
|
601
|
+
let y = -height / 2;
|
|
602
|
+
rows.forEach((lines, index) => {
|
|
603
|
+
const p = part.enter === "none" ? 1 : clamp01((since - index * part.dur) / Math.min(part.dur, ENTER));
|
|
604
|
+
const e = MotionEase.out(p);
|
|
605
|
+
ctx.save();
|
|
606
|
+
ctx.globalAlpha *= e;
|
|
607
|
+
ctx.translate(0, (1 - e) * px * 0.8);
|
|
608
|
+
const middle = y + px * 0.65;
|
|
609
|
+
ctx.fillStyle = part.color ?? this.motion.accent;
|
|
610
|
+
ctx.strokeStyle = part.color ?? this.motion.accent;
|
|
611
|
+
const mx = -w / 2 + px * 0.45;
|
|
612
|
+
if (part.marker === "dot") {
|
|
613
|
+
ctx.beginPath();
|
|
614
|
+
ctx.arc(mx, middle, px * 0.2, 0, Math.PI * 2);
|
|
615
|
+
ctx.fill();
|
|
616
|
+
} else if (part.marker === "check") {
|
|
617
|
+
ctx.lineWidth = px * 0.14;
|
|
618
|
+
ctx.lineCap = "round";
|
|
619
|
+
ctx.lineJoin = "round";
|
|
620
|
+
ctx.beginPath();
|
|
621
|
+
ctx.moveTo(mx - px * 0.3, middle);
|
|
622
|
+
ctx.lineTo(mx - px * 0.06, middle + px * 0.26);
|
|
623
|
+
ctx.lineTo(mx + px * 0.36, middle - px * 0.28);
|
|
624
|
+
ctx.stroke();
|
|
625
|
+
} else {
|
|
626
|
+
ctx.beginPath();
|
|
627
|
+
ctx.arc(mx, middle, px * 0.46, 0, Math.PI * 2);
|
|
628
|
+
ctx.fill();
|
|
629
|
+
ctx.fillStyle = this.palette.background;
|
|
630
|
+
ctx.font = `700 ${(px * 0.56).toFixed(1)}px ${FONT}`;
|
|
631
|
+
ctx.textAlign = "center";
|
|
632
|
+
ctx.textBaseline = "middle";
|
|
633
|
+
ctx.fillText(String(index + 1), mx, middle + px * 0.02);
|
|
634
|
+
ctx.font = font;
|
|
635
|
+
}
|
|
636
|
+
ctx.fillStyle = this.palette.text;
|
|
637
|
+
ctx.textAlign = "left";
|
|
638
|
+
ctx.textBaseline = "middle";
|
|
639
|
+
for (const line of lines) {
|
|
640
|
+
ctx.fillText(line, -w / 2 + marker, y + px * 0.65);
|
|
641
|
+
y += px * 1.3;
|
|
642
|
+
}
|
|
643
|
+
ctx.restore();
|
|
644
|
+
y += px * 0.55;
|
|
645
|
+
});
|
|
646
|
+
}
|
|
647
|
+
shape(part, w, h) {
|
|
648
|
+
const ctx = this.context;
|
|
649
|
+
const fill = part.fill ?? (part.stroke ? undefined : part.color ?? this.motion.accent);
|
|
650
|
+
if (part.shape === "circle") {
|
|
651
|
+
ctx.beginPath();
|
|
652
|
+
ctx.ellipse(0, 0, w / 2, h / 2, 0, 0, Math.PI * 2);
|
|
653
|
+
} else {
|
|
654
|
+
this.rounded(-w / 2, -h / 2, w, h, part.radius / 100 * this.unit);
|
|
655
|
+
}
|
|
656
|
+
if (fill) {
|
|
657
|
+
ctx.fillStyle = fill;
|
|
658
|
+
ctx.fill();
|
|
659
|
+
}
|
|
660
|
+
if (part.stroke) {
|
|
661
|
+
ctx.strokeStyle = part.stroke;
|
|
662
|
+
ctx.lineWidth = this.unit * 0.005;
|
|
663
|
+
ctx.stroke();
|
|
664
|
+
}
|
|
665
|
+
}
|
|
666
|
+
stroke(part, grow) {
|
|
667
|
+
const ctx = this.context;
|
|
668
|
+
const x0 = part.at[0] / 100 * this.width;
|
|
669
|
+
const y0 = part.at[1] / 100 * this.height;
|
|
670
|
+
const to = part.to ?? [part.at[0] + part.w, part.at[1]];
|
|
671
|
+
const x1 = to[0] / 100 * this.width;
|
|
672
|
+
const y1 = to[1] / 100 * this.height;
|
|
673
|
+
const p = MotionEase.out(grow);
|
|
674
|
+
const x = x0 + (x1 - x0) * p;
|
|
675
|
+
const y = y0 + (y1 - y0) * p;
|
|
676
|
+
const colour = part.stroke ?? part.color ?? this.motion.accent;
|
|
677
|
+
const width = this.unit * 0.008;
|
|
678
|
+
ctx.save();
|
|
679
|
+
ctx.strokeStyle = colour;
|
|
680
|
+
ctx.fillStyle = colour;
|
|
681
|
+
ctx.lineWidth = width;
|
|
682
|
+
ctx.lineCap = "round";
|
|
683
|
+
ctx.beginPath();
|
|
684
|
+
ctx.moveTo(x0, y0);
|
|
685
|
+
ctx.lineTo(x, y);
|
|
686
|
+
ctx.stroke();
|
|
687
|
+
if (part.shape === "arrow" && p > 0.05) {
|
|
688
|
+
const angle = Math.atan2(y1 - y0, x1 - x0);
|
|
689
|
+
const head = width * 3.4;
|
|
690
|
+
ctx.beginPath();
|
|
691
|
+
ctx.moveTo(x + Math.cos(angle) * head * 0.4, y + Math.sin(angle) * head * 0.4);
|
|
692
|
+
ctx.lineTo(x - Math.cos(angle - 0.5) * head, y - Math.sin(angle - 0.5) * head);
|
|
693
|
+
ctx.lineTo(x - Math.cos(angle + 0.5) * head, y - Math.sin(angle + 0.5) * head);
|
|
694
|
+
ctx.closePath();
|
|
695
|
+
ctx.fill();
|
|
696
|
+
}
|
|
697
|
+
ctx.restore();
|
|
698
|
+
const pad = width * 4;
|
|
699
|
+
return [Math.min(x0, x1) - pad, Math.min(y0, y1) - pad, Math.abs(x1 - x0) + pad * 2, Math.abs(y1 - y0) + pad * 2];
|
|
700
|
+
}
|
|
701
|
+
image(part, w, h) {
|
|
702
|
+
const ctx = this.context;
|
|
703
|
+
const picture = this.pictures(part.src);
|
|
704
|
+
ctx.save();
|
|
705
|
+
this.rounded(-w / 2, -h / 2, w, h, part.radius / 100 * this.unit);
|
|
706
|
+
ctx.clip();
|
|
707
|
+
if (!picture) {
|
|
708
|
+
ctx.fillStyle = this.palette.grid;
|
|
709
|
+
ctx.fillRect(-w / 2, -h / 2, w, h);
|
|
710
|
+
ctx.restore();
|
|
711
|
+
return;
|
|
712
|
+
}
|
|
713
|
+
const size = picture;
|
|
714
|
+
const iw = size.naturalWidth || size.width || 1;
|
|
715
|
+
const ih = size.naturalHeight || size.height || 1;
|
|
716
|
+
const scale = part.fit === "cover" ? Math.max(w / iw, h / ih) : Math.min(w / iw, h / ih);
|
|
717
|
+
ctx.drawImage(picture, -iw * scale / 2, -ih * scale / 2, iw * scale, ih * scale);
|
|
718
|
+
ctx.restore();
|
|
719
|
+
}
|
|
720
|
+
}
|
|
721
|
+
|
|
722
|
+
// web-src/motion-spec.ts
|
|
723
|
+
var SIZES = ["16:9", "9:16", "1:1", "4:3"];
|
|
724
|
+
var ENTERS = ["fade", "rise", "drop", "left", "right", "pop", "zoom", "type", "none"];
|
|
725
|
+
var TRANSITIONS = ["fade", "slide", "zoom", "cut"];
|
|
726
|
+
var TEXT_SIZES2 = ["hero", "title", "subtitle", "body", "caption"];
|
|
727
|
+
var SCENES = 30;
|
|
728
|
+
var ITEMS = 20;
|
|
729
|
+
var AMOUNTS = 12;
|
|
730
|
+
var POINTS = 60;
|
|
731
|
+
var SERIES = 4;
|
|
732
|
+
var TEXT = 300;
|
|
733
|
+
var SCENE_SECONDS = 30;
|
|
734
|
+
var TOTAL_SECONDS = 180;
|
|
735
|
+
var HOLD = 2;
|
|
736
|
+
var WIDTHS = {
|
|
737
|
+
text: 84,
|
|
738
|
+
number: 60,
|
|
739
|
+
bars: 70,
|
|
740
|
+
line: 70,
|
|
741
|
+
pie: 50,
|
|
742
|
+
list: 70,
|
|
743
|
+
shape: 12,
|
|
744
|
+
image: 40
|
|
745
|
+
};
|
|
746
|
+
var DURATIONS = {
|
|
747
|
+
text: 0.6,
|
|
748
|
+
number: 1.4,
|
|
749
|
+
bars: 1.2,
|
|
750
|
+
line: 1.6,
|
|
751
|
+
pie: 1.2,
|
|
752
|
+
list: 0.5,
|
|
753
|
+
shape: 0.6,
|
|
754
|
+
image: 0.8
|
|
755
|
+
};
|
|
756
|
+
function object(value) {
|
|
757
|
+
return value && typeof value === "object" && !Array.isArray(value) ? value : undefined;
|
|
758
|
+
}
|
|
759
|
+
function text(value, max = TEXT) {
|
|
760
|
+
if (typeof value === "number" && Number.isFinite(value)) {
|
|
761
|
+
return String(value);
|
|
762
|
+
}
|
|
763
|
+
return typeof value === "string" && value.trim() ? value.trim().slice(0, max) : undefined;
|
|
764
|
+
}
|
|
765
|
+
function number(value) {
|
|
766
|
+
const parsed = typeof value === "string" && value.trim() ? Number(value) : value;
|
|
767
|
+
return typeof parsed === "number" && Number.isFinite(parsed) ? parsed : undefined;
|
|
768
|
+
}
|
|
769
|
+
function clamp(value, min, max) {
|
|
770
|
+
return Math.min(max, Math.max(min, value));
|
|
771
|
+
}
|
|
772
|
+
function oneOf(value, list, fallback) {
|
|
773
|
+
return list.includes(value) ? value : fallback;
|
|
774
|
+
}
|
|
775
|
+
function point(value) {
|
|
776
|
+
if (!Array.isArray(value) || value.length < 2) {
|
|
777
|
+
return;
|
|
778
|
+
}
|
|
779
|
+
const x = number(value[0]);
|
|
780
|
+
const y = number(value[1]);
|
|
781
|
+
return x === undefined || y === undefined ? undefined : [clamp(x, -50, 150), clamp(y, -50, 150)];
|
|
782
|
+
}
|
|
783
|
+
function fill(value) {
|
|
784
|
+
if (Array.isArray(value)) {
|
|
785
|
+
const [from, to] = value.map((color) => text(color, 60));
|
|
786
|
+
return from && to ? [from, to] : from;
|
|
787
|
+
}
|
|
788
|
+
return text(value, 60);
|
|
789
|
+
}
|
|
790
|
+
function amounts(value) {
|
|
791
|
+
if (!Array.isArray(value)) {
|
|
792
|
+
return [];
|
|
793
|
+
}
|
|
794
|
+
return value.map((entry) => {
|
|
795
|
+
const item = object(entry);
|
|
796
|
+
const amount = number(item?.value);
|
|
797
|
+
if (!item || amount === undefined) {
|
|
798
|
+
return;
|
|
799
|
+
}
|
|
800
|
+
const color = text(item.color, 60);
|
|
801
|
+
return { label: text(item.label, 60) ?? "", value: amount, ...color ? { color } : {} };
|
|
802
|
+
}).filter((item) => !!item).slice(0, AMOUNTS);
|
|
803
|
+
}
|
|
804
|
+
function numbers(value) {
|
|
805
|
+
return Array.isArray(value) ? value.map(number).filter((item) => item !== undefined).slice(0, POINTS) : [];
|
|
806
|
+
}
|
|
807
|
+
function item(value, index) {
|
|
808
|
+
const raw = object(value);
|
|
809
|
+
const type = raw?.type;
|
|
810
|
+
if (!raw || typeof type !== "string" || !(type in WIDTHS)) {
|
|
811
|
+
return;
|
|
812
|
+
}
|
|
813
|
+
const kind = type;
|
|
814
|
+
const h = number(raw.h);
|
|
815
|
+
const color = text(raw.color, 60);
|
|
816
|
+
const emphasis = raw.emphasis === "pulse" || raw.emphasis === "float" ? raw.emphasis : undefined;
|
|
817
|
+
const base = {
|
|
818
|
+
index,
|
|
819
|
+
at: point(raw.at) ?? [50, 50],
|
|
820
|
+
w: clamp(number(raw.w) ?? WIDTHS[kind], 1, 100),
|
|
821
|
+
...h !== undefined ? { h: clamp(h, 1, 100) } : {},
|
|
822
|
+
enter: oneOf(raw.enter, ENTERS, kind === "shape" || kind === "image" ? "pop" : "rise"),
|
|
823
|
+
delay: clamp(number(raw.delay) ?? 0, 0, SCENE_SECONDS),
|
|
824
|
+
dur: clamp(number(raw.dur) ?? DURATIONS[kind], 0.05, 10),
|
|
825
|
+
...color ? { color } : {},
|
|
826
|
+
...emphasis ? { emphasis } : {},
|
|
827
|
+
align: oneOf(raw.align, ["left", "center", "right"], "center")
|
|
828
|
+
};
|
|
829
|
+
switch (kind) {
|
|
830
|
+
case "text": {
|
|
831
|
+
const words = text(raw.text);
|
|
832
|
+
const size = oneOf(raw.size, TEXT_SIZES2, "title");
|
|
833
|
+
return words === undefined ? undefined : { ...base, type: kind, text: words, size, bold: typeof raw.bold === "boolean" ? raw.bold : size === "hero" || size === "title" };
|
|
834
|
+
}
|
|
835
|
+
case "number": {
|
|
836
|
+
const value2 = number(raw.value);
|
|
837
|
+
if (value2 === undefined) {
|
|
838
|
+
return;
|
|
839
|
+
}
|
|
840
|
+
const decimals = number(raw.decimals) ?? (Number.isInteger(value2) ? 0 : Math.min(3, String(value2).split(".")[1]?.length ?? 0));
|
|
841
|
+
const label = text(raw.label);
|
|
842
|
+
return {
|
|
843
|
+
...base,
|
|
844
|
+
type: kind,
|
|
845
|
+
value: value2,
|
|
846
|
+
from: number(raw.from) ?? 0,
|
|
847
|
+
prefix: text(raw.prefix, 20) ?? "",
|
|
848
|
+
suffix: text(raw.suffix, 20) ?? "",
|
|
849
|
+
decimals: clamp(Math.round(decimals), 0, 4),
|
|
850
|
+
size: oneOf(raw.size, TEXT_SIZES2, "hero"),
|
|
851
|
+
...label ? { label } : {}
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
case "bars":
|
|
855
|
+
case "pie": {
|
|
856
|
+
const list = amounts(raw.items);
|
|
857
|
+
if (!list.length) {
|
|
858
|
+
return;
|
|
859
|
+
}
|
|
860
|
+
if (kind === "pie") {
|
|
861
|
+
return { ...base, type: kind, items: list, donut: raw.donut !== false };
|
|
862
|
+
}
|
|
863
|
+
const unit = text(raw.unit, 20);
|
|
864
|
+
return { ...base, type: kind, items: list, horizontal: raw.horizontal === true, ...unit ? { unit } : {} };
|
|
865
|
+
}
|
|
866
|
+
case "line": {
|
|
867
|
+
const series = (Array.isArray(raw.series) ? raw.series : [{ values: raw.values, label: raw.label }]).map((entry) => {
|
|
868
|
+
const line = object(entry);
|
|
869
|
+
const values = numbers(line?.values);
|
|
870
|
+
const label = text(line?.label, 60);
|
|
871
|
+
const tint = text(line?.color, 60);
|
|
872
|
+
return values.length >= 2 ? { values, ...label ? { label } : {}, ...tint ? { color: tint } : {} } : undefined;
|
|
873
|
+
}).filter((line) => !!line).slice(0, SERIES);
|
|
874
|
+
if (!series.length) {
|
|
875
|
+
return;
|
|
876
|
+
}
|
|
877
|
+
const labels = Array.isArray(raw.labels) ? raw.labels.map((label) => text(label, 30) ?? "").slice(0, POINTS) : [];
|
|
878
|
+
const unit = text(raw.unit, 20);
|
|
879
|
+
return { ...base, type: kind, series, labels, ...unit ? { unit } : {} };
|
|
880
|
+
}
|
|
881
|
+
case "list": {
|
|
882
|
+
const list = Array.isArray(raw.items) ? raw.items.map((entry) => text(entry)).filter((entry) => !!entry).slice(0, 10) : [];
|
|
883
|
+
return list.length ? {
|
|
884
|
+
...base,
|
|
885
|
+
type: kind,
|
|
886
|
+
items: list,
|
|
887
|
+
marker: oneOf(raw.marker, ["dot", "number", "check"], "dot"),
|
|
888
|
+
size: oneOf(raw.size, TEXT_SIZES2, "body")
|
|
889
|
+
} : undefined;
|
|
890
|
+
}
|
|
891
|
+
case "shape": {
|
|
892
|
+
const shape = oneOf(raw.shape, ["rect", "circle", "line", "arrow"], "rect");
|
|
893
|
+
const to = point(raw.to);
|
|
894
|
+
const fillColor = text(raw.fill, 60);
|
|
895
|
+
const stroke = text(raw.stroke, 60);
|
|
896
|
+
return {
|
|
897
|
+
...base,
|
|
898
|
+
type: kind,
|
|
899
|
+
shape,
|
|
900
|
+
...to ? { to } : {},
|
|
901
|
+
...fillColor ? { fill: fillColor } : {},
|
|
902
|
+
...stroke ? { stroke } : {},
|
|
903
|
+
radius: clamp(number(raw.radius) ?? 2, 0, 50),
|
|
904
|
+
...shape === "line" || shape === "arrow" ? { enter: oneOf(raw.enter, ENTERS, "fade") } : {}
|
|
905
|
+
};
|
|
906
|
+
}
|
|
907
|
+
case "image": {
|
|
908
|
+
const src = typeof raw.src === "string" ? raw.src.trim() : "";
|
|
909
|
+
return src ? { ...base, type: kind, src, fit: raw.fit === "cover" ? "cover" : "contain", radius: clamp(number(raw.radius) ?? 2, 0, 50) } : undefined;
|
|
910
|
+
}
|
|
911
|
+
}
|
|
912
|
+
}
|
|
913
|
+
function settled(items) {
|
|
914
|
+
return items.reduce((end, part) => Math.max(end, part.delay + part.dur * (part.type === "list" ? part.items.length * 0.5 + 0.5 : 1)), 0);
|
|
915
|
+
}
|
|
916
|
+
function scene(value, index) {
|
|
917
|
+
const raw = object(value);
|
|
918
|
+
if (!raw) {
|
|
919
|
+
return;
|
|
920
|
+
}
|
|
921
|
+
const items = (Array.isArray(raw.items) ? raw.items : []).slice(0, ITEMS).map((part, at) => item(part, at)).filter((part) => !!part);
|
|
922
|
+
const caption = text(raw.caption);
|
|
923
|
+
if (!items.length && !caption) {
|
|
924
|
+
return;
|
|
925
|
+
}
|
|
926
|
+
const background = fill(raw.background);
|
|
927
|
+
return {
|
|
928
|
+
index,
|
|
929
|
+
duration: clamp(number(raw.duration) ?? Math.max(3, settled(items) + HOLD), 0.5, SCENE_SECONDS),
|
|
930
|
+
...background ? { background } : {},
|
|
931
|
+
transition: oneOf(raw.transition, TRANSITIONS, "fade"),
|
|
932
|
+
...caption ? { caption } : {},
|
|
933
|
+
items
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
var MotionSpec;
|
|
937
|
+
((MotionSpec) => {
|
|
938
|
+
function read(code) {
|
|
939
|
+
let value;
|
|
940
|
+
try {
|
|
941
|
+
value = JSON.parse(code);
|
|
942
|
+
} catch (error) {
|
|
943
|
+
throw new Error(error instanceof Error ? error.message : String(error));
|
|
944
|
+
}
|
|
945
|
+
const raw = object(value);
|
|
946
|
+
if (!raw) {
|
|
947
|
+
throw new Error("it is no JSON object");
|
|
948
|
+
}
|
|
949
|
+
const scenes = [];
|
|
950
|
+
let total = 0;
|
|
951
|
+
for (const [index, entry] of (Array.isArray(raw.scenes) ? raw.scenes : []).slice(0, SCENES).entries()) {
|
|
952
|
+
const made = scene(entry, index);
|
|
953
|
+
if (made && total + made.duration <= TOTAL_SECONDS) {
|
|
954
|
+
scenes.push(made);
|
|
955
|
+
total += made.duration;
|
|
956
|
+
}
|
|
957
|
+
}
|
|
958
|
+
if (!scenes.length) {
|
|
959
|
+
throw new Error("it has no scene with a part to show");
|
|
960
|
+
}
|
|
961
|
+
const title = text(raw.title, 80);
|
|
962
|
+
const background = fill(raw.background);
|
|
963
|
+
return {
|
|
964
|
+
...title ? { title } : {},
|
|
965
|
+
size: oneOf(raw.size, SIZES, "16:9"),
|
|
966
|
+
theme: raw.theme === "light" ? "light" : "dark",
|
|
967
|
+
accent: text(raw.accent, 60) ?? "#5b8cff",
|
|
968
|
+
...background ? { background } : {},
|
|
969
|
+
scenes
|
|
970
|
+
};
|
|
971
|
+
}
|
|
972
|
+
MotionSpec.read = read;
|
|
973
|
+
function scenesSoFar(code) {
|
|
974
|
+
return (code.match(/"items"\s*:/g) ?? []).length;
|
|
975
|
+
}
|
|
976
|
+
MotionSpec.scenesSoFar = scenesSoFar;
|
|
977
|
+
function timeline(motion) {
|
|
978
|
+
const starts = [];
|
|
979
|
+
let total = 0;
|
|
980
|
+
for (const part of motion.scenes) {
|
|
981
|
+
starts.push(total);
|
|
982
|
+
total += part.duration;
|
|
983
|
+
}
|
|
984
|
+
return { starts, total };
|
|
985
|
+
}
|
|
986
|
+
MotionSpec.timeline = timeline;
|
|
987
|
+
function at(motion, t) {
|
|
988
|
+
const { starts, total } = timeline(motion);
|
|
989
|
+
const time = clamp(t, 0, total);
|
|
990
|
+
let index = starts.length - 1;
|
|
991
|
+
while (index > 0 && starts[index] > time) {
|
|
992
|
+
index--;
|
|
993
|
+
}
|
|
994
|
+
return { index, local: Math.min(time - starts[index], motion.scenes[index].duration) };
|
|
995
|
+
}
|
|
996
|
+
MotionSpec.at = at;
|
|
997
|
+
function frame(size, short = 720) {
|
|
998
|
+
const [w, h] = size.split(":").map(Number);
|
|
999
|
+
const even = (value) => Math.round(value / 2) * 2;
|
|
1000
|
+
return w >= h ? { width: even(short * w / h), height: short } : { width: short, height: even(short * h / w) };
|
|
1001
|
+
}
|
|
1002
|
+
MotionSpec.frame = frame;
|
|
1003
|
+
})(MotionSpec ||= {});
|
|
1004
|
+
|
|
1005
|
+
// web-src/motion-painter.ts
|
|
1006
|
+
class MotionPainter extends MotionParts {
|
|
1007
|
+
paint(t, width, height) {
|
|
1008
|
+
this.width = width;
|
|
1009
|
+
this.height = height;
|
|
1010
|
+
this.unit = Math.min(width, height);
|
|
1011
|
+
const { index, local } = MotionSpec.at(this.motion, t);
|
|
1012
|
+
const scene2 = this.motion.scenes[index];
|
|
1013
|
+
const ctx = this.context;
|
|
1014
|
+
ctx.save();
|
|
1015
|
+
ctx.globalAlpha = 1;
|
|
1016
|
+
const coming = index > 0 && scene2.transition !== "cut" && local < TRANSITION;
|
|
1017
|
+
if (!coming) {
|
|
1018
|
+
const hits2 = this.scene(index, local, t);
|
|
1019
|
+
ctx.restore();
|
|
1020
|
+
return hits2;
|
|
1021
|
+
}
|
|
1022
|
+
const p = MotionEase.inOut(clamp01(local / TRANSITION));
|
|
1023
|
+
const before = this.motion.scenes[index - 1];
|
|
1024
|
+
ctx.save();
|
|
1025
|
+
if (scene2.transition === "slide") {
|
|
1026
|
+
ctx.translate(-p * width * 0.35, 0);
|
|
1027
|
+
}
|
|
1028
|
+
this.scene(index - 1, before.duration, t);
|
|
1029
|
+
ctx.restore();
|
|
1030
|
+
ctx.save();
|
|
1031
|
+
if (scene2.transition === "slide") {
|
|
1032
|
+
ctx.translate((1 - p) * width, 0);
|
|
1033
|
+
} else {
|
|
1034
|
+
ctx.globalAlpha = p;
|
|
1035
|
+
if (scene2.transition === "zoom") {
|
|
1036
|
+
const scale = 1.08 - 0.08 * p;
|
|
1037
|
+
ctx.translate(width / 2, height / 2);
|
|
1038
|
+
ctx.scale(scale, scale);
|
|
1039
|
+
ctx.translate(-width / 2, -height / 2);
|
|
1040
|
+
}
|
|
1041
|
+
}
|
|
1042
|
+
const hits = this.scene(index, local, t);
|
|
1043
|
+
ctx.restore();
|
|
1044
|
+
ctx.restore();
|
|
1045
|
+
return hits;
|
|
1046
|
+
}
|
|
1047
|
+
scene(index, local, t) {
|
|
1048
|
+
const scene2 = this.motion.scenes[index];
|
|
1049
|
+
this.background(scene2, t);
|
|
1050
|
+
const hits = [];
|
|
1051
|
+
scene2.items.forEach((part, item2) => {
|
|
1052
|
+
const box = this.item(part, local);
|
|
1053
|
+
if (box) {
|
|
1054
|
+
hits.push({ scene: index, item: item2, box });
|
|
1055
|
+
}
|
|
1056
|
+
});
|
|
1057
|
+
this.caption(scene2, local);
|
|
1058
|
+
return hits;
|
|
1059
|
+
}
|
|
1060
|
+
background(scene2, t) {
|
|
1061
|
+
const ctx = this.context;
|
|
1062
|
+
const { width, height } = this;
|
|
1063
|
+
ctx.fillStyle = this.fill(scene2.background ?? this.motion.background ?? this.palette.background);
|
|
1064
|
+
ctx.fillRect(0, 0, width, height);
|
|
1065
|
+
const lights = [
|
|
1066
|
+
[0.18 + 0.06 * Math.sin(t * 0.35), 0.2 + 0.05 * Math.cos(t * 0.3), this.motion.accent],
|
|
1067
|
+
[0.85 + 0.05 * Math.cos(t * 0.27), 0.82 + 0.06 * Math.sin(t * 0.33), COLOURS[4]]
|
|
1068
|
+
];
|
|
1069
|
+
for (const [x, y, colour] of lights) {
|
|
1070
|
+
const radius = Math.max(width, height) * 0.55;
|
|
1071
|
+
const gradient = ctx.createRadialGradient(x * width, y * height, 0, x * width, y * height, radius);
|
|
1072
|
+
gradient.addColorStop(0, colour);
|
|
1073
|
+
gradient.addColorStop(1, "rgba(0, 0, 0, 0)");
|
|
1074
|
+
ctx.save();
|
|
1075
|
+
ctx.globalAlpha *= this.palette.glow;
|
|
1076
|
+
ctx.fillStyle = gradient;
|
|
1077
|
+
ctx.fillRect(0, 0, width, height);
|
|
1078
|
+
ctx.restore();
|
|
1079
|
+
}
|
|
1080
|
+
}
|
|
1081
|
+
item(part, local) {
|
|
1082
|
+
if (local < part.delay && part.enter !== "none") {
|
|
1083
|
+
return;
|
|
1084
|
+
}
|
|
1085
|
+
const since = Math.max(0, local - part.delay);
|
|
1086
|
+
const enter = part.enter === "none" ? 1 : MotionEase.out(clamp01(since / Math.min(part.dur, ENTER)));
|
|
1087
|
+
const grow = part.enter === "none" ? 1 : clamp01(since / part.dur);
|
|
1088
|
+
const ctx = this.context;
|
|
1089
|
+
const w = part.w / 100 * this.width;
|
|
1090
|
+
if (part.type === "shape" && (part.shape === "line" || part.shape === "arrow")) {
|
|
1091
|
+
return this.stroke(part, grow);
|
|
1092
|
+
}
|
|
1093
|
+
const h = this.heightOf(part, w);
|
|
1094
|
+
const cx = part.at[0] / 100 * this.width;
|
|
1095
|
+
const cy = part.at[1] / 100 * this.height;
|
|
1096
|
+
let alpha = enter;
|
|
1097
|
+
let dx = 0;
|
|
1098
|
+
let dy = 0;
|
|
1099
|
+
let scale = 1;
|
|
1100
|
+
switch (part.enter) {
|
|
1101
|
+
case "rise":
|
|
1102
|
+
dy = (1 - enter) * 0.05 * this.height;
|
|
1103
|
+
break;
|
|
1104
|
+
case "drop":
|
|
1105
|
+
dy = -(1 - enter) * 0.05 * this.height;
|
|
1106
|
+
break;
|
|
1107
|
+
case "left":
|
|
1108
|
+
dx = -(1 - enter) * 0.08 * this.width;
|
|
1109
|
+
break;
|
|
1110
|
+
case "right":
|
|
1111
|
+
dx = (1 - enter) * 0.08 * this.width;
|
|
1112
|
+
break;
|
|
1113
|
+
case "pop": {
|
|
1114
|
+
const p = clamp01(since / Math.min(part.dur, ENTER));
|
|
1115
|
+
scale = 0.5 + 0.5 * MotionEase.back(p);
|
|
1116
|
+
alpha = clamp01(p * 2);
|
|
1117
|
+
break;
|
|
1118
|
+
}
|
|
1119
|
+
case "zoom":
|
|
1120
|
+
scale = 1.2 - 0.2 * enter;
|
|
1121
|
+
break;
|
|
1122
|
+
case "type":
|
|
1123
|
+
alpha = part.type === "text" ? 1 : enter;
|
|
1124
|
+
break;
|
|
1125
|
+
}
|
|
1126
|
+
const after = since - part.dur;
|
|
1127
|
+
if (part.emphasis && after > 0) {
|
|
1128
|
+
if (part.emphasis === "pulse") {
|
|
1129
|
+
scale *= 1 + 0.035 * Math.sin(after / 1.4 * Math.PI * 2);
|
|
1130
|
+
} else {
|
|
1131
|
+
dy += Math.sin(after / 2.4 * Math.PI * 2) * 0.008 * this.height;
|
|
1132
|
+
}
|
|
1133
|
+
}
|
|
1134
|
+
ctx.save();
|
|
1135
|
+
ctx.globalAlpha *= alpha;
|
|
1136
|
+
ctx.translate(cx + dx, cy + dy);
|
|
1137
|
+
if (scale !== 1) {
|
|
1138
|
+
ctx.scale(scale, scale);
|
|
1139
|
+
}
|
|
1140
|
+
switch (part.type) {
|
|
1141
|
+
case "text":
|
|
1142
|
+
this.text(part, w, part.enter === "type" ? grow : 1);
|
|
1143
|
+
break;
|
|
1144
|
+
case "number":
|
|
1145
|
+
this.number(part, w, h, grow);
|
|
1146
|
+
break;
|
|
1147
|
+
case "bars":
|
|
1148
|
+
this.bars(part, w, h, since);
|
|
1149
|
+
break;
|
|
1150
|
+
case "line":
|
|
1151
|
+
this.line(part, w, h, grow);
|
|
1152
|
+
break;
|
|
1153
|
+
case "pie":
|
|
1154
|
+
this.pie(part, w, h, grow);
|
|
1155
|
+
break;
|
|
1156
|
+
case "list":
|
|
1157
|
+
this.list(part, w, since);
|
|
1158
|
+
break;
|
|
1159
|
+
case "shape":
|
|
1160
|
+
this.shape(part, w, h);
|
|
1161
|
+
break;
|
|
1162
|
+
case "image":
|
|
1163
|
+
this.image(part, w, h);
|
|
1164
|
+
break;
|
|
1165
|
+
}
|
|
1166
|
+
ctx.restore();
|
|
1167
|
+
return [cx - w / 2, cy - h / 2, w, h];
|
|
1168
|
+
}
|
|
1169
|
+
heightOf(part, w) {
|
|
1170
|
+
if (part.h !== undefined) {
|
|
1171
|
+
return part.h / 100 * this.height;
|
|
1172
|
+
}
|
|
1173
|
+
const ctx = this.context;
|
|
1174
|
+
switch (part.type) {
|
|
1175
|
+
case "text": {
|
|
1176
|
+
const { px, font } = this.font(part.size, part.bold);
|
|
1177
|
+
ctx.font = font;
|
|
1178
|
+
return this.wrap(part.text, w).length * px * 1.22;
|
|
1179
|
+
}
|
|
1180
|
+
case "number": {
|
|
1181
|
+
const { px } = this.font(part.size, true);
|
|
1182
|
+
return px * 1.1 + (part.label ? this.font("body", false).px * 1.6 : 0);
|
|
1183
|
+
}
|
|
1184
|
+
case "list": {
|
|
1185
|
+
const { px, font } = this.font(part.size, false);
|
|
1186
|
+
ctx.font = font;
|
|
1187
|
+
const lines = part.items.reduce((sum, entry) => sum + this.wrap(entry, w - px * 1.8).length, 0);
|
|
1188
|
+
return lines * px * 1.3 + (part.items.length - 1) * px * 0.55;
|
|
1189
|
+
}
|
|
1190
|
+
case "shape":
|
|
1191
|
+
return w;
|
|
1192
|
+
case "pie":
|
|
1193
|
+
return Math.min(w * 0.62, this.height * 0.6);
|
|
1194
|
+
case "image":
|
|
1195
|
+
return Math.min(w * 0.62, this.height * 0.8);
|
|
1196
|
+
default:
|
|
1197
|
+
return Math.min(w * 0.56, this.height * 0.6);
|
|
1198
|
+
}
|
|
1199
|
+
}
|
|
1200
|
+
caption(scene2, local) {
|
|
1201
|
+
if (!scene2.caption) {
|
|
1202
|
+
return;
|
|
1203
|
+
}
|
|
1204
|
+
const ctx = this.context;
|
|
1205
|
+
const { px, font } = this.font("body", false, 0.95);
|
|
1206
|
+
ctx.save();
|
|
1207
|
+
ctx.globalAlpha *= MotionEase.out(clamp01(local / 0.4));
|
|
1208
|
+
ctx.font = font;
|
|
1209
|
+
const lines = this.wrap(scene2.caption, this.width * 0.82).slice(0, 2);
|
|
1210
|
+
const line = px * 1.3;
|
|
1211
|
+
const widest = Math.max(...lines.map((text2) => ctx.measureText(text2).width));
|
|
1212
|
+
const height = lines.length * line + px * 0.7;
|
|
1213
|
+
const bottom = this.height * 0.94;
|
|
1214
|
+
ctx.fillStyle = this.palette.pill;
|
|
1215
|
+
this.rounded(this.width / 2 - widest / 2 - px * 0.8, bottom - height, widest + px * 1.6, height, px * 0.5);
|
|
1216
|
+
ctx.fill();
|
|
1217
|
+
ctx.fillStyle = this.palette.text;
|
|
1218
|
+
ctx.textAlign = "center";
|
|
1219
|
+
ctx.textBaseline = "middle";
|
|
1220
|
+
for (const [index, text2] of lines.entries()) {
|
|
1221
|
+
ctx.fillText(text2, this.width / 2, bottom - height + px * 0.35 + line * (index + 0.5));
|
|
1222
|
+
}
|
|
1223
|
+
ctx.restore();
|
|
1224
|
+
}
|
|
1225
|
+
}
|
|
1226
|
+
|
|
1227
|
+
// web-src/mp4-mux.ts
|
|
1228
|
+
var TICKS = 30000;
|
|
1229
|
+
function u32(value) {
|
|
1230
|
+
return [value >>> 24 & 255, value >>> 16 & 255, value >>> 8 & 255, value & 255];
|
|
1231
|
+
}
|
|
1232
|
+
function u16(value) {
|
|
1233
|
+
return [value >>> 8 & 255, value & 255];
|
|
1234
|
+
}
|
|
1235
|
+
function ascii(text2) {
|
|
1236
|
+
return [...text2].map((char) => char.charCodeAt(0) & 255);
|
|
1237
|
+
}
|
|
1238
|
+
function box(type, ...parts) {
|
|
1239
|
+
const size = 8 + parts.reduce((sum, part) => sum + part.length, 0);
|
|
1240
|
+
const out = new Uint8Array(size);
|
|
1241
|
+
out.set([...u32(size), ...ascii(type)], 0);
|
|
1242
|
+
let at = 8;
|
|
1243
|
+
for (const part of parts) {
|
|
1244
|
+
out.set(part, at);
|
|
1245
|
+
at += part.length;
|
|
1246
|
+
}
|
|
1247
|
+
return out;
|
|
1248
|
+
}
|
|
1249
|
+
function full(type, version, flags, ...parts) {
|
|
1250
|
+
return box(type, [version, flags >>> 16 & 255, flags >>> 8 & 255, flags & 255], ...parts);
|
|
1251
|
+
}
|
|
1252
|
+
var MATRIX = [...u32(65536), ...u32(0), ...u32(0), ...u32(0), ...u32(65536), ...u32(0), ...u32(0), ...u32(0), ...u32(1073741824)];
|
|
1253
|
+
var Mp4Mux;
|
|
1254
|
+
((Mp4Mux) => {
|
|
1255
|
+
function video(video2) {
|
|
1256
|
+
const { width, height, samples } = video2;
|
|
1257
|
+
const delta = Math.round(TICKS / video2.fps);
|
|
1258
|
+
const duration = delta * samples.length;
|
|
1259
|
+
const movieDuration = Math.round(duration / TICKS * 1000);
|
|
1260
|
+
const keys = samples.map((sample, index2) => sample.key ? index2 + 1 : 0).filter(Boolean);
|
|
1261
|
+
const sampleEntry = box("avc1", [0, 0, 0, 0, 0, 0, ...u16(1)], [...u16(0), ...u16(0), ...u32(0), ...u32(0), ...u32(0)], [...u16(width), ...u16(height), ...u32(4718592), ...u32(4718592), ...u32(0), ...u16(1)], new Uint8Array(32), [...u16(24), 255, 255], box("avcC", video2.avcC));
|
|
1262
|
+
const table = (offset) => box("stbl", full("stsd", 0, 0, u32(1), sampleEntry), full("stts", 0, 0, u32(1), u32(samples.length), u32(delta)), ...keys.length && keys.length < samples.length ? [full("stss", 0, 0, u32(keys.length), keys.flatMap(u32))] : [], full("stsc", 0, 0, u32(1), u32(1), u32(samples.length), u32(1)), full("stsz", 0, 0, u32(0), u32(samples.length), samples.flatMap((sample) => u32(sample.data.length))), full("stco", 0, 0, u32(1), u32(offset)));
|
|
1263
|
+
const movie = (offset) => box("moov", full("mvhd", 0, 0, u32(0), u32(0), u32(1000), u32(movieDuration), u32(65536), u16(256), new Uint8Array(10), MATRIX, new Uint8Array(24), u32(2)), box("trak", full("tkhd", 0, 3, u32(0), u32(0), u32(1), u32(0), u32(movieDuration), new Uint8Array(8), u16(0), u16(0), u16(0), u16(0), MATRIX, u32(width << 16), u32(height << 16)), box("mdia", full("mdhd", 0, 0, u32(0), u32(0), u32(TICKS), u32(duration), u16(21956), u16(0)), full("hdlr", 0, 0, u32(0), ascii("vide"), new Uint8Array(12), [...ascii("VideoHandler"), 0]), box("minf", full("vmhd", 0, 1, new Uint8Array(8)), box("dinf", full("dref", 0, 0, u32(1), full("url ", 0, 1))), table(offset)))));
|
|
1264
|
+
const type = box("ftyp", ascii("isom"), u32(512), ascii("isomiso2avc1mp41"));
|
|
1265
|
+
const index = movie(0).length;
|
|
1266
|
+
const bytes = samples.reduce((sum, sample) => sum + sample.data.length, 0);
|
|
1267
|
+
const head = 8;
|
|
1268
|
+
const out = new Uint8Array(type.length + index + head + bytes);
|
|
1269
|
+
out.set(type, 0);
|
|
1270
|
+
out.set(movie(type.length + index + head), type.length);
|
|
1271
|
+
let at = type.length + index;
|
|
1272
|
+
out.set([...u32(head + bytes), ...ascii("mdat")], at);
|
|
1273
|
+
at += head;
|
|
1274
|
+
for (const sample of samples) {
|
|
1275
|
+
out.set(sample.data, at);
|
|
1276
|
+
at += sample.data.length;
|
|
1277
|
+
}
|
|
1278
|
+
return out;
|
|
1279
|
+
}
|
|
1280
|
+
Mp4Mux.video = video;
|
|
1281
|
+
function boxes(file, from = 0, to = file.length) {
|
|
1282
|
+
const found = [];
|
|
1283
|
+
let at = from;
|
|
1284
|
+
while (at + 8 <= to) {
|
|
1285
|
+
const size = (file[at] << 24 | file[at + 1] << 16 | file[at + 2] << 8 | file[at + 3]) >>> 0;
|
|
1286
|
+
const type = String.fromCharCode(...file.subarray(at + 4, at + 8));
|
|
1287
|
+
if (size < 8) {
|
|
1288
|
+
break;
|
|
1289
|
+
}
|
|
1290
|
+
found.push({ type, at, size });
|
|
1291
|
+
at += size;
|
|
1292
|
+
}
|
|
1293
|
+
return found;
|
|
1294
|
+
}
|
|
1295
|
+
Mp4Mux.boxes = boxes;
|
|
1296
|
+
})(Mp4Mux ||= {});
|
|
1297
|
+
|
|
1298
|
+
// web-src/motion-export.ts
|
|
1299
|
+
var CODECS = ["avc1.640028", "avc1.4d0028", "avc1.42e028", "avc1.42e01f"];
|
|
1300
|
+
var FPS = 30;
|
|
1301
|
+
var KEY_EVERY = FPS * 2;
|
|
1302
|
+
async function loadPictures(motion, url) {
|
|
1303
|
+
const sources = [...new Set(motion.scenes.flatMap((scene2) => scene2.items.flatMap((item2) => item2.type === "image" ? [item2.src] : [])))];
|
|
1304
|
+
const loaded = new Map;
|
|
1305
|
+
await Promise.all(sources.map(async (src) => {
|
|
1306
|
+
try {
|
|
1307
|
+
const image = new Image;
|
|
1308
|
+
image.decoding = "async";
|
|
1309
|
+
image.src = src.startsWith("data:") ? src : await url(src);
|
|
1310
|
+
await image.decode();
|
|
1311
|
+
loaded.set(src, image);
|
|
1312
|
+
} catch {}
|
|
1313
|
+
}));
|
|
1314
|
+
return (src) => loaded.get(src);
|
|
1315
|
+
}
|
|
1316
|
+
var MotionExport;
|
|
1317
|
+
((MotionExport) => {
|
|
1318
|
+
function supported() {
|
|
1319
|
+
return typeof VideoEncoder !== "undefined" || typeof MediaRecorder !== "undefined";
|
|
1320
|
+
}
|
|
1321
|
+
MotionExport.supported = supported;
|
|
1322
|
+
async function video(motion, pictures, progress = () => {
|
|
1323
|
+
return;
|
|
1324
|
+
}, short = 720) {
|
|
1325
|
+
const { width, height } = MotionSpec.frame(motion.size, short);
|
|
1326
|
+
const codec = typeof VideoEncoder !== "undefined" ? await pick(width, height) : undefined;
|
|
1327
|
+
if (codec) {
|
|
1328
|
+
return { data: await encode(motion, pictures, codec, width, height, progress), type: "video/mp4", extension: "mp4" };
|
|
1329
|
+
}
|
|
1330
|
+
if (typeof MediaRecorder !== "undefined") {
|
|
1331
|
+
return record(motion, pictures, width, height, progress);
|
|
1332
|
+
}
|
|
1333
|
+
throw new Error("this device cannot make a video");
|
|
1334
|
+
}
|
|
1335
|
+
MotionExport.video = video;
|
|
1336
|
+
async function pick(width, height) {
|
|
1337
|
+
for (const codec of CODECS) {
|
|
1338
|
+
try {
|
|
1339
|
+
const { supported: supported2 } = await VideoEncoder.isConfigSupported({
|
|
1340
|
+
codec,
|
|
1341
|
+
width,
|
|
1342
|
+
height,
|
|
1343
|
+
bitrate: bitrate(width, height),
|
|
1344
|
+
framerate: FPS,
|
|
1345
|
+
avc: { format: "avc" }
|
|
1346
|
+
});
|
|
1347
|
+
if (supported2) {
|
|
1348
|
+
return codec;
|
|
1349
|
+
}
|
|
1350
|
+
} catch {}
|
|
1351
|
+
}
|
|
1352
|
+
return;
|
|
1353
|
+
}
|
|
1354
|
+
function bitrate(width, height) {
|
|
1355
|
+
return Math.round(width * height * FPS * 0.12);
|
|
1356
|
+
}
|
|
1357
|
+
function canvas(width, height) {
|
|
1358
|
+
const made = typeof OffscreenCanvas !== "undefined" ? new OffscreenCanvas(width, height) : Object.assign(document.createElement("canvas"), { width, height });
|
|
1359
|
+
const context = made.getContext("2d");
|
|
1360
|
+
if (!context) {
|
|
1361
|
+
throw new Error("no canvas to draw on");
|
|
1362
|
+
}
|
|
1363
|
+
return { canvas: made, context };
|
|
1364
|
+
}
|
|
1365
|
+
async function encode(motion, pictures, codec, width, height, progress) {
|
|
1366
|
+
const { canvas: frame, context } = canvas(width, height);
|
|
1367
|
+
const painter = new MotionPainter(context, motion, pictures);
|
|
1368
|
+
const samples = [];
|
|
1369
|
+
let description;
|
|
1370
|
+
let failure;
|
|
1371
|
+
const encoder = new VideoEncoder({
|
|
1372
|
+
output: (chunk, meta) => {
|
|
1373
|
+
const data = new Uint8Array(chunk.byteLength);
|
|
1374
|
+
chunk.copyTo(data);
|
|
1375
|
+
samples.push({ data, key: chunk.type === "key" });
|
|
1376
|
+
const config = meta?.decoderConfig?.description;
|
|
1377
|
+
if (config && !description) {
|
|
1378
|
+
description = config instanceof ArrayBuffer ? new Uint8Array(config.slice(0)) : new Uint8Array(config.buffer.slice(0));
|
|
1379
|
+
}
|
|
1380
|
+
},
|
|
1381
|
+
error: (error) => {
|
|
1382
|
+
failure = error;
|
|
1383
|
+
}
|
|
1384
|
+
});
|
|
1385
|
+
encoder.configure({ codec, width, height, bitrate: bitrate(width, height), framerate: FPS, avc: { format: "avc" }, latencyMode: "quality" });
|
|
1386
|
+
const total = Math.round(MotionSpec.timeline(motion).total * FPS);
|
|
1387
|
+
for (let index = 0;index < total; index++) {
|
|
1388
|
+
if (failure) {
|
|
1389
|
+
break;
|
|
1390
|
+
}
|
|
1391
|
+
painter.paint(index / FPS, width, height);
|
|
1392
|
+
const picture = new VideoFrame(frame, { timestamp: Math.round(index * 1e6 / FPS), duration: Math.round(1e6 / FPS) });
|
|
1393
|
+
encoder.encode(picture, { keyFrame: index % KEY_EVERY === 0 });
|
|
1394
|
+
picture.close();
|
|
1395
|
+
while (encoder.encodeQueueSize > 8) {
|
|
1396
|
+
await new Promise((resolve) => setTimeout(resolve, 4));
|
|
1397
|
+
}
|
|
1398
|
+
if (index % 15 === 0) {
|
|
1399
|
+
progress(index / total);
|
|
1400
|
+
await new Promise((resolve) => setTimeout(resolve, 0));
|
|
1401
|
+
}
|
|
1402
|
+
}
|
|
1403
|
+
await encoder.flush();
|
|
1404
|
+
encoder.close();
|
|
1405
|
+
if (failure) {
|
|
1406
|
+
throw failure instanceof Error ? failure : new Error(String(failure));
|
|
1407
|
+
}
|
|
1408
|
+
if (!description || !samples.length) {
|
|
1409
|
+
throw new Error("the encoder gave no video");
|
|
1410
|
+
}
|
|
1411
|
+
progress(1);
|
|
1412
|
+
return Mp4Mux.video({ width, height, fps: FPS, avcC: description, samples });
|
|
1413
|
+
}
|
|
1414
|
+
async function record(motion, pictures, width, height, progress) {
|
|
1415
|
+
const element = Object.assign(document.createElement("canvas"), { width, height });
|
|
1416
|
+
const context = element.getContext("2d");
|
|
1417
|
+
if (!context || !element.captureStream) {
|
|
1418
|
+
throw new Error("this device cannot make a video");
|
|
1419
|
+
}
|
|
1420
|
+
const painter = new MotionPainter(context, motion, pictures);
|
|
1421
|
+
const type = MediaRecorder.isTypeSupported("video/mp4") ? "video/mp4" : "video/webm";
|
|
1422
|
+
const recorder = new MediaRecorder(element.captureStream(FPS), { mimeType: type, videoBitsPerSecond: bitrate(width, height) });
|
|
1423
|
+
const parts = [];
|
|
1424
|
+
recorder.ondataavailable = (event) => event.data.size && parts.push(event.data);
|
|
1425
|
+
const stopped = new Promise((resolve) => {
|
|
1426
|
+
recorder.onstop = () => resolve();
|
|
1427
|
+
});
|
|
1428
|
+
const total = MotionSpec.timeline(motion).total;
|
|
1429
|
+
painter.paint(0, width, height);
|
|
1430
|
+
recorder.start(500);
|
|
1431
|
+
const start = performance.now();
|
|
1432
|
+
await new Promise((resolve) => {
|
|
1433
|
+
const step = () => {
|
|
1434
|
+
const t = (performance.now() - start) / 1000;
|
|
1435
|
+
painter.paint(Math.min(t, total), width, height);
|
|
1436
|
+
progress(Math.min(1, t / total));
|
|
1437
|
+
if (t >= total) {
|
|
1438
|
+
resolve();
|
|
1439
|
+
} else {
|
|
1440
|
+
requestAnimationFrame(step);
|
|
1441
|
+
}
|
|
1442
|
+
};
|
|
1443
|
+
requestAnimationFrame(step);
|
|
1444
|
+
});
|
|
1445
|
+
recorder.stop();
|
|
1446
|
+
await stopped;
|
|
1447
|
+
return { data: new Blob(parts, { type }), type, extension: type === "video/mp4" ? "mp4" : "webm" };
|
|
1448
|
+
}
|
|
1449
|
+
})(MotionExport ||= {});
|
|
1450
|
+
|
|
1451
|
+
// web-src/motion-files.ts
|
|
1452
|
+
var making = new Map;
|
|
1453
|
+
var listeners = new Map;
|
|
1454
|
+
function tell(code, percent) {
|
|
1455
|
+
if (percent === undefined) {
|
|
1456
|
+
making.delete(code);
|
|
1457
|
+
} else {
|
|
1458
|
+
making.set(code, percent);
|
|
1459
|
+
}
|
|
1460
|
+
for (const listener of listeners.get(code) ?? []) {
|
|
1461
|
+
listener(percent);
|
|
1462
|
+
}
|
|
1463
|
+
}
|
|
1464
|
+
var MotionFiles;
|
|
1465
|
+
((MotionFiles) => {
|
|
1466
|
+
function progress(code) {
|
|
1467
|
+
return making.get(code);
|
|
1468
|
+
}
|
|
1469
|
+
MotionFiles.progress = progress;
|
|
1470
|
+
function watch(code, listener) {
|
|
1471
|
+
const set = listeners.get(code) ?? new Set;
|
|
1472
|
+
set.add(listener);
|
|
1473
|
+
listeners.set(code, set);
|
|
1474
|
+
return () => {
|
|
1475
|
+
set.delete(listener);
|
|
1476
|
+
if (!set.size) {
|
|
1477
|
+
listeners.delete(code);
|
|
1478
|
+
}
|
|
1479
|
+
};
|
|
1480
|
+
}
|
|
1481
|
+
MotionFiles.watch = watch;
|
|
1482
|
+
async function save(pi, code) {
|
|
1483
|
+
const words = MotionWords.of(pi.ui.language);
|
|
1484
|
+
if (making.has(code)) {
|
|
1485
|
+
return;
|
|
1486
|
+
}
|
|
1487
|
+
if (!MotionExport.supported()) {
|
|
1488
|
+
pi.ui.notify(words.unsupported, "warning");
|
|
1489
|
+
return;
|
|
1490
|
+
}
|
|
1491
|
+
tell(code, 0);
|
|
1492
|
+
try {
|
|
1493
|
+
const motion = MotionSpec.read(code);
|
|
1494
|
+
const pictures = await loadPictures(motion, (src) => pi.fileUrl ? pi.fileUrl(src) : Promise.resolve(src));
|
|
1495
|
+
const video = await MotionExport.video(motion, pictures, (done) => tell(code, Math.round(done * 100)));
|
|
1496
|
+
const name = `${(motion.title ?? words.untitled).replace(/[\\/:*?"<>|]+/g, " ").trim() || words.untitled}.${video.extension}`;
|
|
1497
|
+
const blob = video.data instanceof Blob ? video.data : new Blob([video.data], { type: video.type });
|
|
1498
|
+
if (pi.ui.saveFile) {
|
|
1499
|
+
const where = await pi.ui.saveFile(blob, name);
|
|
1500
|
+
if (where !== "cancelled" && where !== "denied") {
|
|
1501
|
+
pi.ui.notify(MotionWords.fill(words.exported, { name }), "info");
|
|
1502
|
+
}
|
|
1503
|
+
return;
|
|
1504
|
+
}
|
|
1505
|
+
const url = URL.createObjectURL(blob);
|
|
1506
|
+
const link = document.createElement("a");
|
|
1507
|
+
link.href = url;
|
|
1508
|
+
link.download = name;
|
|
1509
|
+
document.body.appendChild(link);
|
|
1510
|
+
link.click();
|
|
1511
|
+
link.remove();
|
|
1512
|
+
setTimeout(() => URL.revokeObjectURL(url), 60000);
|
|
1513
|
+
pi.ui.notify(MotionWords.fill(words.exported, { name }), "info");
|
|
1514
|
+
} catch (error) {
|
|
1515
|
+
pi.ui.notify(MotionWords.fill(words.exportFailed, { reason: error instanceof Error ? error.message : String(error) }), "error");
|
|
1516
|
+
} finally {
|
|
1517
|
+
tell(code, undefined);
|
|
1518
|
+
}
|
|
1519
|
+
}
|
|
1520
|
+
MotionFiles.save = save;
|
|
1521
|
+
})(MotionFiles ||= {});
|
|
1522
|
+
|
|
1523
|
+
// web-src/motion-protocol.ts
|
|
1524
|
+
var LANGUAGE = "ta-motion";
|
|
1525
|
+
|
|
1526
|
+
// ../ta-protocol/esm/common/work-ai-protocol.js
|
|
1527
|
+
var TA_WORKS = "@celljs/ta-works";
|
|
1528
|
+
var TaWorkAi;
|
|
1529
|
+
(function(TaWorkAi2) {
|
|
1530
|
+
TaWorkAi2.PROMPT = 8000;
|
|
1531
|
+
TaWorkAi2.SYSTEM = 4000;
|
|
1532
|
+
TaWorkAi2.TURNS = 20;
|
|
1533
|
+
TaWorkAi2.TITLE = 60;
|
|
1534
|
+
TaWorkAi2.MAX_TOKENS = 2000;
|
|
1535
|
+
function prompt(input) {
|
|
1536
|
+
const given = typeof input === "string" ? { prompt: input } : input;
|
|
1537
|
+
if (!given || typeof given.prompt !== "string" || !given.prompt.trim() || given.prompt.length > TaWorkAi2.PROMPT) {
|
|
1538
|
+
return;
|
|
1539
|
+
}
|
|
1540
|
+
if (given.system !== undefined && (typeof given.system !== "string" || given.system.length > TaWorkAi2.SYSTEM)) {
|
|
1541
|
+
return;
|
|
1542
|
+
}
|
|
1543
|
+
const history = Array.isArray(given.history) ? given.history : given.history === undefined ? [] : undefined;
|
|
1544
|
+
if (!history || history.length > TaWorkAi2.TURNS) {
|
|
1545
|
+
return;
|
|
1546
|
+
}
|
|
1547
|
+
const turns = [];
|
|
1548
|
+
for (const turn of history) {
|
|
1549
|
+
if ((turn === null || turn === undefined ? undefined : turn.role) !== "user" && (turn === null || turn === undefined ? undefined : turn.role) !== "assistant" || typeof turn.text !== "string" || turn.text.length > TaWorkAi2.PROMPT) {
|
|
1550
|
+
return;
|
|
1551
|
+
}
|
|
1552
|
+
turns.push({ role: turn.role, text: turn.text });
|
|
1553
|
+
}
|
|
1554
|
+
const tokens = typeof given.maxTokens === "number" && given.maxTokens > 0 ? Math.min(TaWorkAi2.MAX_TOKENS, Math.floor(given.maxTokens)) : undefined;
|
|
1555
|
+
return {
|
|
1556
|
+
prompt: given.prompt,
|
|
1557
|
+
...given.system ? { system: given.system } : {},
|
|
1558
|
+
...turns.length ? { history: turns } : {},
|
|
1559
|
+
...tokens ? { maxTokens: tokens } : {}
|
|
1560
|
+
};
|
|
1561
|
+
}
|
|
1562
|
+
TaWorkAi2.prompt = prompt;
|
|
1563
|
+
function ask(input) {
|
|
1564
|
+
const given = input;
|
|
1565
|
+
const asked = prompt(input);
|
|
1566
|
+
if (!asked || typeof (given === null || given === undefined ? undefined : given.grant) !== "string" || !/^(own|v):[\w-]{1,80}$/.test(given.grant) || typeof given.title !== "string") {
|
|
1567
|
+
return;
|
|
1568
|
+
}
|
|
1569
|
+
return { ...asked, grant: given.grant, title: given.title.slice(0, TaWorkAi2.TITLE) };
|
|
1570
|
+
}
|
|
1571
|
+
TaWorkAi2.ask = ask;
|
|
1572
|
+
function grantOf(own, work, version) {
|
|
1573
|
+
return own ? `own:${work}` : `v:${version}`;
|
|
1574
|
+
}
|
|
1575
|
+
TaWorkAi2.grantOf = grantOf;
|
|
1576
|
+
})(TaWorkAi || (TaWorkAi = {}));
|
|
1577
|
+
|
|
1578
|
+
// ../ta-protocol/esm/common/work-mark.js
|
|
1579
|
+
var TaWorkMark;
|
|
1580
|
+
(function(TaWorkMark2) {
|
|
1581
|
+
TaWorkMark2.NAME = /^work-[a-z0-9]{1,32}$/;
|
|
1582
|
+
TaWorkMark2.COMMENT = /<!--\s*ta-work:\s*(work-[a-z0-9]{1,32})\s*-->/;
|
|
1583
|
+
TaWorkMark2.COMMENTED = ["ta-play", "ta-doc", "ta-slides"];
|
|
1584
|
+
function of(language, code) {
|
|
1585
|
+
var _a, _b;
|
|
1586
|
+
if (TaWorkMark2.COMMENTED.includes(language)) {
|
|
1587
|
+
return (_a = TaWorkMark2.COMMENT.exec(code)) === null || _a === undefined ? undefined : _a[1];
|
|
1588
|
+
}
|
|
1589
|
+
try {
|
|
1590
|
+
const work = (_b = JSON.parse(code)) === null || _b === undefined ? undefined : _b.work;
|
|
1591
|
+
return typeof work === "string" && TaWorkMark2.NAME.test(work) ? work : undefined;
|
|
1592
|
+
} catch (_c) {
|
|
1593
|
+
return;
|
|
1594
|
+
}
|
|
1595
|
+
}
|
|
1596
|
+
TaWorkMark2.of = of;
|
|
1597
|
+
function mark(language, work) {
|
|
1598
|
+
return TaWorkMark2.COMMENTED.includes(language) ? `<!-- ta-work: ${work} -->` : `"work": "${work}"`;
|
|
1599
|
+
}
|
|
1600
|
+
TaWorkMark2.mark = mark;
|
|
1601
|
+
})(TaWorkMark || (TaWorkMark = {}));
|
|
1602
|
+
|
|
1603
|
+
// web-src/react.ts
|
|
1604
|
+
var React;
|
|
1605
|
+
function bind(react) {
|
|
1606
|
+
React = react;
|
|
1607
|
+
}
|
|
1608
|
+
|
|
1609
|
+
// web-src/motion-edit.ts
|
|
1610
|
+
var states = new Map;
|
|
1611
|
+
function useMotionEdit(pi, props) {
|
|
1612
|
+
const work = props.closed && !props.conversation ? TaWorkMark.of(LANGUAGE, props.code) : undefined;
|
|
1613
|
+
const editable = !!work && !!pi.packages?.callPackageHost;
|
|
1614
|
+
let state = work ? states.get(work) : undefined;
|
|
1615
|
+
if (!state) {
|
|
1616
|
+
state = {};
|
|
1617
|
+
if (work) {
|
|
1618
|
+
states.set(work, state);
|
|
1619
|
+
}
|
|
1620
|
+
}
|
|
1621
|
+
const [shown, setShown] = React.useState(undefined);
|
|
1622
|
+
const code = shown && shown.base === props.code ? shown.code : props.code;
|
|
1623
|
+
const saved = React.useRef(undefined);
|
|
1624
|
+
const pending = React.useRef(undefined);
|
|
1625
|
+
const latest = React.useRef(props.code);
|
|
1626
|
+
latest.current = props.code;
|
|
1627
|
+
const flush = React.useCallback(async () => {
|
|
1628
|
+
const after = pending.current;
|
|
1629
|
+
pending.current = undefined;
|
|
1630
|
+
if (!work || after === undefined || !pi.packages?.callPackageHost) {
|
|
1631
|
+
return;
|
|
1632
|
+
}
|
|
1633
|
+
const before = saved.current ?? latest.current;
|
|
1634
|
+
try {
|
|
1635
|
+
const answer = await pi.packages.callPackageHost(TA_WORKS, "edit", [work, before, after]);
|
|
1636
|
+
if (answer.ok) {
|
|
1637
|
+
saved.current = after;
|
|
1638
|
+
} else {
|
|
1639
|
+
saved.current = undefined;
|
|
1640
|
+
setShown(undefined);
|
|
1641
|
+
pi.ui.notify(MotionWords.of(pi.ui.language).stale, "info");
|
|
1642
|
+
}
|
|
1643
|
+
} catch (error) {
|
|
1644
|
+
pi.ui.notify(error instanceof Error ? error.message : String(error), "error");
|
|
1645
|
+
}
|
|
1646
|
+
}, [pi, work]);
|
|
1647
|
+
React.useEffect(() => () => {
|
|
1648
|
+
flush();
|
|
1649
|
+
}, [flush]);
|
|
1650
|
+
const change = React.useCallback((mutate) => {
|
|
1651
|
+
let raw;
|
|
1652
|
+
try {
|
|
1653
|
+
raw = JSON.parse(pending.current ?? code);
|
|
1654
|
+
} catch {
|
|
1655
|
+
return;
|
|
1656
|
+
}
|
|
1657
|
+
mutate(raw);
|
|
1658
|
+
const next = JSON.stringify(raw, null, 2);
|
|
1659
|
+
pending.current = next;
|
|
1660
|
+
setShown({ base: latest.current, code: next });
|
|
1661
|
+
}, [code]);
|
|
1662
|
+
return { code, editable, state, change, commit: () => void flush() };
|
|
1663
|
+
}
|
|
1664
|
+
|
|
1665
|
+
// web-src/motion-editor.tsx
|
|
1666
|
+
function at(raw, scene2) {
|
|
1667
|
+
const scenes = raw.scenes;
|
|
1668
|
+
return Array.isArray(scenes) && scenes[scene2] && typeof scenes[scene2] === "object" ? scenes[scene2] : undefined;
|
|
1669
|
+
}
|
|
1670
|
+
function itemAt(raw, scene2, item2) {
|
|
1671
|
+
const items = at(raw, scene2)?.items;
|
|
1672
|
+
return Array.isArray(items) && items[item2] && typeof items[item2] === "object" ? items[item2] : undefined;
|
|
1673
|
+
}
|
|
1674
|
+
function numberOf(text2) {
|
|
1675
|
+
const value = Number(text2.replace(/,/g, "").trim());
|
|
1676
|
+
return text2.trim() && Number.isFinite(value) ? value : undefined;
|
|
1677
|
+
}
|
|
1678
|
+
function MotionEditor({
|
|
1679
|
+
motion,
|
|
1680
|
+
scene: scene2,
|
|
1681
|
+
words,
|
|
1682
|
+
onScene,
|
|
1683
|
+
change,
|
|
1684
|
+
commit
|
|
1685
|
+
}) {
|
|
1686
|
+
const shown = motion.scenes[scene2];
|
|
1687
|
+
const s = shown.index;
|
|
1688
|
+
const field = (key, label, value, set, long = false) => /* @__PURE__ */ React.createElement("div", {
|
|
1689
|
+
className: "tmt-field",
|
|
1690
|
+
key
|
|
1691
|
+
}, /* @__PURE__ */ React.createElement("span", null, label), long ? /* @__PURE__ */ React.createElement("textarea", {
|
|
1692
|
+
rows: 2,
|
|
1693
|
+
"aria-label": label,
|
|
1694
|
+
defaultValue: value,
|
|
1695
|
+
onChange: (event) => set(event.currentTarget.value),
|
|
1696
|
+
onBlur: commit
|
|
1697
|
+
}) : /* @__PURE__ */ React.createElement("input", {
|
|
1698
|
+
type: "text",
|
|
1699
|
+
"aria-label": label,
|
|
1700
|
+
defaultValue: value,
|
|
1701
|
+
onChange: (event) => set(event.currentTarget.value),
|
|
1702
|
+
onBlur: commit,
|
|
1703
|
+
onKeyDown: (event) => event.key === "Enter" && event.currentTarget.blur()
|
|
1704
|
+
}));
|
|
1705
|
+
const part = (item2) => {
|
|
1706
|
+
const key = `${s}.${item2.index}`;
|
|
1707
|
+
const write = (mutate) => change((raw) => {
|
|
1708
|
+
const own = itemAt(raw, s, item2.index);
|
|
1709
|
+
if (own) {
|
|
1710
|
+
mutate(own);
|
|
1711
|
+
}
|
|
1712
|
+
});
|
|
1713
|
+
switch (item2.type) {
|
|
1714
|
+
case "text":
|
|
1715
|
+
return field(key, words.kinds.text, item2.text, (text2) => write((own) => own.text = text2), true);
|
|
1716
|
+
case "number":
|
|
1717
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1718
|
+
className: "tmt-row",
|
|
1719
|
+
key
|
|
1720
|
+
}, field(`${key}.v`, words.kinds.number, String(item2.value), (text2) => {
|
|
1721
|
+
const value = numberOf(text2);
|
|
1722
|
+
if (value !== undefined) {
|
|
1723
|
+
write((own) => own.value = value);
|
|
1724
|
+
}
|
|
1725
|
+
}), item2.label !== undefined && field(`${key}.l`, words.label, item2.label, (text2) => write((own) => own.label = text2)));
|
|
1726
|
+
case "list":
|
|
1727
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1728
|
+
className: "tmt-group",
|
|
1729
|
+
key
|
|
1730
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
1731
|
+
className: "tmt-group-name"
|
|
1732
|
+
}, words.kinds.list), item2.items.map((entry, index) => field(`${key}.${index}`, String(index + 1), entry, (text2) => write((own) => {
|
|
1733
|
+
if (Array.isArray(own.items)) {
|
|
1734
|
+
own.items[index] = text2;
|
|
1735
|
+
}
|
|
1736
|
+
}))));
|
|
1737
|
+
case "bars":
|
|
1738
|
+
case "pie":
|
|
1739
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1740
|
+
className: "tmt-group",
|
|
1741
|
+
key
|
|
1742
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
1743
|
+
className: "tmt-group-name"
|
|
1744
|
+
}, words.kinds[item2.type]), item2.items.map((amount, index) => /* @__PURE__ */ React.createElement("div", {
|
|
1745
|
+
className: "tmt-row",
|
|
1746
|
+
key: `${key}.${index}`
|
|
1747
|
+
}, field(`${key}.${index}.l`, words.label, amount.label, (text2) => write((own) => {
|
|
1748
|
+
const entry = own.items?.[index];
|
|
1749
|
+
if (entry) {
|
|
1750
|
+
entry.label = text2;
|
|
1751
|
+
}
|
|
1752
|
+
})), field(`${key}.${index}.v`, words.value, String(amount.value), (text2) => {
|
|
1753
|
+
const value = numberOf(text2);
|
|
1754
|
+
if (value !== undefined) {
|
|
1755
|
+
write((own) => {
|
|
1756
|
+
const entry = own.items?.[index];
|
|
1757
|
+
if (entry) {
|
|
1758
|
+
entry.value = value;
|
|
1759
|
+
}
|
|
1760
|
+
});
|
|
1761
|
+
}
|
|
1762
|
+
}))));
|
|
1763
|
+
case "line":
|
|
1764
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1765
|
+
className: "tmt-group",
|
|
1766
|
+
key
|
|
1767
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
1768
|
+
className: "tmt-group-name"
|
|
1769
|
+
}, words.kinds.line), field(`${key}.v`, words.values, item2.series[0].values.join(", "), (text2) => {
|
|
1770
|
+
const values = text2.split(/[,,\s]+/).filter(Boolean).map(numberOf);
|
|
1771
|
+
if (values.length >= 2 && values.every((value) => value !== undefined)) {
|
|
1772
|
+
write((own) => {
|
|
1773
|
+
if (Array.isArray(own.series) && own.series[0] && typeof own.series[0] === "object") {
|
|
1774
|
+
own.series[0].values = values;
|
|
1775
|
+
} else {
|
|
1776
|
+
own.values = values;
|
|
1777
|
+
}
|
|
1778
|
+
});
|
|
1779
|
+
}
|
|
1780
|
+
}), item2.labels.length > 0 && field(`${key}.l`, words.labels, item2.labels.join(", "), (text2) => write((own) => own.labels = text2.split(/[,,]\s*/).map((label) => label.trim()))));
|
|
1781
|
+
default:
|
|
1782
|
+
return null;
|
|
1783
|
+
}
|
|
1784
|
+
};
|
|
1785
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
1786
|
+
className: "tmt-editor"
|
|
1787
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
1788
|
+
className: "tmt-scenes"
|
|
1789
|
+
}, motion.scenes.map((entry, index) => /* @__PURE__ */ React.createElement("button", {
|
|
1790
|
+
type: "button",
|
|
1791
|
+
key: entry.index,
|
|
1792
|
+
"data-on": index === scene2 || undefined,
|
|
1793
|
+
onClick: () => onScene(index)
|
|
1794
|
+
}, index + 1))), /* @__PURE__ */ React.createElement("div", {
|
|
1795
|
+
className: "tmt-fields",
|
|
1796
|
+
key: s
|
|
1797
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
1798
|
+
className: "tmt-row"
|
|
1799
|
+
}, field(`${s}.d`, words.seconds, String(shown.duration), (text2) => {
|
|
1800
|
+
const value = numberOf(text2);
|
|
1801
|
+
if (value !== undefined && value >= 0.5 && value <= 30) {
|
|
1802
|
+
change((raw) => {
|
|
1803
|
+
const own = at(raw, s);
|
|
1804
|
+
if (own) {
|
|
1805
|
+
own.duration = value;
|
|
1806
|
+
}
|
|
1807
|
+
});
|
|
1808
|
+
}
|
|
1809
|
+
}), field(`${s}.c`, words.caption, shown.caption ?? "", (text2) => change((raw) => {
|
|
1810
|
+
const own = at(raw, s);
|
|
1811
|
+
if (own) {
|
|
1812
|
+
if (text2.trim()) {
|
|
1813
|
+
own.caption = text2;
|
|
1814
|
+
} else {
|
|
1815
|
+
delete own.caption;
|
|
1816
|
+
}
|
|
1817
|
+
}
|
|
1818
|
+
}))), shown.items.map(part)));
|
|
1819
|
+
}
|
|
1820
|
+
|
|
1821
|
+
// web-src/motion-view.tsx
|
|
1822
|
+
var TAP = 6;
|
|
1823
|
+
function clock(seconds) {
|
|
1824
|
+
const whole = Math.max(0, Math.round(seconds));
|
|
1825
|
+
return `${Math.floor(whole / 60)}:${String(whole % 60).padStart(2, "0")}`;
|
|
1826
|
+
}
|
|
1827
|
+
function Glyph({ kind }) {
|
|
1828
|
+
return /* @__PURE__ */ React.createElement("svg", {
|
|
1829
|
+
viewBox: "0 0 24 24",
|
|
1830
|
+
width: "16",
|
|
1831
|
+
height: "16",
|
|
1832
|
+
"aria-hidden": "true"
|
|
1833
|
+
}, kind === "play" && /* @__PURE__ */ React.createElement("path", {
|
|
1834
|
+
d: "M8 5.5v13l10.5-6.5z",
|
|
1835
|
+
fill: "currentColor"
|
|
1836
|
+
}), kind === "pause" && /* @__PURE__ */ React.createElement("g", {
|
|
1837
|
+
fill: "currentColor"
|
|
1838
|
+
}, /* @__PURE__ */ React.createElement("rect", {
|
|
1839
|
+
x: "6.5",
|
|
1840
|
+
y: "5.5",
|
|
1841
|
+
width: "4",
|
|
1842
|
+
height: "13",
|
|
1843
|
+
rx: "1"
|
|
1844
|
+
}), /* @__PURE__ */ React.createElement("rect", {
|
|
1845
|
+
x: "13.5",
|
|
1846
|
+
y: "5.5",
|
|
1847
|
+
width: "4",
|
|
1848
|
+
height: "13",
|
|
1849
|
+
rx: "1"
|
|
1850
|
+
})), kind === "replay" && /* @__PURE__ */ React.createElement("g", {
|
|
1851
|
+
fill: "none",
|
|
1852
|
+
stroke: "currentColor",
|
|
1853
|
+
strokeWidth: "2.2",
|
|
1854
|
+
strokeLinecap: "round",
|
|
1855
|
+
strokeLinejoin: "round"
|
|
1856
|
+
}, /* @__PURE__ */ React.createElement("path", {
|
|
1857
|
+
d: "M4 12a8 8 0 1 0 2.4-5.7"
|
|
1858
|
+
}), /* @__PURE__ */ React.createElement("path", {
|
|
1859
|
+
d: "M4 4.5v4.5h4.5"
|
|
1860
|
+
})));
|
|
1861
|
+
}
|
|
1862
|
+
function pointOf(motion, raw, hit, scene2, words) {
|
|
1863
|
+
let written;
|
|
1864
|
+
try {
|
|
1865
|
+
written = JSON.parse(raw);
|
|
1866
|
+
} catch {
|
|
1867
|
+
written = undefined;
|
|
1868
|
+
}
|
|
1869
|
+
const at2 = motion.scenes[hit?.scene ?? scene2];
|
|
1870
|
+
const name = MotionWords.fill(words.scene, { n: (hit?.scene ?? scene2) + 1 });
|
|
1871
|
+
if (!hit) {
|
|
1872
|
+
return { anchor: `scenes[${at2.index}]`, label: name, source: JSON.stringify(written?.scenes?.[at2.index] ?? {}) };
|
|
1873
|
+
}
|
|
1874
|
+
const part = at2.items[hit.item];
|
|
1875
|
+
const said = part.type === "text" ? part.text : part.type === "number" ? `${part.prefix}${part.value}${part.suffix}` : part.type === "list" ? part.items[0] : "";
|
|
1876
|
+
const short = said.length > 24 ? `${said.slice(0, 23)}…` : said;
|
|
1877
|
+
return {
|
|
1878
|
+
anchor: `scenes[${at2.index}].items[${part.index}]`,
|
|
1879
|
+
label: `${name} · ${words.kinds[part.type]}${short ? ` "${short}"` : ""}`,
|
|
1880
|
+
source: JSON.stringify(written?.scenes?.[at2.index]?.items?.[part.index] ?? {})
|
|
1881
|
+
};
|
|
1882
|
+
}
|
|
1883
|
+
function motionView(pi) {
|
|
1884
|
+
function Stage({
|
|
1885
|
+
motion,
|
|
1886
|
+
pictures,
|
|
1887
|
+
time,
|
|
1888
|
+
hits,
|
|
1889
|
+
onTap
|
|
1890
|
+
}) {
|
|
1891
|
+
const canvas = React.useRef(null);
|
|
1892
|
+
const from = React.useRef([0, 0]);
|
|
1893
|
+
const { width, height } = MotionSpec.frame(motion.size);
|
|
1894
|
+
React.useEffect(() => {
|
|
1895
|
+
const element = canvas.current;
|
|
1896
|
+
const context = element?.getContext("2d");
|
|
1897
|
+
if (!element || !context) {
|
|
1898
|
+
return;
|
|
1899
|
+
}
|
|
1900
|
+
const painter = new MotionPainter(context, motion, pictures);
|
|
1901
|
+
let shown = -1;
|
|
1902
|
+
let size = "";
|
|
1903
|
+
let frame = 0;
|
|
1904
|
+
const draw = () => {
|
|
1905
|
+
frame = requestAnimationFrame(draw);
|
|
1906
|
+
const scale = window.devicePixelRatio || 1;
|
|
1907
|
+
const w = element.clientWidth;
|
|
1908
|
+
const h = element.clientHeight;
|
|
1909
|
+
const key = `${w}x${h}x${scale}`;
|
|
1910
|
+
if (key !== size) {
|
|
1911
|
+
size = key;
|
|
1912
|
+
element.width = Math.max(1, Math.round(w * scale));
|
|
1913
|
+
element.height = Math.max(1, Math.round(h * scale));
|
|
1914
|
+
shown = -1;
|
|
1915
|
+
}
|
|
1916
|
+
if (time.current === shown || !w || !h) {
|
|
1917
|
+
return;
|
|
1918
|
+
}
|
|
1919
|
+
shown = time.current;
|
|
1920
|
+
context.setTransform(scale, 0, 0, scale, 0, 0);
|
|
1921
|
+
hits.current = painter.paint(shown, w, h);
|
|
1922
|
+
};
|
|
1923
|
+
frame = requestAnimationFrame(draw);
|
|
1924
|
+
return () => cancelAnimationFrame(frame);
|
|
1925
|
+
}, [motion, pictures, time, hits]);
|
|
1926
|
+
return /* @__PURE__ */ React.createElement("canvas", {
|
|
1927
|
+
ref: canvas,
|
|
1928
|
+
className: "tmt-canvas",
|
|
1929
|
+
style: { aspectRatio: `${width} / ${height}` },
|
|
1930
|
+
onPointerDown: (event) => {
|
|
1931
|
+
from.current = [event.clientX, event.clientY];
|
|
1932
|
+
},
|
|
1933
|
+
onClick: (event) => {
|
|
1934
|
+
if (Math.hypot(event.clientX - from.current[0], event.clientY - from.current[1]) >= TAP) {
|
|
1935
|
+
return;
|
|
1936
|
+
}
|
|
1937
|
+
const box2 = event.currentTarget.getBoundingClientRect();
|
|
1938
|
+
onTap(event.clientX - box2.left, event.clientY - box2.top);
|
|
1939
|
+
}
|
|
1940
|
+
});
|
|
1941
|
+
}
|
|
1942
|
+
function Player({ motion, props, edit, words }) {
|
|
1943
|
+
const code = edit.code;
|
|
1944
|
+
const { starts, total } = React.useMemo(() => MotionSpec.timeline(motion), [motion]);
|
|
1945
|
+
const time = React.useRef(edit.state.editing && edit.state.scene !== undefined ? (starts[edit.state.scene] ?? 0) + (motion.scenes[edit.state.scene]?.duration ?? 0) - 0.01 : 0);
|
|
1946
|
+
const hits = React.useRef([]);
|
|
1947
|
+
const card = React.useRef(null);
|
|
1948
|
+
const [now, setNow] = React.useState(time.current);
|
|
1949
|
+
const [playing, setPlaying] = React.useState(false);
|
|
1950
|
+
const [editing, setEditingState] = React.useState(edit.editable && !!edit.state.editing);
|
|
1951
|
+
const setEditing = (on) => {
|
|
1952
|
+
edit.state.editing = on;
|
|
1953
|
+
if (!on) {
|
|
1954
|
+
edit.commit();
|
|
1955
|
+
}
|
|
1956
|
+
setEditingState(on);
|
|
1957
|
+
};
|
|
1958
|
+
const [pictures, setPictures] = React.useState(() => () => {
|
|
1959
|
+
return;
|
|
1960
|
+
});
|
|
1961
|
+
const [exporting, setExporting] = React.useState(() => MotionFiles.progress(props.code));
|
|
1962
|
+
const started = React.useRef(time.current > 0);
|
|
1963
|
+
const clockAt = React.useRef(undefined);
|
|
1964
|
+
React.useEffect(() => MotionFiles.watch(props.code, setExporting), [props.code]);
|
|
1965
|
+
React.useEffect(() => {
|
|
1966
|
+
let alive = true;
|
|
1967
|
+
loadPictures(motion, (src) => pi.fileUrl ? pi.fileUrl(src) : Promise.resolve(src)).then((loaded) => alive && setPictures(() => loaded));
|
|
1968
|
+
return () => {
|
|
1969
|
+
alive = false;
|
|
1970
|
+
};
|
|
1971
|
+
}, [motion]);
|
|
1972
|
+
const seek = React.useCallback((t) => {
|
|
1973
|
+
time.current = Math.min(total, Math.max(0, t));
|
|
1974
|
+
setNow(time.current);
|
|
1975
|
+
if (clockAt.current) {
|
|
1976
|
+
clockAt.current = { at: performance.now(), from: time.current };
|
|
1977
|
+
}
|
|
1978
|
+
}, [total]);
|
|
1979
|
+
React.useEffect(() => {
|
|
1980
|
+
if (!playing) {
|
|
1981
|
+
clockAt.current = undefined;
|
|
1982
|
+
return;
|
|
1983
|
+
}
|
|
1984
|
+
if (time.current >= total - 0.01) {
|
|
1985
|
+
time.current = 0;
|
|
1986
|
+
}
|
|
1987
|
+
clockAt.current = { at: performance.now(), from: time.current };
|
|
1988
|
+
let frame = 0;
|
|
1989
|
+
let told = 0;
|
|
1990
|
+
const tick = () => {
|
|
1991
|
+
const clock2 = clockAt.current;
|
|
1992
|
+
if (!clock2) {
|
|
1993
|
+
return;
|
|
1994
|
+
}
|
|
1995
|
+
const t = clock2.from + (performance.now() - clock2.at) / 1000;
|
|
1996
|
+
time.current = Math.min(total, t);
|
|
1997
|
+
if (t >= total) {
|
|
1998
|
+
setNow(total);
|
|
1999
|
+
setPlaying(false);
|
|
2000
|
+
return;
|
|
2001
|
+
}
|
|
2002
|
+
if (performance.now() - told > 120) {
|
|
2003
|
+
told = performance.now();
|
|
2004
|
+
setNow(time.current);
|
|
2005
|
+
}
|
|
2006
|
+
frame = requestAnimationFrame(tick);
|
|
2007
|
+
};
|
|
2008
|
+
frame = requestAnimationFrame(tick);
|
|
2009
|
+
return () => cancelAnimationFrame(frame);
|
|
2010
|
+
}, [playing, total]);
|
|
2011
|
+
React.useEffect(() => {
|
|
2012
|
+
const element = card.current;
|
|
2013
|
+
if (!element || typeof IntersectionObserver === "undefined") {
|
|
2014
|
+
return;
|
|
2015
|
+
}
|
|
2016
|
+
const still = typeof matchMedia !== "undefined" && matchMedia("(prefers-reduced-motion: reduce)").matches;
|
|
2017
|
+
const observer = new IntersectionObserver(([entry]) => {
|
|
2018
|
+
if (!entry.isIntersecting) {
|
|
2019
|
+
setPlaying(false);
|
|
2020
|
+
} else if (!started.current) {
|
|
2021
|
+
started.current = true;
|
|
2022
|
+
if (still) {
|
|
2023
|
+
seek(total);
|
|
2024
|
+
} else {
|
|
2025
|
+
setPlaying(true);
|
|
2026
|
+
}
|
|
2027
|
+
}
|
|
2028
|
+
}, { threshold: 0.5 });
|
|
2029
|
+
observer.observe(element);
|
|
2030
|
+
return () => observer.disconnect();
|
|
2031
|
+
}, [seek, total]);
|
|
2032
|
+
const scene2 = MotionSpec.at(motion, now).index;
|
|
2033
|
+
const onTap = (x, y) => {
|
|
2034
|
+
if (props.point) {
|
|
2035
|
+
const hit = [...hits.current].reverse().find(({ box: [bx, by, bw, bh] }) => x >= bx && x <= bx + bw && y >= by && y <= by + bh);
|
|
2036
|
+
setPlaying(false);
|
|
2037
|
+
props.point(pointOf(motion, code, hit, MotionSpec.at(motion, time.current).index, words));
|
|
2038
|
+
return;
|
|
2039
|
+
}
|
|
2040
|
+
setPlaying(!playing);
|
|
2041
|
+
};
|
|
2042
|
+
const ended = !playing && now >= total - 0.01;
|
|
2043
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
2044
|
+
className: "tmt-card",
|
|
2045
|
+
ref: card
|
|
2046
|
+
}, motion.title && /* @__PURE__ */ React.createElement("div", {
|
|
2047
|
+
className: "tmt-title"
|
|
2048
|
+
}, motion.title), /* @__PURE__ */ React.createElement("div", {
|
|
2049
|
+
className: "tmt-stage"
|
|
2050
|
+
}, /* @__PURE__ */ React.createElement(Stage, {
|
|
2051
|
+
motion,
|
|
2052
|
+
pictures,
|
|
2053
|
+
time,
|
|
2054
|
+
hits,
|
|
2055
|
+
onTap
|
|
2056
|
+
})), /* @__PURE__ */ React.createElement("div", {
|
|
2057
|
+
className: "tmt-controls"
|
|
2058
|
+
}, /* @__PURE__ */ React.createElement("button", {
|
|
2059
|
+
type: "button",
|
|
2060
|
+
className: "tmt-play",
|
|
2061
|
+
"aria-label": playing ? words.pause : ended ? words.replay : words.play,
|
|
2062
|
+
title: playing ? words.pause : ended ? words.replay : words.play,
|
|
2063
|
+
onClick: () => setPlaying(!playing)
|
|
2064
|
+
}, /* @__PURE__ */ React.createElement(Glyph, {
|
|
2065
|
+
kind: playing ? "pause" : ended ? "replay" : "play"
|
|
2066
|
+
})), /* @__PURE__ */ React.createElement("div", {
|
|
2067
|
+
className: "tmt-track"
|
|
2068
|
+
}, /* @__PURE__ */ React.createElement("input", {
|
|
2069
|
+
type: "range",
|
|
2070
|
+
min: 0,
|
|
2071
|
+
max: total,
|
|
2072
|
+
step: 0.01,
|
|
2073
|
+
value: now,
|
|
2074
|
+
"aria-label": words.play,
|
|
2075
|
+
onChange: (event) => seek(Number(event.currentTarget.value))
|
|
2076
|
+
}), /* @__PURE__ */ React.createElement("div", {
|
|
2077
|
+
className: "tmt-ticks",
|
|
2078
|
+
"aria-hidden": "true"
|
|
2079
|
+
}, starts.slice(1).map((start) => /* @__PURE__ */ React.createElement("i", {
|
|
2080
|
+
key: start,
|
|
2081
|
+
style: { left: `${start / total * 100}%` }
|
|
2082
|
+
})))), /* @__PURE__ */ React.createElement("span", {
|
|
2083
|
+
className: "tmt-time"
|
|
2084
|
+
}, exporting !== undefined ? MotionWords.fill(words.exporting, { percent: exporting }) : `${clock(now)} / ${clock(total)}`), edit.editable && /* @__PURE__ */ React.createElement("button", {
|
|
2085
|
+
type: "button",
|
|
2086
|
+
className: "tmt-edit",
|
|
2087
|
+
"data-on": editing || undefined,
|
|
2088
|
+
onClick: () => setEditing(!editing)
|
|
2089
|
+
}, editing ? words.done : words.edit)), edit.editable && editing && /* @__PURE__ */ React.createElement(MotionEditor, {
|
|
2090
|
+
motion,
|
|
2091
|
+
scene: scene2,
|
|
2092
|
+
words,
|
|
2093
|
+
onScene: (index) => {
|
|
2094
|
+
edit.state.scene = index;
|
|
2095
|
+
setPlaying(false);
|
|
2096
|
+
seek(starts[index] + motion.scenes[index].duration - 0.01);
|
|
2097
|
+
},
|
|
2098
|
+
change: edit.change,
|
|
2099
|
+
commit: edit.commit
|
|
2100
|
+
}));
|
|
2101
|
+
}
|
|
2102
|
+
return function MotionView(props) {
|
|
2103
|
+
const words = MotionWords.of(pi.ui.language);
|
|
2104
|
+
const edit = useMotionEdit(pi, props);
|
|
2105
|
+
const code = edit.code;
|
|
2106
|
+
const read = React.useMemo(() => {
|
|
2107
|
+
if (!props.closed) {
|
|
2108
|
+
return;
|
|
2109
|
+
}
|
|
2110
|
+
try {
|
|
2111
|
+
return { motion: MotionSpec.read(code) };
|
|
2112
|
+
} catch (error) {
|
|
2113
|
+
return { error: error instanceof Error ? error.message : String(error) };
|
|
2114
|
+
}
|
|
2115
|
+
}, [code, props.closed]);
|
|
2116
|
+
if (!read) {
|
|
2117
|
+
const count = MotionSpec.scenesSoFar(props.code);
|
|
2118
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
2119
|
+
className: "tmt-card tmt-making"
|
|
2120
|
+
}, /* @__PURE__ */ React.createElement("div", {
|
|
2121
|
+
className: "tmt-making-line"
|
|
2122
|
+
}, words.making), count > 0 && /* @__PURE__ */ React.createElement("div", {
|
|
2123
|
+
className: "tmt-muted"
|
|
2124
|
+
}, MotionWords.fill(words.scenesSoFar, { count })));
|
|
2125
|
+
}
|
|
2126
|
+
if ("error" in read) {
|
|
2127
|
+
return /* @__PURE__ */ React.createElement("div", {
|
|
2128
|
+
className: "tmt-card tmt-failed"
|
|
2129
|
+
}, MotionWords.fill(words.failed, { reason: read.error ?? "" }));
|
|
2130
|
+
}
|
|
2131
|
+
return /* @__PURE__ */ React.createElement(Player, {
|
|
2132
|
+
motion: read.motion,
|
|
2133
|
+
props,
|
|
2134
|
+
edit,
|
|
2135
|
+
words
|
|
2136
|
+
});
|
|
2137
|
+
};
|
|
2138
|
+
}
|
|
2139
|
+
|
|
2140
|
+
// web-src/style.ts
|
|
2141
|
+
var STYLE = `
|
|
2142
|
+
/* An animation of a reply (\`\`\`ta-motion): a title, the frame, a line of controls, the fields to change it on its work's page. */
|
|
2143
|
+
.tmt-card {
|
|
2144
|
+
--tmt-s: var(--ta-font-scale, 1);
|
|
2145
|
+
margin: 2px 0 12px;
|
|
2146
|
+
padding: 12px;
|
|
2147
|
+
max-width: 600px;
|
|
2148
|
+
border: 1px solid var(--ta-border);
|
|
2149
|
+
border-radius: 12px;
|
|
2150
|
+
background: var(--ta-card);
|
|
2151
|
+
color: var(--ta-text);
|
|
2152
|
+
font-size: calc(13px * var(--tmt-s));
|
|
2153
|
+
line-height: 1.5;
|
|
2154
|
+
}
|
|
2155
|
+
.ta-code-embedded > .tmt-card { margin: 0; padding: 0; max-width: none; border: 0; background: none; }
|
|
2156
|
+
.tmt-title { font-weight: 600; margin-bottom: 8px; }
|
|
2157
|
+
.tmt-stage { border-radius: 8px; overflow: hidden; background: #0b1020; }
|
|
2158
|
+
.tmt-canvas { display: block; width: 100%; height: auto; cursor: pointer; touch-action: manipulation; }
|
|
2159
|
+
.tmt-controls { display: flex; align-items: center; gap: 10px; margin-top: 8px; }
|
|
2160
|
+
.tmt-play {
|
|
2161
|
+
flex: none; display: inline-flex; align-items: center; justify-content: center;
|
|
2162
|
+
width: 30px; height: 30px; border: 0; border-radius: 50%;
|
|
2163
|
+
background: var(--ta-accent); color: #fff; cursor: pointer;
|
|
2164
|
+
}
|
|
2165
|
+
.tmt-track { position: relative; flex: 1; min-width: 0; display: flex; align-items: center; }
|
|
2166
|
+
.tmt-track input { width: 100%; margin: 0; accent-color: var(--ta-accent); }
|
|
2167
|
+
.tmt-ticks { position: absolute; left: 6px; right: 6px; top: 50%; height: 0; pointer-events: none; }
|
|
2168
|
+
.tmt-ticks i { position: absolute; top: -5px; width: 2px; height: 10px; margin-left: -1px; border-radius: 1px; background: var(--ta-border); }
|
|
2169
|
+
.tmt-time { flex: none; min-width: 72px; text-align: right; color: var(--ta-muted); font-variant-numeric: tabular-nums; font-size: calc(12px * var(--tmt-s)); }
|
|
2170
|
+
.tmt-edit {
|
|
2171
|
+
flex: none; padding: 3px 10px; border: 1px solid var(--ta-border); border-radius: 999px;
|
|
2172
|
+
background: transparent; color: var(--ta-text); font: inherit; cursor: pointer;
|
|
2173
|
+
}
|
|
2174
|
+
.tmt-edit[data-on] { border-color: var(--ta-accent); color: var(--ta-accent); }
|
|
2175
|
+
.tmt-making, .tmt-failed { color: var(--ta-muted); }
|
|
2176
|
+
.tmt-making-line { color: var(--ta-text); }
|
|
2177
|
+
.tmt-muted { color: var(--ta-muted); font-size: calc(12px * var(--tmt-s)); }
|
|
2178
|
+
.tmt-editor { margin-top: 10px; padding-top: 10px; border-top: 1px solid var(--ta-border); }
|
|
2179
|
+
.tmt-scenes { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 10px; }
|
|
2180
|
+
.tmt-scenes button {
|
|
2181
|
+
min-width: 30px; height: 26px; padding: 0 8px; border: 1px solid var(--ta-border); border-radius: 8px;
|
|
2182
|
+
background: transparent; color: var(--ta-text); font: inherit; cursor: pointer;
|
|
2183
|
+
}
|
|
2184
|
+
.tmt-scenes button[data-on] { border-color: var(--ta-accent); background: var(--ta-accent); color: #fff; }
|
|
2185
|
+
.tmt-fields { display: flex; flex-direction: column; gap: 8px; }
|
|
2186
|
+
.tmt-row { display: flex; gap: 8px; }
|
|
2187
|
+
.tmt-row > .tmt-field { flex: 1; min-width: 0; }
|
|
2188
|
+
.tmt-field { display: flex; flex-direction: column; gap: 3px; }
|
|
2189
|
+
.tmt-field span, .tmt-group-name { color: var(--ta-muted); font-size: calc(12px * var(--tmt-s)); }
|
|
2190
|
+
.tmt-field input, .tmt-field textarea {
|
|
2191
|
+
width: 100%; box-sizing: border-box; padding: 6px 8px; border: 1px solid var(--ta-border); border-radius: 8px;
|
|
2192
|
+
background: var(--ta-input, transparent); color: var(--ta-text); font: inherit; resize: vertical;
|
|
2193
|
+
}
|
|
2194
|
+
.tmt-field input:focus, .tmt-field textarea:focus { outline: none; border-color: var(--ta-accent); }
|
|
2195
|
+
.tmt-group { display: flex; flex-direction: column; gap: 6px; }
|
|
2196
|
+
`;
|
|
2197
|
+
function injectStyle() {
|
|
2198
|
+
const element = document.createElement("style");
|
|
2199
|
+
element.dataset.taPackage = "ta-motion";
|
|
2200
|
+
element.textContent = STYLE;
|
|
2201
|
+
document.head.appendChild(element);
|
|
2202
|
+
return () => element.remove();
|
|
2203
|
+
}
|
|
2204
|
+
|
|
2205
|
+
// web-src/index.tsx
|
|
2206
|
+
function activate(pi) {
|
|
2207
|
+
bind(pi.React);
|
|
2208
|
+
if (!pi.registerCodeRenderer) {
|
|
2209
|
+
return () => {
|
|
2210
|
+
return;
|
|
2211
|
+
};
|
|
2212
|
+
}
|
|
2213
|
+
const removeStyle = injectStyle();
|
|
2214
|
+
pi.registerCodeRenderer(LANGUAGE, motionView(pi), { pointable: true });
|
|
2215
|
+
pi.registerCodeMenu?.((block) => {
|
|
2216
|
+
if (!block.drawn || block.language !== LANGUAGE) {
|
|
2217
|
+
return [];
|
|
2218
|
+
}
|
|
2219
|
+
const Icon = pi.ui.icons.Download;
|
|
2220
|
+
return [
|
|
2221
|
+
{
|
|
2222
|
+
key: "export",
|
|
2223
|
+
label: MotionWords.of(pi.ui.language).export,
|
|
2224
|
+
...Icon ? { icon: Icon } : {},
|
|
2225
|
+
run: () => void MotionFiles.save(pi, block.code)
|
|
2226
|
+
}
|
|
2227
|
+
];
|
|
2228
|
+
});
|
|
2229
|
+
return removeStyle;
|
|
2230
|
+
}
|
|
2231
|
+
export {
|
|
2232
|
+
activate as default
|
|
2233
|
+
};
|