Skip to main content
Introducing packages.sweber.dev
All posts
Release1 min read

Lagrangian 0.4.0: polygons and a Svelte adapter

Bodies in the world can now be any convex polygon, from triangles to hexagons and arrows. Svelte gets its own small adapter next to React.

By Seya Weber

Lagrangian 0.4.0 is out under the MIT licence: @sweberdev/lagrangian and @sweberdev/lagrangian-react, plus the new @sweberdev/lagrangian-svelte. The Vue adapter follows as soon as npm lets us publish the package.

What is new

  • Polygons. Give a body vertices and it becomes any convex shape: triangles, hexagons, arrows, shards. Polygons turn around their centre of mass, get mass and inertia from the outline, collide with circles, boxes and each other, and tip over, stack and slide with the same friction as the other bodies. regularPolygon(sides, radius) builds the common ones. A concave outline collides as its convex hull.
  • Svelte. @sweberdev/lagrangian-svelte offers actions (use:spring, use:drag, use:scroll, use:physics, use:body) and a spring store.
  • The package is now about 12 kB min+gzip for everything, still tree-shakeable, with the size checked in CI.

A small example

import { regularPolygon, World } from "@sweberdev/lagrangian";

const world = new World({ bounds: box }).start();
world.add({
  x: 400, y: 40,
  vertices: regularPolygon(6, 36),
  restitution: 0.4,
  element: hex,
});
<aside use:spring={{ props: { x: open ? 0 : -320 }, options: { bounce: 0.15 } }}>…</aside>

Try it

The world guide covers polygons, and there is a guide for Svelte. Lagrangian Pro works with this release.

Package in this post

Lagrangian

Animation 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
View package

Related posts

Release

Lagrangian 0.1.0 released

Animation with real physics: springs that keep their momentum when you interrupt them, throws that land on snap points, and a small 2D world with gravity, collisions and rolling. About 7 kB.

Lagrangian