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 🙏

© 2026 – Pkg Stats / Ryan Hefner

@magic-spells/physics-engine

v1.0.0

Published

A physics-based engine for fun animations.

Downloads

63

Readme

@magic-spells/physics-engine

~1.1 KB gzipped

Spring physics engine for smooth, natural animations. Drives a numeric value from start to end using a spring-damper model, emitting position and progress on every frame.

🔍 Live Demo - See it in action!

Install

npm install @magic-spells/physics-engine
import PhysicsEngine from '@magic-spells/physics-engine';
<!-- CDN (UMD) — exposes window.PhysicsEngine -->
<script src="https://unpkg.com/@magic-spells/physics-engine/dist/physics-engine.min.js"></script>

Usage

const spring = new PhysicsEngine({ attraction: 0.026, friction: 0.28 });

spring.on('change', ({ position, progress }) => {
  element.style.transform = `translateX(${position}px)`;
});

await spring.animateTo(0, 400);

Chained animations with initial velocity:

const spring = new PhysicsEngine({ attraction: 0.02, friction: 0.14 });

spring.on('change', ({ position, progress }) => {
  element.style.opacity = progress;
  element.style.transform = `translateY(${position}px)`;
});

// Animate forward, then back with carry-over velocity
await spring.animateTo(0, 300, 100);
await spring.animateTo(300, 0, -50);

API reference

new PhysicsEngine({ attraction?, friction? })

Create a spring engine.

  • attraction number — Spring stiffness. Default 0.026. Must be in range (0, 1) exclusive.
  • friction number — Velocity damping. Default 0.28. Must be in range (0, 1) exclusive.

Throws if either value is outside the valid range.

animateTo(startValue, endValue, velocity?) -> Promise

Animate from startValue to endValue. Returns a Promise that resolves when the spring settles or stop() is called.

  • startValue number — Starting position.
  • endValue number — Target position.
  • velocity number — Initial velocity. Default 0.

If called while already animating, the previous animation is silently stopped and its Promise resolves.

stop()

Halts the current animation. Emits a stop event and resolves the pending Promise.

setAttraction(n)

Update attraction while running or idle. Throws if n is not in (0, 1).

setFriction(n)

Update friction while running or idle. Throws if n is not in (0, 1).

isAnimating

Boolean property. true while an animation is in progress.

Events

| Event | Payload | When | |-------|---------|------| | change | { position, progress } | Every animation frame | | complete | { position, progress } | Spring has settled at the target | | stop | { position } | stop() was called |

Register and remove listeners with on, off, and removeAllListeners:

function onChange({ position, progress }) { /* ... */ }

spring.on('change', onChange);
spring.off('change', onChange);
spring.removeAllListeners('change'); // remove all for one event
spring.removeAllListeners();         // remove all listeners

All three methods return the instance for chaining.

How it works

Each frame the engine computes a spring force ((target - current) * attraction), adds it to the velocity, damps the velocity by friction, and advances the position. The time delta is normalized to a 16.66ms baseline so the spring behaves consistently across different frame rates. The animation settles when both position and velocity are within 0.01 of the target.

License

MIT