Back to Explore
THE PLAYGROUND · GESTURES

Gesture-driven Rotation

Drag across a surface to turn an object.

Gesture-driven RotationGestures · Drag across a surface to turn an object.
DRAG
N
DRAG TO INTERACT · 600 ms
1import { useState } from "react";2import { motion } from "motion/react";3 4const surface = { width: 100, height: 100, borderRadius: 16, background: "#b8ed79", display: "grid", placeItems: "center", color: "#141714" };5 6export default function GestureRotateDemo() {7  const [dismissed, setDismissed] = useState(false);8  const [rotation, setRotation] = useState(0);9  return <div style={{width: 320, overflow: "hidden"}}>10    <button onClick={() => {setDismissed(false); setRotation(0);}}>Reset</button>11    {!dismissed && <motion.div style={surface} drag="x" dragConstraints={{left: 0, right: 0, top: 0, bottom: 0}} dragElastic={0.25} animate={{rotate: rotation}} transition={{ type: "spring", stiffness: 180, damping: 14, mass: 1 }}12      onDrag={(_, info) => setRotation(v => v + info.delta.x * 1)}13      onDragEnd={(_, info) => {  }}>Drag me</motion.div>}14    15  </div>;16}

Install: npm install motion. Translate horizontal drag distance into rotation. This compact example illustrates the core technique; the styled playground contains additional presentation details.