Cannon.js

~34 kB min+gz

Add realistic 3D physics — gravity, collisions, and constraints — to your web projects.


01

See It in Action

Real 3D physics in the browser — objects that fall, collide, and swing like the real world.

Spheres with different sizes fall onto a ground plane and bounce off each other — basic rigid body physics.

Preview

02

What is Cannon.js?

Cannon.js is a JavaScript library that adds 3D physics to your web pages. It simulates gravity, collisions, and forces so objects in your 3D scenes behave like they would in the real world — they fall, bounce, stack, and tumble.

The original cannon.js by Stefan Hedman is no longer maintained. The recommended fork is cannon-es (by pmndrs), which adds TypeScript support and ES module imports. The latest version is 0.20.0, published in 2022.

🎲
3D Rigid Body Physics
Simulate spheres, boxes, cylinders, and custom shapes falling, bouncing, and colliding in full 3D space.
🔗
Constraints & Joints
Connect objects with hinges, ropes, and locks to build chains, doors, ragdolls, and mechanical assemblies.
🎮
Three.js Integration
Pairs naturally with Three.js — Cannon.js moves the physics bodies, Three.js draws them on screen.

03

When to Use Cannon.js

Use when
3D browser games with realistic object physics
Interactive product viewers where objects react to clicks and drags
Physics-based 3D animations on landing pages
Educational simulations demonstrating gravity, momentum, or collisions
Architectural walkthroughs with movable furniture and objects
Avoid when
2D-only projects — use Matter.js instead, it is simpler and lighter
Soft body, cloth, or fluid simulations — Cannon.js only handles rigid bodies
Performance-critical projects — consider Rapier (WASM-based, significantly faster)

04

How Cannon.js Compares

Cannon.js vs Matter.js

Cannon.js is for 3D physics. Matter.js is for 2D physics. Pick based on whether your project is 2D or 3D.

Cannon.js wins at
3D physics, Three.js integration, 3D game development
Matter.js wins at
2D physics, simpler API, built-in renderer, smaller bundle
View Full Comparison →
Cannon.js vs Ammo.js

Ammo.js is a port of the Bullet physics engine — more features (soft bodies, vehicles) but much larger and harder to learn. Both are legacy compared to Rapier. Cannon-es is the lighter, easier option.

Cannon.js wins at
Smaller bundle, cleaner JavaScript API, faster setup
Ammo.js wins at
Soft body physics, vehicle simulation, Bullet engine compatibility
View Full Comparison →
Cannon.js vs Rapier

Rapier is the modern choice — WASM-based, significantly faster with SIMD support, and actively maintained (v0.30.0 in 2025). For new projects, Rapier is usually the better pick. Cannon-es has more tutorials and a gentler learning curve.

Cannon.js wins at
More tutorials, simpler API, gentler learning curve
Rapier wins at
Much faster (WASM + SIMD), active development, deterministic simulation
View Full Comparison →

05

Get Started with Cannon.js

Terminal
npm install cannon-es
Terminal
yarn add cannon-es
HTML
<script src="https://cdn.jsdelivr.net/gh/KLA6/[email protected]/cannon-es.umd.js"></script>
Quick Start
import * as CANNON from "cannon-es";

// Create a physics world with gravity
const world = new CANNON.World({
  gravity: new CANNON.Vec3(0, -9.82, 0)
});

// Add a sphere that will fall
const sphere = new CANNON.Body({
  mass: 5,
  shape: new CANNON.Sphere(1),
  position: new CANNON.Vec3(0, 10, 0)
});
world.addBody(sphere);

// Add a static ground plane
const ground = new CANNON.Body({
  mass: 0,  // mass 0 = static, won't move
  shape: new CANNON.Plane()
});
ground.quaternion.setFromEuler(-Math.PI / 2, 0, 0);
world.addBody(ground);

// Run the simulation
function animate() {
  requestAnimationFrame(animate);
  world.fixedStep();  // advance physics by 1/60s
  console.log(sphere.position.y);  // watch it fall!
}
animate();

06

Related Libraries