Getting Started
Components
Special Effects
Animations
Text Animations
Backgrounds
A stunning image reveal animation that splits images into grid pieces with staggered fade-in and grayscale-to-color transitions.
motion-svruned
pixel-image.svelte
<script lang="ts"> import PixelImage from "$lib/components/magic/pixel-image/pixel-image.svelte"; import Image from "$lib/assets/pixel-image-demo.jpg";</script>
<PixelImage src={Image} customGrid={{ rows: 4, cols: 6 }} grayscaleAnimation />Installation
pnpm dlx shadcn-svelte@latest add https://sv-animations.vercel.app/r/pixel-image.json
Install dependencies
Install the required packages for this component:
pnpm add motion-sv runed
Copy the Source Code
Copy and paste the following code into your project:
pixel-image.svelte
<script lang="ts"> import { cn } from "#lib/utils.js"; import { useInView } from "motion-sv"; import { watch } from "runed";
type Grid = { rows: number; cols: number; };
const DEFAULT_GRIDS: Record<string, Grid> = { "6x4": { rows: 4, cols: 6 }, "8x8": { rows: 8, cols: 8 }, "8x3": { rows: 3, cols: 8 }, "4x6": { rows: 6, cols: 4 }, "3x8": { rows: 8, cols: 3 }, };
type PredefinedGridKey = keyof typeof DEFAULT_GRIDS;
interface Props { src: string; grid?: PredefinedGridKey; customGrid?: Grid; grayscaleAnimation?: boolean; pixelFadeInDuration?: number; maxAnimationDelay?: number; colorRevealDelay?: number; once?: boolean; }
let { src, grid = "6x4", grayscaleAnimation = true, pixelFadeInDuration = 1000, maxAnimationDelay = 1200, colorRevealDelay = 1300, customGrid, once = false, }: Props = $props();
let showColor = $state(false);
const MIN_GRID = 1; const MAX_GRID = 16;
let dimensions = $derived.by(() => { const isValidGrid = (grid?: Grid) => { if (!grid) return false; const { rows, cols } = grid; return ( Number.isInteger(rows) && Number.isInteger(cols) && rows >= MIN_GRID && cols >= MIN_GRID && rows <= MAX_GRID && cols <= MAX_GRID ); };
return isValidGrid(customGrid) ? customGrid! : DEFAULT_GRIDS[grid]; });
let pieces = $derived.by(() => { const total = dimensions.rows * dimensions.cols; return Array.from({ length: total }, (_, index) => { const row = Math.floor(index / dimensions.cols); const col = index % dimensions.cols;
const clipPath = `polygon( ${col * (100 / dimensions.cols)}% ${row * (100 / dimensions.rows)}%, ${(col + 1) * (100 / dimensions.cols)}% ${row * (100 / dimensions.rows)}%, ${(col + 1) * (100 / dimensions.cols)}% ${(row + 1) * (100 / dimensions.rows)}%, ${col * (100 / dimensions.cols)}% ${(row + 1) * (100 / dimensions.rows)}% )`;
const delay = Math.random() * maxAnimationDelay; return { clipPath, delay, }; }); });
let element: HTMLDivElement | null = $state(null); let view = useInView( () => element!, () => ({ once }) as any // type definition bug );
watch( () => view.current, (isInView) => { if (isInView) { // Element entered view - start color reveal after delay const colorTimeout = setTimeout(() => { showColor = true; }, colorRevealDelay); return () => clearTimeout(colorTimeout); } else if (!once) { // Element left view and once is false - reset animation showColor = false; } } );
// $inspect(view.current, "In View");</script>
<div class="relative h-72 w-72 select-none md:h-96 md:w-96" bind:this={element}> {#each pieces as piece, index} <div class={cn( "absolute inset-0 transition-all ease-out", view.current ? "opacity-100" : "opacity-0" )} style="clip-path: {piece.clipPath}; transition-delay: {piece.delay}ms; transition-duration: {pixelFadeInDuration}ms;" > <img {src} alt="Pixel image piece {index + 1}" class={cn( "z-1 rounded-[2.5rem] object-cover", grayscaleAnimation && (showColor ? "grayscale-0" : "grayscale") )} style={grayscaleAnimation ? `filter: ${showColor ? "grayscale(0)" : "grayscale(1)"}; transition: filter ${pixelFadeInDuration}ms cubic-bezier(0.4, 0, 0.2, 1)` : ""} draggable="false" /> </div> {/each}</div> index.ts
import PixelImage from "./pixel-image.svelte";export { PixelImage };Install and integrate the Svelte component documented below.https://sv-animations.vercel.app/magic/docs/components/pixel-image/llms.txt
Follow the guide's installation and usage instructions.Match the project's existing structure, imports, and styles.Keep changes scoped to the component and verify it works.Reply with a short bullet list of completed changes.Usage
<script lang="ts"> import { PixelImage } from "#lib/components/magic/pixel-image/index.js";</script>
<PixelImage src="https://images.unsplash.com/photo-1500530855697-b586d89ba3ee" />Examples
Color Only (Once)
Pixel image with once={true} - animation plays only once and stays visible.
color-only.svelte
<script lang="ts"> import PixelImage from "$lib/components/magic/pixel-image/pixel-image.svelte"; import Image from "$lib/assets/pixel-image-demo.jpg";</script>
<PixelImage src={Image} customGrid={{ rows: 5, cols: 7 }} grayscaleAnimation={false} pixelFadeInDuration={800} maxAnimationDelay={1000} once/>Fast Reveal (Replay)
Quick animation that replays each time you scroll into view (default once={false}).
fast-reveal.svelte
<script lang="ts"> import PixelImage from "$lib/components/magic/pixel-image/pixel-image.svelte"; import Image from "$lib/assets/pixel-image-demo.jpg";</script>
<PixelImage src={Image} grid="6x4" pixelFadeInDuration={500} maxAnimationDelay={600} colorRevealDelay={700} grayscaleAnimation/>Grid Variations
Compare different grid sizes - see how 8x8 vs 8x3 grids affect the animation.
8x8 Grid
8x3 Grid
grid-variations.svelte
<script lang="ts"> import PixelImage from "$lib/components/magic/pixel-image/pixel-image.svelte"; import Image from "$lib/assets/pixel-image-demo.jpg";</script>
<div class="flex flex-wrap items-center justify-center gap-12"> <div class="flex flex-col items-center gap-4"> <PixelImage src={Image} grid="8x8" /> <p class="text-muted-foreground text-sm">8x8 Grid</p> </div>
<div class="flex flex-col items-center gap-4"> <PixelImage src={Image} grid="8x3" /> <p class="text-muted-foreground text-sm">8x3 Grid</p> </div></div>Props
Pixel Image
A component that creates stunning image reveal animations by splitting images into a grid of pieces with staggered fade-in effects.
| Name | Type | Default | Description |
|---|---|---|---|
src | string | Required | The image source URL |
grid | "6x4" | "8x8" | "8x3" | "4x6" | "3x8" | "6x4" | Predefined grid configuration |
customGrid | { rows: number; cols: number } | - | Custom grid dimensions (overrides grid prop) |
grayscaleAnimation | boolean | true | Enable grayscale-to-color transition effect |
pixelFadeInDuration | number | 1000 | Duration of each piece's fade-in animation (ms) |
maxAnimationDelay | number | 1200 | Maximum random delay for piece animations (ms) |
colorRevealDelay | number | 1300 | Delay before color reveal begins (ms) |
once | boolean | false | If true, animation plays only once. If false, replays each time element enters view |