Someone is building a box of pastries on your bakery site. They tap "Add" next to a lemon tart, and a tiny number in the corner changes from 0 to 1. Plenty of people miss that, tap again, and end up with two tarts they did not want. This experiment makes the moment hard to miss: the item jumps into a small drawstring bag, the bag opens its mouth, gulps it down and wobbles with the extra weight. The count and total sit right underneath.
0 items · $0.00
How it works
- Measure the trip. When an Add button is pressed, the script finds the centre of that button and the mouth of the bag, both measured inside the demo, so it works wherever the two end up on the page.
- Fly along a curve. A gold token with the item's first letter is created and moved along a gentle arc with the Web Animations API. The arc is a simple curve worked out in 15 steps, and the token shrinks a little as it travels.
- Open, then gulp. While anything is in the air the bag holds its mouth open. When the token lands, it is removed, the bag squashes wide and springs back, and a soft gold lump sinks through it. All of that is a CSS animation on the SVG.
- Update the real numbers. The item count and the total are plain text under the bag, and a live region reads out what was added and the new total.
- Respect calm settings. With reduced motion turned on, nothing flies or wobbles. The numbers update straight away and get a short gold outline instead.
The code
Three short files, no libraries; the bag is a few lines of inline SVG.
<div class="gp" data-lab="gulp">
<ul class="gp-menu" aria-label="Today's pastries">
<li class="gp-item">
<span class="gp-name">Lemon tart</span>
<span class="gp-price">$4.25</span>
<button class="gp-add" type="button" data-name="Lemon tart" data-price="4.25" aria-label="Add lemon tart to your box">Add</button>
</li>
<li class="gp-item">
<span class="gp-name">Almond croissant</span>
<span class="gp-price">$3.75</span>
<button class="gp-add" type="button" data-name="Almond croissant" data-price="3.75" aria-label="Add almond croissant to your box">Add</button>
</li>
<li class="gp-item">
<span class="gp-name">Cardamom bun</span>
<span class="gp-price">$3.50</span>
<button class="gp-add" type="button" data-name="Cardamom bun" data-price="3.50" aria-label="Add cardamom bun to your box">Add</button>
</li>
</ul>
<div class="gp-box">
<svg class="gp-bag" viewBox="0 0 120 130" aria-hidden="true">
<g class="gp-sack">
<path class="gp-body" d="M30 32 Q34 46 38 52 Q6 64 10 92 Q16 124 60 124 Q104 124 110 92 Q114 64 82 52 Q86 46 90 32 Z"/>
<circle class="gp-lump" cx="60" cy="90" r="14"/>
<path class="gp-band" d="M34 52 Q60 60 86 52"/>
<path class="gp-tie" d="M80 56 Q92 62 88 76 M80 56 Q98 58 100 70"/>
<g class="gp-mouth"><ellipse cx="60" cy="32" rx="32" ry="8"/></g>
</g>
</svg>
<p class="gp-sum"><b class="gp-count">0</b> <span class="gp-unit">items</span> · <span class="gp-total">$0.00</span></p>
<button class="gp-clear" type="button">Empty the box</button>
</div>
<p class="gp-status" aria-live="polite"></p>
</div>.gp {
position: relative;
display: flex;
flex-wrap: wrap;
align-items: center;
justify-content: center;
gap: 18px 28px;
width: 100%;
max-width: 560px;
margin: 0 auto;
font-family: system-ui, sans-serif;
color: #101a14;
}
.gp-menu { flex: 1 1 260px; margin: 0; padding: 0; list-style: none; }
.gp-item {
display: flex;
align-items: center;
gap: 12px;
margin: 0;
padding: 12px 4px;
border-bottom: 1px solid #c9cec4;
}
.gp-item:last-child { border-bottom: 0; }
.gp-name { flex: 1; font-weight: 600; font-size: 16px; }
.gp-price { color: #46534a; font-size: 15px; font-variant-numeric: tabular-nums; }
.gp-add {
min-width: 64px;
min-height: 40px;
padding: 0 16px;
border: 0;
border-radius: 999px;
background: #133621;
color: #f4d96c;
font: 600 15px/1 system-ui, sans-serif;
cursor: pointer;
}
.gp-add:active { transform: scale(.96); }
.gp-add:focus-visible, .gp-clear:focus-visible { outline: 2px solid #133621; outline-offset: 3px; }
.gp-box { flex: 0 0 150px; display: grid; justify-items: center; gap: 4px; }
.gp-bag { display: block; width: 120px; height: 130px; overflow: visible; }
/* Each part scales around its own centre; the sack squashes from its base. */
.gp-sack, .gp-mouth, .gp-lump { transform-box: fill-box; }
.gp-sack { transform-origin: 50% 100%; }
.gp-body { fill: #133621; }
.gp-band, .gp-tie { fill: none; stroke: #f4d96c; stroke-width: 5; stroke-linecap: round; }
.gp-tie { stroke-width: 3; }
.gp-mouth { transform-origin: 50% 50%; transform: scaleY(.2); transition: transform .22s ease; }
.gp-mouth ellipse { fill: #101a14; stroke: #2f6b45; stroke-width: 3; }
.gp-lump { fill: #f4d96c; opacity: 0; transform-origin: 50% 50%; }
.gp-box.is-open .gp-mouth { transform: scaleY(1); }
/* The gulp: the sack bulges sideways, then wobbles back while the lump sinks. */
.gp-box.is-gulp .gp-sack { animation: gp-gulp .6s ease-out; }
.gp-box.is-gulp .gp-lump { animation: gp-sink .6s ease-in; }
@keyframes gp-gulp {
30% { transform: scale(1.2, .86); }
55% { transform: scale(.94, 1.08); }
78% { transform: scale(1.04, .97); }
}
@keyframes gp-sink {
0% { opacity: .55; transform: translateY(-30px) scale(1.1); }
100% { opacity: 0; transform: translateY(14px) scale(.6); }
}
.gp-token {
position: absolute;
left: 0;
top: 0;
width: 34px;
height: 34px;
margin: -17px 0 0 -17px;
border-radius: 50%;
background: #f4d96c;
box-shadow: 0 3px 8px rgba(16, 26, 20, .25);
color: #133621;
font: 700 15px/34px system-ui, sans-serif;
text-align: center;
pointer-events: none;
z-index: 2;
}
.gp-sum { margin: 0; font-size: 15px; color: #46534a; font-variant-numeric: tabular-nums; }
.gp-count { color: #133621; font-size: 18px; }
.gp-clear {
padding: 6px 8px;
border: 0;
background: none;
color: #133621;
font: 500 14px system-ui, sans-serif;
text-decoration: underline;
cursor: pointer;
}
.gp-status { flex: 1 0 100%; min-height: 2.8em; line-height: 1.4; margin: 0; text-align: center; font-size: 15px; font-weight: 600; color: #133621; }
@media (prefers-reduced-motion: reduce) {
.gp-mouth { transition: none; }
.gp-box.is-gulp .gp-sack, .gp-box.is-gulp .gp-lump { animation: none; }
.gp-box.is-gulp .gp-sum { outline: 2px solid #f4d96c; outline-offset: 3px; border-radius: 4px; }
}(function () {
var root = document.querySelector('[data-lab="gulp"]');
if (!root) return;
var box = root.querySelector('.gp-box');
var bag = root.querySelector('.gp-bag');
var countEl = root.querySelector('.gp-count');
var totalEl = root.querySelector('.gp-total');
var unitEl = root.querySelector('.gp-unit');
var status = root.querySelector('.gp-status');
var FLIGHT = 620; // milliseconds the item takes to reach the bag
var ARC = 70; // how high the item rises above its path, in pixels
var count = 0, total = 0, inFlight = 0;
var calm = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
// Centre of an element, measured from the top left of the demo.
function centre(el) {
var r = el.getBoundingClientRect(), o = root.getBoundingClientRect();
return { x: r.left - o.left + r.width / 2, y: r.top - o.top + r.height / 2 };
}
// Restart the gulp animation even if the last one has not finished.
function gulp() {
box.classList.remove('is-gulp');
void box.offsetWidth;
box.classList.add('is-gulp');
}
function show() {
countEl.textContent = count;
unitEl.textContent = count === 1 ? 'item' : 'items';
totalEl.textContent = '$' + total.toFixed(2);
}
function record(name, price) {
count += 1;
total += price;
show();
status.textContent = name + ' is in your box. ' + count + ' ' + unitEl.textContent + ', $' + total.toFixed(2) + '.';
}
function add(btn) {
var name = btn.getAttribute('data-name');
var price = parseFloat(btn.getAttribute('data-price'));
if (calm || !root.animate) { record(name, price); gulp(); return; }
// A gold token with the item's first letter flies along a curve to the bag.
var a = centre(btn), b = centre(bag);
b.y -= 38; // the mouth sits near the top of the bag
var peak = { x: (a.x + b.x) / 2, y: Math.min(a.y, b.y) - ARC };
var token = document.createElement('span');
token.className = 'gp-token';
token.setAttribute('aria-hidden', 'true');
token.textContent = name.charAt(0);
root.appendChild(token);
var frames = [], i, t, x, y;
for (i = 0; i <= 14; i++) {
t = i / 14;
x = (1 - t) * (1 - t) * a.x + 2 * (1 - t) * t * peak.x + t * t * b.x;
y = (1 - t) * (1 - t) * a.y + 2 * (1 - t) * t * peak.y + t * t * b.y;
frames.push({ transform: 'translate(' + x + 'px,' + y + 'px) scale(' + (1 - t * 0.55) + ')' });
}
inFlight += 1;
box.classList.add('is-open');
var anim = token.animate(frames, { duration: FLIGHT, easing: 'cubic-bezier(.45,0,.7,1)', fill: 'forwards' });
anim.onfinish = function () {
token.parentNode.removeChild(token);
inFlight -= 1;
if (!inFlight) box.classList.remove('is-open');
record(name, price);
gulp();
};
}
root.addEventListener('click', function (e) {
var btn = e.target.closest('.gp-add');
if (btn) add(btn);
});
root.querySelector('.gp-clear').addEventListener('click', function () {
count = 0;
total = 0;
show();
status.textContent = 'Your box is empty.';
});
})();When to use it, and when not to
It fits places where people add several small things in a row and where a bit of warmth suits the brand: a bakery box, a coffee bundle, a gift shop's "build a hamper", a pet treat sampler, a kids' party menu. The movement points the eye to where the order is kept, which helps on a long page where the cart is easy to lose track of.
It is a poor fit for serious or high value purchases, such as legal services, medical supplies or anything people compare carefully before they buy. There, a calm confirmation reads as more trustworthy than a bag that wobbles. It also gets tiring for big orders: if people regularly add twenty items, keep the flight short or skip it after the first few adds.
On accessibility, the effect is decoration on top of a normal button. Every button has a clear label, the bag is hidden from screen readers, and the text under it carries the real information. The flight is under a second, so it never blocks a fast tapper; several tokens can be in the air at once.
Make it yours
- Speed and height: change
FLIGHTfor how long the trip takes andARCfor how high the token rises. Around 500 to 700 milliseconds feels lively without slowing anyone down. - The bag: the shape is the
.gp-bodypath in the SVG, and the colours are the fills on.gp-body,.gp-bandand.gp-mouth ellipse. Swap the path for a box or a basket if that suits your shop better. - The gulp: the squash lives in the
gp-gulpkeyframes. Smaller numbers, such asscale(1.08, .94), give a gentler swallow.
Frequently asked questions
Can I connect it to my real cart?
Yes. Put your own add to cart call inside add(), before the animation starts, and update the count from your cart's response rather than the demo's local numbers.
Does it work on phones?
Yes. On a narrow screen the bag drops below the list and the token simply flies downward to it. The buttons are 40 pixels tall, which is comfortable to tap.
Will it slow my page down?
No. Nothing runs until someone presses Add, there is no animation loop, and each token is removed from the page as soon as it lands.