Grunt task for converting a set of images into a spritesheet and corresponding CSS variables.
A folder of icons processed by grunt-spritesmith:
generates a spritesheet:
and CSS variables (available in CSS, JSON, SASS, SCSS, LESS, Stylus):
$fork_offset_x = 0px;
$fork_offset_y = 0px;
$fork_width = 32px;
$fork_height = 32px;
...
$github_offset_x = -32px;
$github_offset_y = 0px;
$github_width = 32px;
$github_height = 32px;
...grunt-spritesmith is supported and tested on Windows, Linux, and Mac OSX.
Support us via gratipay or spread word on Twitter
We have moved to pixelsmith as the default engine. It is node based and should support your sprites. Any other engines must be installed outside of spritesmith. This will lead to cleaner and faster installations.
We have moved to binary-tree as the default algorithm. We changed this to give the best possible packing out of the box. If you were using top-down as the default, please specify it in your configuration.
We have moved from destImg and destCSS to dest and destCss respectively. This adds support for grunt-newer and makes naming consistent.
We have moved the cssClass option for the css template to cssSelector. This makes it more semantically appropriate and eaiser to find.
grunt-spritesmith can be installed via npm: npm install grunt-spritesmith
Then, add and configure it to your Gruntfile.js:
module.exports = function (grunt) {
// Configure grunt
grunt.initConfig({
sprite:{
all: {
src: 'path/to/your/sprites/*.png',
dest: 'destination/of/spritesheet.png',
destCss: 'destination/of/sprites.css'
}
}
});
// Load in `grunt-spritesmith`
grunt.loadNpmTasks('grunt-spritesmith');Run the grunt sprite task:
$ grunt sprite
Running "sprite:all" (sprite) task
Files "destination/of/spritesheet.png", "destination/of/sprites.css" created.
Done, without errors.Results are a spritesheet:
and CSS:
.icon-fork {
background-image: url(spritesheet.png);
background-position: 0px 0px;
width: 32px;
height: 32px;
}
...
grunt-spritesmith is a [grunt multitask][multitask]. It supports the following parameters:
- src
String|String[]- Images to use as sprites in spritesheet- For example this can be a glob,
sprites/*.pngor an array of files['sprite1.png', sprite2.png']
- For example this can be a glob,
- dest
String- Output location for generated spritesheet - destCss
String- Output location for generated CSS - imgPath
String- Optional override for path specified in CSS- For example if
../sprite.pngis given, then the CSS will have:background-image: url(../sprite.png);
- For example if
- padding
Number- Padding to place to right and bottom between sprites- By default there is no padding
- An example usage can be found in the Examples section
- algorithm
String- Algorithm to use for positioning sprites in spritesheet- By default this is
binary-treewhich yields the best possible packing - An example usage can be found in the Examples section
- For more algorithm options, see the Algorithms section
- By default this is
- algorithmOpts
Mixed- Options to pass through to algorithm- For example we can skip sorting in some algorithms via
{algorithmOpts: {sort: false}}- This is useful for sprite animations
- See your algorithm's documentation for available options
- For example we can skip sorting in some algorithms via
- engine
String-spritesmithengine to use- By default this is
pixelsmith, anodebased engine - Alternative engines must be installed via
npm install - An example usage can be found in the Examples section
- For more engine options, see the Engines section
- By default this is
- engineOpts
Object- Options to pass through to engine for settings- For example
phantomjssmithacceptstimeoutvia{engineOpts: {timeout: 10000}} - See your engine's documentation for available options
- For example
- imgOpts
Object- Options to pass through to engine uring export- For example
gmsmithsupportsqualityvia{exportOpts: {quality: 75}} - See your engine's documentation for available options
- For example
- cssFormat
String- CSS format to use- By default this is the format inferred by
destCss'extension- For example
.styl -> stylus
- For example
- For more format options, see our formatting library
- By default this is the format inferred by
- cssTemplate
String|Function- CSS template to use for rendering output CSS- This overrides
cssFormat - If a
Stringis provided, it must be a path to a mustache template- An example usage can be found in the Examples section
- If a
Functionis provided, it must have a signature offunction (params)- An example usage can be found in the Examples section
- For more templating information, see the Templating section
- This overrides
- cssVarMap
Function- Mapping function for each filename to CSS variable- For more information, see Variable mapping
- cssOpts
Object- Options to pass through to templater- For example
{cssOpts: {functions: false}}skips output of mixins - See your template's documentation for available options
- For example
Images can be laid out in different fashions depending on the algorithm. We use layout to provide you as many options as possible. At the time of writing, here are your options for algorithm:
top-down |
left-right |
diagonal |
alt-diagonal |
binary-tree |
|---|---|---|---|---|
![]() |
![]() |
![]() |
![]() |
![]() |
More information can be found in the layout documentation:
https://github.com/twolfson/layout
The cssTemplate option allows for using a custom template. An example template can be found at:
https://github.com/twolfson/spritesheet-templates/blob/4.2.0/lib/templates/stylus.template.mustache
The parameters passed into your template are known as params. We add some normalized properties viaspritesheet-templates for your convenience.
- params
ObjectContainer for parameters- items
Object[]- Array of sprite information- name
String- Name of the sprite file (sans extension) - x
Number- Horizontal position of sprite's left edge in spritesheet - y
Number- Vertical position of sprite's top edge in spritesheet - width
Number- Width of sprite - height
Number- Height of sprite - total_width
Number- Width of entire spritesheet - total_height
Number- Height of entire spritesheet - image
String- Relative URL path from CSS to spritesheet - escaped_image
String- URL encodedimage - source_image
String- Path to the original sprite file - offset_x
Number- Negative value ofx. Useful tobackground-position - offset_y
Number- Negative value ofy. Useful tobackground-position - px
Object- Container for numeric values includingpx- x
String-xsuffixed withpx - y
String-ysuffixed withpx - width
String-widthsuffixed withpx - height
String-heightsuffixed withpx - total_width
String-total_widthsuffixed withpx - total_height
String-total_heightsuffixed withpx - offset_x
String-offset_xsuffixed withpx - offset_y
String-offset_ysuffixed withpx
- x
- options
Object- Options passed in viacssOptsingrunt-spritesmithconfig
- name
- items
An example sprite item is
{
"name": "sprite2",
"x": 10,
"y": 20,
"width": 20,
"height": 30,
"total_width": 80,
"total_height": 100,
"image": "nested/dir/spritesheet.png",
"escaped_image": "nested/dir/spritesheet.png",
"source_image": "path/to/original/sprite.png",
"offset_x": -10,
"offset_y": -20,
"px": {
"x": "10px",
"y": "20px",
"width": "20px",
"height": "30px",
"total_width": "80px",
"total_height": "100px",
"offset_x": "-10px",
"offset_y": "-20px"
}
}Example usages can be found as:
The cssVarMap option allows customization of the CSS variable names
If you would like to customize CSS selectors in the
csstemplate, please see https://github.com/twolfson/spritesheet-templates#css
Your cssVarMap should be a function with the signature function (sprite). It will receive the same parameters as items from Templating except for escaped_image, offset_x, offset_y, and px.
// Prefix all sprite names with `sprite-` (e.g. `home` -> `sprite-home`)
cssVarMap: function (sprite) {
sprite.name = 'sprite_' + sprite.name;
}
// Generates:
// $sprite_fork_x = 0px;
// $sprite_fork_y = 0px;
// As oppposed to default:
// $fork_x = 0px;
// $fork_y = 0px;An engine can greatly improve the speed of your build (e.g. canvassmith) or support obscure image formats (e.g. gmsmith).
All spritesmith engines adhere to a common specification and test suite:
https://github.com/twolfson/spritesmith-engine-test
Below is a list of known engines with their tradeoffs:
pixelsmith is a node based engine that runs on top of get-pixels and save-pixels.
Key differences: Doesn't support uncommon image formats (e.g. tiff) and not as fast as a compiled library (e.g. canvassmith).
phantomjssmith is a phantomjs based engine. It was originally built to provide cross-platform compatibility but has since been succeeded by pixelsmith.
Requirements: phantomjs must be installed on your machine and on your PATH environment variable. Visit the phantomjs website for installation instructions.
Key differences: phantomjs is cross-platform and supports all image formats.
canvassmith is a node-canvas based engine that runs on top of Cairo.
Requirements: Cairo and node-gyp must be installed on your machine.
Instructions on how to install Cairo are provided in the node-canvas wiki.
node-gyp should be installed via npm:
npm install -g node-gypKey differences: canvas has the best performance (useful for over 100 sprites). However, it is UNIX only.
gmsmith is a gm based engine that runs on top of either Graphics Magick or Image Magick.
Requirements: Either Graphics Magick or Image Magick must be installed on your machine.
For the best results, install from the site rather than through a package manager (e.g. apt-get). This avoids potential transparency issues which have been reported.
Image Magick is implicitly discovered. However, you can explicitly use it via engineOpts
{
engineOpts: {
imagemagick: true
}
}Key differences: gmsmith allows for configuring image quality whereas others do not.
In this example, we will use the alt-diagonal algorithm to layout sprites in a non-intersecting manner.
Configuration:
{
src: ['fork.png', 'github.png', 'twitter.png'],
dest: 'spritesheet.algorithm.png',
destCss: 'spritesheet.algorithm.styl',
algorithm: 'alt-diagonal'
}Output:
In this example, we will use the gmsmith engine to support obscure image formats.
Requirements:
Install gmsmith to our node_modules via npm install.
npm install gmsmithAlternatively, we can use --save or --save-dev to save to our package.json's dependencies or devDependenices.
npm install gmsmith --save # Updates {"dependencies": {"gmsmith": "1.2.3"}}
npm install gmsmith --save-dev # Updates {"devDependencies": {"gmsmith": "1.2.3"}}Configuration:
{
src: ['fork.png', 'github.png', 'twitter.png'],
dest: 'spritesheet.algorithm.png',
destCss: 'spritesheet.algorithm.styl',
engine: 'gmsmith'
}Output:
The padding option allows for inserting spacing between images.
Configuration:
{
src: ['fork.png', 'github.png', 'twitter.png'],
dest: 'spritesheet.padding.png',
destCss: 'spritesheet.padding.styl',
padding: 20 // Exaggerated for visibility, normal usage is 1 or 2
}Output:
In this example, we will use cssTemplate with a mustache template to generate CSS that uses :before selectors.
Template:
{{#items}}
.icon-{{name}}:before {
display: block;
background-image: url({{{escaped_image}}});
background-position: {{px.offset_x}} {{px.offset_y}};
width: {{px.width}};
height: {{px.height}};
}
{{/items}}Configuration:
{
src: ['fork.png', 'github.png', 'twitter.png'],
dest: 'spritesheet.mustacheStr.png',
destCss: 'spritesheet.mustacheStr.css',
cssTemplate: 'mustacheStr.css.mustache'
}Output:
.icon-fork:before {
display: block;
background-image: url(spritesheet.mustacheStr.png);
background-position: 0px 0px;
width: 32px;
height: 32px;
}
.icon-github:before {
/* ... */In this example, we will use cssTemplate with a custom function that generates YAML.
Configuration:
// var yaml = require('js-yaml');
{
src: ['fork.png', 'github.png', 'twitter.png'],
dest: 'spritesheet.yamlTemplate.png',
destCss: 'spritesheet.yamlTemplate.yml',
cssTemplate: function (params) {
// Convert items from an array into an object
var itemObj = {};
params.items.forEach(function (item) {
// Grab the name and store the item under it
var name = item.name;
itemObj[name] = item;
// Delete the name from the item
delete item.name;
});
// Return stringified itemObj
return yaml.safeDump(itemObj);
}
}Output:
fork:
x: 0
"y": 0
width: 32
height: 32
source_image: fork.png
image: spritesheet.yamlTemplate.png
total_width: 64
total_height: 64
offset_x: -0.0
offset_y: -0.0
px:
x: 0px
"y": 0px
offset_x: 0px
offset_y: 0px
height: 32px
width: 32px
total_height: 64px
total_width: 64px
escaped_image: spritesheet.yamlTemplate.png
github:
x: 32
# ...In lieu of a formal styleguide, take care to maintain the existing coding style. Add unit tests for any new or changed functionality. Lint via npm run lint and test via npm test.
GitHub and Twitter icons were taken from Alex Peattie's JustVector Social Icons.
Fork designed by P.J. Onori from The Noun Project
Plus and Equals icons were built using the Ubuntu Light typeface.
Copyright (c) 2012-2014 Ensighten
Licensed under the MIT license.




