aicommunicator 0.0.0-stage → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +55 -0
- package/README.md +93 -2
- package/bin/aic.js +20427 -0
- package/host-ui/host.js +5151 -0
- package/host-ui/index.html +483 -0
- package/host-ui/lang-en.js +631 -0
- package/package.json +30 -4
- package/plugin-guide.md +997 -0
- package/plugins/plugin-animation/LICENSE +55 -0
- package/plugins/plugin-animation/dist/logic.js +2225 -0
- package/plugins/plugin-animation/docs/manual.md +217 -0
- package/plugins/plugin-animation/manifest.json +31 -0
- package/plugins/plugin-animation/ui/icon.svg +6 -0
- package/plugins/plugin-animation/ui/index.html +2266 -0
- package/plugins/plugin-html-design/LICENSE +55 -0
- package/plugins/plugin-html-design/dist/logic.js +972 -0
- package/plugins/plugin-html-design/docs/manual.md +100 -0
- package/plugins/plugin-html-design/manifest.json +39 -0
- package/plugins/plugin-html-design/samples/exhibition-page.json +570 -0
- package/plugins/plugin-html-design/ui/icon.svg +5 -0
- package/plugins/plugin-html-design/ui/index.html +1241 -0
- package/plugins/plugin-html-slides/LICENSE +55 -0
- package/plugins/plugin-html-slides/dist/logic.js +1323 -0
- package/plugins/plugin-html-slides/docs/manual.md +121 -0
- package/plugins/plugin-html-slides/manifest.json +39 -0
- package/plugins/plugin-html-slides/samples/first-week-lecture.json +1129 -0
- package/plugins/plugin-html-slides/ui/icon.svg +5 -0
- package/plugins/plugin-html-slides/ui/index.html +1349 -0
- package/plugins/plugin-mindmap/LICENSE +55 -0
- package/plugins/plugin-mindmap/dist/logic.js +966 -0
- package/plugins/plugin-mindmap/docs/manual.md +145 -0
- package/plugins/plugin-mindmap/manifest.json +39 -0
- package/plugins/plugin-mindmap/samples/book-club.json +480 -0
- package/plugins/plugin-mindmap/ui/icon.svg +5 -0
- package/plugins/plugin-mindmap/ui/index.html +1774 -0
- package/plugins/plugin-plan/LICENSE +55 -0
- package/plugins/plugin-plan/dist/logic.js +1029 -0
- package/plugins/plugin-plan/docs/manual.md +261 -0
- package/plugins/plugin-plan/manifest.json +51 -0
- package/plugins/plugin-plan/samples/kimchi-day.json +1514 -0
- package/plugins/plugin-plan/samples/meeting-outline.json +653 -0
- package/plugins/plugin-plan/samples/reading-class.json +1577 -0
- package/plugins/plugin-plan/ui/icon.svg +7 -0
- package/plugins/plugin-plan/ui/index.html +1523 -0
- package/plugins/plugin-png/LICENSE +55 -0
- package/plugins/plugin-png/dist/logic.js +1758 -0
- package/plugins/plugin-png/docs/manual.md +344 -0
- package/plugins/plugin-png/manifest.json +35 -0
- package/plugins/plugin-png/ui/icon.svg +5 -0
- package/plugins/plugin-png/ui/index.html +2885 -0
- package/plugins/plugin-process-log/LICENSE +55 -0
- package/plugins/plugin-process-log/dist/logic.js +406 -0
- package/plugins/plugin-process-log/docs/manual.md +131 -0
- package/plugins/plugin-process-log/manifest.json +43 -0
- package/plugins/plugin-process-log/samples/design-decisions.json +241 -0
- package/plugins/plugin-process-log/ui/icon.svg +7 -0
- package/plugins/plugin-process-log/ui/index.html +721 -0
- package/plugins/plugin-story/LICENSE +55 -0
- package/plugins/plugin-story/dist/logic.js +2201 -0
- package/plugins/plugin-story/docs/manual.md +428 -0
- package/plugins/plugin-story/manifest.json +49 -0
- package/plugins/plugin-story/samples/scene-body.json +758 -0
- package/plugins/plugin-story/samples/time-and-groups.json +989 -0
- package/plugins/plugin-story/ui/icon.svg +4 -0
- package/plugins/plugin-story/ui/index.html +2783 -0
- package/plugins/plugin-svg/LICENSE +55 -0
- package/plugins/plugin-svg/dist/logic.js +2150 -0
- package/plugins/plugin-svg/docs/manual.md +314 -0
- package/plugins/plugin-svg/manifest.json +45 -0
- package/plugins/plugin-svg/samples/rocket-icon.json +1305 -0
- package/plugins/plugin-svg/samples/yard-cat.json +7796 -0
- package/plugins/plugin-svg/ui/icon.svg +4 -0
- package/plugins/plugin-svg/ui/index.html +2017 -0
- package/plugins/plugin-tree/LICENSE +55 -0
- package/plugins/plugin-tree/dist/logic.js +505 -0
- package/plugins/plugin-tree/docs/manual.md +140 -0
- package/plugins/plugin-tree/manifest.json +45 -0
- package/plugins/plugin-tree/samples/book-club.json +473 -0
- package/plugins/plugin-tree/samples/sort-memos.json +604 -0
- package/plugins/plugin-tree/ui/icon.svg +7 -0
- package/plugins/plugin-tree/ui/index.html +1305 -0
- package/schema/aic_describe.input.json +11 -0
- package/schema/aic_describe.output.json +67 -0
- package/schema/aic_observe.input.json +14 -0
- package/schema/aic_observe.output.json +47 -0
- package/schema/aic_send.input.json +15 -0
- package/schema/aic_send.output.json +38 -0
- package/schema/aic_update.input.json +19 -0
- package/schema/aic_update.output.json +39 -0
- package/schema/common.json +127 -0
- package/template/plugin/LICENSE +55 -0
- package/template/plugin/README.md +27 -0
- package/template/plugin/docs/manual.md +31 -0
- package/template/plugin/logic.js +337 -0
- package/template/plugin/manifest.json +27 -0
- package/template/plugin/ui/icon.svg +4 -0
- package/template/plugin/ui/index.html +269 -0
|
@@ -0,0 +1,2225 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
(() => {
|
|
3
|
+
// packages/plugin-sdk/src/index.ts
|
|
4
|
+
var SEPARATOR = " ";
|
|
5
|
+
var ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz";
|
|
6
|
+
function sortKey(order, id) {
|
|
7
|
+
return `${order}${SEPARATOR}${id}`;
|
|
8
|
+
}
|
|
9
|
+
function boundAfterOrder(order) {
|
|
10
|
+
return `${order}!`;
|
|
11
|
+
}
|
|
12
|
+
function between(lo, hi) {
|
|
13
|
+
const out = [];
|
|
14
|
+
for (const c of ALPHABET) {
|
|
15
|
+
if ((lo === void 0 || c > lo) && (hi === void 0 || c < hi)) out.push(c);
|
|
16
|
+
}
|
|
17
|
+
return out;
|
|
18
|
+
}
|
|
19
|
+
function orderBetween(left, right) {
|
|
20
|
+
if (left === void 0 && right === void 0) return "V";
|
|
21
|
+
const L = left ?? "";
|
|
22
|
+
const U = right;
|
|
23
|
+
if (U !== void 0 && !(L < U)) throw new Error(`orderBetween: \uC67C\uCABD ${L} \uC774 \uC624\uB978\uCABD ${U} \uBCF4\uB2E4 \uC55E\uC774\uC5B4\uC57C \uD55C\uB2E4`);
|
|
24
|
+
let i = 0;
|
|
25
|
+
while (U !== void 0 && i < L.length && i < U.length && L[i] === U[i]) i += 1;
|
|
26
|
+
if (U === void 0) i = 0;
|
|
27
|
+
if (i >= L.length && U !== void 0) {
|
|
28
|
+
let prefix = U.slice(0, i);
|
|
29
|
+
for (let k = i; k < U.length; k += 1) {
|
|
30
|
+
const uc2 = U[k];
|
|
31
|
+
const cands2 = between(void 0, uc2);
|
|
32
|
+
if (cands2.length > 0) return prefix + cands2[cands2.length - 1];
|
|
33
|
+
if (prefix !== "" && !prefix.endsWith(SEPARATOR) && uc2 > SEPARATOR) return prefix;
|
|
34
|
+
prefix += uc2;
|
|
35
|
+
}
|
|
36
|
+
throw new Error(`orderBetween: ${L} \uACFC ${U} \uC0AC\uC774\uC5D0 \uAC12\uC744 \uB9CC\uB4E4 \uC218 \uC5C6\uB2E4`);
|
|
37
|
+
}
|
|
38
|
+
const lc = L[i];
|
|
39
|
+
const uc = U === void 0 ? void 0 : U[i];
|
|
40
|
+
const cands = between(lc, uc);
|
|
41
|
+
if (cands.length > 0) {
|
|
42
|
+
if (uc === void 0) return L.slice(0, i) + cands[0];
|
|
43
|
+
return L.slice(0, i) + cands[Math.floor(cands.length / 2)];
|
|
44
|
+
}
|
|
45
|
+
for (let j = i + 1; j < L.length; j += 1) {
|
|
46
|
+
const up = between(L[j], void 0);
|
|
47
|
+
if (up.length > 0) return L.slice(0, j) + up[0];
|
|
48
|
+
}
|
|
49
|
+
return `${L}V`;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
// packages/image-quad/src/index.ts
|
|
53
|
+
function overlaps(a, b) {
|
|
54
|
+
return a.x < b.x + b.w && a.x + a.w > b.x && a.y < b.y + b.h && a.y + a.h > b.y;
|
|
55
|
+
}
|
|
56
|
+
var GLYPHS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789*=%&@$?!~^<>/|(){}[];:,-_";
|
|
57
|
+
var GLYPH_EMPTY = ".";
|
|
58
|
+
var GLYPH_OVERFLOW = "#";
|
|
59
|
+
function glyphMap(area, cols, rows, topAt, glyphOf, empty = GLYPH_EMPTY) {
|
|
60
|
+
const w = area.w / cols;
|
|
61
|
+
const h = area.h / rows;
|
|
62
|
+
const out = [];
|
|
63
|
+
for (let row = 0; row < rows; row += 1) {
|
|
64
|
+
let line = "";
|
|
65
|
+
for (let col = 0; col < cols; col += 1) {
|
|
66
|
+
const x = area.x + col * area.w / cols;
|
|
67
|
+
const y = area.y + row * area.h / rows;
|
|
68
|
+
const key = topAt({ col, row, x, y, w, h, cx: x + w / 2, cy: y + h / 2 });
|
|
69
|
+
line += key === null ? empty : glyphOf(key);
|
|
70
|
+
}
|
|
71
|
+
out.push(line);
|
|
72
|
+
}
|
|
73
|
+
return out;
|
|
74
|
+
}
|
|
75
|
+
|
|
76
|
+
// packages/plugin-animation/src/geometry.ts
|
|
77
|
+
var SHAPES = ["rect", "circle", "ellipse", "line", "polygon", "path"];
|
|
78
|
+
var SHAPE_WORD = { rect: "rectangle", circle: "circle", ellipse: "ellipse", line: "line", polygon: "polygon", path: "path" };
|
|
79
|
+
var GEO_FIELDS = {
|
|
80
|
+
rect: ["x", "y", "w", "h", "rx"],
|
|
81
|
+
circle: ["cx", "cy", "r"],
|
|
82
|
+
ellipse: ["cx", "cy", "rx", "ry"],
|
|
83
|
+
line: ["x1", "y1", "x2", "y2"],
|
|
84
|
+
polygon: ["points"],
|
|
85
|
+
path: ["d"]
|
|
86
|
+
};
|
|
87
|
+
var IDENTITY = { move: [0, 0], rotate: 0, scale: [1, 1], pivot: null };
|
|
88
|
+
var MAX_PARTS = 2e3;
|
|
89
|
+
var MAX_D = 4e3;
|
|
90
|
+
var MAX_POINTS = 200;
|
|
91
|
+
var MAX_COORD = 1e5;
|
|
92
|
+
var MAX_SIDE = 4096;
|
|
93
|
+
var Reject = class extends Error {
|
|
94
|
+
constructor(code, message) {
|
|
95
|
+
super(message);
|
|
96
|
+
this.code = code;
|
|
97
|
+
}
|
|
98
|
+
code;
|
|
99
|
+
};
|
|
100
|
+
var roundTo = (v, k) => {
|
|
101
|
+
const r = Math.round(v * k) / k;
|
|
102
|
+
return r === 0 ? 0 : r;
|
|
103
|
+
};
|
|
104
|
+
var r3 = (v) => roundTo(v, 1e3);
|
|
105
|
+
var r4 = (v) => roundTo(v, 1e4);
|
|
106
|
+
var fmt = (v) => String(r4(v));
|
|
107
|
+
var fmt1 = (v) => String(roundTo(v, 10));
|
|
108
|
+
function numInput(v, what, opts = {}) {
|
|
109
|
+
if (typeof v !== "number" || !Number.isFinite(v)) throw new Reject("INVALID_INPUT", `${what} is a number (write it without quotes). Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
110
|
+
const min = opts.min ?? -MAX_COORD;
|
|
111
|
+
const max = opts.max ?? MAX_COORD;
|
|
112
|
+
if (v < min || v > max) throw new Reject("INVALID_INPUT", `${what} is between ${min} and ${max}. Got: ${v}`);
|
|
113
|
+
if (opts.positive && !(v > 0)) throw new Reject("INVALID_INPUT", `${what} must be greater than 0. Got: ${v}`);
|
|
114
|
+
if (opts.nonNegative && v < 0) throw new Reject("INVALID_INPUT", `${what} is 0 or more. Got: ${v}`);
|
|
115
|
+
return r3(v);
|
|
116
|
+
}
|
|
117
|
+
function vecInput(v, what) {
|
|
118
|
+
let pair = null;
|
|
119
|
+
if (Array.isArray(v) && v.length === 2) pair = v;
|
|
120
|
+
else if (v && typeof v === "object" && !Array.isArray(v) && Object.keys(v).sort().join() === "x,y") pair = [v.x, v.y];
|
|
121
|
+
if (!pair) throw new Reject("INVALID_INPUT", `${what} is two numbers like [x, y]. Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
122
|
+
return [numInput(pair[0], `x of ${what}`), numInput(pair[1], `y of ${what}`)];
|
|
123
|
+
}
|
|
124
|
+
var COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6}|[0-9a-f]{8})$/;
|
|
125
|
+
function colorInput(v, what) {
|
|
126
|
+
if (v === null || v === "none") return null;
|
|
127
|
+
if (typeof v === "string" && COLOR.test(v.trim().toLowerCase())) return v.trim().toLowerCase();
|
|
128
|
+
throw new Reject("INVALID_INPUT", `${what} is a color like #rgb, #rrggbb or #rrggbbaa, or no color ("none" or null). Color names such as red are not accepted. Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
129
|
+
}
|
|
130
|
+
var isColor = (v) => v === null || typeof v === "string" && COLOR.test(v);
|
|
131
|
+
var ARGS = { M: 2, L: 2, H: 1, V: 1, C: 6, S: 4, Q: 4, T: 2, Z: 0 };
|
|
132
|
+
var TOKEN = /([MmLlHhVvCcSsQqTtZzAa])|([-+]?(?:\d+\.?\d*|\.\d+)(?:[eE][-+]?\d+)?)|([\s,]+)|(.)/g;
|
|
133
|
+
function normalizePath(d) {
|
|
134
|
+
if (typeof d !== "string" || d.trim() === "") return { problem: 'The d of a path is text that is not empty. Example: "M 10 10 L 30 10 Q 40 20 30 30 Z"' };
|
|
135
|
+
if (d.length > MAX_D) return { problem: `The d of the path is longer than ${MAX_D} characters (${d.length}). Split the curve into several parts` };
|
|
136
|
+
const tokens = [];
|
|
137
|
+
for (const m of d.matchAll(TOKEN)) {
|
|
138
|
+
if (m[1]) tokens.push(m[1]);
|
|
139
|
+
else if (m[2]) tokens.push(Number(m[2]));
|
|
140
|
+
else if (m[4]) return { problem: `The d of the path has a character that cannot be read: "${m[4]}". Use only command letters (M L H V C S Q T Z), numbers, spaces and commas` };
|
|
141
|
+
}
|
|
142
|
+
const out = [];
|
|
143
|
+
let i = 0;
|
|
144
|
+
let cur = [0, 0];
|
|
145
|
+
let start = [0, 0];
|
|
146
|
+
let first = true;
|
|
147
|
+
while (i < tokens.length) {
|
|
148
|
+
const t = tokens[i];
|
|
149
|
+
if (typeof t !== "string") return { problem: `The d of the path goes on with the number ${t} without a command letter. Write a command letter (such as M, L or C) before the numbers` };
|
|
150
|
+
i += 1;
|
|
151
|
+
const up = t.toUpperCase();
|
|
152
|
+
if (up === "A") return { problem: "Arcs (A, a) are not accepted. Write the same shape with C (cubic curve) or Q (quadratic curve). A half circle can be written with two C commands" };
|
|
153
|
+
if (first && up !== "M") return { problem: "The d of a path starts with M (the start point)" };
|
|
154
|
+
const rel = t !== up;
|
|
155
|
+
if (up === "Z") {
|
|
156
|
+
out.push("Z");
|
|
157
|
+
cur = start;
|
|
158
|
+
continue;
|
|
159
|
+
}
|
|
160
|
+
let cmd = up;
|
|
161
|
+
let groups = 0;
|
|
162
|
+
while (i < tokens.length && typeof tokens[i] === "number") {
|
|
163
|
+
const n3 = ARGS[cmd];
|
|
164
|
+
const args = tokens.slice(i, i + n3);
|
|
165
|
+
if (args.length < n3 || args.some((a) => typeof a !== "number")) return { problem: `The ${t} command of the path needs numbers in sets of ${ARGS[up]}` };
|
|
166
|
+
i += n3;
|
|
167
|
+
const nums = args.map((v) => {
|
|
168
|
+
if (!Number.isFinite(v) || Math.abs(v) > MAX_COORD) return NaN;
|
|
169
|
+
return v;
|
|
170
|
+
});
|
|
171
|
+
if (nums.some((v) => Number.isNaN(v))) return { problem: `Numbers in a path are between -${MAX_COORD} and ${MAX_COORD}` };
|
|
172
|
+
let abs;
|
|
173
|
+
if (cmd === "H") abs = [r3(rel ? cur[0] + nums[0] : nums[0])];
|
|
174
|
+
else if (cmd === "V") abs = [r3(rel ? cur[1] + nums[0] : nums[0])];
|
|
175
|
+
else abs = nums.map((v, k) => r3(rel ? v + cur[k % 2] : v));
|
|
176
|
+
out.push(`${cmd} ${abs.map(fmt).join(" ")}`);
|
|
177
|
+
if (cmd === "H") cur = [abs[0], cur[1]];
|
|
178
|
+
else if (cmd === "V") cur = [cur[0], abs[0]];
|
|
179
|
+
else cur = [abs[abs.length - 2], abs[abs.length - 1]];
|
|
180
|
+
if (cmd === "M") {
|
|
181
|
+
start = cur;
|
|
182
|
+
cmd = "L";
|
|
183
|
+
}
|
|
184
|
+
groups += 1;
|
|
185
|
+
first = false;
|
|
186
|
+
}
|
|
187
|
+
if (groups === 0) return { problem: `There are no numbers after the ${t} command of the path` };
|
|
188
|
+
}
|
|
189
|
+
if (out.length === 0) return { problem: "The d of the path has no commands" };
|
|
190
|
+
const norm = out.join(" ");
|
|
191
|
+
if (norm.length > MAX_D) return { problem: `Written again with absolute positions, the d of the path is longer than ${MAX_D} characters (${norm.length}). Split the curve into several parts` };
|
|
192
|
+
return { d: norm };
|
|
193
|
+
}
|
|
194
|
+
var CURVE_STEPS = 12;
|
|
195
|
+
var ROUND_STEPS = 32;
|
|
196
|
+
function pathOutline(d) {
|
|
197
|
+
const t = d.split(" ");
|
|
198
|
+
const subs = [];
|
|
199
|
+
let curSub = null;
|
|
200
|
+
let cur = [0, 0];
|
|
201
|
+
let start = [0, 0];
|
|
202
|
+
let lastC = null;
|
|
203
|
+
let lastQ = null;
|
|
204
|
+
let i = 0;
|
|
205
|
+
const num = () => Number(t[i++]);
|
|
206
|
+
const push = (p) => {
|
|
207
|
+
if (!curSub) {
|
|
208
|
+
curSub = { pts: [cur], closed: false };
|
|
209
|
+
subs.push(curSub);
|
|
210
|
+
}
|
|
211
|
+
curSub.pts.push(p);
|
|
212
|
+
};
|
|
213
|
+
const cubic = (a, b, e) => {
|
|
214
|
+
for (let k = 1; k <= CURVE_STEPS; k += 1) {
|
|
215
|
+
const u = k / CURVE_STEPS;
|
|
216
|
+
const v = 1 - u;
|
|
217
|
+
push([v * v * v * cur[0] + 3 * v * v * u * a[0] + 3 * v * u * u * b[0] + u * u * u * e[0], v * v * v * cur[1] + 3 * v * v * u * a[1] + 3 * v * u * u * b[1] + u * u * u * e[1]]);
|
|
218
|
+
}
|
|
219
|
+
};
|
|
220
|
+
const quad = (a, e) => {
|
|
221
|
+
for (let k = 1; k <= CURVE_STEPS; k += 1) {
|
|
222
|
+
const u = k / CURVE_STEPS;
|
|
223
|
+
const v = 1 - u;
|
|
224
|
+
push([v * v * cur[0] + 2 * v * u * a[0] + u * u * e[0], v * v * cur[1] + 2 * v * u * a[1] + u * u * e[1]]);
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
while (i < t.length) {
|
|
228
|
+
const c = t[i++];
|
|
229
|
+
let nextC = null;
|
|
230
|
+
let nextQ = null;
|
|
231
|
+
if (c === "M") {
|
|
232
|
+
cur = [num(), num()];
|
|
233
|
+
start = cur;
|
|
234
|
+
curSub = { pts: [cur], closed: false };
|
|
235
|
+
subs.push(curSub);
|
|
236
|
+
} else if (c === "L") {
|
|
237
|
+
const p = [num(), num()];
|
|
238
|
+
push(p);
|
|
239
|
+
cur = p;
|
|
240
|
+
} else if (c === "H") {
|
|
241
|
+
const p = [num(), cur[1]];
|
|
242
|
+
push(p);
|
|
243
|
+
cur = p;
|
|
244
|
+
} else if (c === "V") {
|
|
245
|
+
const p = [cur[0], num()];
|
|
246
|
+
push(p);
|
|
247
|
+
cur = p;
|
|
248
|
+
} else if (c === "C" || c === "S") {
|
|
249
|
+
const a = c === "C" ? [num(), num()] : lastC ? [2 * cur[0] - lastC[0], 2 * cur[1] - lastC[1]] : cur;
|
|
250
|
+
const b = [num(), num()];
|
|
251
|
+
const e = [num(), num()];
|
|
252
|
+
cubic(a, b, e);
|
|
253
|
+
cur = e;
|
|
254
|
+
nextC = b;
|
|
255
|
+
} else if (c === "Q" || c === "T") {
|
|
256
|
+
const a = c === "Q" ? [num(), num()] : lastQ ? [2 * cur[0] - lastQ[0], 2 * cur[1] - lastQ[1]] : cur;
|
|
257
|
+
const e = [num(), num()];
|
|
258
|
+
quad(a, e);
|
|
259
|
+
cur = e;
|
|
260
|
+
nextQ = a;
|
|
261
|
+
} else if (c === "Z") {
|
|
262
|
+
if (curSub) curSub.closed = true;
|
|
263
|
+
cur = start;
|
|
264
|
+
curSub = null;
|
|
265
|
+
}
|
|
266
|
+
lastC = nextC;
|
|
267
|
+
lastQ = nextQ;
|
|
268
|
+
}
|
|
269
|
+
return subs;
|
|
270
|
+
}
|
|
271
|
+
var n = (v) => v;
|
|
272
|
+
function outline(shape, geo) {
|
|
273
|
+
if (shape === "rect") {
|
|
274
|
+
const [x, y, w, h] = [n(geo.x), n(geo.y), n(geo.w), n(geo.h)];
|
|
275
|
+
return [{ closed: true, pts: [[x, y], [x + w, y], [x + w, y + h], [x, y + h]] }];
|
|
276
|
+
}
|
|
277
|
+
if (shape === "circle" || shape === "ellipse") {
|
|
278
|
+
const rx = shape === "circle" ? n(geo.r) : n(geo.rx);
|
|
279
|
+
const ry = shape === "circle" ? n(geo.r) : n(geo.ry);
|
|
280
|
+
const pts = [];
|
|
281
|
+
for (let k = 0; k < ROUND_STEPS; k += 1) {
|
|
282
|
+
const a = Math.PI * 2 * k / ROUND_STEPS;
|
|
283
|
+
pts.push([n(geo.cx) + rx * Math.cos(a), n(geo.cy) + ry * Math.sin(a)]);
|
|
284
|
+
}
|
|
285
|
+
return [{ closed: true, pts }];
|
|
286
|
+
}
|
|
287
|
+
if (shape === "line") return [{ closed: false, pts: [[n(geo.x1), n(geo.y1)], [n(geo.x2), n(geo.y2)]] }];
|
|
288
|
+
if (shape === "polygon") return [{ closed: true, pts: geo.points.map((p) => [p[0], p[1]]) }];
|
|
289
|
+
return pathOutline(String(geo.d));
|
|
290
|
+
}
|
|
291
|
+
var UNIT = [1, 0, 0, 1, 0, 0];
|
|
292
|
+
function mul(m, k) {
|
|
293
|
+
return [m[0] * k[0] + m[2] * k[1], m[1] * k[0] + m[3] * k[1], m[0] * k[2] + m[2] * k[3], m[1] * k[2] + m[3] * k[3], m[0] * k[4] + m[2] * k[5] + m[4], m[1] * k[4] + m[3] * k[5] + m[5]];
|
|
294
|
+
}
|
|
295
|
+
var apply = (m, p) => [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]];
|
|
296
|
+
function invert(m) {
|
|
297
|
+
const det = m[0] * m[3] - m[1] * m[2];
|
|
298
|
+
if (Math.abs(det) < 1e-12) return null;
|
|
299
|
+
const [a, b, c, d, e, f] = m;
|
|
300
|
+
return [d / det, -b / det, -c / det, a / det, (c * f - d * e) / det, (b * e - a * f) / det];
|
|
301
|
+
}
|
|
302
|
+
var isIdentityTurn = (p) => p.rotate === 0 && p.scale[0] === 1 && p.scale[1] === 1;
|
|
303
|
+
var isIdentity = (p) => isIdentityTurn(p) && p.move[0] === 0 && p.move[1] === 0;
|
|
304
|
+
function placeMatrix(p, pivot) {
|
|
305
|
+
const t = p.rotate * Math.PI / 180;
|
|
306
|
+
const cos = Math.cos(t);
|
|
307
|
+
const sin = Math.sin(t);
|
|
308
|
+
const [sx, sy] = p.scale;
|
|
309
|
+
const a = cos * sx;
|
|
310
|
+
const b = sin * sx;
|
|
311
|
+
const c = -sin * sy;
|
|
312
|
+
const d = cos * sy;
|
|
313
|
+
return [a, b, c, d, pivot[0] + p.move[0] - (a * pivot[0] + c * pivot[1]), pivot[1] + p.move[1] - (b * pivot[0] + d * pivot[1])];
|
|
314
|
+
}
|
|
315
|
+
function placeFromMatrix(m, pivot, keepPivot) {
|
|
316
|
+
const [a, b, c, d, e, f] = m;
|
|
317
|
+
const sx = Math.hypot(a, b);
|
|
318
|
+
if (sx < 1e-9) return null;
|
|
319
|
+
const t = Math.atan2(b, a);
|
|
320
|
+
const cos = Math.cos(t);
|
|
321
|
+
const sin = Math.sin(t);
|
|
322
|
+
const sy = -c * sin + d * cos;
|
|
323
|
+
const size = Math.max(sx, Math.abs(sy), 1);
|
|
324
|
+
if (Math.abs(c + sin * sy) > 1e-6 * size || Math.abs(d - cos * sy) > 1e-6 * size || Math.abs(sy) < 1e-9) return null;
|
|
325
|
+
let rotate = r3(t * 180 / Math.PI);
|
|
326
|
+
if (rotate === -180) rotate = 180;
|
|
327
|
+
const scale = [r4(sx), r4(sy)];
|
|
328
|
+
if (rotate === 0 && scale[0] === 1 && scale[1] === 1) return { move: [r3(e), r3(f)], rotate: 0, scale: [1, 1], pivot: keepPivot };
|
|
329
|
+
const q2 = { move: [0, 0], rotate, scale, pivot };
|
|
330
|
+
const base = placeMatrix(q2, pivot);
|
|
331
|
+
return { move: [r3(e - base[4]), r3(f - base[5])], rotate, scale, pivot };
|
|
332
|
+
}
|
|
333
|
+
function boxOf(points) {
|
|
334
|
+
if (points.length === 0) return null;
|
|
335
|
+
let x0 = Infinity;
|
|
336
|
+
let y0 = Infinity;
|
|
337
|
+
let x1 = -Infinity;
|
|
338
|
+
let y1 = -Infinity;
|
|
339
|
+
for (const [x, y] of points) {
|
|
340
|
+
if (x < x0) x0 = x;
|
|
341
|
+
if (y < y0) y0 = y;
|
|
342
|
+
if (x > x1) x1 = x;
|
|
343
|
+
if (y > y1) y1 = y;
|
|
344
|
+
}
|
|
345
|
+
return { x: x0, y: y0, w: x1 - x0, h: y1 - y0 };
|
|
346
|
+
}
|
|
347
|
+
function union(a, b) {
|
|
348
|
+
if (!a) return b;
|
|
349
|
+
if (!b) return a;
|
|
350
|
+
const x = Math.min(a.x, b.x);
|
|
351
|
+
const y = Math.min(a.y, b.y);
|
|
352
|
+
return { x, y, w: Math.max(a.x + a.w, b.x + b.w) - x, h: Math.max(a.y + a.h, b.y + b.h) - y };
|
|
353
|
+
}
|
|
354
|
+
var grow = (r, m) => ({ x: r.x - m, y: r.y - m, w: r.w + 2 * m, h: r.h + 2 * m });
|
|
355
|
+
var center = (r) => [r3(r.x + r.w / 2), r3(r.y + r.h / 2)];
|
|
356
|
+
var stretch = (m) => Math.max(Math.hypot(m[0], m[1]), Math.hypot(m[2], m[3]));
|
|
357
|
+
function insideFill(subs, x, y) {
|
|
358
|
+
let inside = false;
|
|
359
|
+
for (const s of subs) {
|
|
360
|
+
const pts = s.pts;
|
|
361
|
+
for (let i = 0, j = pts.length - 1; i < pts.length; j = i, i += 1) {
|
|
362
|
+
const [xi, yi] = pts[i];
|
|
363
|
+
const [xj, yj] = pts[j];
|
|
364
|
+
if (yi > y !== yj > y && x < (xj - xi) * (y - yi) / (yj - yi) + xi) inside = !inside;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
return inside;
|
|
368
|
+
}
|
|
369
|
+
function nearEdge(subs, x, y, lim) {
|
|
370
|
+
for (const s of subs) {
|
|
371
|
+
const pts = s.pts;
|
|
372
|
+
const m = s.closed ? pts.length : pts.length - 1;
|
|
373
|
+
if (pts.length === 1 && Math.hypot(x - pts[0][0], y - pts[0][1]) <= lim) return true;
|
|
374
|
+
for (let i = 0; i < m; i += 1) {
|
|
375
|
+
const a = pts[i];
|
|
376
|
+
const b = pts[(i + 1) % pts.length];
|
|
377
|
+
const dx = b[0] - a[0];
|
|
378
|
+
const dy = b[1] - a[1];
|
|
379
|
+
const L = dx * dx + dy * dy;
|
|
380
|
+
const t = L ? Math.max(0, Math.min(1, ((x - a[0]) * dx + (y - a[1]) * dy) / L)) : 0;
|
|
381
|
+
if (Math.hypot(x - (a[0] + t * dx), y - (a[1] + t * dy)) <= lim) return true;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
return false;
|
|
385
|
+
}
|
|
386
|
+
var finite = (v) => typeof v === "number" && Number.isFinite(v) && Math.abs(v) <= MAX_COORD;
|
|
387
|
+
var vec = (v) => Array.isArray(v) && v.length === 2 && finite(v[0]) && finite(v[1]);
|
|
388
|
+
function placeProblem(p) {
|
|
389
|
+
const x = p;
|
|
390
|
+
if (!x || typeof x !== "object") return "There is no placement (place)";
|
|
391
|
+
if (!vec(x.move) || !finite(x.rotate) || !vec(x.scale) || !(x.pivot === null || vec(x.pivot))) return "The numbers of the placement (place) are wrong";
|
|
392
|
+
if (x.scale[0] === 0 || x.scale[1] === 0) return "The scale of a placement is not 0";
|
|
393
|
+
if (x.pivot === null && !isIdentityTurn(x)) return "A rotated or resized placement needs a pivot";
|
|
394
|
+
return null;
|
|
395
|
+
}
|
|
396
|
+
function geoProblem(shape, geo) {
|
|
397
|
+
if (!geo || typeof geo !== "object") return "There are no shape values (geo)";
|
|
398
|
+
const keys = Object.keys(geo).sort().join();
|
|
399
|
+
if (keys !== [...GEO_FIELDS[shape]].sort().join()) return `The shape value fields of the ${SHAPE_WORD[shape]} do not match (${keys})`;
|
|
400
|
+
if (shape === "polygon") {
|
|
401
|
+
const pts = geo.points;
|
|
402
|
+
if (!Array.isArray(pts) || pts.length < 3 || pts.length > MAX_POINTS || !pts.every(vec)) return `A polygon has 3 to ${MAX_POINTS} points, each [x, y]`;
|
|
403
|
+
return null;
|
|
404
|
+
}
|
|
405
|
+
if (shape === "path") {
|
|
406
|
+
const norm = normalizePath(geo.d);
|
|
407
|
+
if ("problem" in norm) return norm.problem;
|
|
408
|
+
if (norm.d !== geo.d) return "The d of the path is not in the normalized form";
|
|
409
|
+
return null;
|
|
410
|
+
}
|
|
411
|
+
for (const k of GEO_FIELDS[shape]) if (!finite(geo[k])) return `${k} of the ${SHAPE_WORD[shape]} is not a number`;
|
|
412
|
+
if (shape === "rect" && !(n(geo.w) > 0 && n(geo.h) > 0 && n(geo.rx) >= 0)) return "A rectangle has width and height greater than 0 and corner radius 0 or more";
|
|
413
|
+
if (shape === "circle" && !(n(geo.r) > 0)) return "A circle has a radius greater than 0";
|
|
414
|
+
if (shape === "ellipse" && !(n(geo.rx) > 0 && n(geo.ry) > 0)) return "An ellipse has radii greater than 0";
|
|
415
|
+
return null;
|
|
416
|
+
}
|
|
417
|
+
function partProblem(c) {
|
|
418
|
+
const p = c;
|
|
419
|
+
if (!p || typeof p !== "object") return "The part has no content";
|
|
420
|
+
if (typeof p.name !== "string" || typeof p.order !== "string" || p.order === "") return "The name or order value of the part is wrong";
|
|
421
|
+
if (!(p.parent === null || typeof p.parent === "string")) return "The parent of the part is wrong";
|
|
422
|
+
if (typeof p.opacity !== "number" || !(p.opacity >= 0 && p.opacity <= 1)) return "Opacity is from 0 to 1";
|
|
423
|
+
const pp = placeProblem(p.place);
|
|
424
|
+
if (pp) return pp;
|
|
425
|
+
if (p.kind === "group") return null;
|
|
426
|
+
if (p.kind !== "shape" || !SHAPES.includes(p.shape)) return "The kind of the part is wrong";
|
|
427
|
+
const gp = geoProblem(p.shape, p.geo);
|
|
428
|
+
if (gp) return gp;
|
|
429
|
+
if (!isColor(p.fill) || !isColor(p.stroke)) return "A color is wrong";
|
|
430
|
+
if (p.shape === "line" && (p.fill !== null || p.stroke === null)) return "A line has no fill and needs a stroke color";
|
|
431
|
+
if (p.fill === null && p.stroke === null) return "There is no fill and no stroke color";
|
|
432
|
+
if (typeof p.strokeWidth !== "number" || !(p.strokeWidth > 0 && p.strokeWidth <= 1e3)) return "Stroke width is greater than 0 and at most 1000";
|
|
433
|
+
return null;
|
|
434
|
+
}
|
|
435
|
+
|
|
436
|
+
// packages/plugin-animation/src/motion.js
|
|
437
|
+
var MO_PROPS = ["move", "rotate", "scale", "opacity", "fill", "stroke"];
|
|
438
|
+
var MO_EASES = ["linear", "ease-in", "ease-out", "ease-in-out", "hold"];
|
|
439
|
+
var MO_BEZIER = { "ease-in": [0.42, 0, 1, 1], "ease-out": [0, 0, 0.58, 1], "ease-in-out": [0.42, 0, 0.58, 1] };
|
|
440
|
+
function moBezier(x1, y1, x2, y2, x) {
|
|
441
|
+
const at = (a, b, u) => 3 * (1 - u) * (1 - u) * u * a + 3 * (1 - u) * u * u * b + u * u * u;
|
|
442
|
+
let lo = 0;
|
|
443
|
+
let hi = 1;
|
|
444
|
+
for (let i = 0; i < 48; i += 1) {
|
|
445
|
+
const mid = (lo + hi) / 2;
|
|
446
|
+
if (at(x1, x2, mid) < x) lo = mid;
|
|
447
|
+
else hi = mid;
|
|
448
|
+
}
|
|
449
|
+
return at(y1, y2, (lo + hi) / 2);
|
|
450
|
+
}
|
|
451
|
+
function moEase(name, u) {
|
|
452
|
+
if (u <= 0) return 0;
|
|
453
|
+
if (u >= 1) return 1;
|
|
454
|
+
if (name === "hold") return 0;
|
|
455
|
+
const b = MO_BEZIER[name];
|
|
456
|
+
return b ? moBezier(b[0], b[1], b[2], b[3], u) : u;
|
|
457
|
+
}
|
|
458
|
+
function moRgb(c) {
|
|
459
|
+
const h = c.length === 4 ? c[1] + c[1] + c[2] + c[2] + c[3] + c[3] : c.slice(1, 7);
|
|
460
|
+
return [parseInt(h.slice(0, 2), 16), parseInt(h.slice(2, 4), 16), parseInt(h.slice(4, 6), 16)];
|
|
461
|
+
}
|
|
462
|
+
function moHex(rgb) {
|
|
463
|
+
return `#${rgb.map((v) => Math.max(0, Math.min(255, Math.round(v))).toString(16).padStart(2, "0")).join("")}`;
|
|
464
|
+
}
|
|
465
|
+
function moMix(prop, a, b, e) {
|
|
466
|
+
if (e <= 0) return a;
|
|
467
|
+
if (e >= 1) return b;
|
|
468
|
+
if (prop === "move" || prop === "scale") return [a[0] + (b[0] - a[0]) * e, a[1] + (b[1] - a[1]) * e];
|
|
469
|
+
if (prop === "fill" || prop === "stroke") {
|
|
470
|
+
const x = moRgb(a);
|
|
471
|
+
const y = moRgb(b);
|
|
472
|
+
return moHex([x[0] + (y[0] - x[0]) * e, x[1] + (y[1] - x[1]) * e, x[2] + (y[2] - x[2]) * e]);
|
|
473
|
+
}
|
|
474
|
+
return a + (b - a) * e;
|
|
475
|
+
}
|
|
476
|
+
function moValueAt(prop, keys, t) {
|
|
477
|
+
if (!keys || keys.length === 0) return void 0;
|
|
478
|
+
if (t <= keys[0].t) return keys[0].value;
|
|
479
|
+
const last = keys[keys.length - 1];
|
|
480
|
+
if (t >= last.t) return last.value;
|
|
481
|
+
let i = 0;
|
|
482
|
+
while (i + 1 < keys.length && keys[i + 1].t <= t) i += 1;
|
|
483
|
+
const a = keys[i];
|
|
484
|
+
const b = keys[i + 1];
|
|
485
|
+
return moMix(prop, a.value, b.value, moEase(a.ease, (t - a.t) / (b.t - a.t)));
|
|
486
|
+
}
|
|
487
|
+
function moStateAt(part, tracks, t) {
|
|
488
|
+
const pick = (prop, base) => {
|
|
489
|
+
const v = tracks ? moValueAt(prop, tracks[prop], t) : void 0;
|
|
490
|
+
return v === void 0 ? base : v;
|
|
491
|
+
};
|
|
492
|
+
return {
|
|
493
|
+
move: pick("move", part.place.move),
|
|
494
|
+
rotate: pick("rotate", part.place.rotate),
|
|
495
|
+
scale: pick("scale", part.place.scale),
|
|
496
|
+
pivot: part.place.pivot,
|
|
497
|
+
opacity: pick("opacity", part.opacity),
|
|
498
|
+
fill: pick("fill", part.fill === void 0 ? null : part.fill),
|
|
499
|
+
stroke: pick("stroke", part.stroke === void 0 ? null : part.stroke)
|
|
500
|
+
};
|
|
501
|
+
}
|
|
502
|
+
function moMatrix(st) {
|
|
503
|
+
const p = st.pivot || [0, 0];
|
|
504
|
+
const r = st.rotate * Math.PI / 180;
|
|
505
|
+
const cos = Math.cos(r);
|
|
506
|
+
const sin = Math.sin(r);
|
|
507
|
+
const a = cos * st.scale[0];
|
|
508
|
+
const b = sin * st.scale[0];
|
|
509
|
+
const c = -sin * st.scale[1];
|
|
510
|
+
const d = cos * st.scale[1];
|
|
511
|
+
return [a, b, c, d, p[0] + st.move[0] - (a * p[0] + c * p[1]), p[1] + st.move[1] - (b * p[0] + d * p[1])];
|
|
512
|
+
}
|
|
513
|
+
function moMul(m, k) {
|
|
514
|
+
return [m[0] * k[0] + m[2] * k[1], m[1] * k[0] + m[3] * k[1], m[0] * k[2] + m[2] * k[3], m[1] * k[2] + m[3] * k[3], m[0] * k[4] + m[2] * k[5] + m[4], m[1] * k[4] + m[3] * k[5] + m[5]];
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
// packages/plugin-animation/src/model.ts
|
|
518
|
+
var idNumber = (id) => Number(id.replace(/\D/g, ""));
|
|
519
|
+
var byIdNumber = (a, b) => idNumber(a.id) - idNumber(b.id) || (a.id < b.id ? -1 : a.id > b.id ? 1 : 0);
|
|
520
|
+
var all = (s, type) => Object.values(s.records).filter((r) => r.type === type);
|
|
521
|
+
var keyOf = (r) => sortKey(r.content.order, r.id);
|
|
522
|
+
var byKey = (a, b) => keyOf(a) < keyOf(b) ? -1 : keyOf(a) > keyOf(b) ? 1 : 0;
|
|
523
|
+
var propIndex = (p) => MO_PROPS.indexOf(p);
|
|
524
|
+
function canvasRecord(s) {
|
|
525
|
+
return all(s, "canvas").sort(byIdNumber)[0];
|
|
526
|
+
}
|
|
527
|
+
function childrenIndex(s) {
|
|
528
|
+
const out = /* @__PURE__ */ new Map();
|
|
529
|
+
for (const r of all(s, "part")) {
|
|
530
|
+
const p = r.content.parent;
|
|
531
|
+
const list = out.get(p) ?? [];
|
|
532
|
+
list.push(r);
|
|
533
|
+
out.set(p, list);
|
|
534
|
+
}
|
|
535
|
+
for (const list of out.values()) list.sort(byKey);
|
|
536
|
+
return out;
|
|
537
|
+
}
|
|
538
|
+
function buildModel(s) {
|
|
539
|
+
const cv = canvasRecord(s);
|
|
540
|
+
const canvas = cv ? { id: cv.id, c: cv.content } : null;
|
|
541
|
+
const kids = childrenIndex(s);
|
|
542
|
+
const order = [];
|
|
543
|
+
const byId = /* @__PURE__ */ new Map();
|
|
544
|
+
const walk = (r, parent, depth) => {
|
|
545
|
+
const node = { id: r.id, c: r.content, parent, children: [], z: order.length, depth, tracks: {}, keyCount: 0 };
|
|
546
|
+
order.push(node);
|
|
547
|
+
byId.set(r.id, node);
|
|
548
|
+
if (node.c.kind === "group") {
|
|
549
|
+
for (const k of kids.get(r.id) ?? []) if (!byId.has(k.id)) node.children.push(walk(k, node, depth + 1));
|
|
550
|
+
}
|
|
551
|
+
return node;
|
|
552
|
+
};
|
|
553
|
+
const roots = (kids.get(null) ?? []).map((r) => walk(r, null, 0));
|
|
554
|
+
for (const r of all(s, "part").sort(byKey)) if (!byId.has(r.id)) roots.push(walk(r, null, 0));
|
|
555
|
+
const keys = all(s, "key").map((r) => ({ id: r.id, c: r.content }));
|
|
556
|
+
const zOf = (k) => byId.get(k.c.part)?.z ?? Number.MAX_SAFE_INTEGER;
|
|
557
|
+
keys.sort((a, b) => zOf(a) - zOf(b) || propIndex(a.c.prop) - propIndex(b.c.prop) || a.c.t - b.c.t || byIdNumber(a, b));
|
|
558
|
+
for (const k of keys) {
|
|
559
|
+
const node = byId.get(k.c.part);
|
|
560
|
+
if (!node || propIndex(k.c.prop) < 0) continue;
|
|
561
|
+
const list = node.tracks[k.c.prop] ?? (node.tracks[k.c.prop] = []);
|
|
562
|
+
list.push({ id: k.id, t: k.c.t, value: k.c.value, ease: k.c.ease });
|
|
563
|
+
node.keyCount += 1;
|
|
564
|
+
}
|
|
565
|
+
const marks = all(s, "mark").map((r) => ({ id: r.id, c: r.content }));
|
|
566
|
+
marks.sort((a, b) => a.c.from - b.c.from || a.c.to - b.c.to || byIdNumber(a, b));
|
|
567
|
+
return { canvas, order, roots, byId, keys, keyById: new Map(keys.map((k) => [k.id, k])), marks };
|
|
568
|
+
}
|
|
569
|
+
var shapeOf = (n3) => n3.c.kind === "shape" ? n3.c : null;
|
|
570
|
+
function stateAt(n3, t) {
|
|
571
|
+
const sc = shapeOf(n3);
|
|
572
|
+
return moStateAt({ place: n3.c.place, opacity: n3.c.opacity, fill: sc?.fill ?? null, stroke: sc?.stroke ?? null }, n3.tracks, t);
|
|
573
|
+
}
|
|
574
|
+
function frameAt(m, t) {
|
|
575
|
+
const out = /* @__PURE__ */ new Map();
|
|
576
|
+
const visit = (n3, parentWorld) => {
|
|
577
|
+
const st = stateAt(n3, t);
|
|
578
|
+
const own = moMatrix(st);
|
|
579
|
+
const world = moMul(parentWorld, own);
|
|
580
|
+
const pose = { st, own, world, outline: [], half: 0, box: null };
|
|
581
|
+
out.set(n3.id, pose);
|
|
582
|
+
const sc = shapeOf(n3);
|
|
583
|
+
if (sc) {
|
|
584
|
+
pose.outline = outline(sc.shape, sc.geo).map((x) => ({ closed: x.closed, pts: x.pts.map((p) => apply(world, p)) }));
|
|
585
|
+
pose.half = st.stroke ? sc.strokeWidth / 2 * stretch(world) : 0;
|
|
586
|
+
const b = boxOf(pose.outline.flatMap((x) => x.pts));
|
|
587
|
+
pose.box = b ? grow(b, pose.half) : null;
|
|
588
|
+
} else {
|
|
589
|
+
for (const k of n3.children) pose.box = union(pose.box, visit(k, world));
|
|
590
|
+
}
|
|
591
|
+
return pose.box;
|
|
592
|
+
};
|
|
593
|
+
for (const r of m.roots) visit(r, UNIT);
|
|
594
|
+
return out;
|
|
595
|
+
}
|
|
596
|
+
function innerPoints(n3, t) {
|
|
597
|
+
const sc = shapeOf(n3);
|
|
598
|
+
if (sc) return outline(sc.shape, sc.geo).flatMap((x) => x.pts);
|
|
599
|
+
return n3.children.flatMap((k) => {
|
|
600
|
+
const own = moMatrix(stateAt(k, t));
|
|
601
|
+
return innerPoints(k, t).map((p) => apply(own, p));
|
|
602
|
+
});
|
|
603
|
+
}
|
|
604
|
+
function defaultPivot(n3) {
|
|
605
|
+
const b = boxOf(innerPoints(n3, 0));
|
|
606
|
+
return b ? center(b) : [0, 0];
|
|
607
|
+
}
|
|
608
|
+
var anchorOf = (n3) => n3.c.place.pivot ?? defaultPivot(n3);
|
|
609
|
+
var groupsOf = (node) => {
|
|
610
|
+
const out = [];
|
|
611
|
+
for (let p = node.parent; p; p = p.parent) out.push(p);
|
|
612
|
+
return out;
|
|
613
|
+
};
|
|
614
|
+
function subtreeOf(n3) {
|
|
615
|
+
return [n3, ...n3.children.flatMap(subtreeOf)];
|
|
616
|
+
}
|
|
617
|
+
function shapesAt(m, frame, x, y) {
|
|
618
|
+
const out = [];
|
|
619
|
+
for (let i = m.order.length - 1; i >= 0; i -= 1) {
|
|
620
|
+
const n3 = m.order[i];
|
|
621
|
+
const pose = frame.get(n3.id);
|
|
622
|
+
if (n3.c.kind !== "shape" || !pose?.box) continue;
|
|
623
|
+
const b = pose.box;
|
|
624
|
+
if (x < b.x || x > b.x + b.w || y < b.y || y > b.y + b.h) continue;
|
|
625
|
+
const filled = pose.st.fill !== null && n3.c.shape !== "line" && insideFill(pose.outline, x, y);
|
|
626
|
+
if (filled || pose.st.stroke !== null && nearEdge(pose.outline, x, y, Math.max(pose.half, 0.5))) out.push(n3);
|
|
627
|
+
}
|
|
628
|
+
return out;
|
|
629
|
+
}
|
|
630
|
+
var rootOf = (n3) => {
|
|
631
|
+
let p = n3;
|
|
632
|
+
while (p.parent) p = p.parent;
|
|
633
|
+
return p;
|
|
634
|
+
};
|
|
635
|
+
|
|
636
|
+
// packages/plugin-animation/src/samples.ts
|
|
637
|
+
var BOUNCE = {
|
|
638
|
+
id: "bounce",
|
|
639
|
+
version: "1",
|
|
640
|
+
title: "\uD280\uB294 \uACF5",
|
|
641
|
+
about: "A 1.2 second loop in a 160\xD7120 picture where a ball bounces up from the ground and comes back down. The ball is a group whose position and size move. The shadow's size and opacity move, and the ball body's fill color moves. Use it to try selecting keyframes and pointing at things",
|
|
642
|
+
width: 160,
|
|
643
|
+
height: 120,
|
|
644
|
+
duration: 1.2,
|
|
645
|
+
loop: true,
|
|
646
|
+
parts: [
|
|
647
|
+
{ key: "ground", name: "\uBC14\uB2E5", shape: "line", geo: { x1: 20, y1: 102, x2: 140, y2: 102 }, stroke: "#8d6e63", strokeWidth: 2 },
|
|
648
|
+
{ key: "shadow", name: "\uADF8\uB9BC\uC790", shape: "ellipse", geo: { cx: 80, cy: 102, rx: 16, ry: 3.5 }, fill: "#5d4037", pivot: [80, 102] },
|
|
649
|
+
{ key: "ball", name: "\uACF5", group: true, pivot: [80, 100] },
|
|
650
|
+
{ key: "ball-body", parent: "ball", name: "\uACF5 \uBAB8\uD1B5", shape: "circle", geo: { cx: 80, cy: 86, r: 14 }, fill: "#e53935" },
|
|
651
|
+
{ key: "ball-shine", parent: "ball", name: "\uACF5 \uBE5B", shape: "ellipse", geo: { cx: 75, cy: 80, rx: 4, ry: 2.5 }, fill: "#ffcdd2" }
|
|
652
|
+
],
|
|
653
|
+
keys: [
|
|
654
|
+
{ part: "ball", prop: "move", t: 0, value: [0, 0], ease: "ease-out" },
|
|
655
|
+
{ part: "ball", prop: "move", t: 0.6, value: [0, -60], ease: "ease-in" },
|
|
656
|
+
{ part: "ball", prop: "move", t: 1.2, value: [0, 0] },
|
|
657
|
+
{ part: "ball", prop: "scale", t: 0, value: [1.2, 0.8], ease: "ease-out" },
|
|
658
|
+
{ part: "ball", prop: "scale", t: 0.15, value: [1, 1] },
|
|
659
|
+
{ part: "ball", prop: "scale", t: 1.05, value: [1, 1], ease: "ease-in" },
|
|
660
|
+
{ part: "ball", prop: "scale", t: 1.2, value: [1.2, 0.8] },
|
|
661
|
+
{ part: "shadow", prop: "scale", t: 0, value: [1, 1], ease: "ease-out" },
|
|
662
|
+
{ part: "shadow", prop: "scale", t: 0.6, value: [0.55, 0.55], ease: "ease-in" },
|
|
663
|
+
{ part: "shadow", prop: "scale", t: 1.2, value: [1, 1] },
|
|
664
|
+
{ part: "shadow", prop: "opacity", t: 0, value: 0.45, ease: "ease-out" },
|
|
665
|
+
{ part: "shadow", prop: "opacity", t: 0.6, value: 0.15, ease: "ease-in" },
|
|
666
|
+
{ part: "shadow", prop: "opacity", t: 1.2, value: 0.45 },
|
|
667
|
+
{ part: "ball-body", prop: "fill", t: 0, value: "#e53935" },
|
|
668
|
+
{ part: "ball-body", prop: "fill", t: 0.6, value: "#ff7043" },
|
|
669
|
+
{ part: "ball-body", prop: "fill", t: 1.2, value: "#e53935" }
|
|
670
|
+
],
|
|
671
|
+
marks: [
|
|
672
|
+
{ name: "\uBC14\uB2E5\uC5D0 \uB2FF\uC74C", from: 0, to: 0 },
|
|
673
|
+
{ name: "\uC62C\uB77C\uAC10", from: 0, to: 0.6 },
|
|
674
|
+
{ name: "\uAC00\uC7A5 \uB192\uC740 \uACF3", from: 0.6, to: 0.6 },
|
|
675
|
+
{ name: "\uB0B4\uB824\uC634", from: 0.6, to: 1.2 }
|
|
676
|
+
]
|
|
677
|
+
};
|
|
678
|
+
var SAMPLES = [BOUNCE];
|
|
679
|
+
var sampleById = (id) => SAMPLES.find((x) => x.id === id);
|
|
680
|
+
|
|
681
|
+
// packages/plugin-animation/src/svg.ts
|
|
682
|
+
var esc = (t) => t.replace(/&/g, "&").replace(/</g, "<").replace(/>/g, ">").replace(/"/g, """).replace(/'/g, "'");
|
|
683
|
+
var n2 = (v) => fmt(v);
|
|
684
|
+
var isUnit = (st) => st.move[0] === 0 && st.move[1] === 0 && st.rotate === 0 && st.scale[0] === 1 && st.scale[1] === 1;
|
|
685
|
+
function transformText(st) {
|
|
686
|
+
if (isUnit(st)) return null;
|
|
687
|
+
const out = [];
|
|
688
|
+
if (st.move[0] !== 0 || st.move[1] !== 0) out.push(`translate(${fmt(st.move[0])} ${fmt(st.move[1])})`);
|
|
689
|
+
if (st.rotate !== 0 || st.scale[0] !== 1 || st.scale[1] !== 1) {
|
|
690
|
+
const pivot = st.pivot ?? [0, 0];
|
|
691
|
+
out.push(`translate(${fmt(pivot[0])} ${fmt(pivot[1])})`);
|
|
692
|
+
if (st.rotate !== 0) out.push(`rotate(${fmt(st.rotate)})`);
|
|
693
|
+
if (st.scale[0] !== 1 || st.scale[1] !== 1) out.push(st.scale[0] === st.scale[1] ? `scale(${fmt(st.scale[0])})` : `scale(${fmt(st.scale[0])} ${fmt(st.scale[1])})`);
|
|
694
|
+
out.push(`translate(${fmt(-pivot[0])} ${fmt(-pivot[1])})`);
|
|
695
|
+
}
|
|
696
|
+
return out.join(" ");
|
|
697
|
+
}
|
|
698
|
+
function geoAttrs(shape, g) {
|
|
699
|
+
if (shape === "rect") return `x="${n2(g.x)}" y="${n2(g.y)}" width="${n2(g.w)}" height="${n2(g.h)}"${g.rx > 0 ? ` rx="${n2(g.rx)}"` : ""}`;
|
|
700
|
+
if (shape === "circle") return `cx="${n2(g.cx)}" cy="${n2(g.cy)}" r="${n2(g.r)}"`;
|
|
701
|
+
if (shape === "ellipse") return `cx="${n2(g.cx)}" cy="${n2(g.cy)}" rx="${n2(g.rx)}" ry="${n2(g.ry)}"`;
|
|
702
|
+
if (shape === "line") return `x1="${n2(g.x1)}" y1="${n2(g.y1)}" x2="${n2(g.x2)}" y2="${n2(g.y2)}"`;
|
|
703
|
+
if (shape === "polygon") return `points="${g.points.map((p) => `${fmt(p[0])},${fmt(p[1])}`).join(" ")}"`;
|
|
704
|
+
return `d="${esc(String(g.d))}"`;
|
|
705
|
+
}
|
|
706
|
+
function openTag(node, st, transform) {
|
|
707
|
+
const c = node.c;
|
|
708
|
+
const head = `id="${node.id}"${c.name ? ` data-name="${esc(c.name)}"` : ""}`;
|
|
709
|
+
const tail = `${st.opacity !== 1 ? ` opacity="${fmt(st.opacity)}"` : ""}${transform ? ` transform="${transform}"` : ""}`;
|
|
710
|
+
if (c.kind === "group") return { tag: "g", attrs: `${head}${tail}` };
|
|
711
|
+
const fill = c.shape === "line" ? "" : ` fill="${st.fill ?? "none"}"`;
|
|
712
|
+
const stroke = st.stroke ? ` stroke="${st.stroke}" stroke-width="${fmt(c.strokeWidth)}"` : "";
|
|
713
|
+
return { tag: c.shape, attrs: `${head} ${geoAttrs(c.shape, c.geo)}${fill}${stroke}${tail}` };
|
|
714
|
+
}
|
|
715
|
+
function header(m, comment, box) {
|
|
716
|
+
const { width, height } = m.canvas.c;
|
|
717
|
+
const b = box ?? { x: 0, y: 0, w: width, h: height };
|
|
718
|
+
const out = [`<svg xmlns="http://www.w3.org/2000/svg" width="${fmt(b.w)}" height="${fmt(b.h)}" viewBox="${fmt(b.x)} ${fmt(b.y)} ${fmt(b.w)} ${fmt(b.h)}">`];
|
|
719
|
+
if (comment) out.push(` <!-- ${comment.replace(/--/g, "- -")} -->`);
|
|
720
|
+
return out;
|
|
721
|
+
}
|
|
722
|
+
function stillSvg(m, t, box) {
|
|
723
|
+
if (!m.canvas) return null;
|
|
724
|
+
const out = header(m, void 0, box);
|
|
725
|
+
const frame = box ? frameAt(m, t) : null;
|
|
726
|
+
const away = (node) => {
|
|
727
|
+
if (!frame || !box) return false;
|
|
728
|
+
const b = frame.get(node.id)?.box;
|
|
729
|
+
return !b || b.x > box.x + box.w + 1 || b.y > box.y + box.h + 1 || b.x + b.w < box.x - 1 || b.y + b.h < box.y - 1;
|
|
730
|
+
};
|
|
731
|
+
const element = (node, pad) => {
|
|
732
|
+
if (away(node)) return;
|
|
733
|
+
const st = stateAt(node, t);
|
|
734
|
+
const { tag, attrs } = openTag(node, st, transformText(st));
|
|
735
|
+
if (node.c.kind !== "group" || node.children.length === 0) {
|
|
736
|
+
out.push(`${pad}<${tag} ${attrs}/>`);
|
|
737
|
+
return;
|
|
738
|
+
}
|
|
739
|
+
out.push(`${pad}<g ${attrs}>`);
|
|
740
|
+
for (const k of node.children) element(k, `${pad} `);
|
|
741
|
+
out.push(`${pad}</g>`);
|
|
742
|
+
};
|
|
743
|
+
for (const r of m.roots) element(r, " ");
|
|
744
|
+
out.push("</svg>");
|
|
745
|
+
return `${out.join("\n")}
|
|
746
|
+
`;
|
|
747
|
+
}
|
|
748
|
+
var TIMING = {
|
|
749
|
+
linear: "linear",
|
|
750
|
+
"ease-in": `cubic-bezier(${MO_BEZIER["ease-in"].join(", ")})`,
|
|
751
|
+
"ease-out": `cubic-bezier(${MO_BEZIER["ease-out"].join(", ")})`,
|
|
752
|
+
"ease-in-out": `cubic-bezier(${MO_BEZIER["ease-in-out"].join(", ")})`,
|
|
753
|
+
hold: "step-end"
|
|
754
|
+
};
|
|
755
|
+
var percent = (t, duration) => `${String(Math.round(t / duration * 1e8) / 1e6)}%`;
|
|
756
|
+
var pivotOf = (st) => st.pivot ?? [0, 0];
|
|
757
|
+
function wrapTransform(kind, value, pivot) {
|
|
758
|
+
if (kind === "move") {
|
|
759
|
+
const v = value;
|
|
760
|
+
return { attr: `translate(${fmt(v[0])} ${fmt(v[1])})`, css: `translate(${fmt(v[0])}px, ${fmt(v[1])}px)` };
|
|
761
|
+
}
|
|
762
|
+
const [px, py] = pivot;
|
|
763
|
+
const around = (attr, css) => ({
|
|
764
|
+
attr: `translate(${fmt(px)} ${fmt(py)}) ${attr} translate(${fmt(-px)} ${fmt(-py)})`,
|
|
765
|
+
css: `translate(${fmt(px)}px, ${fmt(py)}px) ${css} translate(${fmt(-px)}px, ${fmt(-py)}px)`
|
|
766
|
+
});
|
|
767
|
+
if (kind === "rotate") return around(`rotate(${fmt(value)})`, `rotate(${fmt(value)}deg)`);
|
|
768
|
+
const s = value;
|
|
769
|
+
return around(`scale(${fmt(s[0])} ${fmt(s[1])})`, `scale(${fmt(s[0])}, ${fmt(s[1])})`);
|
|
770
|
+
}
|
|
771
|
+
function keyframesText(name, keys, duration, decl) {
|
|
772
|
+
const stops = [];
|
|
773
|
+
const push = (t, k, timing) => {
|
|
774
|
+
const at = percent(t, duration);
|
|
775
|
+
const body = `${decl(k.value)}${timing ? ` animation-timing-function: ${timing};` : ""}`;
|
|
776
|
+
if (stops.length && stops[stops.length - 1].at === at) stops[stops.length - 1] = { at, body };
|
|
777
|
+
else stops.push({ at, body });
|
|
778
|
+
};
|
|
779
|
+
if (keys[0].t > 0) push(0, keys[0], "linear");
|
|
780
|
+
keys.forEach((k, i) => push(k.t, k, i + 1 < keys.length ? TIMING[k.ease] : k.t < duration ? "linear" : null));
|
|
781
|
+
const last = keys[keys.length - 1];
|
|
782
|
+
if (last.t < duration) push(duration, last, null);
|
|
783
|
+
return `@keyframes ${name} {
|
|
784
|
+
${stops.map((s) => ` ${s.at} { ${s.body} }`).join("\n")}
|
|
785
|
+
}`;
|
|
786
|
+
}
|
|
787
|
+
function movingSvg(m, comment) {
|
|
788
|
+
if (!m.canvas) return null;
|
|
789
|
+
const { duration, loop } = m.canvas.c;
|
|
790
|
+
const css = [];
|
|
791
|
+
const play = (name) => `${name} ${fmt(duration)}s linear ${loop ? "infinite" : "1"} both`;
|
|
792
|
+
const body = [];
|
|
793
|
+
const element = (node, pad) => {
|
|
794
|
+
const st = stateAt(node, 0);
|
|
795
|
+
const tracks = node.tracks;
|
|
796
|
+
const wraps = [];
|
|
797
|
+
const split = Boolean(tracks.move || tracks.rotate || tracks.scale);
|
|
798
|
+
if (split) {
|
|
799
|
+
if (tracks.move || st.move[0] !== 0 || st.move[1] !== 0) wraps.push("move");
|
|
800
|
+
if (tracks.rotate || st.rotate !== 0) wraps.push("rotate");
|
|
801
|
+
if (tracks.scale || st.scale[0] !== 1 || st.scale[1] !== 1) wraps.push("scale");
|
|
802
|
+
}
|
|
803
|
+
let inner = pad;
|
|
804
|
+
for (const w of wraps) {
|
|
805
|
+
const id = `${node.id}-${w}`;
|
|
806
|
+
body.push(`${inner}<g id="${id}" transform="${wrapTransform(w, st[w], pivotOf(st)).attr}">`);
|
|
807
|
+
const keys = tracks[w];
|
|
808
|
+
if (keys) {
|
|
809
|
+
css.push(keyframesText(id, keys, duration, (v) => `transform: ${wrapTransform(w, v, pivotOf(st)).css};`));
|
|
810
|
+
css.push(`#${id} { animation: ${play(id)}; }`);
|
|
811
|
+
}
|
|
812
|
+
inner += " ";
|
|
813
|
+
}
|
|
814
|
+
const own = [];
|
|
815
|
+
for (const prop of ["opacity", "fill", "stroke"]) {
|
|
816
|
+
const keys = tracks[prop];
|
|
817
|
+
if (!keys) continue;
|
|
818
|
+
const name = `${node.id}-${prop}`;
|
|
819
|
+
css.push(keyframesText(name, keys, duration, (v) => `${prop}: ${typeof v === "number" ? fmt(v) : String(v)};`));
|
|
820
|
+
own.push(play(name));
|
|
821
|
+
}
|
|
822
|
+
if (own.length) css.push(`#${node.id} { animation: ${own.join(", ")}; }`);
|
|
823
|
+
const { tag, attrs } = openTag(node, st, split ? null : transformText(st));
|
|
824
|
+
if (node.c.kind !== "group" || node.children.length === 0) body.push(`${inner}<${tag} ${attrs}/>`);
|
|
825
|
+
else {
|
|
826
|
+
body.push(`${inner}<g ${attrs}>`);
|
|
827
|
+
for (const k of node.children) element(k, `${inner} `);
|
|
828
|
+
body.push(`${inner}</g>`);
|
|
829
|
+
}
|
|
830
|
+
for (let i = wraps.length - 1; i >= 0; i -= 1) body.push(`${pad}${" ".repeat(i)}</g>`);
|
|
831
|
+
};
|
|
832
|
+
for (const r of m.roots) element(r, " ");
|
|
833
|
+
const out = header(m, comment);
|
|
834
|
+
if (css.length) out.push(" <style>", ...css.join("\n").split("\n").map((l) => ` ${l}`), " </style>");
|
|
835
|
+
out.push(...body, "</svg>");
|
|
836
|
+
return `${out.join("\n")}
|
|
837
|
+
`;
|
|
838
|
+
}
|
|
839
|
+
function movingHtml(m, title, comment) {
|
|
840
|
+
const svg = movingSvg(m);
|
|
841
|
+
if (!svg || !m.canvas) return null;
|
|
842
|
+
const { width, height } = m.canvas.c;
|
|
843
|
+
return [
|
|
844
|
+
"<!doctype html>",
|
|
845
|
+
'<html lang="ko">',
|
|
846
|
+
"<head>",
|
|
847
|
+
'<meta charset="utf-8">',
|
|
848
|
+
'<meta name="viewport" content="width=device-width, initial-scale=1">',
|
|
849
|
+
`<title>${esc(title)}</title>`,
|
|
850
|
+
...comment ? [`<!-- ${comment.replace(/--/g, "- -")} -->`] : [],
|
|
851
|
+
"<style>",
|
|
852
|
+
" html, body { height: 100%; margin: 0; }",
|
|
853
|
+
" body { display: flex; align-items: center; justify-content: center; background: #f5f5f2; }",
|
|
854
|
+
` svg { display: block; width: min(96vw, calc(96vh * ${fmt(width)} / ${fmt(height)})); height: auto; cursor: pointer; }`,
|
|
855
|
+
" svg.paused, svg.paused * { animation-play-state: paused !important; }",
|
|
856
|
+
"</style>",
|
|
857
|
+
"</head>",
|
|
858
|
+
"<body>",
|
|
859
|
+
svg.trimEnd(),
|
|
860
|
+
"<script>",
|
|
861
|
+
"document.querySelector('svg').addEventListener('click', function () { this.classList.toggle('paused'); });",
|
|
862
|
+
"</script>",
|
|
863
|
+
"</body>",
|
|
864
|
+
"</html>",
|
|
865
|
+
""
|
|
866
|
+
].join("\n");
|
|
867
|
+
}
|
|
868
|
+
|
|
869
|
+
// packages/plugin-animation/src/logic.ts
|
|
870
|
+
var PACKAGE_ID = "aic.animation";
|
|
871
|
+
var VERSION = "0.1.4";
|
|
872
|
+
var SELECTION_FORMAT = "aic.animation/selection-1";
|
|
873
|
+
var SVG_MEDIA = "image/svg+xml; charset=utf-8";
|
|
874
|
+
var HTML_MEDIA = "text/html; charset=utf-8";
|
|
875
|
+
var partC = (r) => r.content;
|
|
876
|
+
var MAX_NAME = 100;
|
|
877
|
+
var MAX_NOTE = 1e3;
|
|
878
|
+
var MAX_REASON = 1e3;
|
|
879
|
+
var MAX_KEYS = 5e3;
|
|
880
|
+
var MAX_MARKS = 200;
|
|
881
|
+
var MIN_DURATION = 0.1;
|
|
882
|
+
var MAX_DURATION = 600;
|
|
883
|
+
var DEFAULT_DURATION = 2;
|
|
884
|
+
var MAX_TURN = 3600;
|
|
885
|
+
var PROP_WORD = { move: "position", rotate: "rotation", scale: "size", opacity: "opacity", fill: "fill color", stroke: "stroke color" };
|
|
886
|
+
var EASE_WORD = { linear: "steady", "ease-in": "slow start", "ease-out": "slow stop", "ease-in-out": "slow start and slow stop", hold: "stays, then jumps" };
|
|
887
|
+
var EASE_HELP = MO_EASES.map((e) => `${e}(${EASE_WORD[e]})`).join(", ");
|
|
888
|
+
var PROP_HELP = MO_PROPS.map((p) => `${p}(${PROP_WORD[p]})`).join(", ");
|
|
889
|
+
var sec = (t) => `${fmt(t)}s`;
|
|
890
|
+
var q = (t) => `"${t}"`;
|
|
891
|
+
var propName = (p) => p;
|
|
892
|
+
function valueText(prop, v) {
|
|
893
|
+
if (prop === "move") return `(${fmt(v[0])}, ${fmt(v[1])})`;
|
|
894
|
+
if (prop === "rotate") return `${fmt(v)}\xB0`;
|
|
895
|
+
if (prop === "scale") {
|
|
896
|
+
const s = v;
|
|
897
|
+
return s[0] === s[1] ? fmt(s[0]) : `width ${fmt(s[0])} height ${fmt(s[1])}`;
|
|
898
|
+
}
|
|
899
|
+
if (prop === "opacity") return fmt(v);
|
|
900
|
+
return String(v);
|
|
901
|
+
}
|
|
902
|
+
function roundValue(prop, v) {
|
|
903
|
+
if (prop === "move") return [r3(v[0]), r3(v[1])];
|
|
904
|
+
if (prop === "scale") return [r4(v[0]), r4(v[1])];
|
|
905
|
+
if (prop === "rotate" || prop === "opacity") return r3(v);
|
|
906
|
+
return v;
|
|
907
|
+
}
|
|
908
|
+
var boxText = (r) => `x ${fmt1(r.x)}~${fmt1(r.x + r.w)} y ${fmt1(r.y)}~${fmt1(r.y + r.h)}`;
|
|
909
|
+
var kindWord = (n3) => n3.c.kind === "group" ? "group" : SHAPE_WORD[n3.c.shape];
|
|
910
|
+
var partLabel = (n3) => `(${n3.id})${n3.c.name ? ` ${q(n3.c.name)}` : ""} ${kindWord(n3)}`;
|
|
911
|
+
var keyText = (prop, k, withEase = true) => `(${k.id}) ${sec(k.t)} ${valueText(prop, k.value)}${withEase ? ` [${k.ease}]` : ""}`;
|
|
912
|
+
var markText = (id, c) => `(${id}) ${q(c.name)} ${c.from === c.to ? sec(c.from) : `${sec(c.from)}~${sec(c.to)}`}`;
|
|
913
|
+
function geometryText(c) {
|
|
914
|
+
const g = c.geo;
|
|
915
|
+
if (c.shape === "polygon") return `points ${g.points.map((p) => `${fmt(p[0])},${fmt(p[1])}`).join(" ")}`;
|
|
916
|
+
if (c.shape === "path") return `d ${q(String(g.d))}`;
|
|
917
|
+
return GEO_FIELDS[c.shape].filter((k) => !(k === "rx" && c.shape === "rect" && g.rx === 0)).map((k) => `${k} ${fmt(g[k])}`).join(" ");
|
|
918
|
+
}
|
|
919
|
+
function paintText(c) {
|
|
920
|
+
const out = [];
|
|
921
|
+
if (c.shape !== "line") out.push(`fill ${c.fill ?? "none"}`);
|
|
922
|
+
if (c.stroke) out.push(`stroke ${c.stroke} width ${fmt(c.strokeWidth)}`);
|
|
923
|
+
return out.join(", ");
|
|
924
|
+
}
|
|
925
|
+
function placeText(p) {
|
|
926
|
+
const out = [];
|
|
927
|
+
if (p.move[0] !== 0 || p.move[1] !== 0) out.push(`move (${fmt(p.move[0])}, ${fmt(p.move[1])})`);
|
|
928
|
+
if (p.rotate !== 0) out.push(`rotate ${fmt(p.rotate)}\xB0`);
|
|
929
|
+
if (p.scale[0] !== 1 || p.scale[1] !== 1) out.push(`scale ${valueText("scale", p.scale)}`);
|
|
930
|
+
if (p.pivot) out.push(`pivot (${fmt(p.pivot[0])}, ${fmt(p.pivot[1])})`);
|
|
931
|
+
return out.join(", ");
|
|
932
|
+
}
|
|
933
|
+
function partLine(n3, star = "") {
|
|
934
|
+
const c = n3.c;
|
|
935
|
+
const bits = [];
|
|
936
|
+
if (c.kind === "group") bits.push(`group, ${n3.children.length} parts`);
|
|
937
|
+
else bits.push(`${c.shape} ${geometryText(c)}`, paintText(c));
|
|
938
|
+
if (c.opacity !== 1) bits.push(`opacity ${fmt(c.opacity)}`);
|
|
939
|
+
const place = placeText(c.place);
|
|
940
|
+
if (place) bits.push(place);
|
|
941
|
+
const moving = MO_PROPS.filter((p) => n3.tracks[p]);
|
|
942
|
+
if (moving.length) bits.push(`${n3.keyCount} keyframes (${moving.join(", ")})`);
|
|
943
|
+
return `(${n3.id})${star}${c.name ? ` ${q(c.name)}` : ""} ${bits.filter(Boolean).join(", ")}`;
|
|
944
|
+
}
|
|
945
|
+
function trackLine(prop, keys, star = () => "") {
|
|
946
|
+
return `${propName(prop)}: ${keys.map((k, i) => `(${k.id})${star(k.id)} ${sec(k.t)} ${valueText(prop, k.value)}${i + 1 < keys.length ? ` [${k.ease}]` : ""}`).join(" ")}`;
|
|
947
|
+
}
|
|
948
|
+
function need(s, id, type, what) {
|
|
949
|
+
if (typeof id !== "string" || id === "") throw new Reject("INVALID_INPUT", `The id of the ${what} is required`);
|
|
950
|
+
const r = s.records[id];
|
|
951
|
+
if (!r) throw new Reject("TARGET_NOT_FOUND", `Cannot find ${what} ${id}`);
|
|
952
|
+
if (r.type !== type) throw new Reject("INVALID_INPUT", `${id} is not a ${what} (it is a ${r.type})`);
|
|
953
|
+
return r;
|
|
954
|
+
}
|
|
955
|
+
function needCanvas(s) {
|
|
956
|
+
const r = canvasRecord(s);
|
|
957
|
+
if (!r) throw new Reject("INVALID_STATE", "There is no picture yet. Start one with canvas.start first");
|
|
958
|
+
return { r, c: r.content };
|
|
959
|
+
}
|
|
960
|
+
function onlyKeys(input, allowed, op) {
|
|
961
|
+
const extra = Object.keys(input).filter((k) => !allowed.includes(k));
|
|
962
|
+
if (extra.length) throw new Reject("INVALID_INPUT", `Fields ${op} does not know: ${extra.join(", ")}. Fields it takes: ${allowed.join(", ")}`);
|
|
963
|
+
}
|
|
964
|
+
function textInput(v, what, ctx, max) {
|
|
965
|
+
if (v === void 0 || v === null) return "";
|
|
966
|
+
if (typeof v !== "string") throw new Reject("INVALID_INPUT", `${what} must be text`);
|
|
967
|
+
const t = v.replace(/\r\n?/g, "\n").trim();
|
|
968
|
+
if (t.length > max) throw new Reject("INVALID_INPUT", `${what} is longer than ${max} characters (${t.length})`);
|
|
969
|
+
if (ctx.tempNames.includes(t)) throw new Reject("INVALID_INPUT", `"${t}" is the same as a temporary name in this request, so it cannot be used as text`);
|
|
970
|
+
return t;
|
|
971
|
+
}
|
|
972
|
+
var PLACE_KEYS = ["move", "rotate", "scale", "pivot"];
|
|
973
|
+
var PAINT_KEYS = ["fill", "stroke", "strokeWidth", "opacity"];
|
|
974
|
+
function pointsInput(v) {
|
|
975
|
+
let pairs;
|
|
976
|
+
if (typeof v === "string") {
|
|
977
|
+
const nums = v.trim().split(/[\s,]+/).filter(Boolean).map(Number);
|
|
978
|
+
if (nums.length % 2 !== 0 || nums.some((x) => !Number.isFinite(x))) throw new Reject("INVALID_INPUT", 'points looks like [[x, y], ...] or "x,y x,y ..."');
|
|
979
|
+
pairs = [];
|
|
980
|
+
for (let i = 0; i < nums.length; i += 2) pairs.push([nums[i], nums[i + 1]]);
|
|
981
|
+
} else if (Array.isArray(v)) pairs = v;
|
|
982
|
+
else throw new Reject("INVALID_INPUT", "points is a list of corners like [[x, y], ...]");
|
|
983
|
+
if (pairs.length < 3 || pairs.length > MAX_POINTS) throw new Reject("INVALID_INPUT", `A polygon has 3 to ${MAX_POINTS} points (got ${pairs.length})`);
|
|
984
|
+
return pairs.map((p, i) => vecInput(p, `points[${i}]`));
|
|
985
|
+
}
|
|
986
|
+
function geoInput(shape, input, base) {
|
|
987
|
+
const g = {};
|
|
988
|
+
for (const k of GEO_FIELDS[shape]) {
|
|
989
|
+
if (input[k] === void 0) {
|
|
990
|
+
if (base) g[k] = base[k];
|
|
991
|
+
else if (shape === "rect" && k === "rx") g[k] = 0;
|
|
992
|
+
else throw new Reject("INVALID_INPUT", `A ${shape} needs ${k}. Shape values: ${GEO_FIELDS[shape].join(", ")}`);
|
|
993
|
+
continue;
|
|
994
|
+
}
|
|
995
|
+
if (k === "points") g.points = pointsInput(input.points);
|
|
996
|
+
else if (k === "d") {
|
|
997
|
+
const norm = normalizePath(input.d);
|
|
998
|
+
if ("problem" in norm) throw new Reject("INVALID_INPUT", norm.problem);
|
|
999
|
+
g.d = norm.d;
|
|
1000
|
+
} else g[k] = numInput(input[k], `${k} of the ${shape}`);
|
|
1001
|
+
}
|
|
1002
|
+
const problem = geoProblem(shape, g);
|
|
1003
|
+
if (problem) throw new Reject("INVALID_INPUT", problem);
|
|
1004
|
+
return g;
|
|
1005
|
+
}
|
|
1006
|
+
function scaleInput(v) {
|
|
1007
|
+
const one = (x, what) => {
|
|
1008
|
+
if (typeof x !== "number" || !Number.isFinite(x) || Math.abs(x) > 100 || Math.abs(x) < 0.01) {
|
|
1009
|
+
throw new Reject("INVALID_INPUT", `${what} is a number whose size is between 0.01 and 100. A negative number flips. Got: ${JSON.stringify(x) ?? "nothing"}`);
|
|
1010
|
+
}
|
|
1011
|
+
return r4(x);
|
|
1012
|
+
};
|
|
1013
|
+
if (typeof v === "number") return [one(v, "scale"), one(v, "scale")];
|
|
1014
|
+
if (Array.isArray(v) && v.length === 2) return [one(v[0], "scale width"), one(v[1], "scale height")];
|
|
1015
|
+
if (v && typeof v === "object" && Object.keys(v).sort().join() === "x,y") return [one(v.x, "scale width"), one(v.y, "scale height")];
|
|
1016
|
+
throw new Reject("INVALID_INPUT", "scale is one number (same width and height) or [width, height]");
|
|
1017
|
+
}
|
|
1018
|
+
function placeInput(input, base, pivotFor, keepPivot = false) {
|
|
1019
|
+
const move = input.move !== void 0 ? vecInput(input.move, "move") : base.move;
|
|
1020
|
+
const rotate = input.rotate !== void 0 ? numInput(input.rotate, "rotate (degrees)", { min: -360, max: 360 }) : base.rotate;
|
|
1021
|
+
const scale = input.scale !== void 0 ? scaleInput(input.scale) : base.scale;
|
|
1022
|
+
let pivot = input.pivot === void 0 ? base.pivot : input.pivot === null ? null : vecInput(input.pivot, "pivot");
|
|
1023
|
+
const next = { move, rotate, scale, pivot };
|
|
1024
|
+
if (pivot === null && (keepPivot || !isIdentityTurn(next))) pivot = pivotFor();
|
|
1025
|
+
return { move, rotate, scale, pivot };
|
|
1026
|
+
}
|
|
1027
|
+
function paintInput(shape, input, base) {
|
|
1028
|
+
let fill = input.fill !== void 0 ? colorInput(input.fill, "fill") : base?.fill;
|
|
1029
|
+
let stroke = input.stroke !== void 0 ? colorInput(input.stroke, "stroke") : base?.stroke;
|
|
1030
|
+
if (shape === "line") {
|
|
1031
|
+
if (input.fill !== void 0 && fill !== null) throw new Reject("INVALID_INPUT", "A line has no fill. Give its color with stroke");
|
|
1032
|
+
fill = null;
|
|
1033
|
+
if (!stroke) throw new Reject("INVALID_INPUT", "A line needs a stroke color");
|
|
1034
|
+
} else {
|
|
1035
|
+
if (fill === void 0 && stroke === void 0) throw new Reject("INVALID_INPUT", 'Give fill (fill color) or stroke (stroke color). For no color use "none"');
|
|
1036
|
+
fill ??= null;
|
|
1037
|
+
stroke ??= null;
|
|
1038
|
+
if (fill === null && stroke === null) throw new Reject("INVALID_INPUT", "With no fill and no stroke color the shape is invisible. Give a color to fill or stroke");
|
|
1039
|
+
}
|
|
1040
|
+
const strokeWidth = input.strokeWidth !== void 0 ? numInput(input.strokeWidth, "strokeWidth (stroke width)", { min: 0, max: 1e3, positive: true }) : base?.strokeWidth ?? 1;
|
|
1041
|
+
const opacity = input.opacity !== void 0 ? numInput(input.opacity, "opacity", { min: 0, max: 1 }) : base?.opacity ?? 1;
|
|
1042
|
+
return { fill, stroke: stroke ?? null, strokeWidth, opacity };
|
|
1043
|
+
}
|
|
1044
|
+
function durationInput(v) {
|
|
1045
|
+
if (typeof v !== "number" || !Number.isFinite(v) || v < MIN_DURATION || v > MAX_DURATION) throw new Reject("INVALID_INPUT", `duration (length) is a number of seconds from ${MIN_DURATION} to ${MAX_DURATION}. Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
1046
|
+
return r3(v);
|
|
1047
|
+
}
|
|
1048
|
+
function timeInput(v, what, duration) {
|
|
1049
|
+
if (typeof v !== "number" || !Number.isFinite(v)) throw new Reject("INVALID_INPUT", `${what} is a number of seconds (write it without quotes). Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
1050
|
+
const t = r3(v);
|
|
1051
|
+
if (t < 0 || t > duration) throw new Reject("INVALID_INPUT", `${what} is between 0 and the picture length ${fmt(duration)}s. Got: ${v}. For a longer motion, first make duration longer with canvas.set`);
|
|
1052
|
+
return t;
|
|
1053
|
+
}
|
|
1054
|
+
function easeInput(v) {
|
|
1055
|
+
if (typeof v !== "string" || !MO_EASES.includes(v)) throw new Reject("INVALID_INPUT", `ease is one of ${EASE_HELP}. Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
1056
|
+
return v;
|
|
1057
|
+
}
|
|
1058
|
+
function propInput(v) {
|
|
1059
|
+
if (typeof v !== "string" || propIndex(v) < 0) throw new Reject("INVALID_INPUT", `prop is one of ${PROP_HELP}. Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
1060
|
+
return v;
|
|
1061
|
+
}
|
|
1062
|
+
var KEY_COLOR = /^#(?:[0-9a-f]{3}|[0-9a-f]{6})$/;
|
|
1063
|
+
function keyValueInput(prop, v) {
|
|
1064
|
+
if (prop === "move") return vecInput(v, "move");
|
|
1065
|
+
if (prop === "rotate") return numInput(v, "rotate (degrees)", { min: -MAX_TURN, max: MAX_TURN });
|
|
1066
|
+
if (prop === "scale") return scaleInput(v);
|
|
1067
|
+
if (prop === "opacity") return numInput(v, "opacity", { min: 0, max: 1 });
|
|
1068
|
+
const c = typeof v === "string" ? v.trim().toLowerCase() : v;
|
|
1069
|
+
if (typeof c !== "string" || !KEY_COLOR.test(c)) throw new Reject("INVALID_INPUT", `A keyframe's ${prop} is a color like #rgb or #rrggbb. It does not take none or a color with transparency (#rrggbbaa). To make something disappear, use opacity. Got: ${JSON.stringify(v) ?? "nothing"}`);
|
|
1070
|
+
return c.length === 4 ? `#${c[1]}${c[1]}${c[2]}${c[2]}${c[3]}${c[3]}` : c;
|
|
1071
|
+
}
|
|
1072
|
+
function propProblem(c, id, prop) {
|
|
1073
|
+
if (prop !== "fill" && prop !== "stroke") return null;
|
|
1074
|
+
if (c.kind === "group") return `${id} is a group, so it has no color. Give ${PROP_WORD[prop]} keyframes to the shapes inside it`;
|
|
1075
|
+
if (prop === "fill" && (c.shape === "line" || c.fill === null)) return `${id} has no fill, so its fill color cannot move.${c.shape === "line" ? " A line uses stroke" : " First give fill a color with part.set"}`;
|
|
1076
|
+
if (prop === "stroke" && c.stroke === null) return `${id} has no stroke color, so its stroke color cannot move. First give stroke a color with part.set`;
|
|
1077
|
+
return null;
|
|
1078
|
+
}
|
|
1079
|
+
function kidsOf(s, parent, except = /* @__PURE__ */ new Set()) {
|
|
1080
|
+
return all(s, "part").filter((r) => partC(r).parent === parent && !except.has(r.id)).sort(byKey);
|
|
1081
|
+
}
|
|
1082
|
+
var leftKey = (r) => r ? r.id.startsWith("$") ? boundAfterOrder(partC(r).order) : keyOf(r) : void 0;
|
|
1083
|
+
var rightKey = (r) => r ? r.id.startsWith("$") ? partC(r).order : keyOf(r) : void 0;
|
|
1084
|
+
function parentInput(s, v) {
|
|
1085
|
+
if (v === void 0 || v === null || v === "") return null;
|
|
1086
|
+
const r = need(s, v, "part", "group (parent)");
|
|
1087
|
+
if (partC(r).kind !== "group") throw new Reject("INVALID_INPUT", `${r.id} is a shape, not a group. Give parent the id of a group`);
|
|
1088
|
+
return r.id;
|
|
1089
|
+
}
|
|
1090
|
+
var placeWord = (s, parent) => parent === null ? "the top level" : `group ${parent}${partC(s.records[parent]).name ? ` (${partC(s.records[parent]).name})` : ""}`;
|
|
1091
|
+
function positionInput(s, input, self) {
|
|
1092
|
+
if (input.above !== void 0 && input.below !== void 0) throw new Reject("INVALID_INPUT", "Give only one of above and below");
|
|
1093
|
+
const anchorId = input.above ?? input.below;
|
|
1094
|
+
if (anchorId !== void 0) {
|
|
1095
|
+
const a = need(s, anchorId, "part", input.above !== void 0 ? "part for above" : "part for below");
|
|
1096
|
+
if (a.id === self) throw new Reject("INVALID_INPUT", `${a.id} cannot move above or below itself`);
|
|
1097
|
+
const parent2 = partC(a).parent;
|
|
1098
|
+
if ("parent" in input && parentInput(s, input.parent) !== parent2) {
|
|
1099
|
+
throw new Reject("INVALID_INPUT", `${a.id} is in ${placeWord(s, parent2)}. When you give above or below, leave out parent or give the same group`);
|
|
1100
|
+
}
|
|
1101
|
+
const sibs2 = kidsOf(s, parent2, new Set(self ? [self] : []));
|
|
1102
|
+
const at = sibs2.findIndex((r) => r.id === a.id);
|
|
1103
|
+
const [left, right] = input.above !== void 0 ? [sibs2[at], sibs2[at + 1]] : [sibs2[at - 1], sibs2[at]];
|
|
1104
|
+
return { parent: parent2, order: orderBetween(leftKey(left), rightKey(right)), where: `${placeWord(s, parent2)}, right ${input.above !== void 0 ? "above" : "below"} ${a.id}` };
|
|
1105
|
+
}
|
|
1106
|
+
const parent = parentInput(s, input.parent);
|
|
1107
|
+
const sibs = kidsOf(s, parent, new Set(self ? [self] : []));
|
|
1108
|
+
return { parent, order: orderBetween(leftKey(sibs[sibs.length - 1]), void 0), where: `the top of ${placeWord(s, parent)}` };
|
|
1109
|
+
}
|
|
1110
|
+
var near = (a, b) => a.every((v, i) => Math.abs(v - b[i]) < 1e-9);
|
|
1111
|
+
var baseOwn = (n3) => placeMatrix(n3.c.place, n3.c.place.pivot ?? defaultPivot(n3));
|
|
1112
|
+
var TURN_PROPS = ["move", "rotate", "scale"];
|
|
1113
|
+
var turnKeys = (n3) => TURN_PROPS.filter((p) => n3.tracks[p]);
|
|
1114
|
+
var keyIds = (n3, props = MO_PROPS) => props.flatMap((p) => (n3.tracks[p] ?? []).map((k) => k.id));
|
|
1115
|
+
var label = (s, id) => {
|
|
1116
|
+
const r = s.records[id];
|
|
1117
|
+
if (!r) return id;
|
|
1118
|
+
if (r.type === "mark") return `mark ${id} (${r.content.name})`;
|
|
1119
|
+
if (r.type === "key") {
|
|
1120
|
+
const k = r.content;
|
|
1121
|
+
return `keyframe ${id} (${k.part} ${k.prop} ${sec(k.t)})`;
|
|
1122
|
+
}
|
|
1123
|
+
if (r.type !== "part") return id;
|
|
1124
|
+
const c = partC(r);
|
|
1125
|
+
return `${id} (${c.name ? `${c.name}, ` : ""}${c.kind === "group" ? "group" : SHAPE_WORD[c.shape]})`;
|
|
1126
|
+
};
|
|
1127
|
+
function shapeFrom(input, ctx, parent, order) {
|
|
1128
|
+
const shape = input.shape;
|
|
1129
|
+
const geo = geoInput(shape, input);
|
|
1130
|
+
const paint = paintInput(shape, input);
|
|
1131
|
+
const name = textInput(input.name, "name", ctx, MAX_NAME);
|
|
1132
|
+
const place = placeInput(input, IDENTITY, () => center(boxOf(outline(shape, geo).flatMap((x) => x.pts))));
|
|
1133
|
+
return { kind: "shape", name, parent, order, shape, geo, fill: paint.fill, stroke: paint.stroke, strokeWidth: paint.strokeWidth, opacity: paint.opacity, place };
|
|
1134
|
+
}
|
|
1135
|
+
function shapeInput(v) {
|
|
1136
|
+
if (typeof v !== "string" || !SHAPES.includes(v)) {
|
|
1137
|
+
throw new Reject("INVALID_INPUT", `shape is one of ${SHAPES.join(", ")}. Text and arcs are not used`);
|
|
1138
|
+
}
|
|
1139
|
+
return v;
|
|
1140
|
+
}
|
|
1141
|
+
function sampleChanges(sample, canvasId, reason, ctx, id) {
|
|
1142
|
+
const changes = [
|
|
1143
|
+
{
|
|
1144
|
+
id: canvasId,
|
|
1145
|
+
type: "canvas",
|
|
1146
|
+
prefix: "cv",
|
|
1147
|
+
after: { width: sample.width, height: sample.height, duration: sample.duration, loop: sample.loop, source: { sample: sample.id, version: sample.version, title: sample.title }, reason }
|
|
1148
|
+
}
|
|
1149
|
+
];
|
|
1150
|
+
const tempOf = new Map(sample.parts.map((x, i) => [x.key, id(i + 1)]));
|
|
1151
|
+
const contentOf = /* @__PURE__ */ new Map();
|
|
1152
|
+
const lastOrder = /* @__PURE__ */ new Map();
|
|
1153
|
+
sample.parts.forEach((x, i) => {
|
|
1154
|
+
const parent = x.parent ? tempOf.get(x.parent) : null;
|
|
1155
|
+
const prev = lastOrder.get(parent);
|
|
1156
|
+
const order = orderBetween(prev === void 0 ? void 0 : boundAfterOrder(prev), void 0);
|
|
1157
|
+
lastOrder.set(parent, order);
|
|
1158
|
+
const place = { ...IDENTITY, pivot: x.pivot ?? null };
|
|
1159
|
+
const content = x.group ? { kind: "group", name: x.name ?? "", parent, order, opacity: 1, place } : { ...shapeFrom({ shape: x.shape, ...x.geo, fill: x.fill, stroke: x.stroke, strokeWidth: x.strokeWidth, name: x.name }, ctx, parent, order), place };
|
|
1160
|
+
contentOf.set(x.key, content);
|
|
1161
|
+
changes.push({ id: id(i + 1), type: "part", prefix: "pt", after: content });
|
|
1162
|
+
});
|
|
1163
|
+
let k = sample.parts.length + 1;
|
|
1164
|
+
for (const key of sample.keys) {
|
|
1165
|
+
const c = contentOf.get(key.part);
|
|
1166
|
+
const problem = propProblem(c, key.part, key.prop);
|
|
1167
|
+
if (problem) throw new Reject("INVALID_STATE", problem);
|
|
1168
|
+
changes.push({ id: id(k), type: "key", prefix: "kf", after: { part: tempOf.get(key.part), prop: key.prop, t: timeInput(key.t, "at", sample.duration), value: keyValueInput(key.prop, key.value), ease: key.ease ?? "linear" } });
|
|
1169
|
+
k += 1;
|
|
1170
|
+
}
|
|
1171
|
+
for (const mark of sample.marks) {
|
|
1172
|
+
changes.push({ id: id(k), type: "mark", prefix: "mk", after: { name: mark.name, from: mark.from, to: mark.to, note: "" } });
|
|
1173
|
+
k += 1;
|
|
1174
|
+
}
|
|
1175
|
+
return changes;
|
|
1176
|
+
}
|
|
1177
|
+
function keysInput(s, m, input, op) {
|
|
1178
|
+
const ways = [input.key !== void 0, input.keys !== void 0, input.part !== void 0].filter(Boolean).length;
|
|
1179
|
+
if (ways !== 1) throw new Reject("INVALID_INPUT", `Give ${op} one of: key (one keyframe id), keys (a list of ids), or part (that part's keyframes, narrowed with prop, from and to)`);
|
|
1180
|
+
if (input.part === void 0 && (input.prop !== void 0 || input.from !== void 0 || input.to !== void 0)) throw new Reject("INVALID_INPUT", "Give prop, from and to together with part");
|
|
1181
|
+
if (input.key !== void 0) return [m.keyById.get(need(s, input.key, "key", "keyframe").id)];
|
|
1182
|
+
if (input.keys !== void 0) {
|
|
1183
|
+
if (!Array.isArray(input.keys) || input.keys.length === 0) throw new Reject("INVALID_INPUT", "keys is a list of keyframe ids");
|
|
1184
|
+
return [...new Set(input.keys)].map((x) => m.keyById.get(need(s, x, "key", "keyframe").id));
|
|
1185
|
+
}
|
|
1186
|
+
const part = need(s, input.part, "part", "part");
|
|
1187
|
+
const node = m.byId.get(part.id);
|
|
1188
|
+
const duration = needCanvas(s).c.duration;
|
|
1189
|
+
const props = input.prop === void 0 ? MO_PROPS : (Array.isArray(input.prop) ? input.prop : [input.prop]).map(propInput);
|
|
1190
|
+
const from = input.from === void 0 ? 0 : timeInput(input.from, "from", duration);
|
|
1191
|
+
const to = input.to === void 0 ? duration : timeInput(input.to, "to", duration);
|
|
1192
|
+
if (from > to) throw new Reject("INVALID_INPUT", `from (${fmt(from)}) is later than to (${fmt(to)})`);
|
|
1193
|
+
const ids = props.flatMap((p) => (node.tracks[p] ?? []).filter((k) => k.t >= from && k.t <= to).map((k) => k.id));
|
|
1194
|
+
if (ids.length === 0) throw new Reject("TARGET_NOT_FOUND", `${part.id} has no keyframes in that range (${props.join(", ")}, ${sec(from)}~${sec(to)})`);
|
|
1195
|
+
return ids.map((x) => m.keyById.get(x));
|
|
1196
|
+
}
|
|
1197
|
+
function spanInput(input, duration, base) {
|
|
1198
|
+
if (input.at !== void 0 && (input.from !== void 0 || input.to !== void 0)) throw new Reject("INVALID_INPUT", "Do not give at (one moment) together with from and to (a time range)");
|
|
1199
|
+
if (input.at !== void 0) {
|
|
1200
|
+
const t = timeInput(input.at, "at", duration);
|
|
1201
|
+
return { from: t, to: t };
|
|
1202
|
+
}
|
|
1203
|
+
if (input.from === void 0 && input.to === void 0) return void 0;
|
|
1204
|
+
const from = input.from !== void 0 ? timeInput(input.from, "from", duration) : base?.from;
|
|
1205
|
+
const to = input.to !== void 0 ? timeInput(input.to, "to", duration) : base?.to;
|
|
1206
|
+
if (from === void 0 || to === void 0) throw new Reject("INVALID_INPUT", "For a time range give from and to together. For one moment give at");
|
|
1207
|
+
if (from > to) throw new Reject("INVALID_INPUT", `from (${fmt(from)}) is later than to (${fmt(to)})`);
|
|
1208
|
+
return { from, to };
|
|
1209
|
+
}
|
|
1210
|
+
var OPERATIONS = {
|
|
1211
|
+
"canvas.start": (s, input, id, ctx) => {
|
|
1212
|
+
onlyKeys(input, ["sample", "width", "height", "duration", "loop", "reason"], "canvas.start");
|
|
1213
|
+
const existing = canvasRecord(s);
|
|
1214
|
+
if (existing) {
|
|
1215
|
+
const c = existing.content;
|
|
1216
|
+
throw new Reject("INVALID_STATE", `There is only one picture. ${existing.id} already started ${c.source ? `from sample ${c.source.sample}` : "as an empty picture"} (${fmt(c.width)}\xD7${fmt(c.height)}, ${sec(c.duration)}). Change the length with canvas.set`);
|
|
1217
|
+
}
|
|
1218
|
+
const reason = textInput(input.reason, "reason", ctx, MAX_REASON);
|
|
1219
|
+
if (reason === "") throw new Reject("INVALID_INPUT", "Write a reason. It shows later why the picture started this way");
|
|
1220
|
+
const catalog = SAMPLES.map((x) => x.id).join(", ");
|
|
1221
|
+
if (input.sample !== void 0) {
|
|
1222
|
+
if (input.width !== void 0 || input.height !== void 0 || input.duration !== void 0 || input.loop !== void 0) throw new Reject("INVALID_INPUT", "When you start from a sample, leave out width, height, duration and loop. The sample has its own. Change the length with canvas.set after starting");
|
|
1223
|
+
const sample = typeof input.sample === "string" ? sampleById(input.sample) : void 0;
|
|
1224
|
+
if (!sample) throw new Reject("TARGET_NOT_FOUND", `There is no built-in sample ${JSON.stringify(input.sample)}. Samples: ${catalog}`);
|
|
1225
|
+
const n3 = sample.parts.length;
|
|
1226
|
+
return {
|
|
1227
|
+
changes: sampleChanges(sample, id(0), reason, ctx, id),
|
|
1228
|
+
summary: `Started the picture (${id(0)}): sample ${sample.id} (${sample.title}) version ${sample.version}, ${sample.width}\xD7${sample.height}, length ${sec(sample.duration)}, ${n3} parts (${id(1)} to ${id(n3)}), ${sample.keys.length} keyframes, ${sample.marks.length} marks. Reason: ${reason}`
|
|
1229
|
+
};
|
|
1230
|
+
}
|
|
1231
|
+
const side = (v, what) => {
|
|
1232
|
+
if (typeof v !== "number" || !Number.isInteger(v) || v < 1 || v > MAX_SIDE) throw new Reject("INVALID_INPUT", `For an empty picture give ${what} as a whole number from 1 to ${MAX_SIDE}. To start from a sample, give sample one of ${catalog}`);
|
|
1233
|
+
return v;
|
|
1234
|
+
};
|
|
1235
|
+
const width = side(input.width, "width");
|
|
1236
|
+
const height = side(input.height, "height");
|
|
1237
|
+
const duration = input.duration === void 0 ? DEFAULT_DURATION : durationInput(input.duration);
|
|
1238
|
+
if (input.loop !== void 0 && typeof input.loop !== "boolean") throw new Reject("INVALID_INPUT", "loop is true or false");
|
|
1239
|
+
const loop = input.loop !== false;
|
|
1240
|
+
return {
|
|
1241
|
+
changes: [{ id: id(0), type: "canvas", prefix: "cv", after: { width, height, duration, loop, source: null, reason } }],
|
|
1242
|
+
summary: `Started the picture (${id(0)}): empty picture ${width}\xD7${height}, length ${sec(duration)}, ${loop ? "loops" : "plays once"}. Reason: ${reason}`
|
|
1243
|
+
};
|
|
1244
|
+
},
|
|
1245
|
+
"canvas.set": (s, input) => {
|
|
1246
|
+
onlyKeys(input, ["duration", "loop"], "canvas.set");
|
|
1247
|
+
const { r, c } = needCanvas(s);
|
|
1248
|
+
if (input.duration === void 0 && input.loop === void 0) throw new Reject("INVALID_INPUT", "Give duration (length in seconds), loop (true or false), or both");
|
|
1249
|
+
const duration = input.duration === void 0 ? c.duration : durationInput(input.duration);
|
|
1250
|
+
if (input.loop !== void 0 && typeof input.loop !== "boolean") throw new Reject("INVALID_INPUT", "loop is true or false");
|
|
1251
|
+
const loop = input.loop === void 0 ? c.loop : input.loop;
|
|
1252
|
+
if (duration < c.duration) {
|
|
1253
|
+
const late = all(s, "key").filter((k) => k.content.t > duration).sort(byIdNumber);
|
|
1254
|
+
const lateMarks = all(s, "mark").filter((k) => k.content.to > duration).sort(byIdNumber);
|
|
1255
|
+
if (late.length || lateMarks.length) {
|
|
1256
|
+
const ids = [...late, ...lateMarks].map((x) => x.id);
|
|
1257
|
+
throw new Reject("INVALID_STATE", `Making the length ${sec(duration)} would leave ${ids.length} keyframes or marks after it outside (${ids.slice(0, 12).join(", ")}${ids.length > 12 ? " and more" : ""}). First pull them earlier with key.shift, or tidy them with key.remove, mark.set or mark.remove`);
|
|
1258
|
+
}
|
|
1259
|
+
}
|
|
1260
|
+
const words = [];
|
|
1261
|
+
if (duration !== c.duration) words.push(`length ${sec(c.duration)} to ${sec(duration)}`);
|
|
1262
|
+
if (loop !== c.loop) words.push(loop ? "loops" : "plays once");
|
|
1263
|
+
return { changes: [{ id: r.id, type: "canvas", after: { ...c, duration, loop } }], summary: `Changed picture ${r.id}: ${words.join(", ") || "nothing changed"}` };
|
|
1264
|
+
},
|
|
1265
|
+
"part.add": (s, input, id, ctx) => {
|
|
1266
|
+
needCanvas(s);
|
|
1267
|
+
const shape = shapeInput(input.shape);
|
|
1268
|
+
onlyKeys(input, ["shape", ...GEO_FIELDS[shape], ...PAINT_KEYS, "name", "parent", "above", "below", ...PLACE_KEYS], `part.add(${shape})`);
|
|
1269
|
+
const pos = positionInput(s, input, null);
|
|
1270
|
+
const content = shapeFrom(input, ctx, pos.parent, pos.order);
|
|
1271
|
+
return {
|
|
1272
|
+
changes: [{ id: id(0), type: "part", prefix: "pt", after: content }],
|
|
1273
|
+
summary: `Added a part (${id(0)}): ${SHAPE_WORD[shape]}${content.name ? ` "${content.name}"` : ""}, at ${pos.where}`
|
|
1274
|
+
};
|
|
1275
|
+
},
|
|
1276
|
+
"part.set": (s, input, _id, ctx) => {
|
|
1277
|
+
const r = need(s, input.part, "part", "part");
|
|
1278
|
+
const c = partC(r);
|
|
1279
|
+
const node = buildModel(s).byId.get(r.id);
|
|
1280
|
+
for (const p of ["opacity", "fill", "stroke"]) {
|
|
1281
|
+
if (input[p] !== void 0 && node.tracks[p]) throw new Reject("INVALID_STATE", `${r.id} has keyframes for ${propName(p)} (${keyIds(node, [p]).join(", ")}), so the value written on the part does not show in the picture. To change the value at a moment use key.set or key.update. To remove the motion use key.remove`);
|
|
1282
|
+
}
|
|
1283
|
+
const changed = [];
|
|
1284
|
+
let next;
|
|
1285
|
+
if (c.kind === "group") {
|
|
1286
|
+
onlyKeys(input, ["part", "name", "opacity"], "part.set (group)");
|
|
1287
|
+
const name = input.name !== void 0 ? textInput(input.name, "name", ctx, MAX_NAME) : c.name;
|
|
1288
|
+
const opacity = input.opacity !== void 0 ? numInput(input.opacity, "opacity", { min: 0, max: 1 }) : c.opacity;
|
|
1289
|
+
next = { ...c, name, opacity };
|
|
1290
|
+
} else {
|
|
1291
|
+
if (input.shape !== void 0 && input.shape !== c.shape) throw new Reject("INVALID_INPUT", `The shape kind cannot change (${c.shape}). Add a new kind with part.add and remove this one with part.remove`);
|
|
1292
|
+
onlyKeys(input, ["part", "shape", "name", ...GEO_FIELDS[c.shape], ...PAINT_KEYS], `part.set (${c.shape})`);
|
|
1293
|
+
const geo = geoInput(c.shape, input, c.geo);
|
|
1294
|
+
const paint = paintInput(c.shape, input, c);
|
|
1295
|
+
const name = input.name !== void 0 ? textInput(input.name, "name", ctx, MAX_NAME) : c.name;
|
|
1296
|
+
next = { ...c, name, geo, fill: paint.fill, stroke: paint.stroke, strokeWidth: paint.strokeWidth, opacity: paint.opacity };
|
|
1297
|
+
for (const k of GEO_FIELDS[c.shape]) if (JSON.stringify(geo[k]) !== JSON.stringify(c.geo[k])) changed.push(`${k} ${JSON.stringify(c.geo[k])} to ${JSON.stringify(geo[k])}`);
|
|
1298
|
+
if (next.fill !== c.fill) changed.push(`fill ${c.fill ?? "none"} to ${next.fill ?? "none"}`);
|
|
1299
|
+
if (next.stroke !== c.stroke) changed.push(`stroke color ${c.stroke ?? "none"} to ${next.stroke ?? "none"}`);
|
|
1300
|
+
if (next.strokeWidth !== c.strokeWidth) changed.push(`stroke width ${fmt(c.strokeWidth)} to ${fmt(next.strokeWidth)}`);
|
|
1301
|
+
}
|
|
1302
|
+
if (next.name !== c.name) changed.unshift(`name "${c.name}" to "${next.name}"`);
|
|
1303
|
+
if (next.opacity !== c.opacity) changed.push(`opacity ${fmt(c.opacity)} to ${fmt(next.opacity)}`);
|
|
1304
|
+
return { changes: [{ id: r.id, type: "part", after: next }], summary: `Changed ${label(s, r.id)}: ${changed.length ? changed.join(", ") : "nothing changed"}` };
|
|
1305
|
+
},
|
|
1306
|
+
"part.place": (s, input) => {
|
|
1307
|
+
const r = need(s, input.part, "part", "part");
|
|
1308
|
+
onlyKeys(input, ["part", ...PLACE_KEYS], "part.place");
|
|
1309
|
+
if (!PLACE_KEYS.some((k) => input[k] !== void 0)) throw new Reject("INVALID_INPUT", "Give at least one of move, rotate, scale and pivot");
|
|
1310
|
+
const c = partC(r);
|
|
1311
|
+
const node = buildModel(s).byId.get(r.id);
|
|
1312
|
+
for (const p of TURN_PROPS) {
|
|
1313
|
+
if (input[p] !== void 0 && node.tracks[p]) throw new Reject("INVALID_STATE", `${r.id} has keyframes for ${propName(p)} (${keyIds(node, [p]).join(", ")}), so the placement written on the part does not show in the picture. To change the value at a moment use key.set or key.update. To remove the motion use key.remove`);
|
|
1314
|
+
}
|
|
1315
|
+
const place = placeInput(input, c.place, () => defaultPivot(node), Boolean(node.tracks.rotate || node.tracks.scale));
|
|
1316
|
+
const next = { ...c, place };
|
|
1317
|
+
const words = [];
|
|
1318
|
+
if (input.move !== void 0) words.push(`move (${fmt(place.move[0])}, ${fmt(place.move[1])})`);
|
|
1319
|
+
if (input.rotate !== void 0) words.push(`rotate ${fmt(place.rotate)}\xB0`);
|
|
1320
|
+
if (input.scale !== void 0) words.push(place.scale[0] === place.scale[1] ? `scale ${fmt(place.scale[0])}` : `scale width ${fmt(place.scale[0])} height ${fmt(place.scale[1])}`);
|
|
1321
|
+
if (place.pivot && (input.pivot !== void 0 || c.place.pivot === null)) words.push(`pivot (${fmt(place.pivot[0])}, ${fmt(place.pivot[1])})`);
|
|
1322
|
+
return { changes: [{ id: r.id, type: "part", after: next }], summary: `Placed ${label(s, r.id)}: ${words.join(", ") || "nothing changed"}` };
|
|
1323
|
+
},
|
|
1324
|
+
"part.group": (s, input, id, ctx) => {
|
|
1325
|
+
onlyKeys(input, ["parts", "name"], "part.group");
|
|
1326
|
+
if (!Array.isArray(input.parts) || input.parts.length === 0) throw new Reject("INVALID_INPUT", "Give parts a list of the part ids to group");
|
|
1327
|
+
const ids = [...new Set(input.parts)];
|
|
1328
|
+
const members = ids.map((x) => need(s, x, "part", "part to group"));
|
|
1329
|
+
const parent = partC(members[0]).parent;
|
|
1330
|
+
const stray = members.filter((r) => partC(r).parent !== parent);
|
|
1331
|
+
if (stray.length) throw new Reject("INVALID_INPUT", `Parts to group must be in the same place (the same group or the top level). ${members[0].id} is in ${placeWord(s, parent)}, ${stray.map((r) => `${r.id} is in ${placeWord(s, partC(r).parent)}`).join(", ")}. First move them to the same place with part.order`);
|
|
1332
|
+
const name = textInput(input.name, "name", ctx, MAX_NAME);
|
|
1333
|
+
const set = new Set(members.map((r) => r.id));
|
|
1334
|
+
const sibs = kidsOf(s, parent);
|
|
1335
|
+
const top = Math.max(...members.map((r) => sibs.findIndex((x) => x.id === r.id)));
|
|
1336
|
+
const left = sibs.slice(0, top).filter((r) => !set.has(r.id)).pop();
|
|
1337
|
+
const right = sibs.slice(top + 1).find((r) => !set.has(r.id));
|
|
1338
|
+
const group = { kind: "group", name, parent, order: orderBetween(leftKey(left), rightKey(right)), opacity: 1, place: IDENTITY };
|
|
1339
|
+
const inside = sibs.filter((r) => set.has(r.id));
|
|
1340
|
+
return {
|
|
1341
|
+
changes: [{ id: id(0), type: "part", prefix: "pt", after: group }, ...inside.map((r) => ({ id: r.id, type: "part", after: { ...partC(r), parent: id(0) }, moved: true }))],
|
|
1342
|
+
summary: `Made a group (${id(0)})${name ? ` "${name}"` : ""}: ${inside.map((r) => r.id).join(", ")}, in ${placeWord(s, parent)}. The keyframes of the parts inside stay as they are`
|
|
1343
|
+
};
|
|
1344
|
+
},
|
|
1345
|
+
"part.ungroup": (s, input) => {
|
|
1346
|
+
onlyKeys(input, ["part"], "part.ungroup");
|
|
1347
|
+
const g = need(s, input.part, "part", "group");
|
|
1348
|
+
const gc = partC(g);
|
|
1349
|
+
if (gc.kind !== "group") throw new Reject("INVALID_INPUT", `${g.id} is not a group`);
|
|
1350
|
+
const m = buildModel(s);
|
|
1351
|
+
const gn = m.byId.get(g.id);
|
|
1352
|
+
if (gn.keyCount > 0) throw new Reject("INVALID_STATE", `Group ${g.id} has keyframes (${keyIds(gn).slice(0, 8).join(", ")}${gn.keyCount > 8 ? " and more" : ""}), so it cannot be ungrouped. Ungrouping would lose that motion. First remove the group's keyframes with key.remove`);
|
|
1353
|
+
const inside = kidsOf(s, g.id);
|
|
1354
|
+
const gm = baseOwn(gn);
|
|
1355
|
+
const turned = !isIdentity(gc.place);
|
|
1356
|
+
const sibs = kidsOf(s, gc.parent);
|
|
1357
|
+
const at = sibs.findIndex((r) => r.id === g.id);
|
|
1358
|
+
const right = sibs[at + 1];
|
|
1359
|
+
let left = sibs[at - 1];
|
|
1360
|
+
const changes = [];
|
|
1361
|
+
for (const r of inside) {
|
|
1362
|
+
const c = partC(r);
|
|
1363
|
+
const kn = m.byId.get(r.id);
|
|
1364
|
+
if (turned && turnKeys(kn).length) throw new Reject("INVALID_STATE", `Group ${g.id} has a placement (move, rotate, scale) and ${r.id} inside it has ${turnKeys(kn).join(", ")} keyframes. Ungrouping would mean rewriting all those keyframe values, and this plugin does not do that. Reset the group's placement with part.place (move [0, 0], rotate 0, scale 1) and then ungroup`);
|
|
1365
|
+
const place = turned ? placeFromMatrix(mul(gm, baseOwn(kn)), c.place.pivot ?? defaultPivot(kn), c.place.pivot) : c.place;
|
|
1366
|
+
if (!place) throw new Reject("INVALID_STATE", `Joining the placement of group ${g.id} with that of ${r.id} mixes rotation with unequal width and height scale, which skews the shape. First make one of them scale width and height equally, or set its rotation to 0`);
|
|
1367
|
+
const order = orderBetween(leftKey(left), rightKey(right));
|
|
1368
|
+
const next = { ...c, parent: gc.parent, order, place, opacity: kn.tracks.opacity ? c.opacity : r3(c.opacity * gc.opacity) };
|
|
1369
|
+
changes.push({ id: r.id, type: "part", after: next, moved: true });
|
|
1370
|
+
left = { ...r, content: next };
|
|
1371
|
+
}
|
|
1372
|
+
changes.push({ id: g.id, type: "part", after: null });
|
|
1373
|
+
return { changes, summary: `Ungrouped and removed ${label(s, g.id)}: the parts inside (${inside.map((r) => r.id).join(", ") || "none"}) still show in the same spot in ${placeWord(s, gc.parent)}` };
|
|
1374
|
+
},
|
|
1375
|
+
"part.order": (s, input) => {
|
|
1376
|
+
onlyKeys(input, ["part", "parent", "above", "below"], "part.order");
|
|
1377
|
+
const r = need(s, input.part, "part", "part");
|
|
1378
|
+
if (!("parent" in input) && input.above === void 0 && input.below === void 0) throw new Reject("INVALID_INPUT", "Give one of parent (the group to move into, null for the top level), above and below");
|
|
1379
|
+
const c = partC(r);
|
|
1380
|
+
const pos = positionInput(s, input, r.id);
|
|
1381
|
+
const m = buildModel(s);
|
|
1382
|
+
const node = m.byId.get(r.id);
|
|
1383
|
+
if (pos.parent !== null && subtreeOf(node).some((x) => x.id === pos.parent)) throw new Reject("INVALID_INPUT", `${pos.parent} is ${r.id} itself or a group inside it, so ${r.id} cannot move into it`);
|
|
1384
|
+
let place = c.place;
|
|
1385
|
+
let kept = "";
|
|
1386
|
+
if (pos.parent !== c.parent) {
|
|
1387
|
+
const frame = frameAt(m, 0);
|
|
1388
|
+
const from = c.parent ? frame.get(c.parent)?.world ?? UNIT : UNIT;
|
|
1389
|
+
const to = pos.parent ? frame.get(pos.parent)?.world ?? UNIT : UNIT;
|
|
1390
|
+
if (!near(from, to)) {
|
|
1391
|
+
if (turnKeys(node).length) throw new Reject("INVALID_STATE", `${r.id} has ${turnKeys(node).join(", ")} keyframes and the target group has a different placement. Moving it would mean rewriting all those keyframe values, and this plugin does not do that. Move it into a group with the same placement, or remove the keyframes first`);
|
|
1392
|
+
const inv = invert(to);
|
|
1393
|
+
place = inv ? placeFromMatrix(mul(inv, mul(from, baseOwn(node))), c.place.pivot ?? defaultPivot(node), c.place.pivot) : null;
|
|
1394
|
+
if (!place) throw new Reject("INVALID_STATE", `Moving ${r.id} into ${placeWord(s, pos.parent)} mixes the two placements (rotation and unequal width and height scale), which skews the shape. First make the group scale width and height equally, or set its rotation to 0`);
|
|
1395
|
+
kept = ". The placement was rewritten so it shows in the same spot at the first moment";
|
|
1396
|
+
}
|
|
1397
|
+
}
|
|
1398
|
+
const next = { ...c, parent: pos.parent, order: pos.order, place };
|
|
1399
|
+
return { changes: [{ id: r.id, type: "part", after: next, moved: true }], summary: `Moved ${label(s, r.id)}: to ${pos.where}${kept}` };
|
|
1400
|
+
},
|
|
1401
|
+
"part.remove": (s, input) => {
|
|
1402
|
+
onlyKeys(input, ["part"], "part.remove");
|
|
1403
|
+
const r = need(s, input.part, "part", "part");
|
|
1404
|
+
const m = buildModel(s);
|
|
1405
|
+
const parts = subtreeOf(m.byId.get(r.id));
|
|
1406
|
+
const keys = parts.flatMap((n3) => keyIds(n3));
|
|
1407
|
+
return {
|
|
1408
|
+
changes: [...keys.map((x) => ({ id: x, type: "key", after: null })), ...parts.map((n3) => ({ id: n3.id, type: "part", after: null }))],
|
|
1409
|
+
summary: `Removed a part (${label(s, r.id)})${parts.length > 1 ? `, with the ${parts.length - 1} parts inside it` : ""}${keys.length ? `, with its ${keys.length} keyframes` : ""}`
|
|
1410
|
+
};
|
|
1411
|
+
},
|
|
1412
|
+
"key.set": (s, input, id) => {
|
|
1413
|
+
onlyKeys(input, ["part", "at", ...MO_PROPS, "ease"], "key.set");
|
|
1414
|
+
const { c: cv } = needCanvas(s);
|
|
1415
|
+
const r = need(s, input.part, "part", "part");
|
|
1416
|
+
const c = partC(r);
|
|
1417
|
+
const t = timeInput(input.at, "at (time)", cv.duration);
|
|
1418
|
+
const props = MO_PROPS.filter((p) => input[p] !== void 0);
|
|
1419
|
+
if (props.length === 0) throw new Reject("INVALID_INPUT", `Give at least one value for that moment: ${PROP_HELP}. Example: { part: "${r.id}", at: ${fmt(t)}, rotate: 30 }`);
|
|
1420
|
+
const ease = input.ease === void 0 ? void 0 : easeInput(input.ease);
|
|
1421
|
+
const node = buildModel(s).byId.get(r.id);
|
|
1422
|
+
const changes = [];
|
|
1423
|
+
const words = [];
|
|
1424
|
+
let fresh = 0;
|
|
1425
|
+
for (const prop of props) {
|
|
1426
|
+
const problem = propProblem(c, r.id, prop);
|
|
1427
|
+
if (problem) throw new Reject("INVALID_INPUT", problem);
|
|
1428
|
+
const value = keyValueInput(prop, input[prop]);
|
|
1429
|
+
const old = (node.tracks[prop] ?? []).find((k) => k.t === t);
|
|
1430
|
+
if (old) {
|
|
1431
|
+
changes.push({ id: old.id, type: "key", after: { part: r.id, prop, t, value, ease: ease ?? old.ease } });
|
|
1432
|
+
words.push(`${prop} ${valueText(prop, value)} (changed ${old.id})`);
|
|
1433
|
+
} else {
|
|
1434
|
+
const kid = id(fresh);
|
|
1435
|
+
fresh += 1;
|
|
1436
|
+
changes.push({ id: kid, type: "key", prefix: "kf", after: { part: r.id, prop, t, value, ease: ease ?? "linear" } });
|
|
1437
|
+
words.push(`${prop} ${valueText(prop, value)} (${kid})`);
|
|
1438
|
+
}
|
|
1439
|
+
}
|
|
1440
|
+
let pivotNote = "";
|
|
1441
|
+
if (c.place.pivot === null && (props.includes("rotate") || props.includes("scale"))) {
|
|
1442
|
+
const pivot = defaultPivot(node);
|
|
1443
|
+
changes.push({ id: r.id, type: "part", after: { ...c, place: { ...c.place, pivot } } });
|
|
1444
|
+
pivotNote = `. Set the pivot to the center of the part's area (${fmt(pivot[0])}, ${fmt(pivot[1])})`;
|
|
1445
|
+
}
|
|
1446
|
+
return { changes, summary: `Keyframe of ${label(s, r.id)} at ${sec(t)}: ${words.join(", ")}${ease ? `, ${ease} until the next keyframe` : ""}${pivotNote}` };
|
|
1447
|
+
},
|
|
1448
|
+
"key.update": (s, input) => {
|
|
1449
|
+
onlyKeys(input, ["key", "at", "value", "ease"], "key.update");
|
|
1450
|
+
const { c: cv } = needCanvas(s);
|
|
1451
|
+
const r = need(s, input.key, "key", "keyframe");
|
|
1452
|
+
const k = r.content;
|
|
1453
|
+
if (input.at === void 0 && input.value === void 0 && input.ease === void 0) throw new Reject("INVALID_INPUT", "Give at least one of at (time), value and ease (how it changes)");
|
|
1454
|
+
const t = input.at === void 0 ? k.t : timeInput(input.at, "at (time)", cv.duration);
|
|
1455
|
+
const value = input.value === void 0 ? k.value : keyValueInput(k.prop, input.value);
|
|
1456
|
+
const ease = input.ease === void 0 ? k.ease : easeInput(input.ease);
|
|
1457
|
+
if (t !== k.t) {
|
|
1458
|
+
const clash = all(s, "key").find((x) => x.id !== r.id && x.content.part === k.part && x.content.prop === k.prop && x.content.t === t);
|
|
1459
|
+
if (clash) throw new Reject("INVALID_STATE", `${k.part} already has keyframe ${clash.id} for ${propName(k.prop)} at ${sec(t)}. Change it with key.update or remove it with key.remove, then move this one`);
|
|
1460
|
+
}
|
|
1461
|
+
const words = [];
|
|
1462
|
+
if (t !== k.t) words.push(`time ${sec(k.t)} to ${sec(t)}`);
|
|
1463
|
+
if (JSON.stringify(value) !== JSON.stringify(k.value)) words.push(`value ${valueText(k.prop, k.value)} to ${valueText(k.prop, value)}`);
|
|
1464
|
+
if (ease !== k.ease) words.push(`ease ${k.ease} to ${ease}`);
|
|
1465
|
+
return { changes: [{ id: r.id, type: "key", after: { ...k, t, value, ease } }], summary: `Changed keyframe ${r.id} (${k.part} ${k.prop}): ${words.join(", ") || "nothing changed"}` };
|
|
1466
|
+
},
|
|
1467
|
+
"key.shift": (s, input) => {
|
|
1468
|
+
onlyKeys(input, ["key", "keys", "part", "prop", "from", "to", "by"], "key.shift");
|
|
1469
|
+
const { c: cv } = needCanvas(s);
|
|
1470
|
+
const m = buildModel(s);
|
|
1471
|
+
const picked = keysInput(s, m, input, "key.shift");
|
|
1472
|
+
if (typeof input.by !== "number" || !Number.isFinite(input.by) || r3(input.by) === 0) throw new Reject("INVALID_INPUT", "by is the time to move, in seconds. Positive moves later, negative moves earlier. 0 is not accepted");
|
|
1473
|
+
const by = r3(input.by);
|
|
1474
|
+
const set = new Set(picked.map((k) => k.id));
|
|
1475
|
+
const out = picked.filter((k) => r3(k.c.t + by) < 0 || r3(k.c.t + by) > cv.duration);
|
|
1476
|
+
if (out.length) throw new Reject("INVALID_INPUT", `Moving by ${fmt(by)}s puts ${out.map((k) => `${k.id} (${sec(k.c.t)})`).slice(0, 8).join(", ")}${out.length > 8 ? " and more" : ""} outside 0 to the length ${sec(cv.duration)}. Make by smaller, or make the length longer with canvas.set`);
|
|
1477
|
+
for (const k of picked) {
|
|
1478
|
+
const t = r3(k.c.t + by);
|
|
1479
|
+
const clash = (m.byId.get(k.c.part)?.tracks[k.c.prop] ?? []).find((x) => x.t === t && !set.has(x.id));
|
|
1480
|
+
if (clash) throw new Reject("INVALID_STATE", `Moving ${k.id} to ${sec(t)} lands on keyframe ${clash.id} of the same part and property. Move that one too, or remove it first`);
|
|
1481
|
+
}
|
|
1482
|
+
return {
|
|
1483
|
+
changes: picked.map((k) => ({ id: k.id, type: "key", after: { ...k.c, t: r3(k.c.t + by) } })),
|
|
1484
|
+
summary: `Moved ${picked.length} keyframes ${fmt(Math.abs(by))}s ${by > 0 ? "later" : "earlier"}: ${picked.map((k) => k.id).slice(0, 12).join(", ")}${picked.length > 12 ? " and more" : ""}`
|
|
1485
|
+
};
|
|
1486
|
+
},
|
|
1487
|
+
"key.remove": (s, input) => {
|
|
1488
|
+
onlyKeys(input, ["key", "keys", "part", "prop", "from", "to"], "key.remove");
|
|
1489
|
+
const m = buildModel(s);
|
|
1490
|
+
const picked = keysInput(s, m, input, "key.remove");
|
|
1491
|
+
return {
|
|
1492
|
+
changes: picked.map((k) => ({ id: k.id, type: "key", after: null })),
|
|
1493
|
+
summary: `Removed ${picked.length} keyframes: ${picked.map((k) => `${k.id} (${k.c.part} ${k.c.prop} ${sec(k.c.t)})`).slice(0, 12).join(", ")}${picked.length > 12 ? " and more" : ""}`
|
|
1494
|
+
};
|
|
1495
|
+
},
|
|
1496
|
+
"mark.add": (s, input, id, ctx) => {
|
|
1497
|
+
onlyKeys(input, ["name", "at", "from", "to", "note"], "mark.add");
|
|
1498
|
+
const { c: cv } = needCanvas(s);
|
|
1499
|
+
const name = textInput(input.name, "name", ctx, MAX_NAME);
|
|
1500
|
+
if (name === "") throw new Reject("INVALID_INPUT", "A mark needs a name. The person and the AI point to this moment or time range by that name");
|
|
1501
|
+
const span = spanInput(input, cv.duration);
|
|
1502
|
+
if (!span) throw new Reject("INVALID_INPUT", "Give at (one moment, in seconds) or from and to (a time range, in seconds)");
|
|
1503
|
+
const content = { name, from: span.from, to: span.to, note: textInput(input.note, "note", ctx, MAX_NOTE) };
|
|
1504
|
+
return { changes: [{ id: id(0), type: "mark", prefix: "mk", after: content }], summary: `Added mark ${markText(id(0), content)}` };
|
|
1505
|
+
},
|
|
1506
|
+
"mark.set": (s, input, _id, ctx) => {
|
|
1507
|
+
onlyKeys(input, ["mark", "name", "at", "from", "to", "note"], "mark.set");
|
|
1508
|
+
const { c: cv } = needCanvas(s);
|
|
1509
|
+
const r = need(s, input.mark, "mark", "mark");
|
|
1510
|
+
const c = r.content;
|
|
1511
|
+
const name = input.name !== void 0 ? textInput(input.name, "name", ctx, MAX_NAME) : c.name;
|
|
1512
|
+
if (name === "") throw new Reject("INVALID_INPUT", "The name of a mark cannot be empty");
|
|
1513
|
+
const span = spanInput(input, cv.duration, c) ?? { from: c.from, to: c.to };
|
|
1514
|
+
const next = { name, from: span.from, to: span.to, note: input.note !== void 0 ? textInput(input.note, "note", ctx, MAX_NOTE) : c.note };
|
|
1515
|
+
return { changes: [{ id: r.id, type: "mark", after: next }], summary: `Changed mark ${markText(r.id, c)} to ${markText(r.id, next)}` };
|
|
1516
|
+
},
|
|
1517
|
+
"mark.remove": (s, input) => {
|
|
1518
|
+
onlyKeys(input, ["mark"], "mark.remove");
|
|
1519
|
+
const r = need(s, input.mark, "mark", "mark");
|
|
1520
|
+
return { changes: [{ id: r.id, type: "mark", after: null }], summary: `Removed mark ${markText(r.id, r.content)}` };
|
|
1521
|
+
}
|
|
1522
|
+
};
|
|
1523
|
+
function refsOf(r) {
|
|
1524
|
+
if (r.type === "part" && partC(r).parent) return [{ from: r.id, to: partC(r).parent, kind: "parent" }];
|
|
1525
|
+
if (r.type === "key") return [{ from: r.id, to: r.content.part, kind: "part" }];
|
|
1526
|
+
return [];
|
|
1527
|
+
}
|
|
1528
|
+
var finite2 = (v) => typeof v === "number" && Number.isFinite(v);
|
|
1529
|
+
function keyValueProblem(prop, v) {
|
|
1530
|
+
const pair = (x) => Array.isArray(x) && x.length === 2 && finite2(x[0]) && finite2(x[1]);
|
|
1531
|
+
if (prop === "move") return pair(v) ? null : "A move value is [x, y]";
|
|
1532
|
+
if (prop === "scale") return pair(v) && v[0] !== 0 && v[1] !== 0 ? null : "A scale value is [width, height] with no 0";
|
|
1533
|
+
if (prop === "rotate") return finite2(v) && Math.abs(v) <= MAX_TURN ? null : `A rotate value is degrees from -${MAX_TURN} to ${MAX_TURN}`;
|
|
1534
|
+
if (prop === "opacity") return finite2(v) && v >= 0 && v <= 1 ? null : "An opacity value is from 0 to 1";
|
|
1535
|
+
return typeof v === "string" && /^#[0-9a-f]{6}$/.test(v) ? null : "A color value is #rrggbb";
|
|
1536
|
+
}
|
|
1537
|
+
function validate(candidate, changed) {
|
|
1538
|
+
const out = [];
|
|
1539
|
+
const err = (record, code, message) => out.push({ level: "error", code, record, message });
|
|
1540
|
+
const changedSet = new Set(changed);
|
|
1541
|
+
const firstChanged = (list) => list.map((x) => x.id).find((x) => changedSet.has(x)) ?? list[0]?.id;
|
|
1542
|
+
const canvases = all(candidate, "canvas").sort(byIdNumber);
|
|
1543
|
+
if (canvases.length > 1) err(canvases.map((x) => x.id).find((x) => changedSet.has(x)) ?? canvases[1].id, "second_canvas", `There is only one picture: ${canvases.map((x) => x.id).join(", ")}`);
|
|
1544
|
+
const cv = canvases[0]?.content ?? null;
|
|
1545
|
+
const parts = all(candidate, "part");
|
|
1546
|
+
const keys = all(candidate, "key");
|
|
1547
|
+
const marks = all(candidate, "mark");
|
|
1548
|
+
if (!cv && parts.length + keys.length + marks.length > 0) err(firstChanged([...parts, ...keys, ...marks]), "no_canvas", "There are parts, keyframes or marks but no picture (canvas). Start with canvas.start first");
|
|
1549
|
+
if (parts.length > MAX_PARTS) err(firstChanged(parts), "too_many_parts", `There can be at most ${MAX_PARTS} parts (there are ${parts.length}). Join or remove small parts`);
|
|
1550
|
+
if (keys.length > MAX_KEYS) err(firstChanged(keys), "too_many_keys", `There can be at most ${MAX_KEYS} keyframes (there are ${keys.length})`);
|
|
1551
|
+
if (marks.length > MAX_MARKS) err(firstChanged(marks), "too_many_marks", `There can be at most ${MAX_MARKS} marks (there are ${marks.length})`);
|
|
1552
|
+
const duration = cv?.duration ?? 0;
|
|
1553
|
+
for (const id of changed) {
|
|
1554
|
+
const r = candidate.records[id];
|
|
1555
|
+
if (!r) continue;
|
|
1556
|
+
if (r.type === "canvas") {
|
|
1557
|
+
const c = r.content;
|
|
1558
|
+
if (!Number.isInteger(c.width) || !Number.isInteger(c.height) || c.width < 1 || c.height < 1 || c.width > MAX_SIDE || c.height > MAX_SIDE) err(id, "bad_canvas", `The picture size is a whole number from 1 to ${MAX_SIDE}`);
|
|
1559
|
+
if (!finite2(c.duration) || c.duration < MIN_DURATION || c.duration > MAX_DURATION || typeof c.loop !== "boolean") err(id, "bad_canvas", `The picture length is ${MIN_DURATION} to ${MAX_DURATION} seconds, and loop is true or false`);
|
|
1560
|
+
} else if (r.type === "part") {
|
|
1561
|
+
const problem = partProblem(r.content);
|
|
1562
|
+
if (problem) err(id, "bad_part", `${id}: ${problem}`);
|
|
1563
|
+
const p = partC(r).parent;
|
|
1564
|
+
if (p !== null && p !== void 0) {
|
|
1565
|
+
const to = candidate.records[p];
|
|
1566
|
+
if (!to) err(id, "dangling_ref", `Group ${p}, which holds ${id}, does not exist`);
|
|
1567
|
+
else if (to.type !== "part" || partC(to).kind !== "group") err(id, "wrong_ref_type", `The parent ${p} of ${id} must be a group`);
|
|
1568
|
+
else {
|
|
1569
|
+
const seen = /* @__PURE__ */ new Set([id]);
|
|
1570
|
+
for (let x = p; x !== null && candidate.records[x]?.type === "part"; x = partC(candidate.records[x]).parent) {
|
|
1571
|
+
if (seen.has(x)) {
|
|
1572
|
+
err(id, "group_cycle", `The groups of ${id} form a loop`);
|
|
1573
|
+
break;
|
|
1574
|
+
}
|
|
1575
|
+
seen.add(x);
|
|
1576
|
+
}
|
|
1577
|
+
}
|
|
1578
|
+
}
|
|
1579
|
+
} else if (r.type === "mark") {
|
|
1580
|
+
const c = r.content;
|
|
1581
|
+
if (!c || typeof c.name !== "string" || c.name === "" || typeof c.note !== "string") err(id, "bad_mark", `${id}: the mark's name is wrong`);
|
|
1582
|
+
else if (!finite2(c.from) || !finite2(c.to) || c.from < 0 || c.to < c.from) err(id, "bad_mark", `${id}: the mark's time is wrong`);
|
|
1583
|
+
}
|
|
1584
|
+
}
|
|
1585
|
+
const gone = new Set(changed.filter((x) => !candidate.records[x]));
|
|
1586
|
+
if (gone.size) {
|
|
1587
|
+
for (const r of parts) if (!changedSet.has(r.id) && partC(r).parent && gone.has(partC(r).parent)) err(r.id, "dangling_ref", `${r.id} is inside removed group ${partC(r).parent}`);
|
|
1588
|
+
}
|
|
1589
|
+
const seenAt = /* @__PURE__ */ new Map();
|
|
1590
|
+
for (const r of keys.sort(byIdNumber)) {
|
|
1591
|
+
const k = r.content;
|
|
1592
|
+
const mine = changedSet.has(r.id);
|
|
1593
|
+
if (!k || typeof k.part !== "string" || propIndex(k.prop) < 0 || !MO_EASES.includes(k.ease) || !finite2(k.t)) {
|
|
1594
|
+
err(r.id, "bad_key", `${r.id}: the keyframe's part, property, time or ease is wrong`);
|
|
1595
|
+
continue;
|
|
1596
|
+
}
|
|
1597
|
+
const part = candidate.records[k.part];
|
|
1598
|
+
if (!part || part.type !== "part") {
|
|
1599
|
+
err(mine ? r.id : k.part, "dangling_ref", `Part ${k.part}, which keyframe ${r.id} points to, does not exist. To remove a part use part.remove (it removes its keyframes too)`);
|
|
1600
|
+
continue;
|
|
1601
|
+
}
|
|
1602
|
+
const where = mine || !changedSet.has(k.part) ? r.id : k.part;
|
|
1603
|
+
const vp = keyValueProblem(k.prop, k.value);
|
|
1604
|
+
if (vp) err(r.id, "bad_key", `${r.id}: ${vp}`);
|
|
1605
|
+
const pp = propProblem(partC(part), k.part, k.prop);
|
|
1606
|
+
if (pp) err(where, "key_prop_mismatch", `Keyframe ${r.id}: ${pp}`);
|
|
1607
|
+
if (k.t < 0 || k.t > duration) err(mine ? r.id : canvases[0]?.id ?? r.id, "key_out_of_time", `The time ${sec(k.t)} of keyframe ${r.id} is outside 0 to the length ${sec(duration)}`);
|
|
1608
|
+
if ((k.prop === "rotate" || k.prop === "scale") && partC(part).place.pivot === null) err(where, "no_pivot", `${k.part} has a rotate or scale keyframe (${r.id}) but no pivot. Give a pivot with part.place`);
|
|
1609
|
+
const slot = `${k.part} ${k.prop} ${k.t}`;
|
|
1610
|
+
const other = seenAt.get(slot);
|
|
1611
|
+
if (other) err(mine ? r.id : other, "same_time_key", `${k.part} has two ${propName(k.prop)} keyframes at ${sec(k.t)} (${other}, ${r.id}). Remove one`);
|
|
1612
|
+
else seenAt.set(slot, r.id);
|
|
1613
|
+
}
|
|
1614
|
+
for (const r of marks) {
|
|
1615
|
+
const c = r.content;
|
|
1616
|
+
if (c && finite2(c.to) && c.to > duration) err(changedSet.has(r.id) ? r.id : canvases[0]?.id ?? r.id, "mark_out_of_time", `The time of mark ${r.id} is outside the length ${sec(duration)}`);
|
|
1617
|
+
}
|
|
1618
|
+
return out;
|
|
1619
|
+
}
|
|
1620
|
+
var noCanvasText = () => [
|
|
1621
|
+
"Your state has no picture yet. If this response lists changes by other participants that are not yet in the shared history, the person already started one on the screen. Then do not start a new one: merge those changes with a request that has no operations, and read again. If there are no such changes, start with canvas.start, either from a built-in sample or as an empty picture with a size and length. Ask the person first which one they want.",
|
|
1622
|
+
...SAMPLES.map((x) => `- ${x.id} (${x.title}) version ${x.version}: ${x.about}`),
|
|
1623
|
+
`- Empty picture: give width and height (whole numbers from 1 to ${MAX_SIDE}), duration (length in seconds, ${DEFAULT_DURATION} if left out) and loop (true if left out).`
|
|
1624
|
+
].join("\n");
|
|
1625
|
+
function whereText(keys, t) {
|
|
1626
|
+
const exact = keys.find((k) => k.t === t);
|
|
1627
|
+
if (exact) return `keyframe ${exact.id}`;
|
|
1628
|
+
if (t < keys[0].t) return `before the first keyframe ${keys[0].id} (${sec(keys[0].t)}), so it stays at that value`;
|
|
1629
|
+
const last = keys[keys.length - 1];
|
|
1630
|
+
if (t > last.t) return `after the last keyframe ${last.id} (${sec(last.t)}), so it stays at that value`;
|
|
1631
|
+
const i = keys.findIndex((k) => k.t > t);
|
|
1632
|
+
return `between ${keys[i - 1].id} (${sec(keys[i - 1].t)}) and ${keys[i].id} (${sec(keys[i].t)}), ${keys[i - 1].ease}`;
|
|
1633
|
+
}
|
|
1634
|
+
function overviewRead(m, star) {
|
|
1635
|
+
const c = m.canvas.c;
|
|
1636
|
+
const shapes = m.order.filter((n3) => n3.c.kind === "shape").length;
|
|
1637
|
+
const lines = [
|
|
1638
|
+
`Animation picture (${m.canvas.id})${star(m.canvas.id)}: ${fmt(c.width)}\xD7${fmt(c.height)}, length ${sec(c.duration)}, ${c.loop ? "starts over when it ends" : "plays once"}. ${m.order.length} parts (shapes ${shapes}, groups ${m.order.length - shapes}), ${m.keys.length} keyframes, ${m.marks.length} marks.`,
|
|
1639
|
+
`Start: ${c.source ? `sample ${c.source.sample} (${c.source.title}) version ${c.source.version}` : "empty picture"}. Reason: ${c.reason}`,
|
|
1640
|
+
"Positions are in picture units and y grows downward. Rotation is in degrees clockwise and time is in seconds. To read the picture at one moment, give scope a value like t=0.6."
|
|
1641
|
+
];
|
|
1642
|
+
if (m.marks.length) lines.push("", "Marks (names given to times)", ...m.marks.map((x) => `- ${markText(x.id, x.c).replace(`(${x.id})`, `(${x.id})${star(x.id)}`)}${x.c.note ? `. Note: ${x.c.note}` : ""}`));
|
|
1643
|
+
lines.push("", m.order.length ? "Parts (from the bottom layer up. The placement and colors here are the values of properties without keyframes)" : "There are no parts yet. Draw with part.add.");
|
|
1644
|
+
const walk = (n3) => {
|
|
1645
|
+
lines.push(`${" ".repeat(n3.depth)}- ${partLine(n3, star(n3.id))}`);
|
|
1646
|
+
n3.children.forEach(walk);
|
|
1647
|
+
};
|
|
1648
|
+
m.roots.forEach(walk);
|
|
1649
|
+
const moving = m.order.filter((n3) => n3.keyCount > 0);
|
|
1650
|
+
if (moving.length) {
|
|
1651
|
+
lines.push("", "Motion (parts with keyframes. Square brackets show how the value changes from that keyframe to the next. Before the first keyframe and after the last, the value stays the same)");
|
|
1652
|
+
for (const n3 of moving) {
|
|
1653
|
+
lines.push(`- ${partLabel(n3)}`);
|
|
1654
|
+
for (const p of MO_PROPS) if (n3.tracks[p]) lines.push(` - ${trackLine(p, n3.tracks[p], star)}`);
|
|
1655
|
+
}
|
|
1656
|
+
} else if (m.order.length) lines.push("", "There is no motion yet. Use key.set to write the value of a part at a moment.");
|
|
1657
|
+
return { text: lines.join("\n"), records: [], label: "Read everything" };
|
|
1658
|
+
}
|
|
1659
|
+
function partRead(m, n3, star) {
|
|
1660
|
+
const lines = [`Part ${partLine(n3, star(n3.id))}`];
|
|
1661
|
+
const ups = groupsOf(n3);
|
|
1662
|
+
if (ups.length) lines.push(`Groups around it (nearest first): ${ups.map(partLabel).join(", ")}`);
|
|
1663
|
+
const box = frameAt(m, 0).get(n3.id)?.box;
|
|
1664
|
+
lines.push(box ? `Area it covers at the first moment (0s): ${boxText(box)}` : "It covers no area (an empty group).");
|
|
1665
|
+
const records = [n3.id];
|
|
1666
|
+
if (n3.keyCount) {
|
|
1667
|
+
lines.push("Keyframes (square brackets show how the value changes until the next keyframe)");
|
|
1668
|
+
for (const p of MO_PROPS) {
|
|
1669
|
+
if (!n3.tracks[p]) continue;
|
|
1670
|
+
lines.push(`- ${trackLine(p, n3.tracks[p], star)}`);
|
|
1671
|
+
records.push(...n3.tracks[p].map((k) => k.id));
|
|
1672
|
+
}
|
|
1673
|
+
const still = MO_PROPS.filter((p) => !n3.tracks[p] && !propProblem(n3.c, n3.id, p));
|
|
1674
|
+
if (still.length) lines.push(`Properties without keyframes (${still.join(", ")}) always have the value written on the part.`);
|
|
1675
|
+
} else lines.push("It has no keyframes. It moves when a group around it moves.");
|
|
1676
|
+
if (n3.c.kind === "group") {
|
|
1677
|
+
lines.push(n3.children.length ? "Parts inside (from the bottom layer up)" : "There are no parts inside.");
|
|
1678
|
+
const walk = (k) => {
|
|
1679
|
+
lines.push(`${" ".repeat(k.depth - n3.depth - 1)}- ${partLine(k, star(k.id))}`);
|
|
1680
|
+
records.push(k.id);
|
|
1681
|
+
k.children.forEach(walk);
|
|
1682
|
+
};
|
|
1683
|
+
n3.children.forEach(walk);
|
|
1684
|
+
}
|
|
1685
|
+
return { text: lines.join("\n"), records, label: `Part ${n3.id}` };
|
|
1686
|
+
}
|
|
1687
|
+
function keyRead(m, k) {
|
|
1688
|
+
const n3 = m.byId.get(k.c.part);
|
|
1689
|
+
const track = n3?.tracks[k.c.prop] ?? [];
|
|
1690
|
+
const i = track.findIndex((x) => x.id === k.id);
|
|
1691
|
+
const lines = [`Keyframe (${k.id}): ${propName(k.c.prop)} of ${n3 ? partLabel(n3) : `(${k.c.part}) a missing part`}, ${sec(k.c.t)}, value ${valueText(k.c.prop, k.c.value)}, ${k.c.ease} (${EASE_WORD[k.c.ease]}) until the next keyframe.`];
|
|
1692
|
+
lines.push(i > 0 ? `Just before: ${keyText(k.c.prop, track[i - 1])}` : "There is no keyframe just before. Before this time the value stays the same.");
|
|
1693
|
+
lines.push(i >= 0 && i + 1 < track.length ? `Just after: ${keyText(k.c.prop, track[i + 1])}` : "There is no keyframe just after. After this time the value stays the same.");
|
|
1694
|
+
return { text: lines.join("\n"), records: [k.id, ...n3 ? [n3.id] : []], label: `Keyframe ${k.id}` };
|
|
1695
|
+
}
|
|
1696
|
+
function keysBetween(m, from, to) {
|
|
1697
|
+
const lines = [];
|
|
1698
|
+
const ids = [];
|
|
1699
|
+
for (const n3 of m.order) {
|
|
1700
|
+
for (const p of MO_PROPS) {
|
|
1701
|
+
const inside = (n3.tracks[p] ?? []).filter((k) => k.t >= from && k.t <= to);
|
|
1702
|
+
if (!inside.length) continue;
|
|
1703
|
+
lines.push(`- ${propName(p)} of ${partLabel(n3)}: ${inside.map((k) => keyText(p, k)).join(" ")}`);
|
|
1704
|
+
ids.push(...inside.map((k) => k.id));
|
|
1705
|
+
}
|
|
1706
|
+
}
|
|
1707
|
+
return { lines, ids };
|
|
1708
|
+
}
|
|
1709
|
+
function markRead(m, x, star) {
|
|
1710
|
+
const lines = [`Mark ${markText(x.id, x.c).replace(`(${x.id})`, `(${x.id})${star(x.id)}`)}${x.c.note ? `. Note: ${x.c.note}` : ""}`];
|
|
1711
|
+
const inside = keysBetween(m, x.c.from, x.c.to);
|
|
1712
|
+
lines.push(inside.lines.length ? x.c.from === x.c.to ? "Keyframes at this moment" : "Keyframes in this time range" : x.c.from === x.c.to ? "There are no keyframes at this moment." : "There are no keyframes in this time range.", ...inside.lines);
|
|
1713
|
+
lines.push(`To read the picture at that moment, give scope t=${fmt(x.c.from)}${x.c.from === x.c.to ? "" : ` or t=${fmt(x.c.to)}`}.`);
|
|
1714
|
+
return { text: lines.join("\n"), records: [x.id, ...inside.ids], label: `Mark ${x.id}` };
|
|
1715
|
+
}
|
|
1716
|
+
var MAP_COLS = 16;
|
|
1717
|
+
var MAX_MOMENT_LINES = 200;
|
|
1718
|
+
function momentMap(m, frame) {
|
|
1719
|
+
const { width, height } = m.canvas.c;
|
|
1720
|
+
const rowsN = Math.max(4, Math.min(24, Math.round(MAP_COLS * height / width)));
|
|
1721
|
+
const glyphOf = new Map(m.roots.map((r, i) => [r.id, i < GLYPHS.length ? GLYPHS[i] : GLYPH_OVERFLOW]));
|
|
1722
|
+
const shapes = m.order.filter((n3) => n3.c.kind === "shape" && frame.get(n3.id)?.box).reverse();
|
|
1723
|
+
const rows = glyphMap({ x: 0, y: 0, w: width, h: height }, MAP_COLS, rowsN, (cell) => {
|
|
1724
|
+
const lim = Math.min(cell.w, cell.h) * 0.35;
|
|
1725
|
+
const cellRect = { x: cell.x, y: cell.y, w: cell.w, h: cell.h };
|
|
1726
|
+
for (const n3 of shapes) {
|
|
1727
|
+
const pose = frame.get(n3.id);
|
|
1728
|
+
const b = pose.box;
|
|
1729
|
+
if (!overlaps(b, cellRect) && !(b.x >= cell.x && b.x <= cell.x + cell.w && b.y >= cell.y && b.y <= cell.y + cell.h)) continue;
|
|
1730
|
+
const px = b.w < cell.w ? b.x + b.w / 2 : Math.max(b.x, Math.min(b.x + b.w, cell.cx));
|
|
1731
|
+
const py = b.h < cell.h ? b.y + b.h / 2 : Math.max(b.y, Math.min(b.y + b.h, cell.cy));
|
|
1732
|
+
const thin = (b.w < cell.w || b.h < cell.h) && px >= cell.x && px < cell.x + cell.w && py >= cell.y && py < cell.y + cell.h;
|
|
1733
|
+
const canFill = pose.st.fill !== null && n3.c.shape !== "line";
|
|
1734
|
+
const filled = canFill && (insideFill(pose.outline, cell.cx, cell.cy) || thin && insideFill(pose.outline, px, py));
|
|
1735
|
+
if (filled || pose.st.stroke !== null && nearEdge(pose.outline, cell.cx, cell.cy, Math.max(pose.half, lim))) return rootOf(n3).id;
|
|
1736
|
+
}
|
|
1737
|
+
return null;
|
|
1738
|
+
}, (key) => glyphOf.get(key) ?? GLYPH_OVERFLOW);
|
|
1739
|
+
const legend = m.roots.filter((r) => rows.some((line) => line.includes(glyphOf.get(r.id)))).map((r) => `${glyphOf.get(r.id)} ${partLabel(r)}`);
|
|
1740
|
+
return { rows, legend };
|
|
1741
|
+
}
|
|
1742
|
+
function momentRead(m, t) {
|
|
1743
|
+
const c = m.canvas.c;
|
|
1744
|
+
const frame = frameAt(m, t);
|
|
1745
|
+
const lines = [`The picture at ${sec(t)} (length ${sec(c.duration)}, picture ${fmt(c.width)}\xD7${fmt(c.height)}).`];
|
|
1746
|
+
const marks = m.marks.filter((x) => x.c.from <= t && t <= x.c.to);
|
|
1747
|
+
if (marks.length) lines.push(`Marks over this moment: ${marks.map((x) => markText(x.id, x.c)).join(", ")}`);
|
|
1748
|
+
const moving = m.order.filter((n3) => n3.keyCount > 0);
|
|
1749
|
+
const records = [];
|
|
1750
|
+
if (moving.length) {
|
|
1751
|
+
lines.push("", "Values at this moment for parts with keyframes");
|
|
1752
|
+
for (const n3 of moving) {
|
|
1753
|
+
const st = frame.get(n3.id).st;
|
|
1754
|
+
const bits = MO_PROPS.filter((p) => n3.tracks[p]).map((p) => `${p} ${valueText(p, roundValue(p, st[p]))} (${whereText(n3.tracks[p], t)})`);
|
|
1755
|
+
lines.push(`- ${partLabel(n3)}: ${bits.join(", ")}`);
|
|
1756
|
+
records.push(n3.id);
|
|
1757
|
+
}
|
|
1758
|
+
} else lines.push("", "There are no keyframes, so the picture is the same at every moment.");
|
|
1759
|
+
lines.push("", "Area each part covers at this moment (from the bottom layer up)");
|
|
1760
|
+
for (const n3 of m.order.slice(0, MAX_MOMENT_LINES)) {
|
|
1761
|
+
const b = frame.get(n3.id)?.box;
|
|
1762
|
+
lines.push(`${" ".repeat(n3.depth)}- ${partLabel(n3)}: ${b ? boxText(b) : "no area"}`);
|
|
1763
|
+
}
|
|
1764
|
+
if (m.order.length > MAX_MOMENT_LINES) lines.push(`- and ${m.order.length - MAX_MOMENT_LINES} more. Read them by part id`);
|
|
1765
|
+
if (m.order.length) {
|
|
1766
|
+
const map = momentMap(m, frame);
|
|
1767
|
+
lines.push("", `Letter map (${MAP_COLS} columns by ${map.rows.length} rows. Each cell shows the letter of the top-level part drawn on top there. Empty cells are dots)`, ...map.rows, ...map.legend);
|
|
1768
|
+
}
|
|
1769
|
+
return { text: lines.join("\n"), records, label: `The picture at ${sec(t)}` };
|
|
1770
|
+
}
|
|
1771
|
+
var MOMENT = /^t=(-?\d+(?:\.\d+)?)$/;
|
|
1772
|
+
var MAX_TARGETS = 20;
|
|
1773
|
+
function readScope(m, scope, star) {
|
|
1774
|
+
const ids = scope?.targets?.length ? scope.targets : scope?.target !== void 0 ? [scope.target] : [];
|
|
1775
|
+
if (ids.length === 0) return overviewRead(m, star);
|
|
1776
|
+
if (ids.length > MAX_TARGETS) throw new Reject("INVALID_INPUT", `You can read at most ${MAX_TARGETS} scope targets at once (got ${ids.length})`);
|
|
1777
|
+
const reads = ids.map((id) => {
|
|
1778
|
+
const at = typeof id === "string" ? MOMENT.exec(id) : null;
|
|
1779
|
+
if (at) {
|
|
1780
|
+
const t = r3(Number(at[1]));
|
|
1781
|
+
const duration = m.canvas.c.duration;
|
|
1782
|
+
if (t < 0 || t > duration) throw new Reject("INVALID_INPUT", `t=${fmt(t)} is not between 0 and the length ${sec(duration)}`);
|
|
1783
|
+
return momentRead(m, t);
|
|
1784
|
+
}
|
|
1785
|
+
const part = m.byId.get(id);
|
|
1786
|
+
if (part) return partRead(m, part, star);
|
|
1787
|
+
const key = m.keyById.get(id);
|
|
1788
|
+
if (key) return keyRead(m, key);
|
|
1789
|
+
const mark = m.marks.find((x) => x.id === id);
|
|
1790
|
+
if (mark) return markRead(m, mark, star);
|
|
1791
|
+
if (m.canvas.id === id) return overviewRead(m, star);
|
|
1792
|
+
throw new Reject("TARGET_NOT_FOUND", `Cannot find scope ${JSON.stringify(id)}. A scope is a part id (pt3), a keyframe id (kf5), a mark id (mk2) or a moment (t=0.6)`);
|
|
1793
|
+
});
|
|
1794
|
+
return { text: reads.map((x) => x.text).join("\n\n"), records: [...new Set(reads.flatMap((x) => x.records))], label: reads.map((x) => x.label).join(", ") };
|
|
1795
|
+
}
|
|
1796
|
+
function localBox(c) {
|
|
1797
|
+
const b = boxOf(outline(c.shape, c.geo).flatMap((x) => x.pts));
|
|
1798
|
+
const half = c.stroke ? c.strokeWidth / 2 : 0;
|
|
1799
|
+
return b ? { x: b.x - half, y: b.y - half, w: b.w + 2 * half, h: b.h + 2 * half } : null;
|
|
1800
|
+
}
|
|
1801
|
+
function screenData(m) {
|
|
1802
|
+
const frame = m.canvas ? frameAt(m, 0) : /* @__PURE__ */ new Map();
|
|
1803
|
+
const box = (r) => r ? { x: r3(r.x), y: r3(r.y), w: r3(r.w), h: r3(r.h) } : null;
|
|
1804
|
+
return {
|
|
1805
|
+
package: { id: PACKAGE_ID, version: VERSION },
|
|
1806
|
+
selectionFormat: SELECTION_FORMAT,
|
|
1807
|
+
canvas: m.canvas ? { id: m.canvas.id, width: m.canvas.c.width, height: m.canvas.c.height, duration: m.canvas.c.duration, loop: m.canvas.c.loop, source: m.canvas.c.source, reason: m.canvas.c.reason } : null,
|
|
1808
|
+
svg: stillSvg(m, 0),
|
|
1809
|
+
parts: m.order.map((n3) => {
|
|
1810
|
+
const c = n3.c;
|
|
1811
|
+
const shape = c.kind === "shape" ? { shape: c.shape, fill: c.fill, stroke: c.stroke, strokeWidth: c.strokeWidth } : {};
|
|
1812
|
+
return {
|
|
1813
|
+
id: n3.id,
|
|
1814
|
+
kind: c.kind,
|
|
1815
|
+
name: c.name,
|
|
1816
|
+
parent: n3.parent?.id ?? null,
|
|
1817
|
+
z: n3.z,
|
|
1818
|
+
depth: n3.depth,
|
|
1819
|
+
...shape,
|
|
1820
|
+
opacity: c.opacity,
|
|
1821
|
+
place: c.place,
|
|
1822
|
+
/** 움직임이 지나는 곳을 그릴 때의 점(자기 배치를 적용하기 전의 위치). */
|
|
1823
|
+
anchor: anchorOf(n3).map(r3),
|
|
1824
|
+
/** 도형이 자기 배치를 적용하기 전에 차지하는 영역(선 굵기 포함). 화면이 어느 순간의 테두리를 계산할 때 쓴다. 묶음은 null 이다. */
|
|
1825
|
+
local: c.kind === "shape" ? box(localBox(c)) : null,
|
|
1826
|
+
/** 첫 순간에 차지하는 영역. */
|
|
1827
|
+
box: box(frame.get(n3.id)?.box),
|
|
1828
|
+
keys: n3.keyCount,
|
|
1829
|
+
...c.kind === "group" ? { children: n3.children.map((k) => k.id) } : {}
|
|
1830
|
+
};
|
|
1831
|
+
}),
|
|
1832
|
+
keys: m.keys.filter((k) => m.byId.has(k.c.part)).map((k) => ({ id: k.id, part: k.c.part, prop: k.c.prop, t: k.c.t, value: k.c.value, ease: k.c.ease })),
|
|
1833
|
+
marks: m.marks.map((x) => ({ id: x.id, name: x.c.name, from: x.c.from, to: x.c.to, note: x.c.note })),
|
|
1834
|
+
samples: SAMPLES.map((x) => ({ id: x.id, version: x.version, title: x.title, about: x.about, width: x.width, height: x.height, duration: x.duration, parts: x.parts.length, keys: x.keys.length })),
|
|
1835
|
+
props: MO_PROPS.map((p) => ({ id: p, word: PROP_WORD[p] })),
|
|
1836
|
+
eases: MO_EASES.map((e) => ({ id: e, word: EASE_WORD[e] })),
|
|
1837
|
+
limits: { parts: MAX_PARTS, keys: MAX_KEYS, marks: MAX_MARKS, side: MAX_SIDE, minDuration: MIN_DURATION, maxDuration: MAX_DURATION, d: MAX_D, points: MAX_POINTS }
|
|
1838
|
+
};
|
|
1839
|
+
}
|
|
1840
|
+
function project(s, scope, mine) {
|
|
1841
|
+
const starred = new Set(mine.flatMap((p) => p.changes.filter((c) => c.after !== null).map((c) => c.record)));
|
|
1842
|
+
const m = buildModel(s);
|
|
1843
|
+
const scoped = !!(scope && (scope.target !== void 0 || scope.targets && scope.targets.length));
|
|
1844
|
+
if (!m.canvas) return { text: noCanvasText(), scopeLabel: "No picture", json: screenData(m) };
|
|
1845
|
+
const read = readScope(m, scope, (id) => starred.has(id) ? " *" : "");
|
|
1846
|
+
return { text: read.text, scopeLabel: read.label, json: screenData(m), ...scoped ? { records: read.records } : {} };
|
|
1847
|
+
}
|
|
1848
|
+
var idList = (v) => v === void 0 || Array.isArray(v) && v.length <= 2e3 && v.every((x) => typeof x === "string" && x !== "");
|
|
1849
|
+
function selectionProblem(v) {
|
|
1850
|
+
const x = v;
|
|
1851
|
+
if (typeof x !== "object" || x === null || x.format !== SELECTION_FORMAT) return `The selection format is not ${SELECTION_FORMAT}`;
|
|
1852
|
+
if (typeof x.rev !== "string") return "The selection has no drawing basis (rev)";
|
|
1853
|
+
if (!finite2(x.at) || x.at < 0) return "The selection has no moment the person was looking at (at, in seconds)";
|
|
1854
|
+
if (x.targets !== void 0) {
|
|
1855
|
+
if (!Array.isArray(x.targets) || x.targets.length > 2e3) return "The selection targets is a list";
|
|
1856
|
+
for (const t of x.targets) {
|
|
1857
|
+
if (!t || typeof t.part !== "string" || t.part === "") return "Each item in targets needs part (a part id)";
|
|
1858
|
+
if (t.props !== void 0 && (!Array.isArray(t.props) || t.props.some((p) => propIndex(p) < 0))) return `props in targets is a list of ${MO_PROPS.join(", ")}`;
|
|
1859
|
+
}
|
|
1860
|
+
}
|
|
1861
|
+
if (x.time !== void 0 && x.time !== null && !(finite2(x.time.from) && finite2(x.time.to) && x.time.from >= 0 && x.time.from <= x.time.to)) return "The selection time is { from, to } and from is not later than to";
|
|
1862
|
+
if (!idList(x.keys) || !idList(x.seen)) return "The selection keys and seen are lists of keyframe ids";
|
|
1863
|
+
if (x.mark !== void 0 && x.mark !== null && typeof x.mark !== "string") return "The selection mark is a mark id";
|
|
1864
|
+
if (x.point !== void 0 && x.point !== null && !(finite2(x.point.x) && finite2(x.point.y))) return "The selection point is { x, y }";
|
|
1865
|
+
if (x.area !== void 0 && x.area !== null && !(finite2(x.area.x) && finite2(x.area.y) && finite2(x.area.w) && finite2(x.area.h) && x.area.w >= 0 && x.area.h >= 0)) return "The selection area is { x, y, w, h }";
|
|
1866
|
+
if (x.labels !== void 0 && (typeof x.labels !== "object" || x.labels === null || Array.isArray(x.labels))) return "The selection labels is an object from id to text";
|
|
1867
|
+
return null;
|
|
1868
|
+
}
|
|
1869
|
+
var MAX_SELECTION_TARGETS = 30;
|
|
1870
|
+
var MAX_SELECTION_KEYS = 60;
|
|
1871
|
+
function describeSelection(s, input) {
|
|
1872
|
+
const problem = selectionProblem(input?.selection);
|
|
1873
|
+
if (problem) return { error: "INVALID_INPUT", summary: problem };
|
|
1874
|
+
const sel = input.selection;
|
|
1875
|
+
const m = buildModel(s);
|
|
1876
|
+
if (!m.canvas) return { error: "INVALID_STATE", summary: "There is no picture, so the selection cannot be described" };
|
|
1877
|
+
const cv = m.canvas.c;
|
|
1878
|
+
const clamp = (t) => Math.min(r3(t), cv.duration);
|
|
1879
|
+
const at = clamp(sel.at);
|
|
1880
|
+
const gone = /* @__PURE__ */ new Set();
|
|
1881
|
+
const targets = [];
|
|
1882
|
+
const add = (id) => {
|
|
1883
|
+
if (!targets.includes(id)) targets.push(id);
|
|
1884
|
+
};
|
|
1885
|
+
const was = (id) => sel.labels?.[id] ?? "name unknown";
|
|
1886
|
+
const lines = [];
|
|
1887
|
+
const now = typeof input.current === "string" && input.current !== sel.rev ? ` Things changed after that (now ${input.current}). The values below are computed again from the current state.` : "";
|
|
1888
|
+
lines.push(`Basis when selected: ${sel.rev}.${now} Picture ${fmt(cv.width)}\xD7${fmt(cv.height)}, length ${sec(cv.duration)}, ${cv.loop ? "loops" : "plays once"}. Positions are in picture units and time is in seconds.`);
|
|
1889
|
+
lines.push(sel.playing ? `The person sent this while it was playing. The play position when sent was ${sec(at)} (they were not looking at one paused moment).` : `The paused moment the person was looking at: ${sec(at)}.`);
|
|
1890
|
+
const time = sel.time ? { from: clamp(sel.time.from), to: clamp(sel.time.to) } : null;
|
|
1891
|
+
const moment = time !== null && time.from === time.to;
|
|
1892
|
+
if (sel.mark) {
|
|
1893
|
+
const mk = m.marks.find((x) => x.id === sel.mark);
|
|
1894
|
+
if (mk) {
|
|
1895
|
+
add(mk.id);
|
|
1896
|
+
lines.push(`Selected mark: ${markText(mk.id, mk.c)}${mk.c.note ? `. Note: ${mk.c.note}` : ""}${time && (mk.c.from !== time.from || mk.c.to !== time.to) ? `. When selected its time was ${moment ? sec(time.from) : `${sec(time.from)}~${sec(time.to)}`} (the mark changed after that)` : ""}`);
|
|
1897
|
+
} else {
|
|
1898
|
+
gone.add(sel.mark);
|
|
1899
|
+
lines.push(`The selected mark (${sel.mark}) ${was(sel.mark)} has since been removed.`);
|
|
1900
|
+
}
|
|
1901
|
+
}
|
|
1902
|
+
if (time) {
|
|
1903
|
+
const over = m.marks.filter((x) => x.id !== sel.mark && x.c.from <= time.to && time.from <= x.c.to);
|
|
1904
|
+
lines.push(`Selected time: ${moment ? `the moment ${sec(time.from)}` : `from ${sec(time.from)} to ${sec(time.to)} (${sec(r3(time.to - time.from))} long)`}.${over.length ? ` Overlapping marks: ${over.map((x) => markText(x.id, x.c)).join(", ")}.` : ""}`);
|
|
1905
|
+
} else if (!sel.keys?.length) lines.push("No time was selected. The values below are for the moment the person was looking at.");
|
|
1906
|
+
const t0 = time ? time.from : at;
|
|
1907
|
+
const t1 = time ? time.to : at;
|
|
1908
|
+
const frame0 = frameAt(m, t0);
|
|
1909
|
+
const frame1 = t1 === t0 ? frame0 : frameAt(m, t1);
|
|
1910
|
+
const valueAt = (n3, p, t) => valueText(p, roundValue(p, moValueAt(p, n3.tracks[p], t) ?? stateAt(n3, t)[p]));
|
|
1911
|
+
const picked = sel.targets ?? [];
|
|
1912
|
+
if (picked.length) {
|
|
1913
|
+
lines.push(`${picked.length} selected targets${picked.length > 1 ? " (the person picked them one by one. All of them are targets)" : ""}:`);
|
|
1914
|
+
for (const target of picked.slice(0, MAX_SELECTION_TARGETS)) {
|
|
1915
|
+
const n3 = m.byId.get(target.part);
|
|
1916
|
+
if (!n3) {
|
|
1917
|
+
gone.add(target.part);
|
|
1918
|
+
lines.push(`- (${target.part}) ${was(target.part)} (since removed)`);
|
|
1919
|
+
continue;
|
|
1920
|
+
}
|
|
1921
|
+
add(n3.id);
|
|
1922
|
+
const ups = groupsOf(n3);
|
|
1923
|
+
const chosen = target.props?.length ? MO_PROPS.filter((p) => target.props.includes(p)) : null;
|
|
1924
|
+
lines.push(`- ${partLabel(n3)}${ups.length ? `, groups around it (nearest first) ${ups.map(partLabel).join(", ")}` : ""}${chosen ? `. Selected properties: ${chosen.map(propName).join(", ")}` : ". No property was selected (the whole part)"}`);
|
|
1925
|
+
const props = chosen ?? MO_PROPS.filter((p) => n3.tracks[p]);
|
|
1926
|
+
for (const p of props) {
|
|
1927
|
+
const keys = n3.tracks[p];
|
|
1928
|
+
if (!keys) {
|
|
1929
|
+
const bad = propProblem(n3.c, n3.id, p);
|
|
1930
|
+
lines.push(` - ${propName(p)}: ${bad ? `cannot be used on this part (${bad})` : `no keyframes. Always ${valueAt(n3, p, t0)} (the value written on the part). To make it move, write ${p} for this part with key.set`}`);
|
|
1931
|
+
continue;
|
|
1932
|
+
}
|
|
1933
|
+
if (!time) {
|
|
1934
|
+
lines.push(` - ${propName(p)}: ${valueAt(n3, p, at)} at the moment being looked at (${whereText(keys, at)}). All keyframes: ${keys.slice(0, MAX_SELECTION_KEYS).map((k) => keyText(p, k)).join(" ")}${keys.length > MAX_SELECTION_KEYS ? ` and ${keys.length - MAX_SELECTION_KEYS} more` : ""}`);
|
|
1935
|
+
} else if (moment) {
|
|
1936
|
+
lines.push(` - ${propName(p)}: ${valueAt(n3, p, t0)} at this moment (${whereText(keys, t0)})`);
|
|
1937
|
+
} else {
|
|
1938
|
+
const inside = keys.filter((k) => k.t >= t0 && k.t <= t1);
|
|
1939
|
+
const before = keys.filter((k) => k.t < t0).pop();
|
|
1940
|
+
const after = keys.find((k) => k.t > t1);
|
|
1941
|
+
lines.push(
|
|
1942
|
+
` - ${propName(p)}: ${valueAt(n3, p, t0)} at ${sec(t0)}, ${valueAt(n3, p, t1)} at ${sec(t1)}. Keyframes in the range: ${inside.length ? inside.slice(0, MAX_SELECTION_KEYS).map((k) => keyText(p, k)).join(" ") : "none"}. Just before: ${before ? keyText(p, before) : "none"}. Just after: ${after ? keyText(p, after) : "none"}`
|
|
1943
|
+
);
|
|
1944
|
+
inside.forEach((k) => add(k.id));
|
|
1945
|
+
}
|
|
1946
|
+
}
|
|
1947
|
+
if (!chosen && props.length === 0) lines.push(` - No keyframes. Values written on the part: ${placeText(n3.c.place) || "no placement"}, opacity ${fmt(n3.c.opacity)}${ups.some((g) => g.keyCount > 0) ? ". It moves with the keyframes of the groups around it" : ""}`);
|
|
1948
|
+
else if (!chosen) {
|
|
1949
|
+
const still = MO_PROPS.filter((p) => !n3.tracks[p] && !propProblem(n3.c, n3.id, p));
|
|
1950
|
+
if (still.length) lines.push(` - Properties without keyframes: ${still.map((p) => `${p} ${valueAt(n3, p, t0)}`).join(", ")}`);
|
|
1951
|
+
}
|
|
1952
|
+
const b0 = frame0.get(n3.id)?.box;
|
|
1953
|
+
const b1 = frame1.get(n3.id)?.box;
|
|
1954
|
+
if (b0) lines.push(` - Area it covers in the picture: ${boxText(b0)} at ${sec(t0)}${t1 !== t0 && b1 ? `, ${boxText(b1)} at ${sec(t1)}` : ""}`);
|
|
1955
|
+
}
|
|
1956
|
+
if (picked.length > MAX_SELECTION_TARGETS) lines.push(`- and ${picked.length - MAX_SELECTION_TARGETS} more: ${picked.slice(MAX_SELECTION_TARGETS).map((x) => x.part).join(", ")}`);
|
|
1957
|
+
const lost = (sel.seen ?? []).filter((id) => !m.keyById.has(id));
|
|
1958
|
+
if (lost.length) {
|
|
1959
|
+
lost.forEach((id) => gone.add(id));
|
|
1960
|
+
lines.push(`Keyframes that were inside when selected but have since been removed: ${lost.map((id) => `(${id}) ${was(id)}`).join(", ")}`);
|
|
1961
|
+
}
|
|
1962
|
+
}
|
|
1963
|
+
const keyIdsPicked = [...new Set(sel.keys ?? [])];
|
|
1964
|
+
if (keyIdsPicked.length) {
|
|
1965
|
+
lines.push(`${keyIdsPicked.length} selected keyframes (the person picked them one by one):`);
|
|
1966
|
+
for (const id of keyIdsPicked.slice(0, MAX_SELECTION_KEYS)) {
|
|
1967
|
+
const k = m.keyById.get(id);
|
|
1968
|
+
const n3 = k ? m.byId.get(k.c.part) : void 0;
|
|
1969
|
+
if (!k || !n3) {
|
|
1970
|
+
gone.add(id);
|
|
1971
|
+
lines.push(`- (${id}) ${was(id)} (since removed)`);
|
|
1972
|
+
continue;
|
|
1973
|
+
}
|
|
1974
|
+
add(id);
|
|
1975
|
+
const track = n3.tracks[k.c.prop] ?? [];
|
|
1976
|
+
const i = track.findIndex((x) => x.id === id);
|
|
1977
|
+
lines.push(`- (${id}) ${propName(k.c.prop)} of ${partLabel(n3)}, ${sec(k.c.t)}, value ${valueText(k.c.prop, k.c.value)}, ${k.c.ease} until the next keyframe. Just before: ${i > 0 ? keyText(k.c.prop, track[i - 1]) : "none"}. Just after: ${i + 1 < track.length ? keyText(k.c.prop, track[i + 1]) : "none"}`);
|
|
1978
|
+
}
|
|
1979
|
+
if (keyIdsPicked.length > MAX_SELECTION_KEYS) lines.push(`- and ${keyIdsPicked.length - MAX_SELECTION_KEYS} more: ${keyIdsPicked.slice(MAX_SELECTION_KEYS).join(", ")}`);
|
|
1980
|
+
}
|
|
1981
|
+
if (sel.point) {
|
|
1982
|
+
const { x, y } = sel.point;
|
|
1983
|
+
const stack = shapesAt(m, frame0, x, y);
|
|
1984
|
+
lines.push(`Pointed spot: (${fmt(x)}, ${fmt(y)}), in the picture at ${sec(t0)}.`);
|
|
1985
|
+
if (stack.length) {
|
|
1986
|
+
add(stack[0].id);
|
|
1987
|
+
lines.push(`Part drawn on top at that spot at that moment: ${partLabel(stack[0])}${stack[0].parent ? ` (inside group ${stack[0].parent.id})` : ""}${stack.length > 1 ? `. Below it: ${stack.slice(1, 6).map(partLabel).join(", ")}` : ""}`);
|
|
1988
|
+
} else lines.push('Nothing is drawn at that spot at that moment (empty space). If the person says "move it here" or "up to here", this spot is where it should go.');
|
|
1989
|
+
}
|
|
1990
|
+
if (sel.area) {
|
|
1991
|
+
const r = { x: sel.area.x, y: sel.area.y, w: sel.area.w, h: sel.area.h };
|
|
1992
|
+
const over = m.order.filter((n3) => n3.c.kind === "shape" && frame0.get(n3.id)?.box && overlaps(frame0.get(n3.id).box, r));
|
|
1993
|
+
lines.push(`Pointed area: ${boxText(r)}, ${fmt(r.w)}\xD7${fmt(r.h)}, in the picture at ${sec(t0)}.`);
|
|
1994
|
+
lines.push(over.length ? `${over.length} shapes overlap this area at that moment (from the bottom layer up):` : "No part overlaps this area at that moment (empty space).");
|
|
1995
|
+
for (const n3 of over.slice(0, MAX_SELECTION_TARGETS)) {
|
|
1996
|
+
add(n3.id);
|
|
1997
|
+
lines.push(`- ${partLabel(n3)}, ${boxText(frame0.get(n3.id).box)}${n3.parent ? ` (inside group ${n3.parent.id})` : ""}`);
|
|
1998
|
+
}
|
|
1999
|
+
if (over.length > 1) lines.push("Several parts overlap. Decide which one to change from what the person said, or ask the person.");
|
|
2000
|
+
}
|
|
2001
|
+
if (!picked.length && !keyIdsPicked.length && !sel.point && !sel.area) lines.push(time || sel.mark ? "No part or keyframe was selected. The person is talking about the whole picture at that time. Read that moment by giving scope t=seconds." : "Nothing was selected. The person is talking about the whole picture at the moment they were looking at.");
|
|
2002
|
+
if (keyIdsPicked.some((id) => m.keyById.has(id))) lines.push('Change the value, time or ease of these keyframes with key.update. If the person says "only this", send it to these keyframes only.');
|
|
2003
|
+
else if (picked.some((x) => m.byId.has(x.part))) {
|
|
2004
|
+
const some = picked.find((x) => m.byId.has(x.part));
|
|
2005
|
+
const prop = some.props?.[0] ?? "move";
|
|
2006
|
+
lines.push(time && !moment ? `To change only the motion in this range, change the keyframes inside it with key.update, or add values at moments in between with key.set (for example { part: "${some.part}", at: ${fmt(r3((t0 + t1) / 2))}, ${prop}: ... }). Do not touch keyframes outside the range.` : `To change the value at this moment, send key.set with at ${fmt(t0)} (it changes the keyframe at that time if there is one, or makes one). If properties were selected, change only those.`);
|
|
2007
|
+
}
|
|
2008
|
+
return { text: lines.join("\n"), targets, gone: [...gone] };
|
|
2009
|
+
}
|
|
2010
|
+
var TIME_HELP = "The time to see, in seconds. A number from 0 to the picture length. 0 if left out";
|
|
2011
|
+
var VIEWS = [
|
|
2012
|
+
{ summary: "See the picture at one moment. Without it you see the picture at 0s", input: { time: TIME_HELP }, example: { time: 0.6 } },
|
|
2013
|
+
{ summary: "See only one area of the picture at a moment, larger", input: { time: TIME_HELP, region: "The area in picture units, { x, y, w, h }. It must be inside the picture" }, example: { time: 0.6, region: { x: 60, y: 10, w: 40, h: 40 } } },
|
|
2014
|
+
{ summary: "See one part at a moment, larger. The view fits the area the part covers at that moment, with a small margin", input: { time: TIME_HELP, part: "A part or group id, such as pt3. Do not give it together with region" }, example: { time: 0.6, part: "pt3" } }
|
|
2015
|
+
];
|
|
2016
|
+
var VIEW_KEYS = ["time", "region", "part"];
|
|
2017
|
+
function viewInput(m, view) {
|
|
2018
|
+
const { width, height, duration } = m.canvas.c;
|
|
2019
|
+
const extra = Object.keys(view).filter((k) => !VIEW_KEYS.includes(k));
|
|
2020
|
+
if (extra.length) return { error: "INVALID_INPUT", summary: `The view does not know: ${extra.join(", ")}. It takes: ${VIEW_KEYS.join(", ")}` };
|
|
2021
|
+
let t = 0;
|
|
2022
|
+
if (view.time !== void 0) {
|
|
2023
|
+
const time = view.time;
|
|
2024
|
+
if (typeof time !== "number" || !Number.isFinite(time)) return { error: "INVALID_INPUT", summary: `time is a number of seconds (for example 0.6). Got: ${JSON.stringify(time) ?? "nothing"}` };
|
|
2025
|
+
if (time < 0 || time > duration) return { error: "INVALID_INPUT", summary: `time is seconds from 0 to ${fmt(duration)} (the length of this picture). Got: ${fmt(time)}` };
|
|
2026
|
+
t = r3(time);
|
|
2027
|
+
}
|
|
2028
|
+
if (view.region !== void 0 && view.part !== void 0) return { error: "INVALID_INPUT", summary: "Give region or part, not both. region is an area you choose. part fits the view to one part" };
|
|
2029
|
+
if (view.region !== void 0) {
|
|
2030
|
+
const r = view.region;
|
|
2031
|
+
const ok = r && typeof r === "object" && !Array.isArray(r) && Object.keys(r).sort().join() === "h,w,x,y" && [r.x, r.y, r.w, r.h].every((v) => typeof v === "number" && Number.isFinite(v));
|
|
2032
|
+
if (!ok) return { error: "INVALID_INPUT", summary: `region is { x, y, w, h } in picture units, four numbers. Got: ${JSON.stringify(view.region)}` };
|
|
2033
|
+
const b = { x: r3(r.x), y: r3(r.y), w: r3(r.w), h: r3(r.h) };
|
|
2034
|
+
if (!(b.w > 0 && b.h > 0)) return { error: "INVALID_INPUT", summary: `The w and h of region must be greater than 0. Got: w ${fmt(b.w)}, h ${fmt(b.h)}` };
|
|
2035
|
+
if (b.x < 0 || b.y < 0 || b.x + b.w > width || b.y + b.h > height) {
|
|
2036
|
+
return { error: "INVALID_INPUT", summary: `region must be inside the picture (x 0~${fmt(width)}, y 0~${fmt(height)}). Got: x ${fmt(b.x)}~${fmt(b.x + b.w)}, y ${fmt(b.y)}~${fmt(b.y + b.h)}. To see a part that moves outside the picture, give part instead` };
|
|
2037
|
+
}
|
|
2038
|
+
return { t, box: b, region: b };
|
|
2039
|
+
}
|
|
2040
|
+
if (view.part !== void 0) {
|
|
2041
|
+
const id = view.part;
|
|
2042
|
+
if (typeof id !== "string" || id === "") return { error: "INVALID_INPUT", summary: `part is a part or group id, such as pt3. Got: ${JSON.stringify(id)}` };
|
|
2043
|
+
const node = m.byId.get(id);
|
|
2044
|
+
if (!node) return { error: "TARGET_NOT_FOUND", summary: `There is no part ${id}. Parts: ${m.order.slice(0, 30).map((n3) => n3.id).join(", ")}${m.order.length > 30 ? " and more" : ""}` };
|
|
2045
|
+
const b = frameAt(m, t).get(id)?.box;
|
|
2046
|
+
if (!b || !(b.w > 0 || b.h > 0)) return { error: "INVALID_INPUT", summary: `${id} covers no area at ${sec(t)} (an empty group or a shape with no size), so there is nothing to see` };
|
|
2047
|
+
const pad = Math.max(1, Math.max(b.w, b.h) * 0.1);
|
|
2048
|
+
const box = { x: r3(b.x - pad), y: r3(b.y - pad), w: r3(b.w + pad * 2), h: r3(b.h + pad * 2) };
|
|
2049
|
+
return { t, box, part: id };
|
|
2050
|
+
}
|
|
2051
|
+
return { t };
|
|
2052
|
+
}
|
|
2053
|
+
function renderOutput(s, req) {
|
|
2054
|
+
const format = req?.format ?? "svg";
|
|
2055
|
+
if (format !== "svg" && format !== "html") return { error: "UNSUPPORTED_CAPABILITY", summary: `Animation output is svg (a moving SVG) or html (it does not make ${String(req?.format)})` };
|
|
2056
|
+
const m = buildModel(s);
|
|
2057
|
+
if (!m.canvas) return { error: "INVALID_STATE", summary: "There is no picture. Start one with canvas.start first" };
|
|
2058
|
+
const src = m.canvas.c.source;
|
|
2059
|
+
const meta = `${PACKAGE_ID} ${VERSION}; canvas ${m.canvas.id}${src ? `; sample ${src.sample}@${src.version}` : ""}${req?.revision ? `; revision ${req.revision}` : ""}`;
|
|
2060
|
+
const name = src ? src.sample : "animation";
|
|
2061
|
+
const basis = { canvas: m.canvas.id, source: src, revision: req?.revision ?? null, parts: m.order.length, keys: m.keys.length, duration: m.canvas.c.duration, loop: m.canvas.c.loop };
|
|
2062
|
+
const view = req?.view;
|
|
2063
|
+
if (view !== void 0 && view !== null) {
|
|
2064
|
+
if (typeof view !== "object" || Array.isArray(view)) return { error: "INVALID_INPUT", summary: `The view is an object like { "time": seconds } or { "time": seconds, "part": "pt3" }. Got: ${JSON.stringify(view)}` };
|
|
2065
|
+
if (Object.keys(view).length > 0) {
|
|
2066
|
+
if (format !== "svg") return { error: "UNSUPPORTED_CAPABILITY", summary: "A view comes only as svg (html is a file that moves by itself)" };
|
|
2067
|
+
const v = viewInput(m, view);
|
|
2068
|
+
if ("error" in v) return v;
|
|
2069
|
+
const tag = v.part ? `-${v.part}` : v.region ? `-x${fmt(v.region.x)}y${fmt(v.region.y)}w${fmt(v.region.w)}h${fmt(v.region.h)}` : "";
|
|
2070
|
+
const extra = { time: v.t, ...v.region ? { region: v.region } : {}, ...v.part ? { part: v.part, box: v.box } : {} };
|
|
2071
|
+
return { ok: true, mediaType: SVG_MEDIA, fileName: `${name}-${fmt(v.t)}s${tag}.svg`, content: stillSvg(m, v.t, v.box), basis: { ...basis, ...extra } };
|
|
2072
|
+
}
|
|
2073
|
+
}
|
|
2074
|
+
if (format === "html") return { ok: true, mediaType: HTML_MEDIA, fileName: `${name}.html`, content: movingHtml(m, src ? src.title : "Animation", meta), basis };
|
|
2075
|
+
return { ok: true, mediaType: SVG_MEDIA, fileName: `${name}.svg`, content: movingSvg(m, meta), basis };
|
|
2076
|
+
}
|
|
2077
|
+
var AI_GUIDE = [
|
|
2078
|
+
"This screen adds motion to small SVG pictures such as icons and 2D sprites. A picture is split into parts (pt plus a number). A part is a shape (rect, circle, ellipse, line, polygon, path) or a group that holds other parts. The part shapes and the input of the part operations (part.add, part.set, part.place, part.group, part.ungroup, part.order, part.remove) are the same as in the SVG Image screen (aic.svg).",
|
|
2079
|
+
`If there is no picture, ask the person whether to start from a built-in sample (${SAMPLES.map((x) => x.id).join(", ")}) or from an empty picture with a size and length they choose, then start with canvas.start.`,
|
|
2080
|
+
`Motion is written as keyframes (kf plus a number). One keyframe says "this property of this part has this value at this second". There are six properties: ${PROP_HELP}. Their values mean the same as in part.place and part.set (a value to set, not a value to add). Give key.set a part, at (seconds) and the values at that moment, and it makes or changes one keyframe for each property you give. Between two keyframes the value changes by the ease of the earlier keyframe: ${EASE_HELP}. A property without keyframes always has the value written on the part. Before the first keyframe and after the last, the value stays the same.`,
|
|
2081
|
+
"When several parts should move together, group them with part.group and give the keyframes to the group. To swing an arm, give the arm group rotate keyframes and put its pivot (pivot in part.place) at the shoulder. Each part has one pivot for rotation and size, and the pivot does not move. If a part has no pivot when it is first rotated or resized, the pivot is set to the center of its area.",
|
|
2082
|
+
"A property that has keyframes cannot be changed with part.place or part.set, because the value written on the part does not show in the picture. Change the value at a moment with key.set or key.update, move times with key.shift, and remove keyframes with key.remove. For a picture that starts over when it ends, give 0s and the last time the same value so the loop has no jump.",
|
|
2083
|
+
"Give names to times. Leave marks (mk plus a number) with mark.add at key points of the motion (the moment it lands, the highest point) and on time ranges (walking, running). Then you and the person can talk with those names instead of numbers.",
|
|
2084
|
+
`How to read: read_concept with no scope gives the part tree and one line of motion per part. Give scope a part id, keyframe id or mark id to read only that. Give scope a value like t=0.6 to get each part's values at that moment, the area it covers and a letter map. After changing motion, read two or three key moments with t= to check it came out as meant. To see it as a picture, give read_image a view with the time in seconds, like { "time": 0.6 }. Without a view you get the picture at 0s. To look closer, add a region in picture units, like { "time": 0.6, "region": { "x": 60, "y": 10, "w": 40, "h": 40 } }, or a part, like { "time": 0.6, "part": "pt3" }, which fits the view to that part at that moment. The size of read_image applies to that area, so a smaller area shows more detail.`,
|
|
2085
|
+
'Read what the person pointed at on the screen with read_selection. The person can select parts, properties, a moment, a time range, keyframes and spots on the picture, alone or together. The description always includes the moment the person was looking at, and its values are computed from the screen state the person sees now. Things the person selected but did not send also appear, as the current selection on the screen (not sent). So when the person says "this", "here" or "this moment" in the conversation, call read_selection first. If the person selected a time range or properties, change only those properties in that range and do not touch keyframes outside it.',
|
|
2086
|
+
"Use only #rgb, #rrggbb, #rrggbbaa and none for colors. Keyframe colors use only #rgb or #rrggbb. The d of a path uses only M L H V C S Q T Z and their lowercase forms. Write arcs (A) as C or Q. Positions are in picture units, y grows downward, and rotation is in degrees clockwise.",
|
|
2087
|
+
"Write names, notes and reasons in the language the person uses."
|
|
2088
|
+
].join(" ");
|
|
2089
|
+
var GEO_HELP = "rect: x, y, w, h, rx (0 if left out). circle: cx, cy, r. ellipse: cx, cy, rx, ry. line: x1, y1, x2, y2. polygon: points ([[x, y], ...]). path: d";
|
|
2090
|
+
var KEY_PICK = { key: "one keyframe id", keys: "a list of keyframe ids", part: "a part id. Picks that part's keyframes. Narrow them with prop, from and to", prop: `one property or a list (${MO_PROPS.join(", ")}). All properties if left out`, from: "from this time (seconds). 0 if left out", to: "up to this time (seconds). The end if left out" };
|
|
2091
|
+
var DESCRIPTION = {
|
|
2092
|
+
id: PACKAGE_ID,
|
|
2093
|
+
version: VERSION,
|
|
2094
|
+
title: "Animation",
|
|
2095
|
+
aiGuide: AI_GUIDE,
|
|
2096
|
+
views: VIEWS,
|
|
2097
|
+
markGuide: 'Names and notes are plain text. Do not use marks inside text. For a part made in the same request, give "as" a $name and point to it from the input of other operations (part, parts, parent, above, below). When key.set makes several keyframes, "as" names the first keyframe.',
|
|
2098
|
+
operations: [
|
|
2099
|
+
{
|
|
2100
|
+
op: "canvas.start",
|
|
2101
|
+
input: { sample: `built-in sample id (${SAMPLES.map((x) => x.id).join(", ")}). For an empty picture leave it out and give width and height`, width: `width of an empty picture (whole number, 1~${MAX_SIDE})`, height: `height of an empty picture (whole number, 1~${MAX_SIDE})`, duration: `length of the motion (seconds, ${MIN_DURATION}~${MAX_DURATION}). ${DEFAULT_DURATION} if left out`, loop: "whether it starts over when it ends. true if left out", reason: "why the picture starts this way. Required" },
|
|
2102
|
+
summary: "Starts from a built-in sample or an empty picture. There is one picture and its size does not change",
|
|
2103
|
+
example: [{ op: "canvas.start", input: { width: 64, height: 64, duration: 1.5, reason: "to make a waving hand icon" } }]
|
|
2104
|
+
},
|
|
2105
|
+
{ op: "canvas.set", input: { duration: "new length (seconds)", loop: "whether it loops (true or false)" }, summary: "Changes the length or whether it loops. Rejects a shorter length if keyframes or marks fall outside it", example: [{ op: "canvas.set", input: { duration: 3 } }] },
|
|
2106
|
+
{
|
|
2107
|
+
op: "part.add",
|
|
2108
|
+
input: {
|
|
2109
|
+
shape: "one of rect, circle, ellipse, line, polygon, path",
|
|
2110
|
+
"(shape values)": GEO_HELP,
|
|
2111
|
+
fill: "fill color (#rrggbb or none). Not for line",
|
|
2112
|
+
stroke: "stroke color (#rrggbb or none). Required for line",
|
|
2113
|
+
strokeWidth: "stroke width. 1 if left out",
|
|
2114
|
+
opacity: "opacity 0~1. 1 if left out",
|
|
2115
|
+
name: "name. Optional",
|
|
2116
|
+
parent: "id or $name of the group to put it in. The top level if left out",
|
|
2117
|
+
above: "draw it right above this part. Drawn on top if left out",
|
|
2118
|
+
below: "draw it right below this part",
|
|
2119
|
+
"move, rotate, scale, pivot": "starting placement, the same as part.place. Optional"
|
|
2120
|
+
},
|
|
2121
|
+
summary: "Adds one shape part",
|
|
2122
|
+
example: [
|
|
2123
|
+
{ op: "part.add", as: "$arm", input: { shape: "rect", x: 40, y: 20, w: 6, h: 20, rx: 3, fill: "#ffcc80", name: "arm", pivot: [43, 38] } },
|
|
2124
|
+
{ op: "key.set", input: { part: "$arm", at: 0, rotate: -20, ease: "ease-in-out" } },
|
|
2125
|
+
{ op: "key.set", input: { part: "$arm", at: 0.5, rotate: 20, ease: "ease-in-out" } },
|
|
2126
|
+
{ op: "key.set", input: { part: "$arm", at: 1, rotate: -20 } }
|
|
2127
|
+
]
|
|
2128
|
+
},
|
|
2129
|
+
{
|
|
2130
|
+
op: "part.set",
|
|
2131
|
+
input: { part: "part id", name: "new name", "(shape values)": `only the shape values to change. ${GEO_HELP}`, fill: "fill color", stroke: "stroke color", strokeWidth: "stroke width", opacity: "opacity. For a group only name and opacity change" },
|
|
2132
|
+
summary: "Changes only the fields you give. The shape kind cannot change. Change properties that have keyframes (opacity, fill, stroke) with key.set",
|
|
2133
|
+
example: [{ op: "part.set", input: { part: "pt5", name: "right arm" } }]
|
|
2134
|
+
},
|
|
2135
|
+
{
|
|
2136
|
+
op: "part.place",
|
|
2137
|
+
input: { part: "part or group id", move: "move [x, y]", rotate: "rotation (degrees, clockwise, -360~360)", scale: "size. One number or [width, height]. A negative number flips", pivot: "pivot [x, y]. If left out, the one already set, or the center of the part area the first time. null sets it to the center again" },
|
|
2138
|
+
summary: "Sets the placement written on the part (the value of properties without keyframes) and the pivot. Change properties that have keyframes (move, rotate, scale) with key.set",
|
|
2139
|
+
example: [{ op: "part.place", input: { part: "pt3", pivot: [32, 40] } }]
|
|
2140
|
+
},
|
|
2141
|
+
{
|
|
2142
|
+
op: "part.group",
|
|
2143
|
+
input: { parts: "list of part ids in the same place (the same group or the top level)", name: "name of the group" },
|
|
2144
|
+
summary: "Puts several parts in a new group. Group parts that move together and give the keyframes to the group",
|
|
2145
|
+
example: [
|
|
2146
|
+
{ op: "part.group", as: "$head", input: { parts: ["pt4", "pt5"], name: "head" } },
|
|
2147
|
+
{ op: "key.set", input: { part: "$head", at: 0.5, move: [0, -3] } }
|
|
2148
|
+
]
|
|
2149
|
+
},
|
|
2150
|
+
{ op: "part.ungroup", input: { part: "group id" }, summary: "Removes a group and leaves the parts inside where they show. If the group has keyframes, remove them first" },
|
|
2151
|
+
{
|
|
2152
|
+
op: "part.order",
|
|
2153
|
+
input: { part: "part id", parent: "id of the group to move it into. null for the top level", above: "right above this part", below: "right below this part" },
|
|
2154
|
+
summary: "Changes the drawing order or the group a part belongs to. Where it shows at the first moment stays the same",
|
|
2155
|
+
example: [{ op: "part.order", input: { part: "pt7", above: "pt2" } }]
|
|
2156
|
+
},
|
|
2157
|
+
{ op: "part.remove", input: { part: "part id" }, summary: "Removes a part. For a group it also removes the parts inside, and it removes their keyframes too" },
|
|
2158
|
+
{
|
|
2159
|
+
op: "key.set",
|
|
2160
|
+
input: {
|
|
2161
|
+
part: "part or group id",
|
|
2162
|
+
at: "time (seconds), from 0 to the picture length",
|
|
2163
|
+
move: "move at that moment [x, y]",
|
|
2164
|
+
rotate: `rotation at that moment (degrees, clockwise). -${MAX_TURN}~${MAX_TURN}, so you can write several turns like 720`,
|
|
2165
|
+
scale: "size at that moment. One number or [width, height]",
|
|
2166
|
+
opacity: "opacity at that moment, 0~1",
|
|
2167
|
+
fill: "fill color at that moment (#rrggbb). Only for shapes with a fill",
|
|
2168
|
+
stroke: "stroke color at that moment (#rrggbb). Only for shapes with a stroke color",
|
|
2169
|
+
ease: `how the value changes from this keyframe to the next. ${EASE_HELP}. If left out, linear for a new keyframe and unchanged for an existing one`
|
|
2170
|
+
},
|
|
2171
|
+
summary: "Sets the values at one moment. For each property you give, it changes the keyframe at that time if there is one, or makes one. Values are set, not added",
|
|
2172
|
+
example: [
|
|
2173
|
+
{ op: "key.set", input: { part: "pt3", at: 0, move: [0, 0], ease: "ease-out" } },
|
|
2174
|
+
{ op: "key.set", input: { part: "pt3", at: 0.6, move: [0, -40], rotate: 180, ease: "ease-in" } },
|
|
2175
|
+
{ op: "key.set", input: { part: "pt3", at: 1.2, move: [0, 0], rotate: 360 } }
|
|
2176
|
+
]
|
|
2177
|
+
},
|
|
2178
|
+
{ op: "key.update", input: { key: "keyframe id", at: "new time (seconds)", value: "new value, in the shape that fits the keyframe's property", ease: "new ease" }, summary: "Changes the time, value or ease of one keyframe. Only the fields you give change", example: [{ op: "key.update", input: { key: "kf2", at: 0.7, ease: "ease-in-out" } }] },
|
|
2179
|
+
{ op: "key.shift", input: { ...KEY_PICK, by: "time to move (seconds). Positive is later, negative is earlier" }, summary: "Moves several keyframes earlier or later by the same time", example: [{ op: "key.shift", input: { part: "pt3", from: 0.6, by: 0.2 } }] },
|
|
2180
|
+
{ op: "key.remove", input: KEY_PICK, summary: "Removes keyframes. When all keyframes of a property are removed, the property goes back to the value written on the part", example: [{ op: "key.remove", input: { part: "pt3", prop: "rotate" } }] },
|
|
2181
|
+
{ op: "mark.add", input: { name: "name", at: "time of one moment (seconds)", from: "start of a time range (seconds). Give it with to", to: "end of a time range (seconds)", note: "note. Optional" }, summary: "Gives a name to one moment or a time range", example: [{ op: "mark.add", input: { name: "top", at: 0.6 } }, { op: "mark.add", input: { name: "falling", from: 0.6, to: 1.2 } }] },
|
|
2182
|
+
{ op: "mark.set", input: { mark: "mark id", name: "new name", at: "new moment", from: "new start", to: "new end", note: "note" }, summary: "Changes a mark. Only the fields you give change" },
|
|
2183
|
+
{ op: "mark.remove", input: { mark: "mark id" }, summary: "Removes a mark" }
|
|
2184
|
+
]
|
|
2185
|
+
};
|
|
2186
|
+
var run = (f) => {
|
|
2187
|
+
try {
|
|
2188
|
+
return f();
|
|
2189
|
+
} catch (e) {
|
|
2190
|
+
if (e instanceof Reject) return { error: e.code, summary: e.message };
|
|
2191
|
+
throw e;
|
|
2192
|
+
}
|
|
2193
|
+
};
|
|
2194
|
+
var logic = {
|
|
2195
|
+
describe: () => DESCRIPTION,
|
|
2196
|
+
initialize: () => [],
|
|
2197
|
+
plan(state, op, ctx) {
|
|
2198
|
+
const handler = OPERATIONS[op.op];
|
|
2199
|
+
if (!handler) return { error: "INVALID_INPUT", summary: `Unknown operation ${op.op}. See the list of operations in describe_document` };
|
|
2200
|
+
const id = (k) => k === 0 && op.as ? op.as : `$~${ctx.stepIndex}.${k}`;
|
|
2201
|
+
return run(() => {
|
|
2202
|
+
const input = op.input ?? {};
|
|
2203
|
+
if (typeof input !== "object" || Array.isArray(input)) throw new Reject("INVALID_INPUT", "input is an object");
|
|
2204
|
+
const out = handler(state, input, id, ctx);
|
|
2205
|
+
return { ...out, summary: /[.?!]$/.test(out.summary) ? out.summary : `${out.summary}.` };
|
|
2206
|
+
});
|
|
2207
|
+
},
|
|
2208
|
+
validate,
|
|
2209
|
+
project: (state, scope, mine) => run(() => project(state, scope, mine)),
|
|
2210
|
+
extractRefs: refsOf,
|
|
2211
|
+
prepareGeneration: () => ({ error: "UNSUPPORTED_CAPABILITY", summary: "Animation does not make output for the AI to rewrite. The motion becomes an SVG or HTML file through renderOutput" }),
|
|
2212
|
+
interpretSelection: () => ({ candidates: [] }),
|
|
2213
|
+
resolveTarget(state, target) {
|
|
2214
|
+
const r = state.records[target.id];
|
|
2215
|
+
if (!r) return { found: false };
|
|
2216
|
+
if (r.type === "part") return { found: true, label: `part ${label(state, r.id)}` };
|
|
2217
|
+
if (r.type === "key" || r.type === "mark") return { found: true, label: label(state, r.id) };
|
|
2218
|
+
if (r.type === "canvas") return { found: true, label: `picture ${r.id}` };
|
|
2219
|
+
return { found: true, label: r.id };
|
|
2220
|
+
},
|
|
2221
|
+
describeSelection,
|
|
2222
|
+
renderOutput
|
|
2223
|
+
};
|
|
2224
|
+
globalThis.__aicPlugin = logic;
|
|
2225
|
+
})();
|