npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2026 – Pkg Stats / Ryan Hefner

declanvas

v0.1.4

Published

Declarative layout engine for HTML Canvas — compose text, images, and containers with flexbox-like semantics

Readme

declanvas

Declarative layout engine for HTML Canvas. Compose text, images, and containers using a simple element tree — no imperative draw calls needed.

Useful for generating textures for 3D objects, dynamic thumbnails, OG images, or anywhere you need programmatic canvas rendering with layout control.

Demo on CodePen

Install

npm install declanvas

Usage

import { createCanvas } from 'declanvas'

const canvas = await createCanvas({
  width: 1024,
  height: 1024,
  backgroundColor: 'white',
  elements: [
    {
      kind: 'container',
      padding: 40,
      gap: 16,
      children: [
        { kind: 'text', text: 'Hello', fontSize: 72, fontWeight: 'bold' },
        { kind: 'text', text: 'World', fontSize: 48, color: '#666' },
        { kind: 'image', src: '/logo.png', width: 200 },
      ],
    },
  ],
})

document.body.appendChild(canvas)

Elements

Container

Groups child elements with layout control.

{
  kind: 'container',
  children: CanvasElement[],
  direction?: 'row' | 'column',  // default: 'column'
  orientation?: Orientation,      // default: 'topLeft'
  origin?: { x: number, y: number },
  gap?: number,
  padding?: number,
}

Text

Renders a string with configurable font properties.

{
  kind: 'text',
  text: string,
  fontSize?: number,       // default: 50
  fontWeight?: 'bold' | 'normal',
  fontFamily?: string,     // default: 'Arial'
  color?: string,
}

Image

Draws an image. At least one dimension is required — the other is inferred from the aspect ratio.

{
  kind: 'image',
  src: string,
  width?: number,
  height?: number,
}

Layout

Elements are laid out using two properties on containers:

  • direction'column' (vertical, default) or 'row' (horizontal)
  • orientation — controls which corner elements anchor to:
    • 'topLeft' — elements grow right/down (default)
    • 'topRight' — anchored to right edge, grow left/down
    • 'bottomLeft' — anchored to bottom, grow right/up
    • 'bottomRight' — anchored to bottom-right, grow left/up

Grid mode

Set grid: true to tile the element tree across the entire canvas:

const canvas = await createCanvas({
  width: 2048,
  height: 2048,
  grid: true,
  elements: [
    {
      kind: 'container',
      padding: 20,
      children: [
        { kind: 'image', src: '/tile.png', width: 100 },
      ],
    },
  ],
})

Three.js example

import { createCanvas } from 'declanvas'
import * as THREE from 'three'

const canvas = await createCanvas({
  width: 2048,
  height: 2048,
  backgroundColor: 'white',
  elements: [
    {
      kind: 'container',
      padding: 50,
      gap: 20,
      children: [
        { kind: 'text', text: 'Product Label', fontSize: 80, fontWeight: 'bold' },
        { kind: 'image', src: '/brand-logo.png', width: 400 },
      ],
    },
  ],
})

const texture = new THREE.CanvasTexture(canvas)
const material = new THREE.MeshStandardMaterial({ map: texture })

API

createCanvas(props: CanvasProps): Promise<HTMLCanvasElement>

The single entry point. Returns a promise that resolves once all images are loaded and the canvas is fully drawn.

| Property | Type | Default | Description | |---|---|---|---| | elements | ContainerProps[] | required | Root containers to render | | width | number | 1024 | Canvas width in pixels | | height | number | 1024 | Canvas height in pixels | | backgroundColor | string | 'transparent' | Fill color for the canvas background | | grid | boolean | false | Tile the content to fill the canvas |

Browser only

declanvas uses document.createElement('canvas') and new Image() under the hood. It runs in any modern browser but does not work in Node.js without a canvas polyfill like node-canvas.

License

MIT