@mihirsarya/manim-scroll-runtime 0.2.1 → 0.2.2

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,5 +1,5 @@
1
1
  import { NativeTextPlayer, registerNativeAnimation } from "./native-player";
2
2
  import type { ScrollAnimationOptions } from "./types";
3
- export type { RenderManifest, ScrollAnimationOptions, ScrollRange, ScrollRangePreset, ScrollRangeValue, NativeAnimationOptions, } from "./types";
3
+ export type { RenderManifest, ScrollAnimationOptions, ScrollRange, ScrollRangePreset, ScrollRangeValue, NativeAnimationOptions, EasingFunction, EasingPreset, PlaybackOptions, } from "./types";
4
4
  export { NativeTextPlayer, registerNativeAnimation };
5
5
  export declare function registerScrollAnimation(options: ScrollAnimationOptions): Promise<() => void>;
@@ -1,4 +1,4 @@
1
- import type { NativeAnimationOptions } from "./types";
1
+ import type { NativeAnimationOptions, PlaybackOptions } from "./types";
2
2
  /**
3
3
  * NativeTextPlayer - Renders text animation natively in the browser
4
4
  * using SVG paths, replicating Manim's Write/DrawBorderThenFill animation.
@@ -32,8 +32,61 @@ export declare class NativeTextPlayer {
32
32
  private font;
33
33
  /** Last known font size, used to detect changes for inherited sizing */
34
34
  private lastComputedFontSize;
35
+ /** Whether time-based animation is currently playing */
36
+ private _isPlaying;
37
+ /** RAF ID for playback animation loop */
38
+ private playbackRafId;
39
+ /** Start time of the current playback (from performance.now()) */
40
+ private playbackStartTime;
41
+ /** Duration of current playback in milliseconds */
42
+ private playbackDuration;
43
+ /** Easing function for current playback */
44
+ private playbackEasing;
45
+ /** Direction of playback: 1 for forward, -1 for reverse */
46
+ private playbackDirection;
47
+ /** Whether to loop the playback */
48
+ private playbackLoop;
49
+ /** Callback when playback completes */
50
+ private playbackOnComplete?;
51
+ /** Current progress value (0 to 1) - used in controlled mode */
52
+ private currentProgress;
53
+ /** Whether we're in controlled/programmatic mode (no scroll binding) */
54
+ private isControlledMode;
35
55
  constructor(options: NativeAnimationOptions);
36
56
  init(): Promise<void>;
57
+ /**
58
+ * Whether the animation is currently playing (time-based).
59
+ */
60
+ get isPlaying(): boolean;
61
+ /**
62
+ * Get the current progress value (0 to 1).
63
+ */
64
+ get progress(): number;
65
+ /**
66
+ * Set the animation progress directly (0 to 1).
67
+ * This is the primary method for controlled/declarative usage.
68
+ * Stops any ongoing playback.
69
+ */
70
+ setProgress(progress: number): void;
71
+ /**
72
+ * Play the animation over a specified duration.
73
+ * @param options - Playback options (duration, easing, loop, etc.)
74
+ * or just duration in milliseconds
75
+ */
76
+ play(options?: PlaybackOptions | number): void;
77
+ /**
78
+ * Pause the animation playback.
79
+ */
80
+ pause(): void;
81
+ /**
82
+ * Seek to a specific progress value (0 to 1).
83
+ * Unlike setProgress, this doesn't affect the playing state.
84
+ * If playing, the animation will continue from the new position.
85
+ */
86
+ seek(progress: number): void;
87
+ private startPlayback;
88
+ private pausePlayback;
89
+ private playbackTick;
37
90
  private createCharacterPaths;
38
91
  /**
39
92
  * Get the inherited font size from the container's computed style.
@@ -72,6 +72,36 @@ function integerInterpolate(start, end, alpha) {
72
72
  const subalpha = scaledAlpha - index;
73
73
  return [start + index, subalpha];
74
74
  }
75
+ // ============================================================================
76
+ // Easing Functions for Playback
77
+ // ============================================================================
78
+ /** Standard ease-in (quadratic) */
79
+ function easeIn(t) {
80
+ return t * t;
81
+ }
82
+ /** Standard ease-out (quadratic) */
83
+ function easeOut(t) {
84
+ return 1 - (1 - t) * (1 - t);
85
+ }
86
+ /** Standard ease-in-out (quadratic) */
87
+ function easeInOut(t) {
88
+ return t < 0.5 ? 2 * t * t : 1 - Math.pow(-2 * t + 2, 2) / 2;
89
+ }
90
+ /** Resolve an easing preset to an easing function */
91
+ function resolveEasing(easing) {
92
+ if (typeof easing === "function") {
93
+ return easing;
94
+ }
95
+ switch (easing) {
96
+ case "ease-in": return easeIn;
97
+ case "ease-out": return easeOut;
98
+ case "ease-in-out": return easeInOut;
99
+ case "smooth": return smooth;
100
+ case "linear":
101
+ default:
102
+ return linear;
103
+ }
104
+ }
75
105
  /**
76
106
  * Parse a relative unit string (e.g., "100vh", "-50%") to pixels.
77
107
  */
@@ -486,6 +516,7 @@ function splitPathIntoSegments(pathData) {
486
516
  */
487
517
  export class NativeTextPlayer {
488
518
  constructor(options) {
519
+ var _a;
489
520
  this.svg = null;
490
521
  this.fallbackWrapper = null;
491
522
  /** All sub-paths (segments) across all characters, for stroke animation */
@@ -500,6 +531,57 @@ export class NativeTextPlayer {
500
531
  this.font = null;
501
532
  /** Last known font size, used to detect changes for inherited sizing */
502
533
  this.lastComputedFontSize = 0;
534
+ // ==================== Playback State ====================
535
+ /** Whether time-based animation is currently playing */
536
+ this._isPlaying = false;
537
+ /** RAF ID for playback animation loop */
538
+ this.playbackRafId = null;
539
+ /** Start time of the current playback (from performance.now()) */
540
+ this.playbackStartTime = 0;
541
+ /** Duration of current playback in milliseconds */
542
+ this.playbackDuration = 0;
543
+ /** Easing function for current playback */
544
+ this.playbackEasing = linear;
545
+ /** Direction of playback: 1 for forward, -1 for reverse */
546
+ this.playbackDirection = 1;
547
+ /** Whether to loop the playback */
548
+ this.playbackLoop = false;
549
+ /** Current progress value (0 to 1) - used in controlled mode */
550
+ this.currentProgress = 0;
551
+ /** Whether we're in controlled/programmatic mode (no scroll binding) */
552
+ this.isControlledMode = false;
553
+ this.playbackTick = () => {
554
+ var _a, _b, _c;
555
+ if (!this._isPlaying)
556
+ return;
557
+ const elapsed = performance.now() - this.playbackStartTime;
558
+ let rawProgress = elapsed / this.playbackDuration;
559
+ if (rawProgress >= 1) {
560
+ if (this.playbackLoop) {
561
+ // Loop: reset and continue
562
+ this.playbackStartTime = performance.now();
563
+ rawProgress = 0;
564
+ }
565
+ else {
566
+ // Complete
567
+ rawProgress = 1;
568
+ this._isPlaying = false;
569
+ }
570
+ }
571
+ // Apply easing and direction
572
+ const easedProgress = this.playbackEasing(rawProgress);
573
+ this.currentProgress = this.playbackDirection === 1
574
+ ? easedProgress
575
+ : 1 - easedProgress;
576
+ this.render(this.currentProgress);
577
+ (_b = (_a = this.options).onProgress) === null || _b === void 0 ? void 0 : _b.call(_a, this.currentProgress);
578
+ if (this._isPlaying) {
579
+ this.playbackRafId = requestAnimationFrame(this.playbackTick);
580
+ }
581
+ else {
582
+ (_c = this.playbackOnComplete) === null || _c === void 0 ? void 0 : _c.call(this);
583
+ }
584
+ };
503
585
  // Manim defaults: DrawBorderThenFill stroke_width = 2
504
586
  // fontSize: undefined means inherit from parent element
505
587
  this.options = {
@@ -508,6 +590,9 @@ export class NativeTextPlayer {
508
590
  ...options,
509
591
  };
510
592
  this.container = options.container;
593
+ // If progress is provided, we're in controlled mode
594
+ this.isControlledMode = options.progress !== undefined;
595
+ this.currentProgress = (_a = options.progress) !== null && _a !== void 0 ? _a : 0;
511
596
  }
512
597
  async init() {
513
598
  var _a, _b;
@@ -535,14 +620,115 @@ export class NativeTextPlayer {
535
620
  if (this.svg) {
536
621
  this.container.appendChild(this.svg);
537
622
  }
538
- // Setup intersection observer
539
- this.setupObserver();
540
- // Setup resize handling for responsiveness
541
- this.setupResizeHandling();
542
- // Draw initial state (progress = 0)
543
- this.render(0);
623
+ // Only setup scroll-based animation if not in controlled mode
624
+ if (!this.isControlledMode) {
625
+ // Setup intersection observer
626
+ this.setupObserver();
627
+ // Setup resize handling for responsiveness
628
+ this.setupResizeHandling();
629
+ }
630
+ // Draw initial state
631
+ this.render(this.currentProgress);
544
632
  (_b = (_a = this.options).onReady) === null || _b === void 0 ? void 0 : _b.call(_a);
545
633
  }
634
+ // ==================== Public Playback API ====================
635
+ /**
636
+ * Whether the animation is currently playing (time-based).
637
+ */
638
+ get isPlaying() {
639
+ return this._isPlaying;
640
+ }
641
+ /**
642
+ * Get the current progress value (0 to 1).
643
+ */
644
+ get progress() {
645
+ return this.currentProgress;
646
+ }
647
+ /**
648
+ * Set the animation progress directly (0 to 1).
649
+ * This is the primary method for controlled/declarative usage.
650
+ * Stops any ongoing playback.
651
+ */
652
+ setProgress(progress) {
653
+ var _a, _b;
654
+ this.pausePlayback();
655
+ this.currentProgress = clamp(progress, 0, 1);
656
+ this.render(this.currentProgress);
657
+ (_b = (_a = this.options).onProgress) === null || _b === void 0 ? void 0 : _b.call(_a, this.currentProgress);
658
+ }
659
+ /**
660
+ * Play the animation over a specified duration.
661
+ * @param options - Playback options (duration, easing, loop, etc.)
662
+ * or just duration in milliseconds
663
+ */
664
+ play(options) {
665
+ var _a, _b, _c, _d;
666
+ this.pausePlayback();
667
+ // Parse options
668
+ const opts = typeof options === "number"
669
+ ? { duration: options }
670
+ : (options !== null && options !== void 0 ? options : {});
671
+ const duration = (_a = opts.duration) !== null && _a !== void 0 ? _a : 1000;
672
+ const delay = (_b = opts.delay) !== null && _b !== void 0 ? _b : 0;
673
+ this.playbackDuration = duration;
674
+ this.playbackEasing = resolveEasing(opts.easing);
675
+ this.playbackDirection = (_c = opts.direction) !== null && _c !== void 0 ? _c : 1;
676
+ this.playbackLoop = (_d = opts.loop) !== null && _d !== void 0 ? _d : false;
677
+ this.playbackOnComplete = opts.onComplete;
678
+ // Set initial progress based on direction
679
+ if (this.playbackDirection === 1 && this.currentProgress >= 1) {
680
+ this.currentProgress = 0;
681
+ }
682
+ else if (this.playbackDirection === -1 && this.currentProgress <= 0) {
683
+ this.currentProgress = 1;
684
+ }
685
+ // Start playback with optional delay
686
+ if (delay > 0) {
687
+ setTimeout(() => this.startPlayback(), delay);
688
+ }
689
+ else {
690
+ this.startPlayback();
691
+ }
692
+ }
693
+ /**
694
+ * Pause the animation playback.
695
+ */
696
+ pause() {
697
+ this.pausePlayback();
698
+ }
699
+ /**
700
+ * Seek to a specific progress value (0 to 1).
701
+ * Unlike setProgress, this doesn't affect the playing state.
702
+ * If playing, the animation will continue from the new position.
703
+ */
704
+ seek(progress) {
705
+ var _a, _b;
706
+ this.currentProgress = clamp(progress, 0, 1);
707
+ this.render(this.currentProgress);
708
+ (_b = (_a = this.options).onProgress) === null || _b === void 0 ? void 0 : _b.call(_a, this.currentProgress);
709
+ // If playing, reset the start time to continue from current position
710
+ if (this._isPlaying) {
711
+ const rawProgress = this.playbackDirection === 1
712
+ ? this.currentProgress
713
+ : 1 - this.currentProgress;
714
+ // Invert the easing to find approximate time offset
715
+ // For simplicity, use linear approximation
716
+ this.playbackStartTime = performance.now() - (rawProgress * this.playbackDuration);
717
+ }
718
+ }
719
+ // ==================== Private Playback Helpers ====================
720
+ startPlayback() {
721
+ this._isPlaying = true;
722
+ this.playbackStartTime = performance.now();
723
+ this.playbackTick();
724
+ }
725
+ pausePlayback() {
726
+ this._isPlaying = false;
727
+ if (this.playbackRafId !== null) {
728
+ cancelAnimationFrame(this.playbackRafId);
729
+ this.playbackRafId = null;
730
+ }
731
+ }
546
732
  async createCharacterPaths() {
547
733
  var _a, _b;
548
734
  if (!this.svg)
@@ -834,6 +1020,7 @@ export class NativeTextPlayer {
834
1020
  destroy() {
835
1021
  var _a, _b;
836
1022
  this.stop();
1023
+ this.pausePlayback(); // Clean up playback animation
837
1024
  (_a = this.observer) === null || _a === void 0 ? void 0 : _a.disconnect();
838
1025
  (_b = this.resizeObserver) === null || _b === void 0 ? void 0 : _b.disconnect();
839
1026
  if (this.resizeHandler) {
package/dist/types.d.ts CHANGED
@@ -46,6 +46,25 @@ export type ScrollAnimationOptions = {
46
46
  * Options for native text animation (no pre-rendered assets).
47
47
  * Replicates Manim's Write/DrawBorderThenFill animation in the browser.
48
48
  */
49
+ /** Easing function type: takes a progress value (0 to 1) and returns eased progress */
50
+ export type EasingFunction = (t: number) => number;
51
+ /** Pre-built easing presets */
52
+ export type EasingPreset = "linear" | "ease-in" | "ease-out" | "ease-in-out" | "smooth";
53
+ /** Options for duration-based animation playback */
54
+ export type PlaybackOptions = {
55
+ /** Animation duration in milliseconds */
56
+ duration?: number;
57
+ /** Delay before starting in milliseconds */
58
+ delay?: number;
59
+ /** Easing function or preset */
60
+ easing?: EasingPreset | EasingFunction;
61
+ /** Whether to loop the animation */
62
+ loop?: boolean;
63
+ /** Play direction: 1 for forward, -1 for reverse */
64
+ direction?: 1 | -1;
65
+ /** Callback when playback completes (not called if loop is true) */
66
+ onComplete?: () => void;
67
+ };
49
68
  export type NativeAnimationOptions = {
50
69
  /** The container element to render the animation into */
51
70
  container: HTMLElement;
@@ -59,8 +78,13 @@ export type NativeAnimationOptions = {
59
78
  fontUrl?: string;
60
79
  /** Stroke width for the drawing phase (default: 2, matches Manim's DrawBorderThenFill) */
61
80
  strokeWidth?: number;
62
- /** Scroll range configuration */
81
+ /** Scroll range configuration. Ignored when progress is provided. */
63
82
  scrollRange?: ScrollRangeValue;
83
+ /**
84
+ * Explicit progress value (0 to 1). When provided, disables scroll-based control
85
+ * and renders the animation at this exact progress.
86
+ */
87
+ progress?: number;
64
88
  /** Called when animation is loaded and ready */
65
89
  onReady?: () => void;
66
90
  /** Called on scroll progress updates */
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mihirsarya/manim-scroll-runtime",
3
- "version": "0.2.1",
3
+ "version": "0.2.2",
4
4
  "description": "Scroll-driven playback runtime for pre-rendered Manim animations.",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",