Cursor

UI Cursor

An animated cursor that moves between waypoints, clicks (ripple), and drags. The Cursor renderer is a pure function of a numeric CursorStyle channel; useCursorPath reads the frame and eases the path, ripple, and press phases.

UI Cursor preview, rendered with Remotion.

Source

components/kuchi/cursor.tsx
"use client";
import { type KuchiTheme, useKuchiTheme } from "@/lib/kuchi-kit";
export type CursorVariant = "arrow" | "pointer";
export interface CursorStyle {
x: number;
y: number;
scale: number;
rippleOpacity: number;
rippleScale: number;
pressScale?: number;
}
export interface CursorProps {
style?: CursorStyle;
variant?: CursorVariant;
size?: number;
theme?: Partial<KuchiTheme>;
rippleColor?: string;
className?: string;
}
const REST: CursorStyle = {
x: 0,
y: 0,
scale: 1,
rippleOpacity: 0,
rippleScale: 0,
};
const ARROW_PATH =
"M1 1 L1 18.5 L5.6 14.4 L8.7 21.6 L11.6 20.4 L8.5 13.2 L14.5 13.2 Z";
const POINTER_PATH =
"M8 2.2 C8 1.3 8.7 0.7 9.5 0.7 C10.3 0.7 11 1.3 11 2.2 L11 9 " +
"C11 9 11.3 8.4 12.2 8.4 C13 8.4 13.4 9 13.4 9.6 " +
"C13.4 9.6 13.9 9.1 14.7 9.1 C15.5 9.1 15.9 9.7 15.9 10.3 " +
"C15.9 10.3 16.4 9.9 17.1 9.9 C17.9 9.9 18.3 10.5 18.3 11.2 " +
"L18.3 16.8 C18.3 19.8 16.3 22.3 12.8 22.3 L11.4 22.3 " +
"C9 22.3 7.9 21.2 6.6 19.2 L4 15.2 C3.5 14.4 3.7 13.4 4.5 12.9 " +
"C5.1 12.5 5.9 12.6 6.4 13.2 L8 15 Z";
export function Cursor({
style,
variant = "arrow",
size = 28,
theme: themeOverride,
rippleColor,
className,
}: CursorProps) {
const theme = useKuchiTheme(themeOverride, "light");
const v = style ?? REST;
const press = v.pressScale ?? 1;
const pressedScale = v.scale * (0.9 + 0.1 * press);
const ring = rippleColor ?? theme.primary;
const fill = theme.foreground;
const stroke = theme.background;
const rippleSize = size * 1.8;
return (
<div
className={className}
style={{
position: "absolute",
left: 0,
top: 0,
transform: `translate(${v.x}px, ${v.y}px)`,
pointerEvents: "none",
}}
>
{}
<div
style={{
position: "absolute",
left: -rippleSize / 2,
top: -rippleSize / 2,
width: rippleSize,
height: rippleSize,
borderRadius: "50%",
border: `2px solid ${ring}`,
opacity: v.rippleOpacity,
transform: `scale(${v.rippleScale})`,
transformOrigin: "center",
}}
/>
{}
<svg
width={size}
height={size}
viewBox="0 0 24 24"
fill="none"
style={{
position: "absolute",
left: 0,
top: 0,
transform: `scale(${pressedScale})`,
transformOrigin: "0 0",
filter: "drop-shadow(0 1px 2px rgba(0,0,0,0.3))",
overflow: "visible",
}}
>
<path
d={variant === "pointer" ? POINTER_PATH : ARROW_PATH}
fill={fill}
stroke={stroke}
strokeWidth={1.4}
strokeLinejoin="round"
/>
</svg>
</div>
);
}
components/kuchi/use-cursor-path.ts
"use client";
import { useCurrentFrame } from "remotion";
import type { CursorStyle } from "@/components/kuchi/cursor";
import { clamp01, type EasingName, easings } from "@/lib/kuchi-kit";
export interface CursorWaypoint {
at: number;
x: number;
y: number;
duration?: number;
click?: boolean;
press?: boolean;
easing?: EasingName;
}
export const DEFAULT_DURATION = 24;
export const CLICK_FRAMES = 16;
export const PRESS_FRAMES = 8;
export interface CursorPathOptions {
speed?: number;
defaultDuration?: number;
}
function lerp(a: number, b: number, t: number): number {
return a + (b - a) * t;
}
export function ripplePhase(framesSinceClick: number): {
rippleOpacity: number;
rippleScale: number;
} {
if (framesSinceClick < 0 || framesSinceClick >= CLICK_FRAMES) {
return { rippleOpacity: 0, rippleScale: 0 };
}
const p = clamp01(framesSinceClick / CLICK_FRAMES);
return {
rippleOpacity: 0.5 * (1 - p),
rippleScale: 2.5 * easings.out(p),
};
}
export function clickPress(framesSinceClick: number): number {
if (framesSinceClick < 0 || framesSinceClick >= PRESS_FRAMES) return 1;
const half = PRESS_FRAMES / 2;
const p =
framesSinceClick < half
? framesSinceClick / half
: 1 - (framesSinceClick - half) / half;
return 1 - clamp01(p);
}
export function useCursorPath(
waypoints: CursorWaypoint[],
opts: CursorPathOptions = {},
): CursorStyle {
const raw = useCurrentFrame();
return cursorPathAt(waypoints, raw, opts);
}
export function cursorPathAt(
waypoints: CursorWaypoint[],
raw: number,
opts: CursorPathOptions = {},
): CursorStyle {
const { speed = 1, defaultDuration = DEFAULT_DURATION } = opts;
const effectiveFrame = raw * speed;
if (waypoints.length === 0) {
return { x: 0, y: 0, scale: 1, rippleOpacity: 0, rippleScale: 0 };
}
const first = waypoints[0];
if (effectiveFrame <= first.at) {
return {
x: first.x,
y: first.y,
scale: 1,
rippleOpacity: 0,
rippleScale: 0,
pressScale: 1,
};
}
let toIndex = waypoints.length - 1;
for (let i = 1; i < waypoints.length; i++) {
if (waypoints[i].at > effectiveFrame) {
toIndex = i;
break;
}
}
const pastLast = effectiveFrame >= waypoints[waypoints.length - 1].at;
const to = pastLast ? waypoints[waypoints.length - 1] : waypoints[toIndex];
const from = pastLast
? waypoints[waypoints.length - 1]
: waypoints[toIndex - 1];
const dur = to.duration ?? defaultDuration;
const ease = easings[to.easing ?? "inOut"];
const start = to.at - dur;
const t =
pastLast || dur <= 0 ? 1 : ease(clamp01((effectiveFrame - start) / dur));
const x = lerp(from.x, to.x, t);
const y = lerp(from.y, to.y, t);
let lastClickAt = -Infinity;
for (const wp of waypoints) {
if (wp.click && wp.at <= effectiveFrame && wp.at > lastClickAt)
lastClickAt = wp.at;
}
const sinceClick =
lastClickAt === -Infinity ? -1 : effectiveFrame - lastClickAt;
const ripple = ripplePhase(sinceClick);
const clickDip = clickPress(sinceClick);
const holdWp = pastLast ? waypoints[waypoints.length - 1] : from;
const heldPress = holdWp.press ? 0 : 1;
const pressScale = Math.min(clickDip, heldPress);
return {
x,
y,
scale: 1,
rippleOpacity: ripple.rippleOpacity,
rippleScale: ripple.rippleScale,
pressScale,
};
}