BSMNT Scrollytelling Documentation

repository·main·Indexed 23 days ago

https://github.com/basementstudio/scrollytelling

A React library that abstracts GSAP ScrollTrigger to provide a component-based approach to scroll-driven animations. It shifts the animation model from time-based durations to position-based progress using start and end points. The library includes components such as Root, Animation, Waypoint, Parallax, Pin, and Stagger, as well as hooks like useScrollytelling and useScrollToLabel to manage complex scroll-linked animations and React lifecycle cleanup automatically.

Tokens
11.7K
Snippets
14
Records
64
Agent score
81%

What's inside BSMNT Scrollytelling

  1. What is BSMNT Scrollytelling

    main

    BSMNT Scrollytelling is a React library designed for creating scrollytelling animations. It is powered by GSAP ScrollTrigger but provides a component-based abstraction that simplifies the development process.

    Key features include:

    • Sensible Defaults: Automatically applies settings like scrub: true and ease: 'linear'.
    • Lifecycle Management: Handles component mounting and unmounting (including animation cleanup) automatically.
    • Position-based Animations: Allows defining animations using start and end scroll progress values instead of time-based duration.
    • React Compatibility: Designed to work with React Server Components (using 'use client' internally) and supports animation composition via React Context.
  2. Understand Tweens in Scrollytelling

    main

    In this library, a Tween is the mechanism used by the Animation component and its variants (such as Pin and Parallax) to handle property animations.

    While the library follows the GSAP definition of a Tween (a high-performance property setter that calculates values based on a playhead), it implements a specific variation: instead of using a fixed duration, the library uses start and end props. These props determine the animation timing based on the scrolling progress within a <Pin /> component.

  3. Core concepts of BSMNT Scrollytelling

    main

    BSMNT Scrollytelling is a React-friendly abstraction over GSAP ScrollTrigger. It shifts the mental model of scroll animations from time-based durations to position-based progress defined by a start and an end point.

    Key features include:

    • Sensible Defaults: Automatically applies scrub: true and ease: 'linear' for smooth scroll-linked animations.
    • Lifecycle Management: Handles component mounting and unmounting (cleanup) automatically to prevent memory leaks or broken animations in React.
    • Component-Based Composition: Uses React Context to allow animations to be composed at any level of the component tree. While components use 'use client', their parents and children do not necessarily need to.
    • Position-based Animation: Instead of defining how long an animation lasts in seconds, you define where it starts and ends relative to the scroll position.
  4. Debug animations with the Scrollytelling Visualizer

    main

    The Scrollytelling Visualizer is a debugging tool used to inspect and understand created animations. It provides a visual representation of how animations are positioned within a timeline and displays their current states.

    Key features include:

    • Draggable Panel: The visualizer is contained within a panel that can be moved around the screen.
    • Multi-timeline Support: It can handle multiple GSAP timelines, which can be toggled using a built-in dropdown menu.
    • Integration: To enable the visualizer, use the debug prop on the <Root> component.
  5. How to use BSMNT Scrollytelling components

    main

    To use the library, wrap your content in a Scrollytelling.Root component. Inside the root, use Scrollytelling.Animation to define specific scroll-driven movements.

    Animations are controlled via a tween prop. You can provide a single tween object or an array of tween objects to sequence animations based on scroll progress (from 0 to 100).

    • Single Tween: Use tween={{ start: number, end: number, from?: object, to?: object }}.
    • Sequenced Tweens: Use an array tween={[{ start: number, end: number, to: object }, ...]}.

    This approach replaces the manual GSAP useEffect and timeline.revert() pattern with a declarative component structure.

    import * as Scrollytelling from "@bsmnt/scrollytelling";
    
    const Component = () => {
      return (
        <Scrollytelling.Root>
          <div className="container">
            {/* Single animation from 0% to 30% scroll progress */}
            <Scrollytelling.Animation
              tween={{ start: 0, end: 30, from: { opacity: 0, scale: 0.9 } }}
            >
              <h1 className="title">Hello World</h1>
            </Scrollytelling.Animation>
    
            {/* Sequenced animations: 30-80% and 80-100% scroll progress */}
            <Scrollytelling.Animation
              tween=[
                { start: 30, end: 80, to: { rotate: 360 } },
                { start: 80, end: 100, to: { y: 100 } },
              ]
            >
              <div className="box" />
            </Scrollytelling.Animation>
          </div>
        </Scrollytelling.Root>
      );
    };
  6. Register GSAP plugins with `<RegisterGsapPlugins>`

    main

    If your scrollytelling implementation requires specific GSAP plugins (such as CSSRulePlugin or TextPlugin), you must register them using the <RegisterGsapPlugins> component. This component should wrap your <Scrollytelling.Root> component to ensure the plugins are available within the scrollytelling context.

    import * as Scrollytelling from "@bsmnt/scrollytelling";
    import { CSSRulePlugin } from "gsap/dist/CSSRulePlugin";
    import { TextPlugin } from "gsap/dist/TextPlugin";
    
    export const BaboldComp = () => {
      return (
        <>
          <Scrollytelling.RegisterGsapPlugins plugins={[CSSRulePlugin, TextPlugin]}>
            <Scrollytelling.Root scrub={0.75}>
              <div className={s["section"]}>
                {/* ... */}
              </div>
            </Scrollytelling.Root>
          </Scrollytelling.RegisterGsapPlugins>
        </>
      );
    };
  7. Understand the ImageSequenceCanvas supportTable

    main

    The ImageSequenceCanvas component performs feature detection to determine if the browser supports modern image formats. This information is passed to your getFrameSrc function via a SupportTable object.

    SupportTable Structure:

    {
      supportsWebp: boolean;
      supportsAvif: boolean;
    }

    You should use this table to return the most optimized image format available (e.g., preferring .avif over .webp, and .webp over .jpg) to reduce bandwidth and improve performance.