Magnetix.js
Magnetix is a lightweight, composable motion engine for the web. SVG path animation, magnetic interactions, timeline control, and rich text effects all unified under one clean API.
Installation
Import Magnetix directly via ES module. No build step required.
import Magnetix from "./src/index.js"; // Or destructure what you need const { Easings, Timeline, svgMotion } = Magnetix;
Magnetix exports a single default object with all features. Every feature returns a motion object that integrates with the Timeline.
Core Concepts
Three ideas power everything in Magnetix. Understand these and the rest clicks instantly.
// Step 1: Create motions (they don't run yet) const m1 = Magnetix.svgMotion(".box", { ... }); const m2 = Magnetix.magnetic(".circle"); // Step 2: Add to timeline const timeline = new Magnetix.Timeline(); timeline.add(m1, m2); // Step 3: Control everything at once timeline.play(); timeline.pause(); timeline.seek(0.5); // jump to 50%
Magnetic
Attract elements toward the cursor with a configurable pull radius and spring physics. Great for buttons, icons, and interactive CTAs.
const motion = Magnetix.magnetic(".circle", { strength: 0.5, radius: 100 });
| Option | Type | Description |
|---|---|---|
selector |
string | CSS selector for the target element |
strength |
number | Pull force magnitude (default: 0.5) |
radius |
number | Mouse proximity threshold in px (default: 100) |
SVG Motion
Animate any element along a complex bezier path. Define multi-segment cubic curves with per-segment easing and transforms.
const motion = Magnetix.svgMotion(".box", { duration: 12000, path: [ { from: [0, 0], p1: [300, -200], p2: [600, 200], to: [300, 400] }, { from: [300, 400], p1: [0, 600], p2: [-300, 200], to: [0, 0] }, ], transform: { rotate: { value: 720, easing: Easings.easeInOutQuad }, scale: { value: 1.8, easing: Easings.easeInOutQuad }, } });
| Option | Type | Description |
|---|---|---|
duration |
number | Total animation duration in ms |
path |
array | Array of bezier segments. Each has from, p1, p2,
to as [x, y]
|
transform.rotate |
object | { value, easing , unit } — total rotation default(deg) |
transform.scale |
object | { value, easing } — scale multiplier |
repeat |
number | Number of times the animation repeats (default: 0) |
yoyo |
boolean | Plays the animation forward and then in reverse on each repeat (default: false) |
Split Text
Explode text into individual characters, words, or lines and animate each independently with staggered delays and custom transforms.
const motion = Magnetix.splitText(".title", "chars", { from: 0, to: 100, easing: Easings.easeInOutCubic, delay: 100, // stagger delay between chars (ms) duration: 1200, transform: { scale: { value: 2, easing: Easings.easeOutBack }, rotate: { value: 360, easing: Easings.easeInOutCubic } } });
| Option | Type | Description |
|---|---|---|
mode |
string | "chars" / "words" / "lines" |
delay |
number | Stagger delay between each unit in ms |
duration |
number | Duration per unit animation in ms |
transform |
object | Scale, rotate, translateY per character |
repeat |
number | Number of times the animation repeats (default: 0) |
yoyo |
boolean | Plays the animation forward and then in reverse on each repeat (default: false) |
Morph
Smoothly transition between SVG path shapes. Ideal for blob backgrounds, logo transitions, and icon animations.
const motion = Magnetix.morph(".shape", { from: [ { x: 20, y: 20 }, { x: 80, y: 20 }, { x: 50, y: 80 } ], to: [ { x: 30, y: 10 }, { x: 90, y: 50 }, { x: 20, y: 90 } ], pointsCount: 3 });
| Option | Type | Description |
|---|---|---|
from/to |
array | array of points |
delay |
number | Stagger delay between each unit in ms |
duration |
number | Duration per unit animation in ms |
pointsCount |
number | number of points used in the shape |
repeat |
number | Number of times the animation repeats (default: 0) |
yoyo |
boolean | Plays the animation forward and then in reverse on each repeat (default: false) |
Cursor Follow
Attach a custom cursor element that follows the mouse with configurable lag, creating a silky trailing effect.
const motion = Magnetix.cursor(".cursor", { speed: 0.09 // lower = more lag (0.01 – 1.0) });
| Option | Type | Description |
|---|---|---|
speed |
number | Lerp factor per frame. Lower = more trailing. Range: 0.01 – 1.0 |
Drag
Make any element freely draggable with inertia. Elements snap naturally with momentum on release.
const motion = Magnetix.drag(".drag", { speed: 1 });
Parallax
Bind element movement to scroll position at different speeds, creating a depth illusion with multiple layers.
import { parallax } from "./features/parallax/parallex.js"; parallax(".layer-1", { speed: 0.2 }); // slowest parallax(".layer-2", { speed: 0.5 }); // mid parallax(".layer-3", { speed: 0.9 }); // fastest
Reveal
Animate elements into view as they enter the viewport on scroll. Uses IntersectionObserver under the hood.
import reveal from "./features/reveal/reveal.js"; reveal(".section", { threshold: 0.2, // % of element visible before trigger duration: 800, from: { opacity: 0, y: 40 }, easing: Easings.easeOutCubic, });
| Option | Type | Description |
|---|---|---|
threshold |
number | 0 – 1, how much of element must be visible |
duration |
number | Animation duration in ms |
from |
object | Starting CSS state: opacity, x, y, scale, rotate |
easing |
function | Any Easings function default(Linear) |
transform |
object |
Controls element transformations: scale, opacity,
x, y, rotate.Default units: px for position, deg for rotation.
|
Timeline
The Timeline is your conductor. It collects all motions and gives you a single interface to play, pause, and scrub through everything simultaneously.
const timeline = new Magnetix.Timeline(); timeline.add( svgAnimation, // svgMotion magneticAnimation, // magnetic splitTextAnimation, // splitText morphAnimation, // morph cursorAnimation, // cursor dragAnimation, // drag ); timeline.play(); // Seek to any point (0.0 → 1.0) $$('seekBtn').addEventListener('click', () => timeline.seek(0.5)); $$('pauseBtn').addEventListener('click', () => timeline.pause()); $$('playBtn').addEventListener('click', () => timeline.play());
Easings
A full library of easing functions for fine-tuning animation feel. Pass any easing as a function reference.
const { Easings } = Magnetix; // Linear Easings.linear // Polynomial Easings.easeInQuad Easings.easeOutQuad Easings.easeInOutQuad Easings.easeInCubic Easings.easeOutCubic Easings.easeInOutCubic Easings.easeInQuart Easings.easeOutQuart Easings.easeInOutQuart // Special Easings.easeInElastic Easings.easeOutElastic Easings.easeInOutElastic Easings.easeInBack Easings.easeOutBack Easings.easeInOutBack Easings.easeInBounce Easings.easeOutBounce Easings.easeInOutBounce
API Reference
Complete method signatures for every Magnetix feature.
| Param | Type | Required | Description |
|---|---|---|---|
selector |
string | ✓ | CSS selector |
config.duration |
number | ✓ | ms |
config.path |
Segment[] | ✓ | Array of bezier segments |
config.transform |
object | — | { rotate, scale } |
| Param | Type | Required | Description |
|---|---|---|---|
selector |
string | ✓ | CSS selector |
mode |
string | ✓ | "chars" | "words" | "lines" |
config.duration |
number | ✓ | ms per unit |
config.delay |
number | — | stagger ms between units |
config.easing |
function | — | Easings.* |
config.transform |
object | — | scale, rotate, y offset |
| Param | Type | Required | Description |
|---|---|---|---|
selector |
string | ✓ | CSS selector |
options.strength |
number | — | Pull force 0.0–1.0 |
options.radius |
number | — | Trigger radius in px |
| Param | Type | Required | Description |
|---|---|---|---|
selector |
string | ✓ | CSS selector for cursor element |
options.speed |
number | — | Lerp factor 0.01–1.0 |
| Method | Signature | Description |
|---|---|---|
.add() |
add(...motions) | Register one or more motion objects |
.play() |
play() → void | Start or resume all motions |
.pause() |
pause() → void | Pause all motions |
.seek() |
seek(t: 0–1) → void | Jump to position in normalized time |
Best Practices
Hard-won advice for building animations that feel professional.