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.2.1

Published

A dependency-free TypeScript library for rendering declarative, Flexbox-inspired layouts to HTML Canvas

Readme

declanvas

A small, dependency-free TypeScript library for rendering declarative, Flexbox-inspired layouts to an HTMLCanvasElement.

Install

npm install declanvas

Version 0.2 rewrite

Version 0.2 is a complete rewrite with a new typed scene model and an ESM-only package. It replaces the 0.1 createCanvas API with renderCanvas; nodes now use type, style, and children instead of the earlier kind and elements shape.

Example

import { renderCanvas, type CanvasScene } from "declanvas";

const scene = {
  width: 1200,
  height: 630,
  backgroundColor: "#f7f7f8",
  style: {
    flexDirection: "row",
    alignItems: "center",
    justifyContent: "space-between",
    gap: 32,
    padding: 48,
  },
  children: [
    {
      type: "container",
      style: { flexGrow: 1, gap: 16 },
      children: [
        {
          type: "text",
          text: "Build canvases declaratively.",
          style: {
            width: "100%",
            color: "#161618",
            fontFamily: "Inter, sans-serif",
            fontSize: 56,
            fontWeight: 700,
            lineHeight: 64,
            maxLines: 2,
            textOverflow: "ellipsis",
          },
        },
        {
          type: "text",
          text: "Typed objects in, HTMLCanvasElement out.",
          style: { color: "#65656b", fontSize: 24 },
        },
      ],
    },
    {
      type: "image",
      src: "/product.png",
      crossOrigin: "anonymous",
      style: {
        width: 360,
        height: 360,
        maxWidth: "40%",
        objectFit: "contain",
        objectPosition: "center",
      },
    },
  ],
} satisfies CanvasScene;

const canvas = await renderCanvas(scene);
document.body.append(canvas);

API

renderCanvas(scene, options?)

Returns a Promise<HTMLCanvasElement>. Rendering waits for image URLs and fonts registered in document.fonts. The promise rejects with CanvasRenderError if validation or asset loading fails.

options.pixelRatio overrides devicePixelRatio. options.document is useful for rendering against another browser document.

Nodes

  • container lays out its children in a row or column.
  • text measures, wraps, truncates, and paints styled text.
  • image accepts a URL or an existing HTMLImageElement and supports CSS-like object fitting.

Every node is a box. Sizes accept non-negative pixel numbers, percentage strings such as "50%", and "auto" where applicable. The box model supports padding, margins, min/max sizes, backgrounds, uniform borders, rounded corners, opacity, and clipping.

Layout behavior

The layout engine intentionally implements a useful subset of Flexbox:

  • flexDirection: row or column
  • justifyContent: flex-start, center, flex-end, space-between, space-around, or space-evenly
  • alignItems and alignSelf: flex-start, center, flex-end, or stretch
  • gap, flexGrow, flexShrink, and flexBasis
  • Auto margins on the main axis

It does not implement wrapping, reverse directions, order, positioned layout, margin collapsing, CSS inheritance, or the complete CSS sizing algorithm. Percentages resolve against the corresponding parent content dimension; a percentage whose parent dimension is not yet definite behaves as auto.

Numeric dimensions use logical CSS pixels. The returned canvas has logical CSS dimensions and a high-resolution backing store.

Development

npm run typecheck
npm test
npm run build