npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2025 – Pkg Stats / Ryan Hefner

@magic-spells/scroll-velocity

v0.1.0

Published

High-performance scroll velocity tracker with physics-based friction and CSS variable output for velocity-driven animations.

Readme

Scroll Velocity Tracker

High-performance scroll velocity tracker with physics-based friction and CSS variable output for velocity-driven animations.

Live Demo

Features

  • High-performance tracking using requestAnimationFrame (no scroll event performance issues)
  • 🎯 Physics-based simulation with configurable dampening, friction, and attraction to zero
  • 🕵️‍♂️ Smart sampling modes - delta-based for punchy feel or time-based for consistent velocity
  • 🌊 Multiple CSS variables exposed: normalized, absolute, power-curve, and raw velocity values
  • 🌐 Framework agnostic — works with any frontend framework or vanilla JS
  • 📦 Lightweight & zero dependencies - Optimized for performance
  • 🔧 Simple API with start/stop controls and runtime option updates
  • Accessibility-aware - respects prefers-reduced-motion preference

Installation

npm install @magic-spells/scroll-velocity
// Import the class in your JS entry point
import { ScrollVelocity } from '@magic-spells/scroll-velocity';

Or include directly via CDN:

<script src="https://unpkg.com/@magic-spells/scroll-velocity"></script>

Usage

Create and start the velocity tracker:

import { ScrollVelocity } from '@magic-spells/scroll-velocity';

const tracker = new ScrollVelocity({
  target: document.body,       // element that receives CSS variables
  sampleMode: 'delta',         // 'delta' for punchy feel, 'time' for consistent velocity
  dampening: 0.35,             // higher = snappier response to velocity changes
  friction: 0.95,              // velocity persistence per frame (0-1)
  attraction: 0.96,            // pull toward zero each frame (0-1)
  threshold: 0.02,             // stop when velocity drops below this
  maxVelocity: 200,            // clamp raw velocity, used for normalization
  writeCSSVariables: true,     // set to false for programmatic-only usage
  respectReducedMotion: true   // disable when user prefers reduced motion
});

tracker.start();

API

Constructor Options

All options are optional with sensible defaults:

  • target (HTMLElement) - Element to receive CSS variables (default: document.body)
  • sampleMode ('delta'|'time') - Sampling method; 'delta' for punchy old-school feel, 'time' for consistent px/ms
  • dampening (number) - Blend factor toward target velocity; higher = snappier (default: 0.35)
  • friction (number) - Velocity decay per frame, 0-1 (default: 0.92)
  • attraction (number) - Pull toward zero per frame, 0-1 (default: 0.96)
  • threshold (number) - Stop threshold for absolute velocity (default: 0.02)
  • maxVelocity (number) - Clamp for raw velocity, used for normalization (default: 200)
  • writeCSSVariables (boolean) - Whether to write CSS custom properties (default: true)
  • respectReducedMotion (boolean) - Disable when user prefers reduced motion (default: true)

Public Methods

  • start() - Begin tracking scroll velocity
  • stop() - Stop tracking and reset velocity to zero
  • getVelocity() - Returns current raw velocity (signed number)
  • getNormalizedVelocity() - Returns velocity normalized to [-1, 1] range
  • setOptions(options) - Update configuration without recreating instance

CSS Variables

The tracker exposes these CSS custom properties on the target element:

  • --scroll-velocity — Normalized velocity between -1 and 1
  • --scroll-velocity-abs — Absolute value of normalized velocity (0 to 1)
  • --scroll-velocity-pow — Power-curve transformed absolute velocity for enhanced mid-range perception
  • --scroll-velocity-raw — Raw velocity value in pixels (clamped by maxVelocity)

Use these properties to drive CSS animations:

.velocity-element {
  transform: translateX(calc(var(--scroll-velocity) * 100px));
  filter: blur(calc(var(--scroll-velocity-abs) * 5px));
  background-color: hsl(0 100% calc(50% + var(--scroll-velocity-pow) * 30%));
}

.skew-on-scroll {
  transform: skewX(calc(var(--scroll-velocity) * 15deg));
}

.scale-with-speed {
  transform: scale(calc(1 + var(--scroll-velocity-abs) * 0.5));
}

How It Works

  • Smart Sampling: Choose between delta-based sampling (punchy, immediate) or time-based sampling (consistent velocity regardless of scroll event frequency)
  • Physics Simulation: Applies dampening to chase velocity peaks, then friction and attraction-to-zero for natural decay
  • Performance Optimized: Uses requestAnimationFrame for smooth updates only when velocity is non-zero
  • Accessibility: Automatically disables velocity when user has prefers-reduced-motion: reduce set
  • CSS-Driven Animations: Exposes multiple velocity representations as CSS variables for GPU-accelerated animations

The physics simulation creates natural-feeling velocity curves that start responsive and decay smoothly to zero.


Integration Example

const tracker = new ScrollVelocity({
  maxVelocity: 150,
  dampening: 0.4
});

tracker.start();

// Read velocity programmatically
function onAnimationFrame() {
  const velocity = tracker.getVelocity();
  const normalized = tracker.getNormalizedVelocity();
  
  console.log('Raw velocity:', velocity);
  console.log('Normalized velocity:', normalized);
  
  requestAnimationFrame(onAnimationFrame);
}
requestAnimationFrame(onAnimationFrame);

// Update settings at runtime
tracker.setOptions({
  dampening: 0.5,
  friction: 0.95
});

Browser Support

Supports all modern browsers with requestAnimationFrame and matchMedia:

  • Chrome 10+
  • Firefox 4+
  • Safari 6+
  • Edge 12+

License

MIT


Repository & Issues

https://github.com/magic-spells/scroll-velocity

Report bugs and request features via GitHub issues.