# Petal Drift

> Blossom on a spring breeze: petals tumble down in three depths, turning over in 3D and softening out of focus up close, scattering in the cursor's wake.

Category: Backgrounds · Free · Updated 2026-11-09 · Live demo: https://www.motionkitui.com/components/petal-drift

## When to use it

Spring campaigns, weddings, florists and beauty brands, Japanese and seasonal themes, and gentle, romantic landing pages.

## How it works

Each petal is a canvas path, a rounded teardrop with a notch at its tip, with a depth that sets its size, speed and opacity. Squashing it vertically by the cosine of a flip angle makes it look as if it's turning over in 3D, and the lighter gradient only shows when its face is towards you. Petals sway on sine waves on top of a breeze that slowly gusts, the nearest ones are blurred like a shallow depth of field, and moving the pointer pushes nearby petals along with it before they settle back into the wind.

## Installation

```bash
npx shadcn@latest add https://www.motionkitui.com/r/petal-drift.json
```

Or copy `components/motionkit/petal-drift.tsx` below. Requires react and Tailwind CSS v4. Free for personal projects; commercial use needs MotionKit Plus (https://www.motionkitui.com/pricing).

## Props

| Prop | Type | Default | Description |
| --- | --- | --- | --- |
| `colors` | `string[]` | `["#F6C6D0", "#F2B8A2", "#FBE3E8", "#E9A6B8"]` | Petal colours; each petal picks one. |
| `background` | `string` | `"#1A1420"` | Colour behind the petals, or "transparent" to use your own. |
| `count` | `number` | `60` | Petals in the air at once. |
| `wind` | `number` | `1` | Steady breeze from the left. 0 for still air. |
| `interactive` | `boolean` | `true` | Let the pointer scatter petals with a gust. |
| `children` | `ReactNode` |  | Your content, layered above. |

## Accessibility

- **Keyboard: Not applicable.** Decorative, nothing to focus.
- **Screen readers: Pass.** The effect is hidden from assistive tech; only your content is read.
- **Reduced motion: Pass.** With reduced motion on, the petals are drawn once where they hang and stay still. It also stops animating off screen.
- **Colour contrast: Pass.** Petals are small and translucent; ivory text on the default background measures 16:1.

## Source

```tsx
"use client";

import { useEffect, useRef, type ReactNode } from "react";

type PetalDriftProps = {
  children?: ReactNode;
  /** Petal colours; each petal picks one. */
  colors?: string[];
  /** Background behind the petals. Pass "transparent" to lay them over your own. */
  background?: string;
  /** Petals in the air at once. */
  count?: number;
  /** Steady breeze from the left, 0 for still air. */
  wind?: number;
  /** Let the pointer push petals aside with a gust. */
  interactive?: boolean;
  className?: string;
};

type Petal = { x: number; y: number; z: number; size: number; spin: number; spinSpeed: number; flip: number; flipSpeed: number; sway: number; color: string; vx: number; vy: number };

/**
 * Blossom on a spring breeze. Petals tumble down in three depths, turning over in 3D as they fall, swaying, and
 * softening out of focus the nearer they are. Sweep the pointer through them and they scatter in its wake.
 */
export function PetalDrift({
  children,
  colors = ["#F6C6D0", "#F2B8A2", "#FBE3E8", "#E9A6B8"],
  background = "#1A1420",
  count = 60,
  wind = 1,
  interactive = true,
  className = "",
}: PetalDriftProps) {
  const canvasRef = useRef<HTMLCanvasElement>(null);

  useEffect(() => {
    const canvas = canvasRef.current;
    const area = canvas?.parentElement;
    const context = canvas?.getContext("2d");
    if (!canvas || !area || !context) return;
    const still = window.matchMedia("(prefers-reduced-motion: reduce)").matches;

    let width = 0;
    let height = 0;
    let ratio = 1;
    let frame = 0;
    let visible = true;
    let last = 0;
    let time = 0;
    const pointer = { x: -9999, y: -9999, vx: 0, vy: 0, active: false };

    // a seeded random, so a still (reduced motion) frame looks the same every time
    let seed = 7;
    const random = () => ((seed = (seed * 16807) % 2147483647) - 1) / 2147483646;

    const spawn = (anywhere: boolean): Petal => {
      const z = random();
      return {
        x: random() * (width + 200) - 100,
        y: anywhere ? random() * height : -30 - random() * 60,
        z,
        size: 5 + z * 11,
        spin: random() * Math.PI * 2,
        spinSpeed: (random() - 0.5) * 1.6,
        flip: random() * Math.PI * 2,
        flipSpeed: 1.2 + random() * 2.2,
        sway: random() * Math.PI * 2,
        color: colors[Math.floor(random() * colors.length)],
        vx: 0,
        vy: 0,
      };
    };
    let petals: Petal[] = [];

    function resize() {
      const rect = canvas!.getBoundingClientRect();
      ratio = Math.min(window.devicePixelRatio || 1, 2);
      width = rect.width;
      height = rect.height;
      canvas!.width = Math.max(1, Math.round(width * ratio));
      canvas!.height = Math.max(1, Math.round(height * ratio));
      if (!petals.length) {
        seed = 7;
        petals = Array.from({ length: count }, () => spawn(true));
      }
    }

    // a petal: a rounded teardrop with a notch at the tip, flattened by its flip so it seems to turn over
    function drawPetal(petal: Petal) {
      const c = context!;
      const s = petal.size;
      const turn = Math.cos(petal.flip);
      c.save();
      c.translate(petal.x, petal.y);
      c.rotate(petal.spin);
      c.scale(1, Math.max(0.12, Math.abs(turn)));
      c.globalAlpha = 0.45 + petal.z * 0.5;
      // near petals are out of focus, far ones are small and sharp
      c.filter = petal.z > 0.82 ? `blur(${((petal.z - 0.82) * 14).toFixed(1)}px)` : "none";
      const gradient = c.createLinearGradient(0, -s, 0, s);
      gradient.addColorStop(0, petal.color);
      gradient.addColorStop(1, turn > 0 ? "rgba(255,255,255,0.85)" : petal.color);
      c.fillStyle = gradient;
      c.beginPath();
      c.moveTo(0, s);
      c.bezierCurveTo(s * 0.95, s * 0.35, s * 0.75, -s * 0.85, s * 0.18, -s);
      c.lineTo(0, -s * 0.78);
      c.lineTo(-s * 0.18, -s);
      c.bezierCurveTo(-s * 0.75, -s * 0.85, -s * 0.95, s * 0.35, 0, s);
      c.fill();
      c.restore();
    }

    function step(dt: number) {
      time += dt;
      for (let index = 0; index < petals.length; index++) {
        const petal = petals[index];
        const depth = 0.4 + petal.z * 0.9;
        const gust = Math.sin(time * 0.4 + petal.sway) * 0.5 + Math.sin(time * 0.13) * 0.6;
        // the pointer's gust: petals near it are pushed along with its movement
        if (pointer.active) {
          const dx = petal.x - pointer.x;
          const dy = petal.y - pointer.y;
          const near = Math.exp(-(dx * dx + dy * dy) / 9000);
          petal.vx += (pointer.vx * 0.5 + (dx > 0 ? 40 : -40)) * near * dt * 3;
          petal.vy += (pointer.vy * 0.5) * near * dt * 3;
        }
        petal.vx *= 0.96;
        petal.vy *= 0.96;
        petal.x += ((wind * 22 + gust * 14) * depth + petal.vx) * dt + Math.sin(time * 1.3 + petal.sway) * 0.35 * depth;
        petal.y += (16 + petal.z * 26) * dt + petal.vy * dt;
        petal.spin += petal.spinSpeed * dt;
        petal.flip += petal.flipSpeed * dt;
        if (petal.y > height + 30 || petal.x > width + 120 || petal.x < -160) petals[index] = spawn(false);
      }
    }

    function draw() {
      const c = context!;
      c.setTransform(ratio, 0, 0, ratio, 0, 0);
      c.clearRect(0, 0, width, height);
      // far petals first, so near ones pass in front
      [...petals].sort((a, b) => a.z - b.z).forEach(drawPetal);
    }

    function loop(now: number) {
      const dt = last ? Math.min(0.05, (now - last) / 1000) : 0;
      last = now;
      step(dt);
      draw();
      pointer.vx *= 0.9;
      pointer.vy *= 0.9;
      frame = visible ? requestAnimationFrame(loop) : 0;
      if (!frame) last = 0;
    }

    resize();
    draw();
    if (!still) frame = requestAnimationFrame(loop);
    const resizeObserver = new ResizeObserver(() => {
      resize();
      draw();
    });
    resizeObserver.observe(canvas);
    const visibilityObserver = new IntersectionObserver(([entry]) => {
      visible = entry.isIntersecting;
      if (visible && !frame && !still) frame = requestAnimationFrame(loop);
    });
    visibilityObserver.observe(canvas);

    function move(event: PointerEvent) {
      if (!interactive || still) return;
      const rect = canvas!.getBoundingClientRect();
      const x = event.clientX - rect.left;
      const y = event.clientY - rect.top;
      if (pointer.active) {
        pointer.vx = (x - pointer.x) * 30;
        pointer.vy = (y - pointer.y) * 30;
      }
      pointer.x = x;
      pointer.y = y;
      pointer.active = true;
    }
    const leave = () => {
      pointer.active = false;
    };
    area.addEventListener("pointermove", move);
    area.addEventListener("pointerleave", leave);

    return () => {
      cancelAnimationFrame(frame);
      resizeObserver.disconnect();
      visibilityObserver.disconnect();
      area.removeEventListener("pointermove", move);
      area.removeEventListener("pointerleave", leave);
    };
    // the colours are compared by value, so a new array with the same colours doesn't restart it
    // eslint-disable-next-line react-hooks/exhaustive-deps
  }, [colors.join(), count, wind, interactive]);

  return (
    <div className={`relative isolate overflow-hidden ${className}`} style={{ background }}>
      {/* a soft glow of evening light behind the blossom */}
      <div aria-hidden className="absolute inset-0 -z-20 bg-[radial-gradient(ellipse_at_70%_20%,rgb(242_184_162/0.18),transparent_60%),radial-gradient(ellipse_at_10%_90%,rgb(183_154_211/0.14),transparent_55%)]" />
      <canvas ref={canvasRef} aria-hidden className="pointer-events-none absolute inset-0 -z-10 size-full" />
      {children}
    </div>
  );
}
```
