PhantomJS engine for spritesmith


PhantomJS engine for spritesmith.

This depends on having phantomjs installed on your machine. This can be done globally as listed on the PhantomJS website or locally via the node module wrapper. This module has been tested against 1.9.2 and 1.9.8.

PhantomJS installation instructions can be found on the PhantomJS website. When phantomjssmith is running, phantomjs must be resolvable via the PATH environment variable.

A phantomjs wrapper can be install via: npm install phantomjs

When running your phantomjssmith task, it must have node_modules/.bin/ in the PATH environment variable. The easiest way to accomplish this is by using npm scripts:

// Inside our `package.json` 
  "scripts": {
    "build": "grunt sprite"
# Runs `grunt sprite` with `node_modules/.bin/` appended to `PATH`
npm run build

Install the module with: npm install phantomjssmith

// Convert images into phantomjssmith objects 
var images = ['img1.jpg', 'img2.png'];
phantomjssmith.createImages(this.images, function handleImages (errimgs) {
  // Create a canvas to draw onto (200 pixels wide, 300 pixels tall) 
  phantomjssmith.createCanvas(200, 200, function (errcanvas) {
    // Add each image at a specific location (upper left corner = {x, y}) 
    var coordinatesArr = [{x: 0, y: 0}, {x: 50, y: 50}];
    imgs.forEach(function (imgi) {
      var coordinates = coordinatesArr[i];
      canvas.addImage(img, coordinates.x, coordinates.y);
    }, canvas);
    // Export canvas to image 
    canvas['export']({format: 'png'}, function (errresult) {
      result; // Binary string representing a PNG image of the canvas 

This module was built to the specification for all spritesmith modules.

These are options specific phantomjssmith

  • options Object
    • timeout Number - Milliseconds to wait until automatically terminating PhantomJS script. By default, this is 10000 (10 seconds).
    • format String - Output image format to callback with. Currently, png and jpeg are available.
    • quality Number - If you are outputting a jpeg, the quality can be specified from 0 to 100.

In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint using grunt and test via npm test.

Support this project and others by twolfson via gittip.

Copyright (c) 2013 Todd Wolfson

Licensed under the MIT license.