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

ghost-canvas

v0.0.6

Published

Record canvas changes without actually rendering anything, based on jscapturecanvas

Downloads

5

Readme

ghost-canvas

NOTE: This project is only for personal use now. It's in early stage and not tested, use with your own risk.

Record canvas changes without actually rendering anything.

This project is intended to be used in a Web Worker currently. When the canvas's property changed or a method is called, a message is posted via the standard postMessage worker API.

General workflow

Create, load and use Image

Canvas painting usually involves Image manipulation. As web worker do not have Image API, this module fakes a document interface with only the createDocument method for you. So you could create a Image with no pain in worker:

Worker code

var image = document.createElement('img');
var image2 = new Image();

When a new Image is created, this module instead creates a GhostImage instance. Of course it could not do what a real image could do, only a interface. The main thread will be notified for this:

message: { type: 'create', id: 0, tag: 'img' }

When property of a image is set, the main thread will receive a message:

message: { type: 'set', id: 0, tag: 'img', key: 'src', value: 'http://example.com/1.jpg' }

The type: 'set' indicates this is a property set event, id is a unique value assigned to every interface object that is created in the worker. You should create the real objects in the main thread and keep a reference map of id - objects.

The rest params mean that the src property of our object of id=0 is set to "http://example.com/1.jpg"

Note that some code may rely on onload callback on image, so you should return the event to worker manually.

** Main thread code **

var refMap = {};
worker.onMessage = function(e) {
  var data = e.data;
  if (data.tag === 'img') {
    if (data.type === 'create') {
      var image = new Image();
      refMap[data.id] = image;
      image.onload = function() {
        worker.postMessage({ tag: 'img', type: 'onload', id: data.id })
      }
    }
    else if (data.type === 'set') {
      refMap[data.id][data.key] = data.value;
    }
  }
}

Create and use canvas

You could use Canvas and HTMLCanvasContext2d in web worker now.

** Worker code **

var canvas = document.createElement('canvas');
var context = canvas.getContext('2d');
cavans.id = 'canvas1';

Take the follow as an example:

** Worker code **

context.fillStyle = "rgb(20, 20, 20)";
context.fillRect(10, 10, 55, 50);

When fillRect() is called, a message is sent. The module compares every property on the canvas and find the ones that are different from last message, and attach them to attrs of the message.

message: {
  type: 'call',
  method: 'fillRect',
  args: [10, 10, 55, 50],
  from: 'canvas1',
  tag: 'canvas',
  attrs: {
    fillStyle: 'rgb(20, 20, 20)'
  }
}

So you should always handle attrs before method

Create patterns and gradients

Some canvas API methods creates new objects, and could be assigned to the canvas again. This is in fact similar to the situation of Image. The message returned from worker will have an additional id as a reference to use the object later.

** Worker code **

var imageData = ctx.getImageData(0, 0, 100, 100); // a GhostImageData instance is created
// the module will send a message for getImageData here
ctx.putImageData(imageData);
// the module will send a message for putImageData now, instead of sending
// the GhostImageData instance, it will send the id of it.

The message will be:

message: {
  type: 'call',
  method: 'putImageData',
  args: [ { id: 1 }, ...],
  from: 'canvas1',
  tag: 'canvas',
  attrs: {...}
}

Methods fails

drawFocusIfNeeded, measureText, isPointInPath, isPointInStroke

Lisence

MIT