Sync Lottie animation with cursor movement
mainTo sync animation with the cursor, set mode to "cursor". You can use the position property to map cursor coordinates to animation frames.
- Diagonal Sync: Map
x: [0, 1]andy: [0, 1]to aseekaction to complete the animation as the cursor moves from top-left to bottom-right. - Horizontal/Vertical Sync: Map only one axis (e.g.,
x: [0, 1]) to sync movement along that axis. - Hover Effects: Use
position: { x: [0, 1], y: [0, 1] }with alooptype to play a segment while the cursor is inside, and aposition: { x: -1, y: -1 }with astoptype to halt it when the cursor leaves.
// Example: Play segments on hover
const PlaySegmentsOnHover = () => {
const lottieObj = useLottie(options, style);
const Animation = useLottieInteractivity({
lottieObj,
mode: "cursor",
actions: [
{
position: { x: [0, 1], y: [0, 1] },
type: "loop",
frames: [45, 60],
},
{
position: { x: -1, y: -1 },
type: "stop",
frames: [45],
},
],
});
return Animation;
};