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

canvas-dimensions

v1.0.0

Published

utilities for paper and print dimensions

Downloads

49

Readme

canvas-dimensions

A utility that provides user and screen units for a 2D Canvas.

import getDocument from "canvas-dimensions";

const settings = {
  // a standard paper size or [w, h]
  dimensions: "A4",
  // pixel resolution
  pixelsPerInch: 300,
  // a user coordinate space to work in
  units: "cm",
};

const {
  // Size in user space coordinates
  width,
  height,
  // Size in display/screen coordinates
  canvasWidth,
  canvasHeight,
} = getDocument(settings);

// Setup your 2D canvas
canvas.width = canvasWidth;
canvas.height = canvasHeight;

// Scale context to user coordinates
context.scale(canvasWidth / width, canvasHeight / height);

// Draw your graphics in user space coordinates
context.fillRect(0, 0, width, height);

Install

Use npm to install.

npm install canvas-dimensions --save

Usage

output = getDocument(settings = {})

Gets document size information from the given input options.

Input options:

  • dimensions can be a string defining a paper size preset like 'A4' or 'letter' (case insensitive), or an array of [ width, height ]
  • units a string, the output units you would like to work in, can be 'px', 'in', 'cm', 'mm' (default 'px')
  • pixelsPerInch used when converting physical sizes to canvas pixel sizes, defaults to 72
  • orientation an optional string, can be "landscape" or "portrait" and will flip the input dimensions accordingly, mostly useful if you are specifying a paper size preset. Note, the paper size presets are all portrait by default.
  • pixelRatio a factor to multiply the final canvas size by, default 1

The settings input can also just be an [ width, height ] array, which is the same as passing { dimensions: [ width, height ], units: 'px' }.

Output:

  • units a string identifying the user coordinate space, such as 'px' or 'cm'
  • width the document width in user coordinates
  • height the document height in user coordinates
  • pixelsPerInch the document resolution
  • canvasWidth the display/pixel width of the resulting canvas
  • canvasHeight the display/pixel height of the resulting canvas
  • pixelRatio the pixel ratio that has been applied to canvas size

import paperSizes from 'canvas-dimensions/paper-sizes.js'

The raw list of possible paper size keywords, see ./paper-sizes.

Recipes

Some more examples and recipes:

// 1280 x 1280 pixel image
doc = getDocument([1280, 1280]);

// Size canvas to browser size
doc = getDocument({
  dimensions: [window.innerWidth, window.innerHeight],
  pixelRatio: window.devicePixelRatio,
});

// 18 x 18 " artwork ready for print
doc = getDocument({
  dimensions: [18, 18],
  units: "in",
  pixelsPerInch: 300,
});

// A4 landscape artwork, working in millimeters
doc = getDocument({
  dimensions: "A4",
  units: "mm",
  orientation: "landscape",
  pixelsPerInch: 300,
});

License

MIT, see LICENSE.md for details.