Micro-interactions5 min read
Magnetic Cursor Attraction Button
Direct Definition & Architecture
A magnetic button calculates cursor proximity relative to the center of the bounding box. When the cursor enters a proximity threshold (e.g., 60px), the component pulls toward the pointer with damped spring force, releasing back to origin on mouse exit.
Live Physics Canvas
120 FPS GPU RenderStiffness (Tension)220
Damping (Friction)18
Technical Specifications
Bundle Footprint
< 2.1 kB gzipped
Event Overhead
Passive pointer listeners with RAF clamp
Input Devices
Pointer / Mouse (graceful touch fallback)
Output Code
Clean React JSX with hooks
State Machine Graph
RestOffset at (0, 0); zero energy dissipation.
AttractedDisplaced toward cursor proportional to dx * pullFactor.
ReboundingDamped spring return triggered on pointer exit.
Production Code Output
magnetic-button.jsx
import React, { useRef, useState } from 'react';
import { motion } from 'framer-motion';
export function MagneticButton({ children = "Explore Platform", pullFactor = 0.35 }) {
const ref = useRef(null);
const [position, setPosition] = useState({ x: 0, y: 0 });
const handlePointerMove = (e) => {
const { clientX, clientY } = e;
const { left, top, width, height } = ref.current.getBoundingClientRect();
const centerX = left + width / 2;
const centerY = top + height / 2;
const distanceX = (clientX - centerX) * pullFactor;
const distanceY = (clientY - centerY) * pullFactor;
setPosition({ x: distanceX, y: distanceY });
};
const handlePointerLeave = () => {
setPosition({ x: 0, y: 0 });
};
return (
<motion.button
ref={ref}
onPointerMove={handlePointerMove}
onPointerLeave={handlePointerLeave}
animate={{ x: position.x, y: position.y }}
transition={{ type: "spring", stiffness: 220, damping: 18, mass: 0.6 }}
className="px-8 py-4 rounded-2xl bg-stone-900 border border-violet-500/30 text-white font-medium shadow-2xl hover:border-violet-500 transition-colors"
>
{children}
</motion.button>
);
}Architectural Breakdown
Magnetic attraction guides user attention toward primary calls to action by physically responding to cursor approach.
ExodeUI computes distance delta vectors (dx, dy) and multiplies them by a pull factor (0.25 - 0.4) so the movement is subtle rather than jarring.
On pointer leave, the spring controller automatically returns the button to center coordinates with a natural oscillation.
Frequently Asked Questions
Explore Related Motion Guides & Tooling
Build your next living interface today.
Design fluid physics and visual logic in ExodeUI and export directly to React, Swift, and Flutter.