Skip to main content

JavaScript animation example

JavaScript Button Animation: Magnetic Button With Confetti

Magnetic buttons were made popular by award-winning agency sites. As the cursor approaches, the button leans toward it, and the label inside moves even more, which adds depth. Click it and confetti bursts out with gravity and spin. Everything springs back when the pointer leaves.

  • 4.3 KB file
  • 5 live inputs
  • JavaScript runtime
Live, try it

How this magnetic confetti button works

  • Within the attraction radius, the button offset is a fraction of the pointer offset, smoothed by a spring.
  • The label uses a larger fraction than the button, creating a parallax between them.
  • Confetti pieces are rotated rectangles with gravity and drag; "click" fires on press.

Add it to your JavaScript app

Install exodeui-vanilla, download the file below, and paste this. The code updates as you customize. Other runtimes are in the other tabs.

import { ExodeUICanvas } from 'exodeui-vanilla';

// <canvas id="javascript-magnetic-button" style="width: 100%; height: 440px"></canvas>
const player = new ExodeUICanvas({
  element: '#javascript-magnetic-button',
  fit: 'Layout',
  onReady: (engine) => { },
  onTrigger: (name) => {
    // fires: 'click'
  },
});
player.load(await (await fetch('/javascript-magnetic-button.exode')).json());
player.play();

Inputs

InputTypeDefaultRange
labelTextClaim reward—
strengthNumber0.350 – 0.8
accentText#111827hex colour
accent2Text#f59e0bhex colour
bgText#f5f5f4hex colour

Events: click

More JavaScript animations

The same idea in other frameworks: React liquid fill button · Flutter speed dial fab · Flutter animated login form