Utility that takes sprites and converts them into a stylesheet and its coordinates
Installations
npm install spritesmith
Releases
Unable to fetch releases
Developer
twolfson
Developer Guide
Module System
CommonJS
Min. Node Version
>= 4.0.0
Typescript Support
No
Node Version
16.16.0
NPM Version
8.11.0
Statistics
917 Stars
580 Commits
56 Forks
43 Watching
4 Branches
7 Contributors
Updated on 26 Nov 2024
Languages
JavaScript (100%)
Total Downloads
Cumulative downloads
Total Downloads
27,533,083
Last day
-3.3%
11,414
Compared to previous day
Last week
-2.3%
57,690
Compared to previous week
Last month
13.3%
281,688
Compared to previous month
Last year
-39.5%
3,205,472
Compared to previous year
Daily Downloads
Weekly Downloads
Monthly Downloads
Yearly Downloads
spritesmith
Convert images into spritesheets and coordinate maps.
spritesmith
is also available as:
A folder of icons processed by spritesmith
:
generates a spritesheet:
and a coordinate map:
1{ 2 "/home/todd/github/spritesmith/docs/fork.png": { 3 "x": 0, 4 "y": 0, 5 "width": 32, 6 "height": 32 7 }, 8 "/home/todd/github/spritesmith/docs/github.png": { 9 "x": 32, 10 "y": 0, 11 "width": 32, 12 "height": 32 13 }, 14 // ... 15}
Do you like spritesmith
?
Support us via donations or spread word on Twitter
Breaking changes in 2.0.0
We have moved from result.image
being a binary string to it being a Buffer
. This is to use more canonical conventions in Node.js.
We have moved from spritesmith-engine-spec@1.1.0 to spritesmith-engine-spec@2.0.0. This means if you use an custom engine (e.g. gmsmith
, canvassmith
), then you will need to upgrade it.
1npm install my-engine-smith@latest --save-dev
By upgrading the engine, we added support for Vinyl objects via src
as well as future-proof ourselves for forwarding streaming outputs.
Breaking changes in 3.0.0
We have updated our API to return streams for images. This required moving to a constructor
and splitting apart image creation and processing.
We have maintained legacy support for spritesmith
via Spritesmith.run
which has an identical API to the spritesmith
function in spritesmith<3.0.0
.
1// Before 2var spritesmith = require('spritesmith'); 3spritesmith({src: sprites}, function handleResult (err, result) { /* ... */ }); 4 5// After 6var Spritesmith = require('spritesmith'); 7Spritesmith.run({src: sprites}, function handleResult (err, result) { /* ... */ });
Getting started
spritesmith
can be installed via npm: npm install spritesmith
1// Load in dependencies 2var Spritesmith = require('spritesmith'); 3 4// Generate our spritesheet 5var sprites = ['fork.png', 'github.png', 'twitter.png']; 6Spritesmith.run({src: sprites}, function handleResult (err, result) { 7 result.image; // Buffer representation of image 8 result.coordinates; // Object mapping filename to {x, y, width, height} of image 9 result.properties; // Object with metadata about spritesheet {width, height} 10});
Usage with streaming output
We support streaming output by breaking down run
into 2 parts:
1// Load in dependencies 2var Spritesmith = require('spritesmith'); 3 4// Create a new spritesmith and process our images 5var sprites = ['fork.png', 'github.png', 'twitter.png']; 6var spritesmith = new Spritesmith(); 7spritesmith.createImages(sprites, function handleImages (err, images) { 8 images[0].width; // Width of image 9 images[0].height; // Height of image 10 11 // Create our result 12 var result = spritesmith.processImages(images); 13 result.image; // Readable stream outputting image 14 result.coordinates; // Object mapping filename to {x, y, width, height} of image 15 result.properties; // Object with metadata about spritesheet {width, height} 16});
Documentation
spritesmith
exports a Spritesmith
constructor as its module.exports
.
If you would like a faster build time or need to support an obscure image format, see params.engine
.
If you would like to adjust how images are laid out, see params.algorithm
and params.algorithmOpts
.
Spritesmith.run(params, callback)
Helper function that initializes a new Spritesmith
instance, creates images, and processes them into a spritesheet
- params
Object
- Container for parameters- src
String[]|Object[]
- Same assrc
forspritesmith.createImages
- All other parameters accepted by
new Spritesmith
orprocessImages
should be passed in here (e.g.engine
,algorithm
)
- src
- callback
Function
- Error-first function that receives compiled spritesheet and informationcallback
should have signaturefunction (err, result)
- err
Error|null
- If an error occurred, this will be it - result
Object
- Container for result items- Same signature as content returned by
spritesmith.processImages
(i.e.{image, coordinates, properties}
) - image
Buffer
- In-memory representation of image - coordinates
Object
- Same ascoordinates
returned byspritesmith.processImages
- properties
Object
- Same asproperties
returned byspritesmith.processImages
- Same signature as content returned by
new Spritesmith(params)
Constructor for a new Spritesmith
instance
- params
Object
- Container for parameters- engine
String|Object
- Optional engine override to use- By default we use
pixelsmith
, a node-basedspritesmith
engine - An example usage of
engine
can be found in the Examples section - For more engine options, see the Engines section
- By default we use
- engineOpts
Object
- Options to pass through to engine for settings- For example
phantomjssmith
acceptstimeout
via{engineOpts: {timeout: 10000}}
- See your engine's documentation for available options
- For example
- engine
spritesmith.createImages(src, callback)
Interpret images via the spritesmith
engine
- src
String[]|Object[]
- Array of filepaths for images to include in spritesheet- If a
String
is provided, then it's used as the image's filepath - If an
Object
is provided, then it should be a Vinyl object pointing to the source image- Depending on the engine, we may/may not use the contents (e.g.
gmsmith
uses filepaths only)
- Depending on the engine, we may/may not use the contents (e.g.
- If a
- callback
Function
- Error-first function that receives compiled spritesheet and mapcallback
should have signaturefunction (err, images)
- err
Error|null
- If an error occurred, this will be it - images
Object[]
- Array of processed images- Each
image
will be a proprietary object for the engine - Each
image
will line up with the specification from spritesmith-engine-spec - image
Object
- Metadata container about corresponding input image at same index- height
Number
- Height in pixels of corresponding input image at same index - width
Number
- Width in pixels of corresponding input image at same index
- height
- Each
spritesheet.processImages(images, options)
Place interpretted images on a canvas and export spritesheet
- images
Object[]
- Images generated viaspritesmith.createImages
- options
Object
- Container for options- padding
Number
- Padding to use between images- For example if
2
is provided, then there will be a2px
gap to the right and bottom between each image - An example usage of
padding
can be found in the Examples section
- For example if
- exportOpts
Mixed
- Options to pass through to engine for export- For example
gmsmith
supportsquality
via{exportOpts: {quality: 75}}
- See your engine's documentation for available options
- For example
- algorithm
String
- Optional algorithm to pack images with- By default we use
binary-tree
which packs images as efficiently as possible - An example usage of
algorithm
can be found in the Examples section - For more algorithm options, see the Algorithms section
- By default we use
- algorithmOpts
Object
- Optional algorithm options to pass through to algorithm for layout- For example
top-down
supports ignoring sorting via{algorithmOpts: {sort: false}}
- See your algorithm's documentation for available options
- For example
- padding
Returns:
- result
Object
- Container for result information- image
ReadableStream
- Readable stream outputting generated image contents - coordinates
Object
- Map from filepath to coordinate information between original sprite and spritesheetfilepath
will be the same as provided inparams.src
- [filepath]
Object
- Container for coordinate information- For those keeping track, this is
result.coordinates[filepath]
- x
Number
- Horizontal position of top-left corner of original sprite on spritesheet - y
Number
- Vertical position of top-left corner of original sprite on spritesheet - width
Number
- Width of original sprite - height
Number
- Height of original sprite
- For those keeping track, this is
- properties
Object
- Container for information about spritesheet- width
Number
- Width of the spritesheet - height
Number
- Height of the spritesheet
- width
- image
Algorithms
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 params.algorithm
:
top-down | left-right | diagonal | alt-diagonal | binary-tree |
---|---|---|---|---|
More information can be found in the layout
documentation:
https://github.com/twolfson/layout
Engines
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:
https://github.com/twolfson/spritesmith-engine-spec
This repository adheres to specification version: 2.0.0
Below is a list of known engines with their tradeoffs:
pixelsmith
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
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
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
:
1npm install -g node-gyp
Key differences: canvas
has the best performance (useful for over 100 sprites). However, it is UNIX
only.
[node-canvas wiki]: (https://github.com/LearnBoost/node-canvas/wiki/_pages [node-gyp]: https://github.com/TooTallNate/node-gyp/
gmsmith
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
1{ 2 engineOpts: { 3 imagemagick: true 4 } 5}
Key differences: gmsmith
allows for configuring image quality whereas others do not.
Examples
Algorithm
This is an example of using a custom layout via the alt-diagonal
algorithm.
1// Load in dependencies 2var fs = require('fs'); 3var Spritesmith = require('spritesmith'); 4 5// Generate our spritesheet 6Spritesmith.run({ 7 src: [ 8 __dirname + '/fork.png', 9 __dirname + '/github.png', 10 __dirname + '/twitter.png' 11 ], 12 algorithm: 'alt-diagonal' 13}, function handleResult (err, result) { 14 // If there was an error, throw it 15 if (err) { 16 throw err; 17 } 18 19 // Output the image 20 fs.writeFileSync(__dirname + '/alt-diagonal.png', result.image); 21 result.coordinates, result.properties; // Coordinates and properties 22});
Result:
Engine
This is an example of using a custom engine (canvassmith
in this case).
1// Inside package.json 2{ 3 "dependencies": { 4 "canvassmith": "~0.2.4" 5 } 6}
1// In our script 2// Load in dependencies 3var fs = require('fs'); 4var Spritesmith = require('spritesmith'); 5 6// Generate our spritesheet 7Spritesmith.run({ 8 src: [ 9 __dirname + '/fork.png', 10 __dirname + '/github.png', 11 __dirname + '/twitter.png' 12 ], 13 engine: require('canvassmith') 14}, function handleResult (err, result) { 15 // If there was an error, throw it 16 if (err) { 17 throw err; 18 } 19 20 // Output the image 21 fs.writeFileSync(__dirname + '/canvassmith.png', result.image); 22 result.coordinates, result.properties; // Coordinates and properties 23});
Result:
Padding
This is an example of adding padding between images.
1// Load in dependencies 2var fs = require('fs'); 3var Spritesmith = require('spritesmith'); 4 5// Generate our spritesheet 6Spritesmith.run({ 7 src: [ 8 __dirname + '/fork.png', 9 __dirname + '/github.png', 10 __dirname + '/twitter.png' 11 ], 12 padding: 20 // Exaggerated for visibility, normally 1 or 2 13}, function handleResult (err, result) { 14 // If there was an error, throw it 15 if (err) { 16 throw err; 17 } 18 19 // Output the image 20 fs.writeFileSync(__dirname + '/padding.png', result.image); 21 result.coordinates, result.properties; // Coordinates and properties 22});
Result:
Contributing
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
.
Attribution
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.
License
Copyright (c) 2012 Todd Wolfson
Licensed under the MIT license.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
- Info: project has a license file: LICENSE-MIT:0
- Info: FSF or OSI recognized license: MIT License: LICENSE-MIT:0
Reason
5 commit(s) and 2 issue activity found in the last 90 days -- score normalized to 5
Reason
Found 3/30 approved changesets -- score normalized to 1
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
- Warn: no security policy file detected
- Warn: no security file to analyze
- Warn: no security file to analyze
- Warn: no security file to analyze
Reason
project is not fuzzed
Details
- Warn: no fuzzer integrations found
Reason
branch protection not enabled on development/release branches
Details
- Warn: branch protection not enabled for branch 'master'
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
- Warn: 0 commits out of 3 are checked with a SAST tool
Score
3.8
/10
Last Scanned on 2024-11-18
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More