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

drawable

v0.4.0

Published

A way to make styling canvas elements like text and images nicer

Downloads

17

Readme

Drawable

Build Status

A way to make styling canvas elements like text and images nicer. This lib uses node-canvas, please see instructions on how to get this working on your computer.

Why

The idea is to create a somewhat familiar way to style a canvas element to leverage the ability to turn the canvas drawing into an image. Images are more sharable and if done correctly more printable as well :D. This lib leverages node-canvas to be able to do this server side currently, but its goal is to eventually be universal for client and server technologies. That way you could render some awesome interactive charts and then potentially render them also into something like a Github README. Not quite there yet tho.

Be careful

This is currently in some flux, and currently only works on a node server.

Installation

npm i drawable --save

Usage

Drawable is really just a canvas that is meant to use some CSS like syntax to be able to add text and images to a canvas.

Creating a drawable

import Drawable from 'drawable'

const drawable = new Drawable({ width: 200, height: 200, backgroundColor: 'white' });

There are a few styles that are passed here: width, height, and backgroundColor. This does exactly what you would think it would do. It makes a canvas with the width of 200px height of 200px and the backgroundColor of white. If you have used canvas before you would notice that backgroundColor is not a thing. All this is really doing is creating a rectangle that fills the background of the canvas with a fill of the color passed. By default this is transparent.

Styles for constructor

| Styles key | description | default | |-----------------|------------------------------------|-------------| | width | The width of the canvas | 0 | | height | The height of the canvas | 0 | | backgroundColor | The background color of the canvas | transparent |

Adding in some text

const text = Drawable.text('Here is some text', {
  fontSize: 12,
  top: 20,
  left: 20,
  right: 20,
  textAlign: 'center'
});

// append text to drawable instance
drawable.append(text);

This is where the magic of drawable starts to shine. With drawable text we automatically will wrap text based on the size of the canvas and the values passed in for the right and left options. Then we calculate the alignment of the text using textAlign option.

Styles for text

| Styles key | Description | Default | |------------|-------------------------------------------|------------| | top | The top alignment of the text container | 0 | | left | The left alignment of the text container | 0 | | right | The right alignment of the text container | 0 | | color | The color of the text | #222 | | textAlign | Can be: 'center', 'left', or 'right' | left | | lineHeight | The line height of the text | 0 | | fontSize | The size of the text in pixels | 0 | | fontFamily | The font used in the canvas | sans-serif |

Adding in a image

const image = Drawable.image('./path/to/image.jpg', {
  top: 20,
  left: 20,
});

// append text to drawable instance
drawable.append(image);

Images are pretty basic, you give the image a left and top coordinate and it should render. If you are trying to emulate something like a background image you could also use the objectFit style which should allow you to cover or contain the image in the canvas.

Styles for image

| Styles key | Description | Default | |------------|----------------------------------------------------------------------------------------------------------------------------------|---------------| | top | The top alignment of the image | 0 | | left | The left alignment of the image | 0 | | width | The width of the image | images.width | | height | The height of the image | images.height | | objectFit | object-fit css current values supported are "cover" and "contain" | none |

Note: when using object fit it will override the left, top, width, and height values.

TODO

  • make api work on both client and server
  • solidify output methods toBuffer and toDataURI
  • make a public way to expose canvas element ( for appending to DOM )
  • make a few adapters to work with react and ember