framesheet 0.0.0-stage → 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (69) hide show
  1. package/LICENSE +22 -0
  2. package/README.md +212 -2
  3. package/dist/analysis.d.ts +84 -0
  4. package/dist/analysis.d.ts.map +1 -0
  5. package/dist/analysis.js +335 -0
  6. package/dist/analysis.js.map +1 -0
  7. package/dist/checks.d.ts +44 -0
  8. package/dist/checks.d.ts.map +1 -0
  9. package/dist/checks.js +200 -0
  10. package/dist/checks.js.map +1 -0
  11. package/dist/compose.d.ts +117 -0
  12. package/dist/compose.d.ts.map +1 -0
  13. package/dist/compose.js +236 -0
  14. package/dist/compose.js.map +1 -0
  15. package/dist/driver.d.ts +56 -0
  16. package/dist/driver.d.ts.map +1 -0
  17. package/dist/driver.js +94 -0
  18. package/dist/driver.js.map +1 -0
  19. package/dist/generated/assets.d.ts +9 -0
  20. package/dist/generated/assets.d.ts.map +1 -0
  21. package/dist/generated/assets.js +11 -0
  22. package/dist/generated/assets.js.map +1 -0
  23. package/dist/index.d.ts +15 -0
  24. package/dist/index.d.ts.map +1 -0
  25. package/dist/index.js +14 -0
  26. package/dist/index.js.map +1 -0
  27. package/dist/inspect.d.ts +150 -0
  28. package/dist/inspect.d.ts.map +1 -0
  29. package/dist/inspect.js +570 -0
  30. package/dist/inspect.js.map +1 -0
  31. package/dist/models.d.ts +117 -0
  32. package/dist/models.d.ts.map +1 -0
  33. package/dist/models.js +9 -0
  34. package/dist/models.js.map +1 -0
  35. package/dist/pixels.d.ts +46 -0
  36. package/dist/pixels.d.ts.map +1 -0
  37. package/dist/pixels.js +237 -0
  38. package/dist/pixels.js.map +1 -0
  39. package/dist/pyfmt.d.ts +16 -0
  40. package/dist/pyfmt.d.ts.map +1 -0
  41. package/dist/pyfmt.js +54 -0
  42. package/dist/pyfmt.js.map +1 -0
  43. package/dist/report.schema.json +754 -0
  44. package/dist/sources.d.ts +32 -0
  45. package/dist/sources.d.ts.map +1 -0
  46. package/dist/sources.js +123 -0
  47. package/dist/sources.js.map +1 -0
  48. package/dist/test.d.ts +46 -0
  49. package/dist/test.d.ts.map +1 -0
  50. package/dist/test.js +58 -0
  51. package/dist/test.js.map +1 -0
  52. package/dist/timing.d.ts +30 -0
  53. package/dist/timing.d.ts.map +1 -0
  54. package/dist/timing.js +128 -0
  55. package/dist/timing.js.map +1 -0
  56. package/package.json +78 -4
  57. package/src/analysis.ts +416 -0
  58. package/src/checks.ts +261 -0
  59. package/src/compose.ts +303 -0
  60. package/src/driver.ts +115 -0
  61. package/src/generated/assets.ts +14 -0
  62. package/src/index.ts +44 -0
  63. package/src/inspect.ts +690 -0
  64. package/src/models.ts +130 -0
  65. package/src/pixels.ts +248 -0
  66. package/src/pyfmt.ts +55 -0
  67. package/src/sources.ts +131 -0
  68. package/src/test.ts +100 -0
  69. package/src/timing.ts +133 -0
package/src/timing.ts ADDED
@@ -0,0 +1,133 @@
1
+ /** Duration detection and sample-time planning (port of timing.py). */
2
+
3
+ import type { AnimationTiming, DurationInfo } from "./models.js";
4
+ import { g, pyRound } from "./pyfmt.js";
5
+
6
+ export const FALLBACK_SECONDS = 3.0;
7
+ export const MAX_SECONDS = 60.0;
8
+ /** Use the LCM of loop periods only if it is at most this many times the longest period. */
9
+ export const LCM_FACTOR = 4;
10
+ /** One 60 fps frame: the last tile and the loop probe sit this far before the cycle end. */
11
+ export const EPSILON = 1 / 60;
12
+
13
+ /** Normalise raw runtime output (`window.__framesheet.timings()`) to full records. */
14
+ export function parseTimings(raw: unknown[]): AnimationTiming[] {
15
+ return raw.map((item) => {
16
+ const r = item as Record<string, unknown>;
17
+ return {
18
+ kind: r.kind as AnimationTiming["kind"],
19
+ target: (r.target as string | null | undefined) ?? null,
20
+ name: (r.name as string | null | undefined) ?? null,
21
+ start: Number(r.start),
22
+ duration: Number(r.duration),
23
+ iterations: r.iterations == null ? null : Number(r.iterations),
24
+ direction: (r.direction as string | undefined) ?? "normal",
25
+ period: Number(r.period),
26
+ end: r.end == null ? null : Number(r.end),
27
+ transform_box: (r.transform_box as string | null | undefined) ?? null,
28
+ };
29
+ });
30
+ }
31
+
32
+ function gcd(a: number, b: number): number {
33
+ while (b) [a, b] = [b, a % b];
34
+ return a;
35
+ }
36
+
37
+ function lcm(periods: number[]): number {
38
+ const ticks = periods.map((p) => Math.max(1, pyRound(p * 100))); // 10 ms resolution
39
+ return ticks.reduce((a, b) => (a / gcd(a, b)) * b) / 100;
40
+ }
41
+
42
+ /**
43
+ * Pick the time span to sample.
44
+ *
45
+ * - Infinite animations contribute their loop period (`duration`, doubled for
46
+ * `alternate`). Several periods are combined with their least common multiple if that
47
+ * stays reasonable, otherwise the longest period wins.
48
+ * - A positive start (`begin`/`delay`) of a looping animation is added, so the warm-up
49
+ * before the first iteration is visible.
50
+ * - Finite animations contribute their end time (delay + duration x iterations).
51
+ */
52
+ export function detectDuration(animations: AnimationTiming[], override?: number | null): DurationInfo {
53
+ const infinite = animations.filter((a) => a.iterations === null);
54
+ const finite = animations.filter((a) => a.iterations !== null && a.end !== null);
55
+ const looping = infinite.length > 0;
56
+ if (override != null) {
57
+ return {
58
+ seconds: override,
59
+ method: "override",
60
+ looping,
61
+ detail: `--duration ${g(override)}s`,
62
+ animations,
63
+ };
64
+ }
65
+ if (animations.length === 0) {
66
+ return {
67
+ seconds: FALLBACK_SECONDS,
68
+ method: "fallback",
69
+ looping: false,
70
+ detail:
71
+ `no SMIL/CSS/Web Animations found; sampled the ${g(FALLBACK_SECONDS)}s ` +
72
+ "fallback (pass --duration for JS-driven animation)",
73
+ animations: [],
74
+ };
75
+ }
76
+ const parts: string[] = [];
77
+ let seconds = 0;
78
+ if (infinite.length) {
79
+ const periods = [...new Set(infinite.map((a) => pyRound(a.period, 3)))].sort((a, b) => a - b);
80
+ let cycle = lcm(periods);
81
+ const longest = periods[periods.length - 1];
82
+ const list = periods.map((p) => `${g(p)}s`).join(", ");
83
+ if (periods.length === 1) {
84
+ parts.push(`loop period ${g(periods[0])}s`);
85
+ } else if (cycle <= LCM_FACTOR * longest && cycle <= MAX_SECONDS) {
86
+ parts.push(`LCM of loop periods ${list}`);
87
+ } else {
88
+ cycle = longest;
89
+ parts.push(`longest of loop periods ${list} (no short common multiple)`);
90
+ }
91
+ seconds = cycle;
92
+ const warmup = Math.max(Math.max(...infinite.map((a) => a.start)), 0);
93
+ if (warmup > 0) {
94
+ seconds = Math.max(seconds, warmup + cycle);
95
+ parts.push(`+ ${g(warmup)}s positive begin/delay before the first loop`);
96
+ }
97
+ }
98
+ if (finite.length) {
99
+ const lastEnd = Math.max(...finite.map((a) => a.end ?? 0));
100
+ if (lastEnd > seconds) {
101
+ seconds = lastEnd;
102
+ parts.push(`finite animations end at ${g(lastEnd)}s`);
103
+ }
104
+ }
105
+ const kinds = [...new Set(animations.map((a) => a.kind))].sort();
106
+ let detail = `${parts.join("; ")} (${animations.length} animation(s): ${kinds.join(", ")})`;
107
+ if (seconds > MAX_SECONDS) {
108
+ detail += `; capped at ${g(MAX_SECONDS)}s`;
109
+ seconds = MAX_SECONDS;
110
+ }
111
+ if (seconds <= 0) {
112
+ seconds = FALLBACK_SECONDS;
113
+ detail += `; zero-length, using ${g(FALLBACK_SECONDS)}s`;
114
+ }
115
+ return { seconds: pyRound(seconds, 3), method: "detected", looping, detail, animations };
116
+ }
117
+
118
+ /** The probe offset for loop checks: one 60 fps frame, or 1% of very short cycles. */
119
+ export function epsilon(duration: number): number {
120
+ return Math.min(EPSILON, duration / 100);
121
+ }
122
+
123
+ /**
124
+ * Evenly spaced times over `[0, duration]`. The last sample is one 60 fps frame *before*
125
+ * the end: at exactly `t=end` an infinite animation has already wrapped to its first
126
+ * frame, which would make every loop look seamless.
127
+ */
128
+ export function sampleTimes(duration: number, frames: number): number[] {
129
+ if (frames < 2) return [0];
130
+ const times = Array.from({ length: frames }, (_, i) => pyRound((duration * i) / (frames - 1), 4));
131
+ times[times.length - 1] = pyRound(duration - epsilon(duration), 4);
132
+ return times;
133
+ }