webdevpuneet.com

Canvas Rope Physics — Free Verlet Integration Drag Toy Snippet

Canvas Rope Physics · Animations · Plain HTML, CSS & JS · Live preview

CategoryAnimations

Copy & paste

Source Code

<div class="rp-wrap">
  <canvas id="rpCanvas" class="rp-canvas"></canvas>
  <div class="rp-panel">
    <span class="rp-tag">verlet integration</span>
    <p>Drag any point along the rope. Release to let it swing.</p>
    <div class="rp-row">
      <label>Stiffness <span id="rpStiffVal">6</span></label>
      <input type="range" id="rpStiff" min="1" max="16" value="6" step="1" />
    </div>
    <button class="rp-reset" id="rpReset">Reset rope</button>
  </div>
</div>

Extend with AI

Take This Snippet Further With AI

The AI prompt for this one is for Patreon supporters ($3 a month), along with a site with no ads. Supporters enter the passcode once and it is remembered in this browser.

· Become a supporter

What's included

Features

Verlet integration
Position-based physics with no explicit velocity storage.
Iterative constraint solving
Stick distance constraints resolved multiple times per frame.
Adjustable stiffness
A slider controls iteration count live.
Pinned endpoints
Both rope ends stay fixed while the middle swings freely.
Drag-and-release
Grab any segment; release for natural swing-back.
Floor collision
Points are clamped above the canvas bottom edge.
Touch support
Full touchstart/move/end handling alongside mouse.
DPR-aware rendering
Crisp glowing rope on high-density screens.

About this UI Snippet

Canvas Rope Physics — A Verlet Rope With No Physics Library

Screenshot of the Canvas Rope Physics snippet rendered live

This snippet simulates a hanging, draggable rope entirely with the Canvas 2D API and a from-scratch implementation of Verlet integration — the same class of technique used in cloth and rope simulations in games, minus any external physics engine.

Position-based dynamics instead of force-based velocity

Each rope point stores its current position and its *previous* position, not an explicit velocity. Every frame, updatePoints() computes an implicit velocity as (x - px), applies friction to it, and projects the point forward by that amount plus gravity. This is Verlet integration: it's numerically stable and trivially easy to constrain, which is exactly why it's the standard technique for rope, cloth, and soft-body demos rather than a spring-force model.

Stiffness comes from iteration count, not a spring constant

Every stick between two adjacent points is a rigid-distance constraint: satisfyConstraints() measures the current distance between its two points, compares it to the stick's rest length, and nudges both points toward the correct separation. Running that correction pass multiple times per frame — controlled by the Stiffness slider, which maps directly to iteration count — is what makes the rope feel taut rather than stretchy. Few iterations produce a soft, elastic-feeling rope; many iterations converge closer to a rigid rope, all using the exact same constraint code.

Pinned points versus free points

The first and last points are flagged pinned and are skipped in both the integration and constraint-correction steps, anchoring the rope's ends in place while every interior point is free to swing under gravity and be pulled taut by its neighbors.

Dragging without breaking the simulation

While a point is being dragged, it's flagged dragging and both its position and previous position are set to the pointer position every move event — this is what prevents the rope from "flinging" the point once you release it, since with matching previous/current positions its implicit velocity is zero at release.

Pair this with canvas fireworks physics or canvas metaball blobs for other from-scratch canvas physics demos.

Step by step

How to Use

  1. 1
    Paste HTML, CSS, and JSA rope renders, pinned at both ends, sagging under gravity.
  2. 2
    Drag any pointClick or touch near a rope segment and drag it around.
  3. 3
    ReleaseThe point swings freely, pulled taut by its neighboring constraints.
  4. 4
    Adjust stiffnessThe slider changes constraint-solving iterations per frame.
  5. 5
    Low stiffnessThe rope feels soft and elastic, stretching visibly.
  6. 6
    High stiffnessThe rope feels closer to rigid, holding its length tightly.
  7. 7
    ResetThe Reset rope button rebuilds the simulation from scratch.

Real-world uses

Common Use Cases

Physics and game dev reference
Provide a compact example of Verlet integration, where position-based physics stores no explicit velocity at all.
Interactive hero elements
Add a playful draggable rope above a headline, with both ends pinned so the middle sags and swings naturally.
Constraint-solving lessons
Teach how stick constraints resolved several times per frame produce stiffness, using a live slider for the iteration count.
Loading and idle toys
Provide a tactile distraction while content loads, running from scratch on the Canvas 2D API with no physics library.
Particle effect pairing
Combine with canvas fireworks physics in a creative agency showcase of from-scratch canvas simulations without libraries.
Related: Corner Peel Hover Reveal Card
See the Corner Peel Hover Reveal Card for a related animations pattern worth pairing with this one.
Related: Vanilla SVG Icon Morph (No Library)
See the Vanilla SVG Icon Morph (No Library) for a related animations pattern worth pairing with this one.

Got questions?

Frequently Asked Questions

Verlet integration derives velocity implicitly from the difference between a point's current and previous position, rather than storing velocity as its own variable. It is numerically stable and makes distance constraints trivial to apply directly to position, which is why it is the standard technique behind most rope and cloth simulations rather than a force-and-velocity spring model.

It changes how many times per frame the distance-constraint correction pass runs over every stick in the rope. Each pass nudges connected points toward the correct rest length; running it once produces a soft, stretchy rope, while running it many times converges the rope's segments closer to their exact rest length, reading as stiffer and more rope-like.

The first and last points are flagged pinned and are explicitly skipped in both the position-update step and the constraint-correction step. Every other point in the rope is fully free to move, so gravity and dragging only ever affect the interior of the rope, exactly like a rope hung between two fixed hooks.

While dragging, both the point's current position and its previous position are set to the pointer's position on every move event. Since Verlet's implicit velocity is the difference between those two values, keeping them equal means the point has zero velocity at the moment of release, so it starts swinging from rest instead of inheriting a large flick velocity.

Yes — the underlying technique is the same. Extend the points array into a 2D grid instead of a line, add horizontal, vertical, and optionally diagonal stick constraints between neighboring grid points, and pin whichever row or points you want fixed. The integration and constraint-solving functions need no changes; only the topology of points and sticks differs between a rope and a cloth.