Skip to main content
Introducing packages.sweber.dev
All packages
StableMIT + Pro

LagrangianAnimation with real physics: springs that keep their momentum when interrupted, throws with friction onto snap points, and a small 2D world with gravity, collisions, joints and rolling. About 12 kB.

pnpm add @sweberdev/lagrangian

Most web animation is a curve over a fixed duration. Click twice and the element stops dead and starts again; throw it and it ignores how fast you threw. Lagrangian animates with physics instead. Its springs are the exact solution of the damped oscillator, so they know their position and velocity at every moment and simply continue when the target changes, at any frame rate.

Drag an element and Lagrangian measures how fast you let go, lets it glide with friction like iOS scrolling, rubber-bands past the edges and lands exactly on the nearest snap point. For playful interfaces there is a small 2D world: round, rectangular and polygonal bodies with gravity, restitution and Coulomb friction, so they bounce, slide and roll, plus springs, hinges, rods and motors between bodies, sensors for drop zones and a pointer joint for grabbing and throwing. When the user asks for reduced motion, automatic motion jumps to its end.

Example

A short exampleCopy it into your project.

app/drawer.tsx

import { draggable, World } from "@sweberdev/lagrangian"
import { useSpringProps } from "@sweberdev/lagrangian-react"

// Springs that keep their momentum when the state flips mid-flight
const panel = useSpringProps<HTMLElement>({ x: open ? 0 : -320 }, { bounce: 0.2 })

// Throw a sheet onto snap points
draggable(sheet, { axis: "y", snap: { y: [0, 240, 480] } })

// Drop things that bounce and roll
const world = new World({ bounds: box }).start()
world.add({ x: 120, y: 0, radius: 24, restitution: 0.8, element: ball })

Features

What you getFree under MIT, with Pro on top.

Free, MIT

  • Springs solved in closed form, exact at any frame rate
  • Interruptible animation that keeps its velocity
  • Dragging and scrolling with inertia, rubber-banding and snap points
  • 2D physics world with joints and sensors
  • React hooks; about 12 kB in total

Pro

  • Everything in Free
  • Bottom sheet, side drawer, modal, toast and popover
  • Swipe stack, carousel, pull to refresh and stack navigation
  • Sortable lists and boards with keyboard support
  • Jelly, ropes and impact sounds
  • Ready-made recipes and a spring editor
All features in detail

Free, MIT

  • Springs solved in closed form: exact at any frame rate, with duration and bounce or stiffness, damping and mass
  • Interrupt anything: every property keeps its velocity, so a new target continues the motion
  • animate() for transforms, opacity and CSS custom properties, with delay and stagger
  • draggable(): velocity from a least-squares fit, friction like iOS scrolling, rubber-banding past the bounds
  • Snap points the throw lands on exactly, by adjusting the glide instead of jumping
  • scroller(): a scroll area with inertia, rubber-banding, snap points, wheel and keyboard on the same values as your own animations
  • A 2D world: gravity, air drag, collisions between circles, rotating boxes and convex polygons with restitution, Coulomb friction and rolling
  • Reproducible runs: fixed steps, seed and world.random(), advance(), snapshot() and restore(), and a hash to compare two runs, the same at 60 and 144 Hz
  • Springs between bodies, hinges, rods and motors for pendulums, chains and wheels, a pointer joint to grab and throw, every body sleeps on its own when it rests
  • Sensors that report bodies entering and leaving a zone, for drop targets and triggers
  • rk4() and system() for your own equations of motion
  • Reduced motion respected: automatic motion jumps to where it would rest
  • React hooks: useSpringProps, useSpring, useDraggable, useWorld, useBody, plus adapters for Vue and Svelte
  • About 12 kB min+gzip for everything and tree-shakeable down to 2 kB, checked in CI

Pro

  • Bottom sheet with detents: lands where the throw would carry it, rubber-bands at the top, closes on a flick, Escape or a tap on the backdrop
  • Swipe stack: cards tilt around the grip point, fly off at the thrown speed, come back with undo, stamps driven by a CSS variable
  • Sortable lists: drag to reorder with springs, neighbours make room, works with items of different heights
  • Keyboard and screen reader support for sorting: Space, arrow keys, Escape, announcements you can translate
  • Layout transitions for any DOM change, also through React state, and a second change mid-flight keeps its velocity
  • Jelly: squash and stretch from the real velocity, area-preserving, wobbles back when it stops or hits something
  • Ropes, chains and garlands with Verlet integration: grab them, swing them, hang elements from them
  • Impact sounds synthesized with Web Audio for a World: loudness from speed, pitch from size, five materials
  • Stack navigation: pages slide over each other, swipe back from the left edge, shared elements fly between pages, focus and screen reader handling
  • A referee for gestures: a carousel in a drawer, a sheet around a carousel and the swipe back decide who gets the finger, and a part at its limit hands the swipe outwards
  • React hooks for every piece, tested with React 18 and 19 and Strict Mode, plus Vue composables and Svelte actions
  • Pull to refresh: resistance, threshold, held while loading, springs back with its momentum
  • Carousel: a throw carries as far as its speed says, rubber-banded ends, arrow keys and ARIA roles
  • Side drawer from the left or right: draggable, the throw decides, focus goes in and back out
  • Modal dialog with a spring transition, focus trap, scroll lock, Escape and backdrop
  • Toast stack: springs in at the edge, neighbours make room, swipe to throw away, timers pause on hover and focus
  • Sliding indicator for tabs and segmented controls that keeps its velocity when the selection changes
  • Page effect: a section falls apart under gravity, piles up, can be blown away and springs back into the layout
  • Board: drag to rearrange a wrapping grid or the columns of a kanban board, with keyboard and screen reader support
  • Zoom and pan for images and maps: pinch, wheel, double tap and drag with inertia and rubber-banding
  • Popover anchored to its trigger: springs out of the facing side, flips when there is no room, follows scrolling, Escape and outside click
  • Sortable lists scroll the page or their parent while you hold an item near the edge
  • Ready-made recipes: a form in a bottom sheet that guards unsaved changes and the keyboard, a photo gallery that opens from its thumbnail, swipeable list rows with actions
  • Spring editor (beta): sliders, curve, preview and copy-ready code for tuning a spring by eye
  • Reduced motion respected everywhere
FeatureFreePro
Springs, throws, snap points, dragging and scroll areasIncludedIncluded
2D world with gravity, collisions, joints, sensors and rollingIncludedIncluded
React hooks for the coreIncludedIncluded
Bottom sheet with detentsNot includedIncluded
Swipe stack with undoNot includedIncluded
Sortable lists with keyboard supportNot includedIncluded
Pull to refresh, carousel, drawer and dialogNot includedIncluded
Toast stack, popover, tab indicator and page effectsNot includedIncluded
Stack navigation with swipe back and shared elementsNot includedIncluded
Gesture referee for nested drawers, sheets and carouselsNot includedIncluded
Board (grid and kanban) and zoomNot includedIncluded
Layout transitionsNot includedIncluded
Jelly squash and stretchNot includedIncluded
Ropes and chainsNot includedIncluded
Impact soundsNot includedIncluded
LicenceMITCommercial

Pro

Pro packagesAdd-ons on top of the core.

Live demo

Try the themes, browse the catalog and read a real scanner report. The demo also shows the GTM container check, the monitoring of many client sites and the catalog changes per project.

  • @weber-development/lagrangian-pro

    Bottom sheet, drawer, dialog, carousel, stack navigation, pull to refresh, toasts, tab indicator, page effects, board, zoom, swipe stack, sortable lists, layout transitions, jelly, ropes and impact sounds, ready-made recipes and a spring editor, framework-free.

  • @weber-development/lagrangian-pro-react

    React hooks for every piece, for React 18 and 19.

  • @weber-development/lagrangian-pro-vue

    Vue 3 composables for every piece.

  • @weber-development/lagrangian-pro-svelte

    Svelte actions for every piece, for Svelte 3, 4 and 5.

Pricing

PricingPrices are in CHF.

Freelancer

For one developer building interfaces for clients.

CHF 120

per year

2 months free

  • 1 person
  • No support entitlement
Subscribe

Agency

Recommended

For teams of up to ten named people, one price.

CHF 390

per year

2 months free

  • Up to 10 people
  • Support included
Subscribe

Lifetime

Pay once for up to ten people and keep updates and repository access.

CHF 1’290

one-time

  • Up to 10 people
  • Support included
Buy lifetime licence

11 people or more: get in touch

  • After cancelling, every version you already received keeps working. Only updates and repository access end.
  • Your own customers never need a licence for websites you build with Lagrangian Pro.
  • Agency and Lifetime cover up to ten named people for one price: you choose the number of seats at checkout and assign them in Polar.
  • Checkout, invoices and VAT are handled by Polar as merchant of record.
Read the licence terms

FAQ

QuestionsShort answers.

How is this different from Sigmoid?

Sigmoid ties motion to the scroll position with native CSS scroll timelines and turns springs into CSS easing curves. Lagrangian runs physics in real time: interruptible springs, gestures, throws, gravity and collisions. They work well together.

Why not Motion or GSAP?

Both are excellent and do far more, such as layout animations and timelines. Lagrangian focuses on the physics: exact springs, throws that land on snap points and a world with collisions and rolling, in about 12 kB.

Is it a game physics engine?

No. Bodies are circles, rotating boxes and convex polygons with hinges, rods and motors, which keeps the engine small. It is meant for playful interfaces. For concave shapes and thousands of bodies use Matter.js or Rapier.

What happens with reduced motion?

Springs and throws jump to where they would come to rest, so the layout ends up the same. Dragging and the world still follow the pointer, because the user is moving them.

What does Lagrangian Pro add?

Finished interface pieces built on the free physics: a bottom sheet, a side drawer, a dialog, a carousel, pull to refresh, a toast stack, a tab indicator, a page effect that lets a section fall apart, a drag-and-drop board for grids and columns, zoom and pan, a swipe stack, sortable lists, layout transitions, jelly deformation, ropes and impact sounds, with React hooks. The free library stays free and keeps everything it has today.

Do my customers need a licence?

No. You can use Lagrangian Pro in any number of websites and apps you build. Your customers only need a licence if they use it outside the project you delivered.

Is sorting accessible?

Yes. Items can be picked up with Space, moved with the arrow keys and dropped with Space or Enter, and every step is announced to screen readers. You can translate the announcements.