@mihirsarya/manim-scroll-runtime 0.1.2 → 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/dist/index.d.ts CHANGED
@@ -1,3 +1,5 @@
1
+ import { NativeTextPlayer, registerNativeAnimation } from "./native-player";
1
2
  import type { ScrollAnimationOptions } from "./types";
2
- export type { RenderManifest, ScrollAnimationOptions, ScrollRange, ScrollRangePreset, ScrollRangeValue, } from "./types";
3
+ export type { RenderManifest, ScrollAnimationOptions, ScrollRange, ScrollRangePreset, ScrollRangeValue, NativeAnimationOptions, } from "./types";
4
+ export { NativeTextPlayer, registerNativeAnimation };
3
5
  export declare function registerScrollAnimation(options: ScrollAnimationOptions): Promise<() => void>;
package/dist/index.js CHANGED
@@ -1,4 +1,6 @@
1
1
  import { ScrollPlayer } from "./player";
2
+ import { NativeTextPlayer, registerNativeAnimation } from "./native-player";
3
+ export { NativeTextPlayer, registerNativeAnimation };
2
4
  export async function registerScrollAnimation(options) {
3
5
  const player = new ScrollPlayer(options);
4
6
  await player.init();
package/dist/loader.d.ts CHANGED
@@ -4,7 +4,10 @@ export declare class FrameCache {
4
4
  private readonly frameUrls;
5
5
  private frames;
6
6
  private loading;
7
+ private preloadAhead;
7
8
  constructor(frameUrls: string[]);
8
9
  get length(): number;
9
10
  load(index: number): Promise<HTMLImageElement>;
11
+ private loadFrame;
12
+ private preloadNearby;
10
13
  }
package/dist/loader.js CHANGED
@@ -1,10 +1,17 @@
1
+ /**
2
+ * Resolve an asset URL relative to the manifest URL.
3
+ * Handles both absolute manifest URLs (with protocol) and path-only URLs.
4
+ */
1
5
  function resolveAssetUrl(asset, manifestUrl) {
2
- try {
3
- return new URL(asset, manifestUrl).toString();
4
- }
5
- catch {
6
+ // If asset is already absolute, return as-is
7
+ if (asset.startsWith("http://") || asset.startsWith("https://") || asset.startsWith("/")) {
6
8
  return asset;
7
9
  }
10
+ // Get the directory of the manifest
11
+ const lastSlash = manifestUrl.lastIndexOf("/");
12
+ const baseDir = lastSlash >= 0 ? manifestUrl.substring(0, lastSlash + 1) : "";
13
+ // Resolve relative path
14
+ return baseDir + asset;
8
15
  }
9
16
  export async function loadManifest(url) {
10
17
  const response = await fetch(url);
@@ -23,17 +30,26 @@ export class FrameCache {
23
30
  this.frameUrls = frameUrls;
24
31
  this.frames = new Map();
25
32
  this.loading = new Map();
33
+ this.preloadAhead = 5; // Number of frames to preload ahead
26
34
  }
27
35
  get length() {
28
36
  return this.frameUrls.length;
29
37
  }
30
38
  async load(index) {
39
+ // Start preloading nearby frames
40
+ this.preloadNearby(index);
31
41
  if (this.frames.has(index)) {
32
42
  return this.frames.get(index);
33
43
  }
34
44
  if (this.loading.has(index)) {
35
45
  return this.loading.get(index);
36
46
  }
47
+ return this.loadFrame(index);
48
+ }
49
+ async loadFrame(index) {
50
+ if (index < 0 || index >= this.frameUrls.length) {
51
+ throw new Error(`Frame index out of bounds: ${index}`);
52
+ }
37
53
  const url = this.frameUrls[index];
38
54
  const promise = new Promise((resolve, reject) => {
39
55
  const img = new Image();
@@ -51,4 +67,17 @@ export class FrameCache {
51
67
  this.loading.set(index, promise);
52
68
  return promise;
53
69
  }
70
+ preloadNearby(currentIndex) {
71
+ // Preload frames ahead and behind
72
+ for (let offset = 1; offset <= this.preloadAhead; offset++) {
73
+ const ahead = currentIndex + offset;
74
+ const behind = currentIndex - offset;
75
+ if (ahead < this.frameUrls.length && !this.frames.has(ahead) && !this.loading.has(ahead)) {
76
+ this.loadFrame(ahead).catch(() => { }); // Silently ignore preload failures
77
+ }
78
+ if (behind >= 0 && !this.frames.has(behind) && !this.loading.has(behind)) {
79
+ this.loadFrame(behind).catch(() => { }); // Silently ignore preload failures
80
+ }
81
+ }
82
+ }
54
83
  }
@@ -0,0 +1,65 @@
1
+ import type { NativeAnimationOptions } from "./types";
2
+ /**
3
+ * NativeTextPlayer - Renders text animation natively in the browser
4
+ * using SVG paths, replicating Manim's Write/DrawBorderThenFill animation.
5
+ *
6
+ * Phase 1 (progress 0 to 0.5): Draw the stroke progressively
7
+ * Phase 2 (progress 0.5 to 1.0): Fill in the text
8
+ *
9
+ * Key difference from naive implementations: we split each character into
10
+ * individual contours (sub-paths) and apply the lag_ratio to ALL contours
11
+ * across all characters. This matches Manim's behavior where outlines
12
+ * appear progressively rather than all at once.
13
+ */
14
+ export declare class NativeTextPlayer {
15
+ private readonly container;
16
+ private readonly options;
17
+ private svg;
18
+ private fallbackWrapper;
19
+ /** All sub-paths (segments) across all characters, for stroke animation */
20
+ private subPaths;
21
+ /** Fill paths (original closed contours) for the filled state */
22
+ private fillPaths;
23
+ private isActive;
24
+ private rafId;
25
+ private observer?;
26
+ private resizeObserver?;
27
+ private lastProgress;
28
+ private scrollHandler?;
29
+ private resizeHandler?;
30
+ private pendingDraw;
31
+ private pendingResize;
32
+ private font;
33
+ /** Last known font size, used to detect changes for inherited sizing */
34
+ private lastComputedFontSize;
35
+ constructor(options: NativeAnimationOptions);
36
+ init(): Promise<void>;
37
+ private createCharacterPaths;
38
+ /**
39
+ * Get the inherited font size from the container's computed style.
40
+ */
41
+ private getInheritedFontSize;
42
+ private createFallbackTextAnimation;
43
+ private render;
44
+ destroy(): void;
45
+ private setupObserver;
46
+ /**
47
+ * Set up resize handling for responsive behavior:
48
+ * 1. Window resize - recalculate scroll progress (viewport height changes)
49
+ * 2. Container resize - detect font-size changes when using inherited sizing
50
+ */
51
+ private setupResizeHandling;
52
+ /**
53
+ * Rebuild the animation when font size changes (for inherited sizing).
54
+ * This clears and recreates all character paths with the new size.
55
+ */
56
+ private rebuildAnimation;
57
+ private start;
58
+ private stop;
59
+ private tick;
60
+ }
61
+ /**
62
+ * Register a native text animation on a container element.
63
+ * This creates scroll-driven text animation without pre-rendered assets.
64
+ */
65
+ export declare function registerNativeAnimation(options: NativeAnimationOptions): Promise<() => void>;