Spritesmith - Spritesheet Generator

3.5.1 · active · verified Sun Apr 19

Spritesmith is a Node.js utility for programmatically generating spritesheets and corresponding coordinate maps from individual image files. It provides a robust solution for optimizing web assets by combining multiple small images into a single larger image, reducing HTTP requests and improving load times. The current stable version is 3.5.1, and while there isn't a fixed release cadence, major versions introduce significant API changes and improvements, as seen with versions 2.0.0 and 3.0.0. A key differentiator is its pluggable engine architecture, allowing users to switch between various image processing backends like `pixelsmith` (default Node.js-based), `gmsmith`, or `canvassmith` to suit performance or format requirements. It also offers flexible output formats for coordinate data, making it adaptable for integration with various CSS preprocessors and build tools such as Grunt and Gulp via dedicated plugins, and even Webpack.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to generate a spritesheet from a list of image paths and save the resulting image and coordinate data to disk. It uses the `Spritesmith.run` helper function for a simplified API call.

const Spritesmith = require('spritesmith');
const path = require('path');
const fs = require('fs');

// Create dummy image files for demonstration
const dummyImageDir = path.join(__dirname, 'temp-icons');
if (!fs.existsSync(dummyImageDir)) fs.mkdirSync(dummyImageDir);
fs.writeFileSync(path.join(dummyImageDir, 'icon1.png'), Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNkYAAAAAYAAjCB0C8AAAAASUVORK5CYII=', 'base64'));
fs.writeFileSync(path.join(dummyImageDir, 'icon2.png'), Buffer.from('iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mP8/x8AAwAB/2N6HBYAAAAASUVORK5CYII=', 'base64'));

const sprites = [
  path.join(dummyImageDir, 'icon1.png'),
  path.join(dummyImageDir, 'icon2.png')
];

Spritesmith.run({ src: sprites }, function handleResult (err, result) {
  if (err) {
    console.error('Spritesmith error:', err);
    return;
  }

  // result.image is a Buffer representation of the generated spritesheet
  fs.writeFileSync('spritesheet.png', result.image);
  console.log('Spritesheet saved to spritesheet.png');
  console.log('Coordinates:', result.coordinates);
  console.log('Properties:', result.properties);

  // Clean up dummy images
  fs.unlinkSync(path.join(dummyImageDir, 'icon1.png'));
  fs.unlinkSync(path.join(dummyImageDir, 'icon2.png'));
  fs.rmdirSync(dummyImageDir);
});

view raw JSON →