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 🙏

© 2024 – Pkg Stats / Ryan Hefner

svelte-cannon

v0.8.0

Published

Declarative Svelte components for the cannon-es physics engine

Downloads

252

Readme

Svelte Cannon

Use Svelte components to build a cannon-es physics simulation.

Installation

npm install svelte-cannon   # or   yarn add svelte-cannon

Usage

This example uses svelte-cubed to display the objects, but svelte-cannon is renderer engine agnostic.

<script lang="ts">
  import * as CANNON from "cannon-es";
  import * as PE from "svelte-cannon";
  import * as THREE from "three";
  import * as SC from "svelte-cubed";

  let position = new CANNON.Vec3(0, 4, 0);
</script>

<PE.World gravity={[0, -9.81, 0]}>
  <PE.Body mass={1} bind:position>
    <PE.Sphere radius={1} />
  </PE.Body>
</PE.World>

<SC.Canvas>
  <SC.PerspectiveCamera position={[0, 2, 10]} />
  <SC.Mesh
    position={[position.x, position.y, position.z]}
    geometry={new THREE.SphereGeometry(1)}
  />
</SC.Canvas>

Using the import * as PE is helpfull to avoid naming conflicts, three, cannon-es and svelte-cannon all export a Sphere for example. (PE is short for Physics Engine)

Generally you'd want the nest the SC.Canvas inside the PE.World, because that allows subcomponents to create both the physics engine and the render engine components.

Shorthand notations

Allowed values for setting a 3D vector are:

  • [1, 2, 3]
  • new CANNON.Vec3(1, 2, 3)
  • new THREE.Vector3(1, 2, 3)
  • {x: 1, y: 2, z: 3}

This allows for reusing existing variables and reduces boilerplate.

Two way binding caveats

  1. bind:property= for 3D vectors only works when an CANNON.Vec3 object is passed.

This restriction allows svelte-cannon to detect which properties you're interested in. Due to the nature of physics engines a lot of properties could change every on frame, recalculating the velocity of the ground plane would be wasteful.

  1. When the body is awake bind:property= will trigger updates, but the value might not have changed.

Syncing position the THREE.Mesh is a fast operation. $: mesh.position.copy(position); ( CANNON.Vec3 is compatible with THREE.Vector3 ) Creating shadow values, recalculating and checking and for changes would be overhead.

Imprecise stores

writableVec3 creates store, but when you're setting the value with a CANNON.Vec3 that has roughly the same value it won't trigger an update.

<script>
  import * as PE from "svelte-cannon";
  import * as SC from "svelte-cubed";

  const position = PE.writableVec3(0, 4, 0);
  position.precision = 0.001; // 1mm (default)
</script>

<PE.Body bind:position={$position} />
<SC.Mesh position={$position.toArray()} />

This allows you to prevent resending values to the renderer that haven't changed.

From a usage perspective it acts as a writable(new Vec3(0, 4, 0)) but also allows shorthand notations:

  • position.set(1, 2, 3)
  • position.set(new CANNON.Vec3(1, 2, 3))
  • $position = new CANNON.Vec3(1, 2, 3)
  • $position = new THREE.Vector3(1, 2, 3)
  • $position = {x: 1, y: 2, z: 3}
  • $position = [1, 2, 3]
  • $position.x = 1; $position.x = 2; $position.x = 3;

Forces and Constraints

As bodies can have multiple constrains and forces can affect multiple bodies it doesn't translate well to a component hierarchy. HTML also has this problem with <input>s and <label>s, svelte-cannon approach is based on the id= and for= solution:

<PE.Body id="anchor" position={[0, 3, 0]} />
<PE.Body id="ball" mass={1} bind:position>...</PE.Body>

<PE.Spring forA="anchor" forB="ball" stiffness={50} />

Note: These id's not related the numerical id's of the cannon-es Bodies.

Contributing

Setup

yarn install
yarn dev

Linting

yarn lint  # or  npm run lint

Build

yarn package  # or  npm run package