Skip to content
animorio
IT
Back to the library

Candy add to cart

Physics, HoverGSAPMediumCompressed weight: Demo 8.0 KBGSAP 28 KBFreeDownload the zip

The product card of a made-up sweet shop, Perle & Fig: a sea-salt caramel wrapped in cellophane on a pale tile, and next to it the paper bag already half full. Add to cart unwraps it: the two cellophane ends snap open and flutter away, the bare caramel arcs into the bag, bounces on the pile with real gravity, the bag gives on the impact, folds shut and the bag count rolls. Hand-written physics. Minimal style, a single accent.

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>
<!--
  "Candy add to cart" 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>Candy add to cart</title>
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/mona-sans@5.3.0/wdth.css">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fontsource-variable/jetbrains-mono@5.3.0/index.css">
  <link rel="stylesheet" href="style.css">
</head>
<body>
  <!-- The product card of a made-up sweet shop, Perle & Fig. Add to cart unwraps the caramel: the two twisted ends
       snap open and flutter away, the bare caramel flies in an arc into the paper bag and bounces on the pile with
       real gravity, the bag squashes, folds shut and the count in the corner rolls. -->
  <main class="stage">
    <article class="shop" data-shop aria-label="Sea-salt caramel, Perle and Fig">
      <header class="shop__head">
        <p class="brand">Perle &amp; Fig <span>Confectioners</span></p>
        <p class="bagcount" aria-live="polite">
          <svg viewBox="0 0 16 16" aria-hidden="true"><path d="M3.5 5.5h9l-.8 8h-7.4z" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linejoin="round"/><path d="M6 5.5V4.6a2 2 0 0 1 4 0v.9" fill="none" stroke="currentColor" stroke-width="1.3" stroke-linecap="round"/></svg>
          <span class="bagcount__label">Bag</span>
          <span class="count" data-count><span class="count__roll" data-roll><span>0</span><span>1</span><span>2</span><span>3</span><span>4</span><span>5</span><span>6</span><span>7</span><span>8</span><span>9</span></span></span>
        </p>
      </header>

      <div class="scene" data-scene>
        <div class="tile" data-tile>
          <span class="tile__glow" aria-hidden="true"></span>
          <span class="tile__shadow" data-shadow aria-hidden="true"></span>
        </div>

        <div class="bag" data-bag aria-hidden="true">
          <span class="bag__shadow" data-bag-shadow></span>
          <div class="bag__body" data-bag-body>
            <svg class="bag__back" data-squash viewBox="0 0 100 120" preserveAspectRatio="none">
              <path d="M8 6h84l-2 112H10z" fill="var(--kraft-dark)"/>
              <path d="M8 6h84l-1 10H9z" fill="var(--kraft-deep)"/>
            </svg>
            <div class="bag__layer bag__layer--pile" data-squash>
              <span class="pile pile--a"></span>
              <span class="pile pile--b"></span>
              <span class="pile pile--c"></span>
            </div>
            <div class="bag__layer bag__layer--front" data-squash>
            <svg class="bag__front" viewBox="0 0 100 120" preserveAspectRatio="none">
              <path d="M4 18h92l-3 102H7z" fill="var(--kraft)"/>
              <path d="M4 18h92v3.2H4z" fill="var(--kraft-light)"/>
              <path d="M17 21 15 118" stroke="var(--kraft-crease)" stroke-width=".7" fill="none"/>
              <path d="M83 21l2 97" stroke="var(--kraft-crease)" stroke-width=".7" fill="none"/>
              <path d="M7 104h86" stroke="var(--kraft-crease)" stroke-width=".5" fill="none" opacity=".7"/>
            </svg>
            <span class="bag__label"><span>P&amp;F</span></span>
            </div>
            <div class="bag__layer bag__layer--fold" data-squash>
              <span class="bag__fold" data-fold></span>
            </div>
          </div>
        </div>

        <div class="candy" data-candy aria-hidden="true">
          <svg viewBox="0 0 60 40">
            <defs>
              <radialGradient id="caramel" cx="38%" cy="32%" r="75%">
                <stop offset="0" stop-color="var(--candy-light)"/>
                <stop offset=".55" stop-color="var(--accent)"/>
                <stop offset="1" stop-color="var(--candy-deep)"/>
              </radialGradient>
            </defs>
            <ellipse cx="30" cy="20" rx="22" ry="15.5" fill="url(#caramel)"/>
            <path d="M14 14c5-6 18-8 27-3" stroke="#fff" stroke-opacity=".55" stroke-width="2.2" fill="none" stroke-linecap="round"/>
            <ellipse cx="30" cy="20" rx="22" ry="15.5" fill="none" stroke="var(--candy-deep)" stroke-opacity=".35" stroke-width=".8"/>
            <g class="candy__cello" data-cello>
              <ellipse cx="30" cy="20" rx="23.5" ry="17" fill="#fff" fill-opacity=".14" stroke="#fff" stroke-opacity=".75" stroke-width=".7"/>
              <path d="M20 7.5c3-1 7-1.2 10-1" stroke="#fff" stroke-opacity=".9" stroke-width="1.1" fill="none" stroke-linecap="round"/>
            </g>
          </svg>
        </div>
        <div class="wrap wrap--l" data-wrap aria-hidden="true">
          <svg viewBox="0 0 30 40"><path d="M29 15 3 3l4 17-4 17 26-12z" fill="#fff" fill-opacity=".5" stroke="#fff" stroke-width=".8" stroke-linejoin="round"/><path d="M28 17 8 9M28 20H6M28 23 8 31" stroke="var(--cello-line)" stroke-width=".6" fill="none"/></svg>
        </div>
        <div class="wrap wrap--r" data-wrap aria-hidden="true">
          <svg viewBox="0 0 30 40"><path d="M1 15l26-12-4 17 4 17L1 25z" fill="#fff" fill-opacity=".5" stroke="#fff" stroke-width=".8" stroke-linejoin="round"/><path d="M2 17l20-8M2 20h22M2 23l20 8" stroke="var(--cello-line)" stroke-width=".6" fill="none"/></svg>
        </div>
      </div>

      <div class="info">
        <div>
          <h1 class="info__name">Sea-salt caramel</h1>
          <p class="info__meta">Hand-pulled, butter &amp; flaked salt</p>
        </div>
        <p class="info__price">$1.20<span> each</span></p>
      </div>

      <button class="pill" type="button" data-add data-autoclic>
        <span class="pill__fill" data-fill aria-hidden="true"></span>
        <span class="pill__label" data-label-add>Add to cart</span>
        <span class="pill__label pill__label--done" data-label-done aria-hidden="true">
          <svg viewBox="0 0 16 16"><path d="M3.5 8.4 6.6 11.3 12.6 4.9" pathLength="1" data-tick/></svg>
          Added
        </span>
      </button>
    </article>
  </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.

43 values to change

Works with Claude, Claude Code, ChatGPT, Cursor, VS Code, Windsurf, Codex, Gemini CLI

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 “Candy add to cart” 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 warm white page, the Perle & Fig card: at the top the brand and the bag with its number in a black circle, then a pale tile with the sea-salt caramel wrapped up, its two cellophane ends twisted, and next to it the kraft paper bag with the pile of caramels peeking out of the mouth; below, the name, the price and the Add to cart pill. On hover the pill’s label widens and its background swells; pressed, it squeezes. On click the cellophane opens: the ends jump off to the sides and flutter down as they fade; the caramel hops, flies in an arc towards the bag spinning, with its shadow on the tile shrinking away, and drops on the pile: it bounces two or three times, lower each time, the bag squashes on the impact and springs back, and the pill turns into Added with the check drawing itself. Then the top of the bag folds over and closes it, the bag jolts, the number rolls from 2 to 3 and the circle gives a small hop. After a moment the bag opens again, the pill goes back to Add to cart and a new caramel drops onto the tile. When nobody touches, the card adds one by itself.

How it works

The throw

The caramel is an element above the scene, moved with x and y. On click the ballistic throw that takes it to its spot in the bag in FLIGHT seconds is worked out: with gravity g, vx = dx / T and vy = (dy − g·T²/2) / T. From there it is just integration in the GSAP ticker, with the real time between frames: vy grows by g·dt, the position by v·dt.

The bounces

When it reaches the pile the vertical speed flips sign and shrinks by BOUNCE; the horizontal one drops a lot, and a small spring keeps it in the middle of the mouth. Under a certain speed it stops, and its rotation settles on the nearest side with an elastic ease.

The bag

The bag is made of layers: the back, the pile, the caramel, the front and the fold. The container itself is never transformed (otherwise the caramel could not sit between the layers): each layer squashes on its own from the bottom, on a damped spring that every impact kicks.

The cellophane

The two ends are separate elements attached to the caramel’s sides. On click they break off with a speed outwards and upwards, then fall with little gravity, a lot of drag and a push that swings left and right: so they flutter instead of dropping like stones, and fade away.

The pill and the number

The pill’s two labels sit out of the flow, centred over the whole pill, so the hidden one never widens anything. The number is a column of digits inside the circle, which moves up by one height for each caramel with back.out.

Values to change

NameFileDefaultWhat it changes
GRAVITYscript.js2600How strong gravity is, in pixels per second squared.
FLIGHTscript.js0.46How long the arc into the bag lasts, in seconds: longer means higher.
BOUNCEscript.js0.42How much the caramel bounces on the pile.
SPINscript.js520How fast the caramel turns in the air, in degrees per second.
SQUASHscript.js0.11How much the bag gives on the impact.
FLUTTERscript.js1How much the cellophane ends flutter as they fall (0 straight down).
HOLDscript.js1.3How long the bag stays shut before it opens again, in seconds.
AUTO_PILOTscript.js2.6After how many still seconds the card adds a caramel by itself (0 never).
--accentstyle.css#c9592bThe single accent colour: the caramel.

Try them in the Playground

When to use it

On the product page of a sweet shop, a confectioner, a pastry shop, a coffee roaster or any shop selling small things by the piece: wherever adding to cart should give a little extra pleasure.

On phones

The card is sized on the width and the scene on the height too (vh); the caramel takes its size from the tile. Same throw, same physics, by touch.

With reduced motion

No flight and no bounces: the caramel fades out, the pill fades to Added with the check already drawn, the bag closes at once, the number changes without rolling and the new caramel fades in.

Pitfalls

  • An element with a transform creates its own stacking context: if the bag container is squashed, the caramel can no longer sit between the back and the front. The layers are squashed one by one, and the bag’s entrance clears its transform at the end (clearProps).
  • The throw aims at the landing point, not above it: aimed higher, the caramel keeps drifting sideways as it falls and lands off centre.
  • The time between two frames is capped at a thirtieth of a second: after a tab left in the background gravity would throw the caramel out of the scene.
  • The shadow on the tile also follows the caramel’s opacity, otherwise it stays behind alone when the caramel fades.

Libraries

GSAP 3.15.0, Mona Sans (font variabile) 5.3.0, JetBrains Mono (font variabile) 5.3.0.

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

Testing

Tested on 28 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 animations