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

canvas-utils

v0.8.0

Published

Utility functions for the HTML5 canvas

Readme

canvas-utils

NPM version Code Climate Build Status devDependency Status Coverage Status

Utility functions for working with the HTML5 canvas

functions

var canvasUtils = require('canvas-utils');

canvasUtils.convertEventCoords(e, canvas)

This function returns the coordinates of mouse events relative to a DOM element. For example a Canvas.

e is a MouseEvent.

canvas is a a DOM element.

returns an Object with a x and y property representing the pixel on the canvas that the event was fired on.

canvas.addEventListener('click', function(event) {
  var xy = convertEventCoords(event, canvas);
  canvas.getContext('2d').fillRect(xy.x,xy.y,10,10)
});

This is a little verbose. For a nicer way to do this have a look at the CanvasEventEmitter

canvasUtils.rotateContextAt(ctx, x, y, rotation)

Rotates a context around a point.

ctx is a CanvasRenderingContext2D. (That's the thing you get when you do canvas.getContext('2d'))

x is the x-coordinate of the rotation.

y is the y-coordinate.

rotation is the rotation in radians.

Returns the ctx passed in.

canvasUtils.radiansToDegrees(radians)

Converts radiansto degrees.

canvasUtils.degreesToRadians(degrees)

Converts degreesto radians.

CanvasEventEmitter

var createCanvasEventEmitter = require('canvas-utils').createCanvasEventEmitter;

var cee = createCanvasEventEmitter(target, eventSource = window.document);

target is the canvas for which events should be emitted.

eventSource is the element where the event listeners are attached. It defaults to window.document.

Demo for the CanvasEventEmitter on requirebin

Events

A CanvasEventEmitter is a jvent EventEmitter. jvent is a small EventEmitter that behaves pretty much like a standard node EventEmitter. It emits the following events:

| Events with synonyms |
|-----------------------------| | mouseup | | mousedown | | mousemove | | mouseover, mousein | | mouseleave, mouseout | | click | | leftclick | | contextmenu, rightclick |

The event object you get for all of these looks like this:

| Property | Description | |----------------|-------------------------------------------------------| | x | the x coordinate of the event relative to the canvas | | y | the y coordinate of the event relative to the canvas | | target | the canvas the event belongs to (The target you passed into the constructor) | | event | the original DOM event emitted on the eventSource | | button | the button property from the event | | preventDefault | a function that calles preventDefault on the event |

cee.on('click',function(e){
  e.preventDefault();
  ctx.fillRect(e.x-5,e.y-5,10,10);
});

Release History

  • 2018-02-16   v0.8.0   Use jvent and add synonyms for some events.
  • 2018-02-08   v0.7.0   Add imageDataHelper.
  • 2015-03-07   v0.6.0   Is now written in ES6.
  • 2014-11-02   v0.5.0   Add radiansToDegrees and degreesToRadians.
  • 2014-10-13   v0.4.0   Add mouseover and mouseout events.
  • 2014-10-06   v0.3.2   Fix README typo.
  • 2014-10-06   v0.3.1   Fix e.button and e.preventDefault().
  • 2014-10-06   v0.3.0   Improve convertEventCoords.
  • 2014-09-26   v0.2.0   Add contextmenu event.
  • 2014-09-12   v0.1.0   Add CanvasEventEmitter.
  • 2014-08-16   v0.0.1   Initial version.