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 🙏

© 2024 – Pkg Stats / Ryan Hefner

canvasbuilder

v1.0.0

Published

Canvas builder

Downloads

34

Readme

canvasbuilder

canvasbuilder wraps the canvas Node.js package to help build, paint and unit-test canvases.

Installation

npm install canvasbuilder

Example

This example creates a 400x300 canvas, clears it to a white background, draws some text, then exports the canvas to a PNG file:

import { CanvasBuilder } from 'canvasbuilder';

const builder = await new CanvasBuilder()
  .setSize(400, 300)
  .beginPainting()
  .clear('white')
  .setFontSize(60)
  .fillText("Don't Panic", [25, 170])
  .export('readme-text.png');

Don't Panic

To unit-test that code, interrogate the .events property:

expect(builder.events).toEqual([
  {
    function: 'setSize',
    height: 400,
    width: 300,
  },
  {
    function: 'fillRectangle',
    rectangle: [0, 0, 400, 300],
    style: 'white',
  },
  {
    function: 'setFontSize',
    size: 60,
  },
  {
    function: 'fillText',
    at: [25, 170],
    text: "Don't Panic",
  },
  {
    function: 'export',
    to: 'readme-text.png',
  },
]);

Usage

A canvas must be prepared before it can be painted on.

To prepare a canvas, create a new CanvasBuilder then optionally call:

  • registerFont(localPath, style) registers a font to use later. The style describes the font's CSS properties, which must describe the font's family and can optionally include a style and weight.

    const builder = new CanvasBuilder()
      // Register 'Rainbow2000.ttf' as 'Rainbow'
      .registerFont('Rainbow2000.ttf', { family: 'Rainbow' });
  • setSize(width, height) sets the width and height of the canvas.

    const builder = new CanvasBuilder().setSize(1024, 768);

To begin painting on the canvas, call beginPainting().

const builder = new CanvasBuilder()
  .registerFont('Rainbow2000.ttf', { family: 'Rainbow' })
  .setSize(1024, 768)
  .beginPainting();

To paint on a prepared canvas call:

  • clear(style) clears the canvas.

  • drawImage(image, at) draws an image at the given coordinates.

    import { loadImage } from 'canvas';
    
    const image = loadImage('foo.jpg');
    
    const builder = new CanvasBuilder().beginPainting().drawImage(image, [0, 0]);
  • drawImage(image, at, source) draws a subrectangle of an image at the given coordinates. The source is an array describing the subrectangle's x, y, width and height.

    import { loadImage } from 'canvas';
    
    const image = loadImage('foo.jpg');
    
    const builder = new CanvasBuilder()
      .beginPainting()
      .drawImage(image, [0, 0], [3, 7, 22, 14]);
  • export(to) exports the canvas to a PNG file.

  • fillRectangle(rect) fills a rectangle with the current fill style. The rectangle is an array describing x, y, width and height.

  • fillRectangle(rect, style) fills a rectangle with a specific style. This does not replace the default fill style.

    const builder = await new CanvasBuilder()
      .setSize(400, 300)
      .beginPainting()
      .clear('white')
      .setFillStyle('black')
      .fillRectangle([150, 100, 100, 100], 'red')
      .fillRectangle([175, 125, 50, 50])
      .export('fill-overridden.png');

Red rectangle with inner black rectangle

  • fillText(text, at) draws text at the given coordinates.

  • setFillStyle(style) sets a new default fill style.

  • setFontFamily(family) sets a new default font family.

  • setFontSize(family) sets a new default font size.

  • setLineWidth(width) sets a new default stroke line width.

  • setStrokeStyle(width) sets a new default stroke style.

  • strokeRectangle(rect) outlines a rectangle with the current stroke style. The rectangle is an array describing x, y, width and height.

  • strokeRectangle(rect, style) outlines a rectangle with a specific style. This does not replace the default stroke style.

    const builder = await new CanvasBuilder()
      .setSize(400, 300)
      .beginPainting()
      .clear('white')
      .setStrokeStyle('black')
      .setLineWidth(6)
      .strokeRectangle([130, 80, 100, 100], { style: 'red' })
      .strokeRectangle([170, 120, 100, 100])
      .export('stroke-overridden-style.png');

Red rectangle with overlapping black rectangle

Contributions

Raise bug reports, request features and ask questions at github.com/cariad/canvasbuilder-js/issues.

The Author

Hello! 👋 I'm Cariad Eccleston, and I'm a indie + freelance software engineer down by the beach in Devon, UK.

You can find my open source projects at github.com/cariad, my resume at linkedin.com/in/cariad and Mastodon microblog at @[email protected].