asafefe.dev
Lab

Lab/17

Pixel Portrait

The image is averaged into blocks, then drawn as color blocks, a three-tone ordered dither, or halftone dots. A lens around the cursor shows the source.

  • Canvas
  • Image
Hover or drag to reveal
loading
dither / 10px40x40
10px

Installation

Add Pixel Portrait to your project with the shadcn CLI. The files land in components/lab, ready to edit.

$ pnpm dlx shadcn@latest add @soultel/lab-pixel-portrait

Or by URL: https://asafefe.dev/r/lab-pixel-portrait.json

Brings along

  • utils
  • @soultel/lab-canvas

Usage

example.tsx12 lines
import { PixelPortrait } from "@/components/lab/pixel-portrait"

export function Example() {
  return (
    <PixelPortrait
      src="/avatar.png"
      alt="Portrait"
      mode="dither"
      className="max-w-sm rounded-xl"
    />
  )
}

Props

PropTypeDefaultDescription
srcstring-Same-origin or CORS-enabled image. Cropped to a centered square.
altstring-Accessible description.
mode"blocks" | "dither" | "dots""dither"Render style.
blocknumber10Block size in px.
lensbooleantrueReveal the original around the pointer.
circlebooleanfalseClip to a circle instead of the square.
tones[string, string, string]["var(--foreground)", "var(--primary)", "var(--background)"]Dither tones, dark to light.
dotColorstring"var(--foreground)"Halftone dot color.
contrastnumber1.35Contrast boost before dither and halftone.