scrolly-video

repository·main·Indexed 22 days ago

https://github.com/dkaoster/scrolly-video

A component library for scroll-based or externally controlled video playback. It allows developers to tie video progress to scroll position across Vanilla JavaScript, React, Svelte, Vue, and Astro. The library supports multiple playback methods, including WebCodecs and Canvas for performance, as well as HTML5 Video fallbacks using playbackRate and currentTime.

Tokens
3.4K
Snippets
13
Records
16
Agent score
75%

What's inside scrolly-video

  1. Understand ScrollyVideo playback methods

    main

    ScrollyVideo uses three different methods to animate video based on browser support and scroll direction:

    1. WebCodecs and Canvas: The most performant method. It extracts frames using the WebCodecs API and draws them to a canvas. It is currently only available in Chrome and requires time to process frames on load.
    2. HTML5 Video and playbackRate: A fallback for browsers without WebCodecs. It modulates the playbackRate of an <video> tag. This is very smooth for forward scrolling but does not work for reverse scrolling because playbackRate cannot be negative.
    3. HTML5 Video and currentTime: The fallback for reverse scrolling or when other methods fail (e.g., Mobile Safari). It jumps to specific frames using currentTime. For best performance, videos should be encoded with keyframe = 1 to avoid quality drops or large file sizes.
  2. Install and use ScrollyVideo in Vue

    main

    Install the package via npm and import the .vue component.

    npm install scrolly-video --save
    import ScrollyVideo from 'scrolly-video/dist/ScrollyVideo.vue';
    
    <ScrollyVideo src="https://scrollyvideo.js.org/goldengate.mp4" />
  3. Install and use ScrollyVideo in React

    main

    Install the package via npm and import the component from the specific distribution files (.cjs.jsx or .esm.jsx).

    npm install scrolly-video --save
    import ScrollyVideo from 'scrolly-video/dist/ScrollyVideo.cjs.jsx';
    // or
    import ScrollyVideo from 'scrolly-video/dist/ScrollyVideo.esm.jsx';
    
    <ScrollyVideo src="https://scrollyvideo.js.org/goldengate.mp4" />
  4. Setup ScrollyVideo.js for Vanilla JavaScript

    main

    To use ScrollyVideo.js in a plain HTML/JS environment, add a container element and initialize the ScrollyVideo object via a CDN script. You must provide the scrollyVideoContainer ID and the video src.

    <div id="scrolly-video"></div>
    
    <script src="https://cdn.jsdelivr.net/npm/scrolly-video@latest/dist/scrolly-video.js"></script>
    <script type="text/javascript">
      new ScrollyVideo({
        scrollyVideoContainer: "scrolly-video",
        src: "https://scrollyvideo.js.org/goldengate.mp4"
      });
    </script>
  5. Install and use ScrollyVideo in Astro

    main

    Install the package via npm and import the .astro component in your frontmatter.

    npm install scrolly-video --save
    ---
    import ScrollyVideo from 'scrolly-video/dist/ScrollyVideo.astro';
    ---
    
    <ScrollyVideo src="https://scrollyvideo.js.org/goldengate.mp4" />
  6. Install and use ScrollyVideo in Svelte

    main

    Install the package via npm and import the .svelte component.

    npm install scrolly-video --save
    import ScrollyVideo from 'scrolly-video/dist/ScrollyVideo.svelte';
    
    <ScrollyVideo src="https://scrollyvideo.js.org/goldengate.mp4" />
  7. Manually control video progress with setVideoPercentage

    main

    The setVideoPercentage method allows you to manually set the currentTime of the video by passing a progress value between 0 and 1. If trackScroll is enabled, this method will also perform the scroll automatically. This is primarily used in React, Vue, and Svelte components.

    // Signature: (percentage: number, options: { transitionSpeed: number, (progress: number) => number }) => void
    
    scrollyVideo.setVideoPercentage(0.5, { transitionSpeed: 12, easing: d3.easeLinear });
  8. Configure ScrollyVideo options

    main

    The ScrollyVideo constructor accepts the following configuration options:

    OptionTypeDefaultDescription
    srcstringRequiredThe URL of the video source.
    scrollyVideoContainerstring or ElementRequiredThe DOM element or ID where the video will be rendered.
    coverbooleantrueWhether the video should "cover" (fill) the container.
    stickybooleantrueWhether the container should stick to the top of the viewport.
    fullbooleantrueWhether the container should expand to 100vh and 100vw.
    trackScrollbooleantrueWhether the object should automatically respond to window scroll events.
    lockScrollbooleantrueWhether to ignore human scroll while animating setVideoPercentage (when trackScroll is enabled).
    transitionSpeednumber8Speed of the transition between video points.
    frameThresholdnumber0.1The threshold (in seconds) to stop video animation.
    useWebCodecsbooleantrueWhether to attempt using the WebCodecs API for smoother frame decoding.
    onReadyfunction() => {}Callback invoked when the video is decoded and ready.
    onChangefunction() => {}Callback invoked when the video percentage changes.
    debugbooleanfalseWhether to print debug statistics to the console.
  9. Reference ScrollyVideo configuration options

    main

    The following options can be passed to the ScrollyVideo constructor or component props.

    | Parameter | Description | Values | Default |
    |:---|:---|:---|:---|
    | src | The URL of the video (required) | URL | |
    | scrollyVideoContainer | The DOM element of the container, only used for plain js | String / Element | |
    | transitionSpeed | Sets the maximum playbackRate for this video | Number | 8 |
    | frameThreshold | When to stop the video animation, in seconds | Number | 0.1 |
    | cover | Forces the video to cover in it's container | Boolean | true |
    | sticky | Whether the video should have `position: sticky` | Boolean | true |
    | full | Whether the video should take up the entire viewport | Boolean | true |
    | trackScroll | Whether this object should automatically respond to scroll | Boolean | true |
    | lockScroll | Whether it ignores human scroll while it runs `setVideoPercentage` with enabled `trackScroll` | Boolean | true |
    | useWebCodecs | Whether the library should use the webcodecs method, see below | Boolean | true |
    | videoPercentage | Manually specify the position of the video between 0..1, only used for react, vue, and svelte components | Number | |
    | onReady | The callback when it's ready to scroll | VoidFunction | |
    | onChange | The callback for video percentage change | VoidFunction | |
    | debug | Whether to log debug information | Boolean | false |
  10. Initialize ScrollyVideo in vanilla JavaScript

    main

    To create a responsive scrollable video, instantiate the ScrollyVideo class by passing a configuration object to the constructor. You must provide a src (video URL) and a scrollyVideoContainer (either a DOM element or the ID of a container element).

    import ScrollyVideo from './ScrollyVideo';
    
    const scrolly = new ScrollyVideo({
      src: 'path/to/video.mp4',
      scrollyVideoContainer: 'my-container-id',
      cover: true,
      sticky: true,
      full: true,
      trackScroll: true,
      onReady: () => console.log('Video is ready!'),
      onChange: (percent) => console.log('Current progress:', percent)
    });