UI Library for Design Engineers. Animated components and effects you can copy and paste into your apps. Free. Open Source.
Code parth/smooth cursor (#647)
Dillion Vermad9041d1dbd73
diff --git a/__registry__/index.tsx b/__registry__/index.tsxindex 923c5270..65dafe3c 100644--- a/__registry__/index.tsx+++ b/__registry__/index.tsx@@ -225,6 +225,30 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "smooth-cursor": {+ name: "smooth-cursor",+ description:+ "A customizable, physics-based smooth cursor animation component with spring animations and rotation effects",+ type: "registry:ui",+ registryDependencies: undefined,+ files: [+ {+ path: "registry/magicui/smooth-cursor.tsx",+ type: "registry:ui",+ target: "",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import("@/registry/magicui/smooth-cursor.tsx");+ const exportName =+ Object.keys(mod).find(+ (key) =>+ typeof mod[key] === "function" || typeof mod[key] === "object",+ ) || item.name;+ return { default: mod.default || mod[exportName] };+ }),+ meta: undefined,+ },"neon-gradient-card": {name: "neon-gradient-card",description: "A beautiful neon card effect",@@ -1855,6 +1879,29 @@ export const Index: Record<string, any> = {}),meta: undefined,},+ "smooth-cursor-demo": {+ name: "smooth-cursor-demo",+ description: "Basic smooth cursor example",+ type: "registry:example",+ registryDependencies: ["https://magicui.design/r/smooth-cursor"],+ files: [+ {+ path: "registry/example/smooth-cursor-demo.tsx",+ type: "registry:example",+ target: "components/smooth-cursor-demo.tsx",+ },+ ],+ component: React.lazy(async () => {+ const mod = await import("@/registry/example/smooth-cursor-demo.tsx");+ const exportName =+ Object.keys(mod).find(+ (key) =>+ typeof mod[key] === "function" || typeof mod[key] === "object",+ ) || item.name;+ return { default: mod.default || mod[exportName] };+ }),+ meta: undefined,+ },"neon-gradient-card-demo": {name: "neon-gradient-card-demo",description: "Example showing a beautiful neon card effect.",diff --git a/config/docs.ts b/config/docs.tsindex c1834582..09d8cc39 100644--- a/config/docs.ts+++ b/config/docs.ts@@ -246,6 +246,12 @@ export const docsConfig: DocsConfig = {items: [],label: "",},+ {+ title: "Smooth Cursor",+ href: `/docs/components/smooth-cursor`,+ items: [],+ label: "New",+ },],},{diff --git a/content/docs/components/smooth-cursor.mdx b/content/docs/components/smooth-cursor.mdxnew file mode 100644index 00000000..bbcc3829--- /dev/null+++ b/content/docs/components/smooth-cursor.mdx@@ -0,0 +1,121 @@+---+title: Smooth Cursor+date: 2024-04-17+description: A customizable, physics-based smooth cursor animation component for React applications.+author: magicui+published: true+---++<ComponentPreview name="smooth-cursor-demo" />++## Features++- π― Smooth physics-based cursor animations+- π Rotation effects based on movement direction+- β‘ Performance optimized with RAF+- π¨ Fully customizable cursor design+- π¦ Lightweight and easy to implement++## Installation++<Tabs defaultValue="cli">++<TabsList>+ <TabsTrigger value="cli">CLI</TabsTrigger>+ <TabsTrigger value="manual">Manual</TabsTrigger>+</TabsList>+<TabsContent value="cli">++```bash+npx shadcn@latest add "https://magicui.design/r/smooth-cursor"+```++</TabsContent>++<TabsContent value="manual">++<Steps>++<Step>Copy and paste the following code into your project.</Step>++<ComponentSource name="smooth-cursor" />++<Step>Add the component to your page or layout.</Step>++```tsx+import { SmoothCursor } from "@/components/ui/smooth-cursor";++export default function Page() {+ return (+ <>+ <SmoothCursor />+ {/* Your page content */}+ </>+ );+}+```++</Steps>++</TabsContent>++</Tabs>++## Usage++```tsx+import { SmoothCursor } from "@/components/ui/smooth-cursor";+```++```tsx+<SmoothCursor />+```++## Props++| Prop | Type | Default | Description |+| -------------- | -------------- | ---------------------- | ------------------------------------------------------ |+| `cursor` | `JSX.Element` | `<DefaultCursorSVG />` | Custom cursor component to replace the default cursor |+| `springConfig` | `SpringConfig` | See below | Configuration object for the spring animation behavior |++### SpringConfig Type++```typescript+interface SpringConfig {+ damping: number; // Controls how quickly the animation settles+ stiffness: number; // Controls the spring stiffness+ mass: number; // Controls the virtual mass of the animated object+ restDelta: number; // Controls the threshold at which animation is considered complete+}+```++### Default Spring Configuration++```typescript+const defaultSpringConfig = {+ damping: 45,+ stiffness: 400,+ mass: 1,+ restDelta: 0.001,+};+```++## Browser Support++Compatible with all modern browsers that support:++- `requestAnimationFrame`+- CSS transforms+- Pointer events++## Accessibility++When using this component, consider that:++- Users navigating via keyboard will not see the custom cursor+- You may want to provide alternative visual cues for interactive elements+- Some users may have motion sensitivity, so consider providing a way to disable the animation++## Credits++- Credit to [@Code_Parth](https://twitter.com/Code_Parth) for the original concept and implementationdiff --git a/public/r/smooth-cursor-demo.json b/public/r/smooth-cursor-demo.jsonnew file mode 100644index 00000000..67bc0308--- /dev/null+++ b/public/r/smooth-cursor-demo.json@@ -0,0 +1,17 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "smooth-cursor-demo",+ "type": "registry:example",+ "description": "Basic smooth cursor example",+ "registryDependencies": [+ "https://magicui.design/r/smooth-cursor"+ ],+ "files": [+ {+ "path": "registry/example/smooth-cursor-demo.tsx",+ "content": "import { SmoothCursor } from \"@/registry/magicui/smooth-cursor\";\n\nexport default function SmoothCursorDemo() {\n return (\n <div className=\"relative\">\n <span className=\"hidden md:block\">Move your mouse around</span>\n <span className=\"block md:hidden\">Tap anywhere to see the cursor</span>\n <SmoothCursor />\n </div>\n );\n}\n",+ "type": "registry:example",+ "target": "components/smooth-cursor-demo.tsx"+ }+ ]+}\ No newline at end of filediff --git a/public/r/smooth-cursor.json b/public/r/smooth-cursor.jsonnew file mode 100644index 00000000..bfa6977e--- /dev/null+++ b/public/r/smooth-cursor.json@@ -0,0 +1,16 @@+{+ "$schema": "https://ui.shadcn.com/schema/registry-item.json",+ "name": "smooth-cursor",+ "type": "registry:ui",+ "description": "A customizable, physics-based smooth cursor animation component with spring animations and rotation effects",+ "dependencies": [+ "framer-motion"+ ],+ "files": [+ {+ "path": "registry/magicui/smooth-cursor.tsx",+ "content": "\"use client\";\n\nimport { motion, useSpring } from \"motion/react\";\nimport { FC, JSX, useEffect, useRef, useState } from \"react\";\n\ninterface Position {\n x: number;\n y: number;\n}\n\nexport interface SmoothCursorProps {\n cursor?: JSX.Element;\n springConfig?: {\n damping: number;\n stiffness: number;\n mass: number;\n restDelta: number;\n };\n}\n\nconst DefaultCursorSVG: FC = () => {\n return (\n <svg\n xmlns=\"http://www.w3.org/2000/svg\"\n width={50}\n height={54}\n viewBox=\"0 0 50 54\"\n fill=\"none\"\n style={{ scale: 0.5 }}\n >\n <g filter=\"url(#filter0_d_91_7928)\">\n <path\n d=\"M42.6817 41.1495L27.5103 6.79925C26.7269 5.02557 24.2082 5.02558 23.3927 6.79925L7.59814 41.1495C6.75833 42.9759 8.52712 44.8902 10.4125 44.1954L24.3757 39.0496C24.8829 38.8627 25.4385 38.8627 25.9422 39.0496L39.8121 44.1954C41.6849 44.8902 43.4884 42.9759 42.6817 41.1495Z\"\n fill=\"black\"\n />\n <path\n d=\"M43.7146 40.6933L28.5431 6.34306C27.3556 3.65428 23.5772 3.69516 22.3668 6.32755L6.57226 40.6778C5.3134 43.4156 7.97238 46.298 10.803 45.2549L24.7662 40.109C25.0221 40.0147 25.2999 40.0156 25.5494 40.1082L39.4193 45.254C42.2261 46.2953 44.9254 43.4347 43.7146 40.6933Z\"\n stroke=\"white\"\n strokeWidth={2.25825}\n />\n </g>\n <defs>\n <filter\n id=\"filter0_d_91_7928\"\n x={0.602397}\n y={0.952444}\n width={49.0584}\n height={52.428}\n filterUnits=\"userSpaceOnUse\"\n colorInterpolationFilters=\"sRGB\"\n >\n <feFlood floodOpacity={0} result=\"BackgroundImageFix\" />\n <feColorMatrix\n in=\"SourceAlpha\"\n type=\"matrix\"\n values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0\"\n result=\"hardAlpha\"\n />\n <feOffset dy={2.25825} />\n <feGaussianBlur stdDeviation={2.25825} />\n <feComposite in2=\"hardAlpha\" operator=\"out\" />\n <feColorMatrix\n type=\"matrix\"\n values=\"0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.08 0\"\n />\n <feBlend\n mode=\"normal\"\n in2=\"BackgroundImageFix\"\n result=\"effect1_dropShadow_91_7928\"\n />\n <feBlend\n mode=\"normal\"\n in=\"SourceGraphic\"\n in2=\"effect1_dropShadow_91_7928\"\n result=\"shape\"\n />\n </filter>\n </defs>\n </svg>\n );\n};\n\nexport function SmoothCursor({\n cursor = <DefaultCursorSVG />,\n springConfig = {\n damping: 45,\n stiffness: 400,\n mass: 1,\n restDelta: 0.001,\n },\n}: SmoothCursorProps) {\n const [isMoving, setIsMoving] = useState(false);\n const lastMousePos = useRef<Position>({ x: 0, y: 0 });\n const velocity = useRef<Position>({ x: 0, y: 0 });\n const lastUpdateTime = useRef(Date.now());\n const previousAngle = useRef(0);\n const accumulatedRotation = useRef(0);\n\n const cursorX = useSpring(0, springConfig);\n const cursorY = useSpring(0, springConfig);\n const rotation = useSpring(0, {\n ...springConfig,\n damping: 60,\n stiffness: 300,\n });\n const scale = useSpring(1, {\n ...springConfig,\n stiffness: 500,\n damping: 35,\n });\n\n useEffect(() => {\n const updateVelocity = (currentPos: Position) => {\n const currentTime = Date.now();\n const deltaTime = currentTime - lastUpdateTime.current;\n\n if (deltaTime > 0) {\n velocity.current = {\n x: (currentPos.x - lastMousePos.current.x) / deltaTime,\n y: (currentPos.y - lastMousePos.current.y) / deltaTime,\n };\n }\n\n lastUpdateTime.current = currentTime;\n lastMousePos.current = currentPos;\n };\n\n const smoothMouseMove = (e: MouseEvent) => {\n const currentPos = { x: e.clientX, y: e.clientY };\n updateVelocity(currentPos);\n\n const speed = Math.sqrt(\n Math.pow(velocity.current.x, 2) + Math.pow(velocity.current.y, 2),\n );\n\n cursorX.set(currentPos.x);\n cursorY.set(currentPos.y);\n\n if (speed > 0.1) {\n const currentAngle =\n Math.atan2(velocity.current.y, velocity.current.x) * (180 / Math.PI) +\n 90;\n\n let angleDiff = currentAngle - previousAngle.current;\n if (angleDiff > 180) angleDiff -= 360;\n if (angleDiff < -180) angleDiff += 360;\n accumulatedRotation.current += angleDiff;\n rotation.set(accumulatedRotation.current);\n previousAngle.current = currentAngle;\n\n scale.set(0.95);\n setIsMoving(true);\n\n const timeout = setTimeout(() => {\n scale.set(1);\n setIsMoving(false);\n }, 150);\n\n return () => clearTimeout(timeout);\n }\n };\n\n let rafId: number;\n const throttledMouseMove = (e: MouseEvent) => {\n if (rafId) return;\n\n rafId = requestAnimationFrame(() => {\n smoothMouseMove(e);\n rafId = 0;\n });\n };\n\n document.body.style.cursor = \"none\";\n window.addEventListener(\"mousemove\", throttledMouseMove);\n\n return () => {\n window.removeEventListener(\"mousemove\", throttledMouseMove);\n document.body.style.cursor = \"auto\";\n if (rafId) cancelAnimationFrame(rafId);\n };\n }, [cursorX, cursorY, rotation, scale]);\n\n return (\n <motion.div\n style={{\n position: \"fixed\",\n left: cursorX,\n top: cursorY,\n translateX: \"-50%\",\n translateY: \"-50%\",\n rotate: rotation,\n scale: scale,\n zIndex: 100,\n pointerEvents: \"none\",\n willChange: \"transform\",\n }}\n initial={{ scale: 0 }}\n animate={{ scale: 1 }}\n transition={{\n type: \"spring\",\n stiffness: 400,\n damping: 30,\n }}\n >\n {cursor}\n </motion.div>\n );\n}\n",+ "type": "registry:ui"+ }+ ]+}\ No newline at end of filediff --git a/public/registry.json b/public/registry.jsonindex d1b0d6ba..eda6b468 100644--- a/public/registry.json+++ b/public/registry.json@@ -180,6 +180,20 @@}]},+ {+ "name": "smooth-cursor",+ "type": "registry:ui",+ "description": "A customizable, physics-based smooth cursor animation component with spring animations and rotation effects",+ "dependencies": [+ "framer-motion"+ ],+ "files": [+ {+ "path": "registry/magicui/smooth-cursor.tsx",+ "type": "registry:ui"+ }+ ]+ },{"name": "neon-gradient-card","type": "registry:ui",@@ -1455,6 +1469,21 @@}]},+ {+ "name": "smooth-cursor-demo",+ "type": "registry:example",+ "description": "Basic smooth cursor example",+ "registryDependencies": [+ "https://magicui.design/r/smooth-cursor"+ ],+ "files": [+ {+ "path": "registry/example/smooth-cursor-demo.tsx",+ "type": "registry:example",+ "target": "components/smooth-cursor-demo.tsx"+ }+ ]+ },{"name": "neon-gradient-card-demo","type": "registry:example",diff --git a/registry.json b/registry.jsonindex d1b0d6ba..eda6b468 100644--- a/registry.json+++ b/registry.json@@ -180,6 +180,20 @@}]},+ {+ "name": "smooth-cursor",+ "type": "registry:ui",+ "description": "A customizable, physics-based smooth cursor animation component with spring animations and rotation effects",+ "dependencies": [+ "framer-motion"+ ],+ "files": [+ {+ "path": "registry/magicui/smooth-cursor.tsx",+ "type": "registry:ui"+ }+ ]+ },{"name": "neon-gradient-card","type": "registry:ui",@@ -1455,6 +1469,21 @@}]},+ {+ "name": "smooth-cursor-demo",+ "type": "registry:example",+ "description": "Basic smooth cursor example",+ "registryDependencies": [+ "https://magicui.design/r/smooth-cursor"+ ],+ "files": [+ {+ "path": "registry/example/smooth-cursor-demo.tsx",+ "type": "registry:example",+ "target": "components/smooth-cursor-demo.tsx"+ }+ ]+ },{"name": "neon-gradient-card-demo","type": "registry:example",diff --git a/registry/example/smooth-cursor-demo.tsx b/registry/example/smooth-cursor-demo.tsxnew file mode 100644index 00000000..4384a256--- /dev/null+++ b/registry/example/smooth-cursor-demo.tsx@@ -0,0 +1,11 @@+import { SmoothCursor } from "@/registry/magicui/smooth-cursor";++export default function SmoothCursorDemo() {+ return (+ <>+ <span className="hidden md:block">Move your mouse around</span>+ <span className="block md:hidden">Tap anywhere to see the cursor</span>+ <SmoothCursor />+ </>+ );+}diff --git a/registry/magicui/smooth-cursor.tsx b/registry/magicui/smooth-cursor.tsxnew file mode 100644index 00000000..6e71c800--- /dev/null+++ b/registry/magicui/smooth-cursor.tsx@@ -0,0 +1,208 @@+"use client";++import { motion, useSpring } from "motion/react";+import { FC, JSX, useEffect, useRef, useState } from "react";++interface Position {+ x: number;+ y: number;+}++export interface SmoothCursorProps {+ cursor?: JSX.Element;+ springConfig?: {+ damping: number;+ stiffness: number;+ mass: number;+ restDelta: number;+ };+}++const DefaultCursorSVG: FC = () => {+ return (+ <svg+ xmlns="http://www.w3.org/2000/svg"+ width={50}+ height={54}+ viewBox="0 0 50 54"+ fill="none"+ style={{ scale: 0.5 }}+ >+ <g filter="url(#filter0_d_91_7928)">+ <path+ d="M42.6817 41.1495L27.5103 6.79925C26.7269 5.02557 24.2082 5.02558 23.3927 6.79925L7.59814 41.1495C6.75833 42.9759 8.52712 44.8902 10.4125 44.1954L24.3757 39.0496C24.8829 38.8627 25.4385 38.8627 25.9422 39.0496L39.8121 44.1954C41.6849 44.8902 43.4884 42.9759 42.6817 41.1495Z"+ fill="black"+ />+ <path+ d="M43.7146 40.6933L28.5431 6.34306C27.3556 3.65428 23.5772 3.69516 22.3668 6.32755L6.57226 40.6778C5.3134 43.4156 7.97238 46.298 10.803 45.2549L24.7662 40.109C25.0221 40.0147 25.2999 40.0156 25.5494 40.1082L39.4193 45.254C42.2261 46.2953 44.9254 43.4347 43.7146 40.6933Z"+ stroke="white"+ strokeWidth={2.25825}+ />+ </g>+ <defs>+ <filter+ id="filter0_d_91_7928"+ x={0.602397}+ y={0.952444}+ width={49.0584}+ height={52.428}+ filterUnits="userSpaceOnUse"+ colorInterpolationFilters="sRGB"+ >+ <feFlood floodOpacity={0} result="BackgroundImageFix" />+ <feColorMatrix+ in="SourceAlpha"+ type="matrix"+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 127 0"+ result="hardAlpha"+ />+ <feOffset dy={2.25825} />+ <feGaussianBlur stdDeviation={2.25825} />+ <feComposite in2="hardAlpha" operator="out" />+ <feColorMatrix+ type="matrix"+ values="0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0.08 0"+ />+ <feBlend+ mode="normal"+ in2="BackgroundImageFix"+ result="effect1_dropShadow_91_7928"+ />+ <feBlend+ mode="normal"+ in="SourceGraphic"+ in2="effect1_dropShadow_91_7928"+ result="shape"+ />+ </filter>+ </defs>+ </svg>+ );+};++export function SmoothCursor({+ cursor = <DefaultCursorSVG />,+ springConfig = {+ damping: 45,+ stiffness: 400,+ mass: 1,+ restDelta: 0.001,+ },+}: SmoothCursorProps) {+ const [isMoving, setIsMoving] = useState(false);+ const lastMousePos = useRef<Position>({ x: 0, y: 0 });+ const velocity = useRef<Position>({ x: 0, y: 0 });+ const lastUpdateTime = useRef(Date.now());+ const previousAngle = useRef(0);+ const accumulatedRotation = useRef(0);++ const cursorX = useSpring(0, springConfig);+ const cursorY = useSpring(0, springConfig);+ const rotation = useSpring(0, {+ ...springConfig,+ damping: 60,+ stiffness: 300,+ });+ const scale = useSpring(1, {+ ...springConfig,+ stiffness: 500,+ damping: 35,+ });++ useEffect(() => {+ const updateVelocity = (currentPos: Position) => {+ const currentTime = Date.now();+ const deltaTime = currentTime - lastUpdateTime.current;++ if (deltaTime > 0) {+ velocity.current = {+ x: (currentPos.x - lastMousePos.current.x) / deltaTime,+ y: (currentPos.y - lastMousePos.current.y) / deltaTime,+ };+ }++ lastUpdateTime.current = currentTime;+ lastMousePos.current = currentPos;+ };++ const smoothMouseMove = (e: MouseEvent) => {+ const currentPos = { x: e.clientX, y: e.clientY };+ updateVelocity(currentPos);++ const speed = Math.sqrt(+ Math.pow(velocity.current.x, 2) + Math.pow(velocity.current.y, 2),+ );++ cursorX.set(currentPos.x);+ cursorY.set(currentPos.y);++ if (speed > 0.1) {+ const currentAngle =+ Math.atan2(velocity.current.y, velocity.current.x) * (180 / Math.PI) ++ 90;++ let angleDiff = currentAngle - previousAngle.current;+ if (angleDiff > 180) angleDiff -= 360;+ if (angleDiff < -180) angleDiff += 360;+ accumulatedRotation.current += angleDiff;+ rotation.set(accumulatedRotation.current);+ previousAngle.current = currentAngle;++ scale.set(0.95);+ setIsMoving(true);++ const timeout = setTimeout(() => {+ scale.set(1);+ setIsMoving(false);+ }, 150);++ return () => clearTimeout(timeout);+ }+ };++ let rafId: number;+ const throttledMouseMove = (e: MouseEvent) => {+ if (rafId) return;++ rafId = requestAnimationFrame(() => {+ smoothMouseMove(e);+ rafId = 0;+ });+ };++ document.body.style.cursor = "none";+ window.addEventListener("mousemove", throttledMouseMove);++ return () => {+ window.removeEventListener("mousemove", throttledMouseMove);+ document.body.style.cursor = "auto";+ if (rafId) cancelAnimationFrame(rafId);+ };+ }, [cursorX, cursorY, rotation, scale]);++ return (+ <motion.div+ style={{+ position: "fixed",+ left: cursorX,+ top: cursorY,+ translateX: "-50%",+ translateY: "-50%",+ rotate: rotation,+ scale: scale,+ zIndex: 100,+ pointerEvents: "none",+ willChange: "transform",+ }}+ initial={{ scale: 0 }}+ animate={{ scale: 1 }}+ transition={{+ type: "spring",+ stiffness: 400,+ damping: 30,+ }}+ >+ {cursor}+ </motion.div>+ );+}diff --git a/registry/registry-examples.ts b/registry/registry-examples.tsindex ee7ed90f..bbee6634 100644--- a/registry/registry-examples.ts+++ b/registry/registry-examples.ts@@ -185,6 +185,19 @@ export const examples: Registry["items"] = [},],},+ {+ name: "smooth-cursor-demo",+ description: "Basic smooth cursor example",+ type: "registry:example",+ registryDependencies: ["https://magicui.design/r/smooth-cursor"],+ files: [+ {+ path: "registry/example/smooth-cursor-demo.tsx",+ type: "registry:example",+ target: "components/smooth-cursor-demo.tsx",+ },+ ],+ },{name: "neon-gradient-card-demo",type: "registry:example",diff --git a/registry/registry-ui.ts b/registry/registry-ui.tsindex 680792ce..e60559dc 100644--- a/registry/registry-ui.ts+++ b/registry/registry-ui.ts@@ -156,6 +156,19 @@ export const ui: Registry["items"] = [},],},+ {+ name: "smooth-cursor",+ description:+ "A customizable, physics-based smooth cursor animation component with spring animations and rotation effects",+ type: "registry:ui",+ files: [+ {+ path: "registry/magicui/smooth-cursor.tsx",+ type: "registry:ui",+ },+ ],+ dependencies: ["framer-motion"],+ },{name: "neon-gradient-card",type: "registry:ui",