@open-take/compositor 0.4.4 → 0.5.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +476 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2864 -22
- package/dist/index.js.map +1 -1
- package/package.json +2 -2
- package/src/index.ts +35 -0
- package/src/launch-evaluate.ts +147 -0
- package/src/launch-render.ts +929 -0
- package/src/launch-starter.ts +57 -0
- package/src/launch-story.ts +258 -0
- package/src/launch-template.ts +125 -0
- package/src/launch-types.ts +127 -0
- package/src/launch-validate.ts +374 -0
- package/src/motion-evaluate.ts +145 -0
- package/src/motion-quality.ts +632 -0
- package/src/motion-recipes.ts +1021 -0
- package/src/motion-types.ts +163 -0
- package/src/motion-validate.ts +400 -0
- package/src/render-lock.ts +8 -0
- package/src/render.ts +2 -8
- package/src/scene/launch-project.ts +11 -0
- package/src/scene/launch-scene.tsx +426 -0
- package/src/scene/motion-layers.tsx +263 -0
- package/src/types.ts +17 -0
|
@@ -0,0 +1,632 @@
|
|
|
1
|
+
import type { LaunchComposition, LaunchIssue, LaunchTheme } from "./launch-types";
|
|
2
|
+
import { evaluateMotionTrack, motionGraphemes, parseMotionColor } from "./motion-evaluate";
|
|
3
|
+
import type { MotionLayer, MotionScene, MotionTextLayer } from "./motion-types";
|
|
4
|
+
|
|
5
|
+
/** Heuristic reading time: roughly three words or six CJK/fullwidth glyphs per second.
|
|
6
|
+
* The character-rate floor also accounts for unusually long unbroken words. */
|
|
7
|
+
export function estimateMotionReadingSeconds(copy: string, minimum = 0.8): number {
|
|
8
|
+
const glyphs = motionGraphemes(copy);
|
|
9
|
+
const fullwidth =
|
|
10
|
+
/[\p{Script=Han}\p{Script=Hiragana}\p{Script=Katakana}\p{Script=Hangul}\p{Extended_Pictographic}\uFF01-\uFF60]/u;
|
|
11
|
+
let wide = 0;
|
|
12
|
+
const remaining = glyphs
|
|
13
|
+
.map((glyph) => {
|
|
14
|
+
if (!fullwidth.test(glyph)) return glyph;
|
|
15
|
+
wide++;
|
|
16
|
+
return " ";
|
|
17
|
+
})
|
|
18
|
+
.join("");
|
|
19
|
+
const words = remaining.match(/[\p{L}\p{N}]+(?:['’][\p{L}\p{N}]+)*/gu)?.length ?? 0;
|
|
20
|
+
const narrow = motionGraphemes(remaining.replace(/\s/g, "")).length;
|
|
21
|
+
return Math.max(minimum, wide / 6 + Math.max(words / 3, narrow / 18));
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
type Matrix = [number, number, number, number, number, number];
|
|
25
|
+
type Box = { left: number; top: number; right: number; bottom: number };
|
|
26
|
+
type Item = {
|
|
27
|
+
layer: MotionLayer;
|
|
28
|
+
path: string;
|
|
29
|
+
box?: Box;
|
|
30
|
+
matrix: Matrix;
|
|
31
|
+
opacity: number;
|
|
32
|
+
parents: MotionLayer[];
|
|
33
|
+
};
|
|
34
|
+
const identity: Matrix = [1, 0, 0, 1, 0, 0];
|
|
35
|
+
function transform(parent: Matrix, layer: MotionLayer): Matrix {
|
|
36
|
+
const angle = ((layer.rotation ?? 0) * Math.PI) / 180,
|
|
37
|
+
c = Math.cos(angle),
|
|
38
|
+
s = Math.sin(angle),
|
|
39
|
+
sx = layer.scaleX ?? 1,
|
|
40
|
+
sy = layer.scaleY ?? 1;
|
|
41
|
+
const a = c * sx,
|
|
42
|
+
b = s * sx,
|
|
43
|
+
d = -s * sy,
|
|
44
|
+
e = c * sy,
|
|
45
|
+
x = layer.x ?? 0,
|
|
46
|
+
y = layer.y ?? 0;
|
|
47
|
+
return [
|
|
48
|
+
parent[0] * a + parent[2] * b,
|
|
49
|
+
parent[1] * a + parent[3] * b,
|
|
50
|
+
parent[0] * d + parent[2] * e,
|
|
51
|
+
parent[1] * d + parent[3] * e,
|
|
52
|
+
parent[0] * x + parent[2] * y + parent[4],
|
|
53
|
+
parent[1] * x + parent[3] * y + parent[5],
|
|
54
|
+
];
|
|
55
|
+
}
|
|
56
|
+
function boxOf(layer: MotionLayer, m: Matrix): Box | undefined {
|
|
57
|
+
const points =
|
|
58
|
+
layer.type === "line"
|
|
59
|
+
? layer.points
|
|
60
|
+
: "width" in layer &&
|
|
61
|
+
"height" in layer &&
|
|
62
|
+
layer.width !== undefined &&
|
|
63
|
+
layer.height !== undefined
|
|
64
|
+
? [
|
|
65
|
+
[-layer.width / 2, -layer.height / 2],
|
|
66
|
+
[layer.width / 2, -layer.height / 2],
|
|
67
|
+
[layer.width / 2, layer.height / 2],
|
|
68
|
+
[-layer.width / 2, layer.height / 2],
|
|
69
|
+
]
|
|
70
|
+
: undefined;
|
|
71
|
+
if (!points) return undefined;
|
|
72
|
+
const x = points.map((p) => m[0] * p[0]! + m[2] * p[1]! + m[4]),
|
|
73
|
+
y = points.map((p) => m[1] * p[0]! + m[3] * p[1]! + m[5]);
|
|
74
|
+
return {
|
|
75
|
+
left: Math.min(...x),
|
|
76
|
+
right: Math.max(...x),
|
|
77
|
+
top: Math.min(...y),
|
|
78
|
+
bottom: Math.max(...y),
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
const intersects = (a: Box, b: Box) =>
|
|
82
|
+
a.left < b.right && a.right > b.left && a.top < b.bottom && a.bottom > b.top;
|
|
83
|
+
const contains = (a: Box, b: Box) =>
|
|
84
|
+
a.left <= b.left && a.right >= b.right && a.top <= b.top && a.bottom >= b.bottom;
|
|
85
|
+
function intersect(a: Box, b: Box): Box {
|
|
86
|
+
return {
|
|
87
|
+
left: Math.max(a.left, b.left),
|
|
88
|
+
right: Math.min(a.right, b.right),
|
|
89
|
+
top: Math.max(a.top, b.top),
|
|
90
|
+
bottom: Math.min(a.bottom, b.bottom),
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
function flatten(scene: MotionScene, prefix: string): Item[] {
|
|
94
|
+
const out: Item[] = [];
|
|
95
|
+
const visit = (
|
|
96
|
+
layers: MotionLayer[],
|
|
97
|
+
path: string,
|
|
98
|
+
parent: Matrix,
|
|
99
|
+
opacity: number,
|
|
100
|
+
parents: MotionLayer[],
|
|
101
|
+
clip?: Box,
|
|
102
|
+
) => {
|
|
103
|
+
layers.forEach((layer, i) => {
|
|
104
|
+
const matrix = transform(parent, layer),
|
|
105
|
+
nextOpacity = opacity * (layer.opacity ?? 1),
|
|
106
|
+
rawBox = boxOf(layer, matrix),
|
|
107
|
+
box = rawBox && clip ? intersect(rawBox, clip) : rawBox;
|
|
108
|
+
const item = { layer, path: `${path}[${i}]`, matrix, opacity: nextOpacity, parents, box };
|
|
109
|
+
out.push(item);
|
|
110
|
+
if (layer.type === "group")
|
|
111
|
+
visit(
|
|
112
|
+
layer.children,
|
|
113
|
+
`${item.path}.children`,
|
|
114
|
+
matrix,
|
|
115
|
+
nextOpacity,
|
|
116
|
+
[...parents, layer],
|
|
117
|
+
layer.clip && box ? box : clip,
|
|
118
|
+
);
|
|
119
|
+
});
|
|
120
|
+
};
|
|
121
|
+
visit(scene.layers, `${prefix}.layers`, identity, 1, []);
|
|
122
|
+
return out;
|
|
123
|
+
}
|
|
124
|
+
function trackValue(layer: MotionLayer, property: string, time: number, fallback: number): number {
|
|
125
|
+
const track = layer.animations?.find((x) => x.property === property);
|
|
126
|
+
return track
|
|
127
|
+
? (evaluateMotionTrack(track, time) as number)
|
|
128
|
+
: ((layer as unknown as Record<string, number>)[property] ?? fallback);
|
|
129
|
+
}
|
|
130
|
+
type Window = { start: number; end: number };
|
|
131
|
+
function commonWindows(left: Window[], right: Window[]): Window[] {
|
|
132
|
+
const result: Window[] = [];
|
|
133
|
+
let i = 0,
|
|
134
|
+
j = 0;
|
|
135
|
+
while (i < left.length && j < right.length) {
|
|
136
|
+
const a = left[i]!,
|
|
137
|
+
b = right[j]!,
|
|
138
|
+
start = Math.max(a.start, b.start),
|
|
139
|
+
end = Math.min(a.end, b.end);
|
|
140
|
+
if (end > start) result.push({ start, end });
|
|
141
|
+
if (a.end <= b.end) i++;
|
|
142
|
+
else j++;
|
|
143
|
+
}
|
|
144
|
+
return result;
|
|
145
|
+
}
|
|
146
|
+
function usefulHold(scene: MotionScene, item: Item): number {
|
|
147
|
+
if (item.layer.type !== "text") return 0;
|
|
148
|
+
const chain = [...item.parents, item.layer];
|
|
149
|
+
if (scene.motion === "off")
|
|
150
|
+
return (item.layer.reveal ?? 1) >= 0.999 &&
|
|
151
|
+
item.opacity >= 0.5 &&
|
|
152
|
+
Math.hypot(item.matrix[0], item.matrix[1]) > 0.01 &&
|
|
153
|
+
Math.hypot(item.matrix[2], item.matrix[3]) > 0.01
|
|
154
|
+
? scene.durationS
|
|
155
|
+
: 0;
|
|
156
|
+
const transition =
|
|
157
|
+
scene.transition?.type && scene.transition.type !== "cut"
|
|
158
|
+
? Math.min(scene.transition.durationS ?? 0.45, scene.durationS / 2)
|
|
159
|
+
: 0;
|
|
160
|
+
const end = scene.durationS - transition;
|
|
161
|
+
let available: Window[] = [{ start: transition, end }];
|
|
162
|
+
for (const layer of chain)
|
|
163
|
+
for (const track of layer.animations ?? []) {
|
|
164
|
+
const stationary: (Window & { value: number | string })[] = [];
|
|
165
|
+
const add = (start: number, finish: number, value: number | string) => {
|
|
166
|
+
const last = stationary.at(-1);
|
|
167
|
+
if (finish <= start) return;
|
|
168
|
+
if (last && last.end === start && last.value === value) last.end = finish;
|
|
169
|
+
else stationary.push({ start, end: finish, value });
|
|
170
|
+
};
|
|
171
|
+
for (let i = 1; i < track.keyframes.length; i++) {
|
|
172
|
+
const from = track.keyframes[i - 1]!,
|
|
173
|
+
to = track.keyframes[i]!;
|
|
174
|
+
if (from.value === to.value || to.easing === "step") add(from.atS, to.atS, from.value);
|
|
175
|
+
}
|
|
176
|
+
const last = track.keyframes.at(-1)!;
|
|
177
|
+
add(last.atS, end, last.value);
|
|
178
|
+
available = commonWindows(available, stationary);
|
|
179
|
+
if (!available.length) return 0;
|
|
180
|
+
}
|
|
181
|
+
let longest = 0;
|
|
182
|
+
for (const window of available) {
|
|
183
|
+
const middle = (window.start + window.end) / 2;
|
|
184
|
+
const visible =
|
|
185
|
+
chain.reduce((opacity, layer) => opacity * trackValue(layer, "opacity", middle, 1), 1) >= 0.5;
|
|
186
|
+
const revealed = trackValue(item.layer, "reveal", middle, 1) >= 0.999;
|
|
187
|
+
const scaled = chain.every(
|
|
188
|
+
(layer) =>
|
|
189
|
+
trackValue(layer, "scaleX", middle, 1) > 0.01 &&
|
|
190
|
+
trackValue(layer, "scaleY", middle, 1) > 0.01,
|
|
191
|
+
);
|
|
192
|
+
if (visible && revealed && scaled) longest = Math.max(longest, window.end - window.start);
|
|
193
|
+
}
|
|
194
|
+
return longest;
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
function luminance(rgb: number[]): number {
|
|
198
|
+
const values = rgb.slice(0, 3).map((x) => {
|
|
199
|
+
const n = x / 255;
|
|
200
|
+
return n <= 0.04045 ? n / 12.92 : ((n + 0.055) / 1.055) ** 2.4;
|
|
201
|
+
});
|
|
202
|
+
return values[0]! * 0.2126 + values[1]! * 0.7152 + values[2]! * 0.0722;
|
|
203
|
+
}
|
|
204
|
+
function textContrast(
|
|
205
|
+
item: Item,
|
|
206
|
+
index: number,
|
|
207
|
+
items: Item[],
|
|
208
|
+
theme: LaunchTheme,
|
|
209
|
+
scene: MotionScene,
|
|
210
|
+
): number | undefined {
|
|
211
|
+
if (item.layer.type !== "text" || !item.box || item.opacity < 0.999) return undefined;
|
|
212
|
+
const ink = parseMotionColor(item.layer.fill ?? theme.ink),
|
|
213
|
+
canvas = parseMotionColor(scene.background === "dark" ? theme.dark : theme.canvas);
|
|
214
|
+
if (!ink || ink[3] < 0.999 || !canvas || canvas[3] < 0.999) return undefined;
|
|
215
|
+
let background: number[] | undefined = canvas;
|
|
216
|
+
for (let i = 0; i < index; i++) {
|
|
217
|
+
const prior = items[i]!;
|
|
218
|
+
if (
|
|
219
|
+
prior.layer.type === "group" ||
|
|
220
|
+
!prior.box ||
|
|
221
|
+
prior.opacity < 0.001 ||
|
|
222
|
+
!intersects(prior.box, item.box)
|
|
223
|
+
)
|
|
224
|
+
continue;
|
|
225
|
+
if (
|
|
226
|
+
prior.layer.type !== "rect" ||
|
|
227
|
+
Math.abs(prior.matrix[1]) + Math.abs(prior.matrix[2]) > 1e-6
|
|
228
|
+
) {
|
|
229
|
+
background = undefined;
|
|
230
|
+
continue;
|
|
231
|
+
}
|
|
232
|
+
const radius =
|
|
233
|
+
(prior.layer.radius ?? 0) * Math.max(Math.abs(prior.matrix[0]), Math.abs(prior.matrix[3]));
|
|
234
|
+
const inner = {
|
|
235
|
+
left: prior.box.left + radius,
|
|
236
|
+
right: prior.box.right - radius,
|
|
237
|
+
top: prior.box.top + radius,
|
|
238
|
+
bottom: prior.box.bottom - radius,
|
|
239
|
+
};
|
|
240
|
+
const paint = parseMotionColor(prior.layer.fill ?? theme.surface);
|
|
241
|
+
if (!contains(inner, item.box) || !paint) {
|
|
242
|
+
background = undefined;
|
|
243
|
+
continue;
|
|
244
|
+
}
|
|
245
|
+
const alpha = paint[3] * prior.opacity;
|
|
246
|
+
if (alpha >= 0.999) background = paint;
|
|
247
|
+
else if (background)
|
|
248
|
+
background = paint.slice(0, 3).map((n, j) => n * alpha + background![j]! * (1 - alpha));
|
|
249
|
+
}
|
|
250
|
+
// A later image or shape can obscure the text: its visible background is uncertain.
|
|
251
|
+
if (
|
|
252
|
+
items
|
|
253
|
+
.slice(index + 1)
|
|
254
|
+
.some(
|
|
255
|
+
(x) =>
|
|
256
|
+
x.layer.type !== "group" && x.box && x.opacity > 0.001 && intersects(x.box, item.box!),
|
|
257
|
+
)
|
|
258
|
+
)
|
|
259
|
+
return undefined;
|
|
260
|
+
if (!background) return undefined;
|
|
261
|
+
const a = luminance(ink),
|
|
262
|
+
b = luminance(background);
|
|
263
|
+
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
|
|
264
|
+
}
|
|
265
|
+
function denseAnimation(scene: MotionScene, items: Item[]): number {
|
|
266
|
+
if (scene.motion === "off") return 0;
|
|
267
|
+
const events: { time: number; delta: number; id: string }[] = [];
|
|
268
|
+
for (const item of items)
|
|
269
|
+
for (const track of item.layer.animations ?? [])
|
|
270
|
+
for (let i = 1; i < track.keyframes.length; i++) {
|
|
271
|
+
const a = track.keyframes[i - 1]!,
|
|
272
|
+
b = track.keyframes[i]!;
|
|
273
|
+
if (a.value !== b.value && b.easing !== "step")
|
|
274
|
+
events.push(
|
|
275
|
+
{ time: a.atS, delta: 1, id: item.path },
|
|
276
|
+
{ time: b.atS, delta: -1, id: item.path },
|
|
277
|
+
);
|
|
278
|
+
}
|
|
279
|
+
events.sort((a, b) => a.time - b.time || a.delta - b.delta);
|
|
280
|
+
const active = new Map<string, number>();
|
|
281
|
+
let peak = 0;
|
|
282
|
+
for (let i = 0; i < events.length; ) {
|
|
283
|
+
const time = events[i]!.time;
|
|
284
|
+
while (i < events.length && events[i]!.time === time) {
|
|
285
|
+
const event = events[i++]!,
|
|
286
|
+
count = (active.get(event.id) ?? 0) + event.delta;
|
|
287
|
+
if (count > 0) active.set(event.id, count);
|
|
288
|
+
else active.delete(event.id);
|
|
289
|
+
}
|
|
290
|
+
if (i < events.length) peak = Math.max(peak, active.size);
|
|
291
|
+
}
|
|
292
|
+
return peak;
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
type Point = [number, number];
|
|
296
|
+
type ClipBounds = { inverse: Matrix; halfWidth: number; halfHeight: number };
|
|
297
|
+
type SampleItem = Item & { clips: ClipBounds[]; stable: boolean; order: number };
|
|
298
|
+
const connectorProperties = new Set([
|
|
299
|
+
"x",
|
|
300
|
+
"y",
|
|
301
|
+
"scaleX",
|
|
302
|
+
"scaleY",
|
|
303
|
+
"rotation",
|
|
304
|
+
"opacity",
|
|
305
|
+
"width",
|
|
306
|
+
"height",
|
|
307
|
+
"radius",
|
|
308
|
+
"end",
|
|
309
|
+
"stroke",
|
|
310
|
+
"strokeWidth",
|
|
311
|
+
"fill",
|
|
312
|
+
]);
|
|
313
|
+
function pointAt(m: Matrix, p: Point): Point {
|
|
314
|
+
return [m[0] * p[0] + m[2] * p[1] + m[4], m[1] * p[0] + m[3] * p[1] + m[5]];
|
|
315
|
+
}
|
|
316
|
+
function clipBounds(
|
|
317
|
+
matrix: Matrix,
|
|
318
|
+
width: number,
|
|
319
|
+
height: number,
|
|
320
|
+
radius = 0,
|
|
321
|
+
): ClipBounds | undefined {
|
|
322
|
+
const [a, b, c, d, x, y] = matrix,
|
|
323
|
+
determinant = a * d - b * c;
|
|
324
|
+
if (Math.abs(determinant) < 1e-8) return undefined;
|
|
325
|
+
// An inscribed rectangle avoids claiming visibility in rounded-away corners.
|
|
326
|
+
const inset = Math.min(radius, width / 2, height / 2) * (1 - Math.SQRT1_2) + 0.5;
|
|
327
|
+
if (width / 2 <= inset || height / 2 <= inset) return undefined;
|
|
328
|
+
return {
|
|
329
|
+
inverse: [
|
|
330
|
+
d / determinant,
|
|
331
|
+
-b / determinant,
|
|
332
|
+
-c / determinant,
|
|
333
|
+
a / determinant,
|
|
334
|
+
(c * y - d * x) / determinant,
|
|
335
|
+
(b * x - a * y) / determinant,
|
|
336
|
+
],
|
|
337
|
+
halfWidth: width / 2 - inset,
|
|
338
|
+
halfHeight: height / 2 - inset,
|
|
339
|
+
};
|
|
340
|
+
}
|
|
341
|
+
/** Clip the actual segment, not its bounding box, against transformed rectangles. */
|
|
342
|
+
function segmentInterval(
|
|
343
|
+
from: Point,
|
|
344
|
+
to: Point,
|
|
345
|
+
bounds: ClipBounds[],
|
|
346
|
+
): [number, number] | undefined {
|
|
347
|
+
let low = 0,
|
|
348
|
+
high = 1;
|
|
349
|
+
for (const bound of bounds) {
|
|
350
|
+
const a = pointAt(bound.inverse, from),
|
|
351
|
+
b = pointAt(bound.inverse, to);
|
|
352
|
+
for (const [axis, half] of [
|
|
353
|
+
[0, bound.halfWidth],
|
|
354
|
+
[1, bound.halfHeight],
|
|
355
|
+
] as const) {
|
|
356
|
+
const delta = b[axis] - a[axis];
|
|
357
|
+
if (Math.abs(delta) < 1e-10) {
|
|
358
|
+
if (Math.abs(a[axis]) >= half) return undefined;
|
|
359
|
+
} else {
|
|
360
|
+
const first = (-half - a[axis]) / delta,
|
|
361
|
+
last = (half - a[axis]) / delta;
|
|
362
|
+
low = Math.max(low, Math.min(first, last));
|
|
363
|
+
high = Math.min(high, Math.max(first, last));
|
|
364
|
+
if (high - low < 1e-7) return undefined;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
}
|
|
368
|
+
return high > low ? [low, high] : undefined;
|
|
369
|
+
}
|
|
370
|
+
function revealedSegments(
|
|
371
|
+
layer: Extract<MotionLayer, { type: "line" }>,
|
|
372
|
+
matrix: Matrix,
|
|
373
|
+
): [Point, Point][] {
|
|
374
|
+
const lengths = layer.points
|
|
375
|
+
.slice(1)
|
|
376
|
+
.map((point, i) => Math.hypot(point[0] - layer.points[i]![0], point[1] - layer.points[i]![1]));
|
|
377
|
+
let remaining = lengths.reduce((a, b) => a + b, 0) * (layer.end ?? 1);
|
|
378
|
+
const segments: [Point, Point][] = [];
|
|
379
|
+
for (let i = 0; i < lengths.length && remaining > 0; i++) {
|
|
380
|
+
const length = lengths[i]!;
|
|
381
|
+
if (!length) continue;
|
|
382
|
+
const from = layer.points[i]!,
|
|
383
|
+
to = layer.points[i + 1]!,
|
|
384
|
+
fraction = Math.min(1, remaining / length);
|
|
385
|
+
segments.push([
|
|
386
|
+
pointAt(matrix, from),
|
|
387
|
+
pointAt(matrix, [
|
|
388
|
+
from[0] + (to[0] - from[0]) * fraction,
|
|
389
|
+
from[1] + (to[1] - from[1]) * fraction,
|
|
390
|
+
]),
|
|
391
|
+
]);
|
|
392
|
+
remaining -= length;
|
|
393
|
+
}
|
|
394
|
+
return segments;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
function connectorWarnings(scene: MotionScene, items: Item[], theme: LaunchTheme): LaunchIssue[] {
|
|
398
|
+
const lines = items.filter((item) => item.layer.type === "line");
|
|
399
|
+
const media = items.filter((item) => item.layer.type === "image" || item.layer.type === "video");
|
|
400
|
+
if (!lines.length || !media.length) return [];
|
|
401
|
+
const relevant = new Set<MotionLayer>();
|
|
402
|
+
for (const item of [...lines, ...media, ...items.filter((x) => x.layer.type === "rect")])
|
|
403
|
+
for (const layer of [...item.parents, item.layer]) relevant.add(layer);
|
|
404
|
+
const transition =
|
|
405
|
+
scene.motion !== "off" && scene.transition?.type && scene.transition.type !== "cut"
|
|
406
|
+
? Math.min(scene.transition.durationS ?? 0.45, scene.durationS / 2)
|
|
407
|
+
: 0;
|
|
408
|
+
const first = transition,
|
|
409
|
+
last = scene.durationS - transition,
|
|
410
|
+
halfHold = 0.175;
|
|
411
|
+
if (last - first < halfHold * 2) return [];
|
|
412
|
+
// Up to 64 samples and 40k segment comparisons. Dense/short-lived crossings
|
|
413
|
+
// can be missed; this advisory is not an exhaustive visibility certificate.
|
|
414
|
+
const times = Array.from(
|
|
415
|
+
{ length: 32 },
|
|
416
|
+
(_, i) => first + halfHold + ((last - first - halfHold * 2) * (i + 0.5)) / 32,
|
|
417
|
+
);
|
|
418
|
+
const keyed: number[] = [];
|
|
419
|
+
let candidates = 0,
|
|
420
|
+
random = 0x51de;
|
|
421
|
+
const consider = (time: number) => {
|
|
422
|
+
if (time < first + halfHold || time > last - halfHold) return;
|
|
423
|
+
candidates++;
|
|
424
|
+
if (keyed.length < 32) keyed.push(time);
|
|
425
|
+
else {
|
|
426
|
+
random = (Math.imul(random, 1664525) + 1013904223) >>> 0;
|
|
427
|
+
const index = random % candidates;
|
|
428
|
+
if (index < 32) keyed[index] = time;
|
|
429
|
+
}
|
|
430
|
+
};
|
|
431
|
+
if (scene.motion !== "off")
|
|
432
|
+
for (const layer of relevant)
|
|
433
|
+
for (const track of layer.animations ?? []) {
|
|
434
|
+
if (!connectorProperties.has(track.property)) continue;
|
|
435
|
+
for (let i = 0; i < track.keyframes.length; i++) {
|
|
436
|
+
const a = track.keyframes[i]!.atS,
|
|
437
|
+
b = track.keyframes[i + 1]?.atS ?? last;
|
|
438
|
+
if (b - a >= halfHold * 2) consider((a + b) / 2);
|
|
439
|
+
}
|
|
440
|
+
}
|
|
441
|
+
times.push(...keyed);
|
|
442
|
+
const warnings: LaunchIssue[] = [],
|
|
443
|
+
warned = new Set<string>();
|
|
444
|
+
const design = scene.designSize ?? { width: 1920, height: 1080 };
|
|
445
|
+
const frame = clipBounds(identity, design.width, design.height)!;
|
|
446
|
+
let budget = 40_000;
|
|
447
|
+
for (const time of [...new Set(times)].sort((a, b) => a - b)) {
|
|
448
|
+
const states = new Map<MotionLayer, SampleItem>();
|
|
449
|
+
for (const [order, item] of items.entries()) {
|
|
450
|
+
if (!relevant.has(item.layer)) continue;
|
|
451
|
+
const layer = { ...item.layer } as MotionLayer;
|
|
452
|
+
let stable = true;
|
|
453
|
+
if (scene.motion !== "off")
|
|
454
|
+
for (const track of layer.animations ?? []) {
|
|
455
|
+
if (!connectorProperties.has(track.property)) continue;
|
|
456
|
+
(layer as unknown as Record<string, unknown>)[track.property] = evaluateMotionTrack(
|
|
457
|
+
track,
|
|
458
|
+
time,
|
|
459
|
+
);
|
|
460
|
+
for (let i = 1; i < track.keyframes.length; i++) {
|
|
461
|
+
const a = track.keyframes[i - 1]!,
|
|
462
|
+
b = track.keyframes[i]!;
|
|
463
|
+
if (a.value === b.value) continue;
|
|
464
|
+
if (b.easing === "step") {
|
|
465
|
+
if (b.atS > time - halfHold && b.atS < time + halfHold) stable = false;
|
|
466
|
+
} else if (a.atS < time + halfHold && b.atS > time - halfHold) stable = false;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
const parent = states.get(item.parents.at(-1)!);
|
|
470
|
+
const matrix = transform(parent?.matrix ?? identity, layer);
|
|
471
|
+
const clips = [...(parent?.clips ?? [frame])];
|
|
472
|
+
if (layer.type === "group" && layer.clip) {
|
|
473
|
+
const bounds = clipBounds(matrix, layer.width!, layer.height!, layer.radius);
|
|
474
|
+
if (bounds) clips.push(bounds);
|
|
475
|
+
else stable = false;
|
|
476
|
+
}
|
|
477
|
+
states.set(item.layer, {
|
|
478
|
+
...item,
|
|
479
|
+
layer,
|
|
480
|
+
matrix,
|
|
481
|
+
order,
|
|
482
|
+
clips,
|
|
483
|
+
box: boxOf(layer, matrix),
|
|
484
|
+
stable: stable && (parent?.stable ?? true),
|
|
485
|
+
opacity: (parent?.opacity ?? 1) * (layer.opacity ?? 1),
|
|
486
|
+
});
|
|
487
|
+
}
|
|
488
|
+
const sampleMedia = media
|
|
489
|
+
.map((item) => states.get(item.layer)!)
|
|
490
|
+
.filter((x) => x.stable && x.opacity >= 0.5);
|
|
491
|
+
const occluders = [...states.values()].flatMap((item) => {
|
|
492
|
+
if (
|
|
493
|
+
item.layer.type !== "rect" ||
|
|
494
|
+
!item.stable ||
|
|
495
|
+
item.opacity < 0.999 ||
|
|
496
|
+
(parseMotionColor(item.layer.fill ?? theme.surface)?.[3] ?? 0) < 0.999
|
|
497
|
+
)
|
|
498
|
+
return [];
|
|
499
|
+
const bounds = clipBounds(
|
|
500
|
+
item.matrix,
|
|
501
|
+
item.layer.width,
|
|
502
|
+
item.layer.height,
|
|
503
|
+
item.layer.radius,
|
|
504
|
+
);
|
|
505
|
+
return bounds ? [{ order: item.order, bounds: [...item.clips, bounds] }] : [];
|
|
506
|
+
});
|
|
507
|
+
for (const authored of lines) {
|
|
508
|
+
if (warned.has(authored.path)) continue;
|
|
509
|
+
const item = states.get(authored.layer)!;
|
|
510
|
+
if (
|
|
511
|
+
item.layer.type !== "line" ||
|
|
512
|
+
!item.stable ||
|
|
513
|
+
item.opacity < 0.1 ||
|
|
514
|
+
Math.abs(item.matrix[0] * item.matrix[3] - item.matrix[1] * item.matrix[2]) < 1e-8 ||
|
|
515
|
+
(item.layer.strokeWidth ?? 2) <= 0 ||
|
|
516
|
+
item.opacity * (parseMotionColor(item.layer.stroke ?? theme.accent)?.[3] ?? 0) < 0.1
|
|
517
|
+
)
|
|
518
|
+
continue;
|
|
519
|
+
const segments = revealedSegments(item.layer, item.matrix);
|
|
520
|
+
if (!segments.length) continue;
|
|
521
|
+
for (const target of sampleMedia) {
|
|
522
|
+
if (
|
|
523
|
+
target.order >= item.order ||
|
|
524
|
+
!target.box ||
|
|
525
|
+
!item.box ||
|
|
526
|
+
!intersects(item.box, target.box)
|
|
527
|
+
)
|
|
528
|
+
continue;
|
|
529
|
+
if (target.layer.type !== "image" && target.layer.type !== "video") continue;
|
|
530
|
+
const bounds = clipBounds(
|
|
531
|
+
target.matrix,
|
|
532
|
+
target.layer.width,
|
|
533
|
+
target.layer.height,
|
|
534
|
+
target.layer.radius,
|
|
535
|
+
);
|
|
536
|
+
if (!bounds) continue;
|
|
537
|
+
let crossing = false;
|
|
538
|
+
for (const [from, to] of segments) {
|
|
539
|
+
if (--budget < 0) return warnings;
|
|
540
|
+
const hit = segmentInterval(from, to, [...item.clips, ...target.clips, bounds]);
|
|
541
|
+
if (!hit) continue;
|
|
542
|
+
// A known opaque shape painted over either participant can remove
|
|
543
|
+
// this entire intersection. Unknown image alpha is not guessed.
|
|
544
|
+
const hidden = occluders.some((occluder) => {
|
|
545
|
+
if (occluder.order <= target.order) return false;
|
|
546
|
+
const covered = segmentInterval(from, to, occluder.bounds);
|
|
547
|
+
return covered && covered[0] <= hit[0] && covered[1] >= hit[1];
|
|
548
|
+
});
|
|
549
|
+
if (!hidden) {
|
|
550
|
+
crossing = true;
|
|
551
|
+
break;
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
if (!crossing) continue;
|
|
555
|
+
warnings.push({
|
|
556
|
+
severity: "warn",
|
|
557
|
+
path: authored.path,
|
|
558
|
+
message: `Possible obscuring or ambiguous connector: foreground line "${item.layer.id}" enters media "${target.layer.id}" bounds at scene-local ${time.toFixed(2)}s during a sampled visible hold. Inspect the actual media and omit or reposition the line if this is not intentional; geometry does not establish text or semantic overlap.`,
|
|
559
|
+
});
|
|
560
|
+
warned.add(authored.path);
|
|
561
|
+
break;
|
|
562
|
+
}
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
return warnings;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
/** Heuristics for validated compositions, not a guarantee of aesthetics or measured font layout. */
|
|
569
|
+
export function analyzeMotionQuality(composition: LaunchComposition): LaunchIssue[] {
|
|
570
|
+
const issues: LaunchIssue[] = [];
|
|
571
|
+
const warn = (path: string, message: string) => issues.push({ severity: "warn", path, message });
|
|
572
|
+
composition.scenes.forEach((scene, index) => {
|
|
573
|
+
if (scene.type !== "motion") return;
|
|
574
|
+
const prefix = `scenes[${index}]`,
|
|
575
|
+
design = scene.designSize ?? { width: 1920, height: 1080 },
|
|
576
|
+
short = Math.min(design.width, design.height),
|
|
577
|
+
theme = { ...composition.theme, ...scene.colors };
|
|
578
|
+
const frame = {
|
|
579
|
+
left: -design.width / 2,
|
|
580
|
+
right: design.width / 2,
|
|
581
|
+
top: -design.height / 2,
|
|
582
|
+
bottom: design.height / 2,
|
|
583
|
+
},
|
|
584
|
+
items = flatten(scene, prefix);
|
|
585
|
+
items.forEach((item, i) => {
|
|
586
|
+
if (
|
|
587
|
+
item.layer.type !== "group" &&
|
|
588
|
+
item.box &&
|
|
589
|
+
item.opacity > 0.001 &&
|
|
590
|
+
item.box.left < item.box.right &&
|
|
591
|
+
item.box.top < item.box.bottom &&
|
|
592
|
+
!contains(frame, item.box)
|
|
593
|
+
)
|
|
594
|
+
warn(
|
|
595
|
+
item.path,
|
|
596
|
+
"Resting bounds extend outside the design frame; check the authored base position and size. Entrance keyframes are deliberately excluded.",
|
|
597
|
+
);
|
|
598
|
+
if (item.layer.type !== "text") return;
|
|
599
|
+
const text: MotionTextLayer = item.layer,
|
|
600
|
+
scale = Math.min(
|
|
601
|
+
Math.hypot(item.matrix[0], item.matrix[1]),
|
|
602
|
+
Math.hypot(item.matrix[2], item.matrix[3]),
|
|
603
|
+
);
|
|
604
|
+
if (item.opacity > 0.001 && text.fontSize * scale < short * 0.022)
|
|
605
|
+
warn(
|
|
606
|
+
`${item.path}.fontSize`,
|
|
607
|
+
"Text is small relative to the design's short edge; consider a larger font or a simpler message.",
|
|
608
|
+
);
|
|
609
|
+
const ratio = textContrast(item, i, items, theme, scene);
|
|
610
|
+
if (ratio !== undefined && ratio < 4.5)
|
|
611
|
+
warn(
|
|
612
|
+
`${item.path}.fill`,
|
|
613
|
+
`Text contrast is only ${ratio.toFixed(2)}:1 against a known flat background; aim for at least 4.5:1.`,
|
|
614
|
+
);
|
|
615
|
+
const required = estimateMotionReadingSeconds(text.text),
|
|
616
|
+
hold = usefulHold(scene, item);
|
|
617
|
+
if (hold + 0.001 < required)
|
|
618
|
+
warn(
|
|
619
|
+
`${item.path}.animations`,
|
|
620
|
+
`Text has only ${hold.toFixed(2)}s of stable, fully revealed hold; allow about ${required.toFixed(2)}s for this copy. This reading-time estimate is heuristic.`,
|
|
621
|
+
);
|
|
622
|
+
});
|
|
623
|
+
const density = denseAnimation(scene, items);
|
|
624
|
+
if (density > 8)
|
|
625
|
+
warn(
|
|
626
|
+
`${prefix}.layers`,
|
|
627
|
+
`${density} layers animate simultaneously; stagger or simplify the movement to keep attention on the message.`,
|
|
628
|
+
);
|
|
629
|
+
issues.push(...connectorWarnings(scene, items, theme));
|
|
630
|
+
});
|
|
631
|
+
return issues;
|
|
632
|
+
}
|