Skip to content
animorio
IT
All components

Shape-snapping canvas

Drawing, Transitions, PhysicsCanvas, GSAPAdvancedCompressed weight: Demo 14 KBGSAP 28 KBFreeDownload the zip

A premium component for a drawing and whiteboard app: a dark frame holding a sheet of warm squared paper. Draw a wobbly loop, a box, a triangle or a line with a finger, keep the finger still for half a second and the pill at the top fills: the pencil stroke springs into the perfect shape, turns from graphite to blue, takes a pale fill, a halo that opens once, the four selection handles and its size in the pill. The line becomes a connector arrow.

The code

A folder that runs on its own: index.html, style.css, script.js and the libraries from a CDN. The React and Vue tabs hold the same demo as a ready component, which switches its animations off when it leaves the page.

<!doctype html>
<!--
  "Shape-snapping canvas" from Animorio · https://animorio.com
  License: free on your own sites and your clients’ sites, paid work included.
  Not for resale, redistribution, templates or code collections · https://animorio.com/en/license/
-->
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>The drawing canvas that turns sketches into perfect shapes</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <main class="stage">
    <!-- the canvas of a sketching app: draw a loop, a box or a line with a finger, keep the finger still for half a
         second and the wobbly stroke springs into a perfect circle, rectangle, triangle or straight line, takes the
         accent colour, gets its selection handles and its size in the pill at the top -->
    <div class="sk" data-wrap>
      <span class="sk__halo" aria-hidden="true"></span>
      <section class="sk__card" aria-label="Linea sketch canvas">
        <header class="sk__bar">
          <span class="sk__brand">
            <svg class="sk__logo" viewBox="0 0 24 24" aria-hidden="true">
              <rect x="1.5" y="1.5" width="21" height="21" rx="7" class="sk__logo-bg"/>
              <path d="M7 15.6c2.2-.2 3.3-1.6 4.2-3.6.9-2 2-3.6 5.8-3.8" class="sk__logo-line"/>
            </svg>
            <span class="sk__file">Onboarding flow<span class="sk__muted"> / Board 2</span></span>
          </span>
          <span class="sk__actions">
            <span class="sk__zoom" aria-hidden="true">100%</span>
            <button class="sk__icon-btn" type="button" data-undo aria-label="Undo last stroke">
              <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M9 7.5 5 11.5l4 4"/><path d="M5.5 11.5h8.2a4.8 4.8 0 0 1 0 9.6H11"/></svg>
            </button>
          </span>
        </header>

        <div class="sk__paper" data-paper>
          <canvas class="sk__canvas" data-canvas aria-hidden="true"></canvas>
          <div class="sk__pill" data-pill aria-hidden="true">
            <span class="sk__pill-in">
              <svg class="sk__pill-icon" viewBox="0 0 20 20">
                <g data-icon="hold"><path d="M4 13.5c1.8-.4 2.6-3.6 4.6-3.8 2-.2 2.4 3 4.4 2.6 1.4-.3 2-2 3-3.3"/></g>
                <g data-icon="circle"><circle cx="10" cy="10" r="6"/></g>
                <g data-icon="ellipse"><ellipse cx="10" cy="10" rx="7" ry="4.6"/></g>
                <g data-icon="rect"><rect x="3.5" y="5" width="13" height="10" rx="1"/></g>
                <g data-icon="tri"><path d="M10 3.8 16.6 15.6H3.4Z"/></g>
                <g data-icon="line"><path d="M4 15.5 16 4.5"/><path d="M10.5 4.5H16v5.5"/></g>
                <g data-icon="none"><path d="M4 12c2-4 4-4 6 0s4 4 6 0"/></g>
              </svg>
              <span class="sk__pill-text" data-pill-text>Hold to snap</span>
              <span class="sk__pill-bar"><span class="sk__pill-fill" data-pill-fill></span></span>
            </span>
          </div>
          <div class="sk__pencil" data-pencil aria-hidden="true">
            <div class="sk__pencil-art" data-pencil-art>
              <svg viewBox="0 0 44 44">
                <path class="sk__p-cap" d="M30.2 4.6 33 1.8a2.4 2.4 0 0 1 3.4 0l5.8 5.8a2.4 2.4 0 0 1 0 3.4l-2.8 2.8Z"/>
                <path class="sk__p-band" d="M28.4 6.4 30.2 4.6 39.4 13.8 37.6 15.6Z"/>
                <path class="sk__p-body" d="M28.4 6.4 37.6 15.6 14.8 38.4 5.6 29.2Z"/>
                <path class="sk__p-shine" d="M29.6 9.6 10.6 28.6"/>
                <path class="sk__p-wood" d="M5.6 29.2 14.8 38.4 3.2 42.8a1.6 1.6 0 0 1-2-2Z"/>
                <path class="sk__p-lead" d="M2.8 37.6 6.4 41.2 1.6 43a.6.6 0 0 1-.6-.6Z"/>
              </svg>
            </div>
          </div>
        </div>

        <footer class="sk__dock">
          <div class="sk__tools" role="radiogroup" aria-label="Tool">
            <span class="sk__tool-bg" data-tool-bg aria-hidden="true"></span>
            <button class="sk__tool is-on" type="button" role="radio" aria-checked="true" data-tool="pencil" aria-label="Pencil">
              <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M15.8 4.6l3.6 3.6L8.6 19 4 20l1-4.6Z"/><path d="M13.6 6.8l3.6 3.6"/></svg>
            </button>
            <button class="sk__tool" type="button" role="radio" aria-checked="false" data-tool="marker" aria-label="Marker">
              <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M14.5 4.5l5 5-7.6 7.6-5-5Z"/><path d="M6.9 12.1 4.6 17l2.4 2.4 4.9-2.3"/></svg>
            </button>
            <button class="sk__tool" type="button" role="radio" aria-checked="false" data-tool="fine" aria-label="Fine liner">
              <svg viewBox="0 0 24 24" aria-hidden="true"><path d="M18 3.8 20.2 6 9 17.2l-3.6 1.4 1.4-3.6Z"/></svg>
            </button>
          </div>
          <button class="sk__snap" type="button" data-snap aria-pressed="true">
            <span class="sk__snap-label">Snap</span>
            <span class="sk__switch" aria-hidden="true"><span class="sk__knob" data-knob></span></span>
          </button>
        </footer>
      </section>
    </div>
    <p class="sr" aria-live="polite" data-live></p>
  </main>
  <script src="https://cdn.jsdelivr.net/npm/gsap@3.15.0/dist/gsap.min.js"></script>
  <script src="script.js"></script>
</body>
</html>

Playground

Change values, colours and text with the controls and watch the animation respond live. Or write in the code yourself. Then download the folder with your changes.

23 values to change

Or ask your AI assistant for it

With the Animorio connector, Claude Code, ChatGPT, Cursor or VS Code take it from here, guide included, and fit it into your project. Paste this request:

Take “Shape-snapping canvas” from Animorio and fit it into my site
Platform licenseFree on your sites and your clients’ sites. Want it inside a website builder, a template platform or a client dashboard? There’s a license for agencies and platforms.

What it does

On a near black page with a soft blue light, the dark window of Linea, a made-up drawing app, rises: at the top the logo, ‘Onboarding flow / Board 2’, the ‘100%’ zoom and the undo key; in the middle a sheet of warm paper with a very faint grid and its own grain; at the bottom three tools (pencil, marker, fine liner) with the light tile sliding onto the chosen one, and the ‘Snap’ switch. A drawn pencil, with its shadow on the paper, traces a wobbly loop; it stops, and the ‘Hold to snap’ pill drops in at the top with a blue line filling up. Once full, the stroke springs into a perfect circle, passing the mark by a pixel and settling, turns blue, takes a pale fill and a halo that opens once; the selection box appears with its four handles, one after another, and the pill says ‘Circle · 147 pt’. Then a crooked box becomes a rectangle, and the line between the two becomes a straight arrow with its ‘Arrow · 120 pt · 0°’. After a moment everything fades and the loop starts again. With a finger you really draw: circles, ellipses, rectangles (tilted too, straightened when nearly level), triangles and lines; a stroke that is not a shape stays a sketch and the pill says ‘Kept as sketch’. Undo removes the last stroke, the tools change the width and Snap off leaves everything hand-drawn.

How it works

The still finger

Every move longer than 3.5 px records where and when the finger stopped; the GSAP ticker measures how long it has been still and, after 0.14 s, fills the pill's line. At HOLD the stroke is recognised.

Open or closed

If start and end are close and the stroke is at least nearly twice its box's diagonal, it is a loop: the part going back over the start is trimmed. Otherwise it is a line if the chord is at least 84% of the length, and the angle catches on the nearest 45°.

Count the corners, then try

The loop is split at the point farthest from the start and each half is simplified with Ramer-Douglas-Peucker; corners turning less than 24° are dropped. Three corners put forward a triangle, four a rectangle turned along its longest side (level when it nearly is); the circle or ellipse of the box is always in the running. Each candidate is drawn and scored by how far the stroke sits from it on average: the closest wins, and if even that one is past TOLERANCE the stroke stays a sketch.

Point by point

The stroke and the perfect shape are both resampled into 120 evenly spaced points, the shape starts from the point closest to the stroke's start and runs the same way (the sign of the area). One value k from 0 to 1, on an elastic.out, takes every point from the stroke to the shape: that is why the drawing straightens without sliding.

The finish on the canvas

Every stroke is an object with k, colour, fill, halo, selection and opacity moved by GSAP; the canvas is redrawn only when one of them changes. The handles swell one after another with a small bounce, the arrow opens its head with the fill.

Values to change

NameFileDefaultWhat it changes
HOLDscript.js0.55How many seconds to keep the finger still before the stroke becomes a shape.
TOLERANCEscript.js0.12How much wobble a stroke may have and still become a shape: higher forgives more.
SNAP_TIMEscript.js0.8How many seconds the stroke takes to spring into its shape.
BOUNCEscript.js1How springy it is: 0 settles flat, 2 wobbles.
INKscript.js2.6The pencil's width, in pixels.
SPEEDscript.js12 = the demo draws twice as fast, 0.5 = half.
AUTOscript.jstrueDraws by itself until someone touches the sheet.

Try them in the Playground

When to use it

For a digital whiteboard, a wireframe and diagram app, a handwritten notes tool for tablets, or the page that shows off a product's ‘draw it and it becomes a shape’ feature.

On phones

The same animation with adapted sizes: the window is as wide as the screen and at most 88% of its height tall, the demo's shapes lay out on the sheet's width and a finger draws just like the pencil.

With reduced motion

No moving pencil and no spring: the shapes appear already perfect, fading in one after another, with the pill changing its words; drawing by hand, the stroke fades into the shape.

Pitfalls

  • Morphing the stroke into the shape point by point only works if both start at the same point and run the same way: otherwise the circle twists on itself while it straightens.
  • Whoever closes a circle by hand always goes back over the start a little: if that bit is not trimmed, the perfect shape has to squeeze a loop and a quarter into one and the stroke slides.
  • Deciding with a single threshold (how many corners, or how round) goes wrong: a square is not much less round than a wobbly circle, and a long ellipse has four ‘corners’ for Douglas-Peucker. That is why the possible shapes compete and the one closest to the stroke wins.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0.

Compressed with gzip, the three demo files weigh 14 KB; the libraries: GSAP 28 KB.

Testing

Tested on 2 November 2026

  • Desktop: Done
  • Phone: Done
  • Reduced motion: Done
  • Safari on iPhone: To do

License

Use it in your own projects and in your clients’ projects. Do not resell it or redistribute it as is. GSAP stays under its own Standard license. Full license.

All components