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

@ydant/canvas

v0.3.0

Published

Canvas2D rendering target for Ydant

Readme

@ydant/canvas

Canvas2D rendering target for Ydant. Builds a virtual shape tree and paints it to a Canvas2D context.

Installation

pnpm add @ydant/canvas

Usage

import { scope } from "@ydant/core";
import { createBasePlugin } from "@ydant/base";
import { createCanvasBackend, createCanvasPlugin, group, rect, circle } from "@ydant/canvas";

const canvas = createCanvasBackend();

scope(canvas, [createBasePlugin(), createCanvasPlugin()]).mount(() =>
  group(() => [
    rect({ x: "10", y: "20", width: "100", height: "50", fill: "#ff0000" }),
    circle({ cx: "200", cy: "100", r: "30", fill: "#0000ff" }),
  ]),
);

// Paint to a canvas element
const canvasEl = document.getElementById("canvas") as HTMLCanvasElement;
canvas.paint(canvasEl.getContext("2d")!);

The typical pattern is: mount (builds the virtual shape tree) then paint (draws to Canvas2D).

API

createCanvasBackend()

function createCanvasBackend(): CanvasBackend;

interface CanvasBackend extends Backend<"tree" | "decorate" | "schedule"> {
  readonly root: VShapeRoot;
  paint(ctx: CanvasRenderingContext2D): void;
}

Creates a rendering backend for Canvas2D.

  • root — The virtual root node (managed internally by the backend).
  • paint(ctx) — Clears the canvas and draws all shapes. Call this after mounting or on each animation frame.

createCanvasPlugin()

function createCanvasPlugin(): Plugin;

Plugin that processes "shape" spell requests for Canvas rendering. Delegates to the shared processNode utility, creating nodes via ctx.tree.createElement and applying decorations.

Required alongside createBasePlugin() when using canvas shape factories.

Shape

type Shape = Tagged<
  "shape",
  {
    tag: string;
    children: Render;
    decorations?: Array<Attribute | Listener>;
    key?: string | number;
  }
>;

The DSL request type for canvas shapes. Created internally by shape factories (rect, circle, etc.) and processed by createCanvasPlugin().

Shape Factories

All shape factories accept an optional Props object as the first argument and a Builder as the last. Props are string key-value pairs for shape attributes.

| Factory | Tag | Props | | --------------------- | ----------- | ------------------------------------------------------------------------------------- | | group(builder) | "group" | Container only — positions children relative to itself | | rect(builder) | "rect" | x, y, width, height, rx, fill, stroke, lineWidth, opacity | | circle(builder) | "circle" | cx, cy, r, fill, stroke, lineWidth, opacity | | ellipse(builder) | "ellipse" | cx, cy, rx, ry, fill, stroke, lineWidth, opacity | | line(builder) | "line" | x1, y1, x2, y2, stroke, lineWidth, opacity | | canvasPath(builder) | "path" | d (SVG path data), fill, stroke, lineWidth, opacity | | canvasText(builder) | "text" | x, y, content, font, fill, stroke, textAlign, textBaseline, opacity |

paintShape(ctx, shape)

function paintShape(ctx: CanvasRenderingContext2D, shape: VShape): void;

Low-level function that paints a single VShape (and its children) to a Canvas2D context. Used internally by CanvasBackend.paint(), but exported for advanced use cases.

VShape Types

interface VShape {
  kind: "shape";
  tag: string;
  props: Map<string, string>;
  children: VShape[];
}

interface VShapeRoot {
  kind: "root";
  children: VShape[];
}

type VShapeContainer = VShape | VShapeRoot;

Limitations

  • No event handlinginteract capability is not provided. Event listeners inside a canvas shape have no effect at runtime, and cause a compile-time error when the generator type is narrow enough for CapabilityCheck to detect it. Hit-testing could be added in the future.
  • All props are strings — Numeric values are passed as strings and parsed internally during paint.
  • No incremental updates — Each paint() call clears the entire canvas and redraws. For animations, call paint() in a requestAnimationFrame loop.