Gathering detailed insights and metrics for gulp.spritesmith
Gathering detailed insights and metrics for gulp.spritesmith
Gathering detailed insights and metrics for gulp.spritesmith
Gathering detailed insights and metrics for gulp.spritesmith
@types/gulp.spritesmith
TypeScript definitions for gulp.spritesmith
gulp.spritesmith-multi
A wrapper for gulp.spritesmith to generate multiple sprites and stylesheets
gulp-spritesmith-tpls
my css template for gulp.spritesmith-tpls
webpack-spritesmith
Webpack plugin that converts set of images into a spritesheet and SASS/LESS/Stylus mixins
Convert a set of images into a spritesheet and CSS variables via gulp
npm install gulp.spritesmith
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
1,075 Stars
404 Commits
81 Forks
32 Watching
4 Branches
6 Contributors
Updated on 15 Nov 2024
Minified
Minified + Gzipped
JavaScript (63.12%)
SCSS (20.74%)
CSS (9.39%)
Stylus (6.05%)
Handlebars (0.71%)
Cumulative downloads
Total Downloads
Last day
-5%
2,372
Compared to previous day
Last week
3.5%
13,538
Compared to previous week
Last month
-4.1%
60,683
Compared to previous month
Last year
-59%
866,369
Compared to previous year
Convert a set of images into a spritesheet and CSS variables via gulp
This is the official port of grunt-spritesmith, the grunt equivalent of a wrapper around spritesmith.
Alternative output formats include SASS, Stylus, LESS, and JSON.
As of gulp.spritesmith@3.5.0
, retina spritesheets/templates are supported. See the Retina parameters section for more information.
gulp.spritesmith
?Support us via donations or spread word on Twitter
We are normalizing sprite variables even further to convert any non-alphanumeric/non-dash/non-underscore character to a delimiter character (e.g. -
). This allows us to support naming retina sprites with @2x
suffixes, to prevent regressions like grunt-spritesmith#137.
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
This is enables us to use streaming outputs from engines in a future release.
Additionally, we have added support for buffer
and stream
content for in-memory engines (e.g. pixelsmith
, canvassmith
) which resolves #53.
We have completed our integration with streaming outputs from engines. As a result, Vinyl img
files will have stream
contents which were previously buffers.
If your img
pipeline requires Buffer
contents, then this can be remedied via vinyl-buffer:
1// Throws error due to not supporting streams 2spriteData.img.pipe(imagemin()); 3 4// Back to operational 5var buffer = require('vinyl-buffer'); 6spriteData.img.pipe(buffer()).pipe(imagemin());
Gulp 5.0.0 introduced default encodings for src
and dest
streams of utf8
.
Since gulp.spritesmith
inputs and outputs images, we need to set {encoding: false}
for the image .src()
and .dest()
streams.
See "Getting Started" for an hands-on example.
Install the module with: npm install gulp.spritesmith
1var gulp = require('gulp'); 2var spritesmith = require('gulp.spritesmith'); 3 4gulp.task('sprite', function () { 5 var spriteData = gulp.src('images/*.png', {encoding: false}).pipe(spritesmith({ 6 imgName: 'sprite.png', 7 cssName: 'sprite.css' 8 })); 9 return spriteData.pipe(gulp.dest('path/to/output/', {encoding: false})); 10});
In addition to the spriteData
stream, we offer individual streams for images and CSS. This allows for image optimization and CSS minification.
1var gulp = require('gulp'); 2var buffer = require('vinyl-buffer'); 3var csso = require('gulp-csso'); 4var imagemin = require('gulp-imagemin'); 5var merge = require('merge-stream'); 6 7var spritesmith = require('gulp.spritesmith'); 8 9gulp.task('sprite', function () { 10 // Generate our spritesheet 11 var spriteData = gulp.src('images/*.png', {encoding: false}).pipe(spritesmith({ 12 imgName: 'sprite.png', 13 cssName: 'sprite.css' 14 })); 15 16 // Pipe image stream through image optimizer and onto disk 17 var imgStream = spriteData.img 18 // DEV: We must buffer our stream into a Buffer for `imagemin` 19 .pipe(buffer()) 20 .pipe(imagemin()) 21 .pipe(gulp.dest('path/to/image/folder/', {encoding: false})); 22 23 // Pipe CSS stream through CSS optimizer and onto disk 24 var cssStream = spriteData.css 25 .pipe(csso()) 26 .pipe(gulp.dest('path/to/css/folder/')); 27 28 // Return a merged stream to handle both `end` events 29 return merge(imgStream, cssStream); 30});
gulp.spritesmith
presents the spritesmith
function as its module.exports
.
spritesmith(params)
gulp plugin that returns a transform stream with 2 readable stream properties.
The input/output streams interact with Vinyl objects which are gulp's format of choice.
Object
- Container for gulp.spritesmith
parameters
String
- Filename to save image as
.png
and .jpg/jpeg
(limited to specfic engines)imgOpts.format
String
- Filename to save CSS as
String
- Optional path to use in CSS referring to image locationNumber
- Optional amount of pixels to include between images
0
)String
- Optional method for how to pack images
binary-tree
, which packs images as efficiently as possibleObject
- Options to pass through to algorithm
{algorithmOpts: {sort: false}}
String
- Optional image generating engine to use
pixelsmith
, a node
based engine that supports all common image formatsnpm install
Object
- Options to pass through to engine for settings
phantomjssmith
accepts timeout
via {engineOpts: {timeout: 10000}}
Object
- Options to pass through to engine uring export
gmsmith
supports quality
via {imgOpts: {quality: 75}}
String
- CSS format to use
cssName's
extension
.styl -> stylus
String|Function
- CSS template to use for rendering output CSS
cssFormat
String
is provided, it must be a path to a handlebars template
Function
is provided, it must have a signature of function (data)
Object
- Container for helpers to register to handlebars for our template
{half: function (num) { return num/2; }
will add a handlebars helper that halves numbersFunction
- Mapping function for each filename to CSS variable
String
- Name to use for spritesheet related variables in preprocessor templatesObject
- Options to pass through to templater
{cssOpts: {functions: false}}
skips output of mixinsReturns:
stream.Transform
- Stream that outputs image and CSS as Vinyl objectsstream.Readable
- Stream for image output as a Vinyl object
contents
will be a Stream
stream.Readable
- Stream for CSS output as a Vinyl object
contents
will be a Buffer
gulp.spritesmith
supports retina spritesheet generation via retinaSrcFilter
and retinaImgName
. If at least one of these is provided, then we will expect the other and enable retina spritesheet generation.
Repeated parameters have the same properties as above but are repeated for clarity with respect to retina spritesheets.
An example retina spritesheet setup can be found in the Examples section.
We receive both normal and retina sprites from the same gulp.src
so please include them in your original glob. (e.g. *.png
should include icon-home.png
and icon-home@2x.png
).
We strongly encourage using the @2x
suffix for retina sprites over -retina
or -2x
. There are known ordering issues caused when sharing a -
delimiter between sprite names and the retina suffix (see grunt-spritesmith#137).
Object
- Container for gulp.spritesmith
parameters
String|String[]
- Filepaths to filter out from incoming stream for our retina spritesheet
src
(e.g. sprite/*@2x.png
)gulp.src
(e.g. gulp.src('sprite/*.png', {encoding: false})
, retinaSrcFilter: 'sprite/*@2x.png'
)sprites/*@2x.png
will filter out sprite1@2x.png
for a separate retina spritesheet
sprite1.png
and sprite1@2x.png
as a group of normal/retina spritesString
- Filename to save retina spritesheet asString
- Optional path to use in CSS referring to image location
../sprite@2x.png
will yield CSS with:
background-image: url(../sprite@2x.png);
Number
- Padding to place to right and bottom between sprites
cssName's
extension
.styl -> stylus_retina
Function
- Mapping function for each filename to CSS variable
$icon-home
will have group $icon-home-group
)String
- Name to use for retina spritesheet related variables in preprocessor templatesString
- Name to use for retina groups related variables in preprocessor templatesReturns:
stream.Transform
- Stream that outputs image, retina image, and CSS as Vinyl objectsstream.Readable
- Stream for image outputs (normal and retina) as a Vinyl object
contents
will be a Stream
stream.Readable
- Stream for retina CSS output as a Vinyl object
contents
will be a Buffer
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:
The parameters passed into your template are known as data
. We add some normalized properties via spritesheet-templates
for your convenience.
Object
Container for parameters
Object[]
- Array of sprite information
String
- Name of the sprite file (sans extension)Number
- Horizontal position of sprite's left edge in spritesheetNumber
- Vertical position of sprite's top edge in spritesheetNumber
- Width of spriteNumber
- Height of spriteNumber
- Width of entire spritesheetNumber
- Height of entire spritesheetString
- Relative URL path from CSS to spritesheetString
- URL encoded image
String
- Path to the original sprite fileNumber
- Negative value of x
. Useful to background-position
Number
- Negative value of y
. Useful to background-position
Object
- Container for numeric values including px
String
- x
suffixed with px
String
- y
suffixed with px
String
- width
suffixed with px
String
- height
suffixed with px
String
- total_width
suffixed with px
String
- total_height
suffixed with px
String
- offset_x
suffixed with px
String
- offset_y
suffixed with px
Object
- Information about spritesheet
Number
- Width of entire spritesheetNumber
- Height of entire spritesheetString
- Relative URL path from CSS to spritesheetString
- URL encoded image
Object
- Container for numeric values including px
String
- width
suffixed with px
String
- height
suffixed with px
Object
- Container for spritesheet
metadata and its representation
String
- Prefix for spritesheet variablesObject[]
- Array of retina sprite information
sprites
(e.g. name
, width
, source_image
)Object
- Information about retina spritesheet
spritesheet
(e.g. width
, px
)Object
- Container for retina_spritesheet
metadata and its representation
String
- Prefix for spritesheet variablesObject[]
- Array of objects that maps to normal and retina sprites
Object
- Container for data about sprite mapping
String
- Name to refer to mapping byNumber
- Index of corresponding normal/retina sprites from data.sprites
/data.retina_sprites
Object
- Normal sprite from data.sprites
that corresponds to our mapping
data.sprites[*]
(e.g. name
, x
, offset_y
, px
)Object
- Retina sprite from data.retina_sprites
that corresponds to our mapping
data.retina_sprites[*]
(e.g. name
, x
, offset_y
, px
)Object
- Optional container for metadata about retina_groups
and its representation
String
- Name for retina_groups
Object
- Options passed in via cssOpts
in gulp.spritesmith
configAn example sprite
is
1{ 2 "name": "sprite2", 3 "x": 10, 4 "y": 20, 5 "width": 20, 6 "height": 30, 7 "total_width": 80, 8 "total_height": 100, 9 "image": "nested/dir/spritesheet.png", 10 "escaped_image": "nested/dir/spritesheet.png", 11 "source_image": "path/to/original/sprite.png", 12 "offset_x": -10, 13 "offset_y": -20, 14 "px": { 15 "x": "10px", 16 "y": "20px", 17 "width": "20px", 18 "height": "30px", 19 "total_width": "80px", 20 "total_height": "100px", 21 "offset_x": "-10px", 22 "offset_y": "-20px" 23 } 24}
If you are defining a Handlebars template, then you can inherit from an existing template via handlebars-layouts
(e.g. {{#extend "scss"}}
). An example usage can be found in the Examples section.
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
css
template, 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 sprites
from Templating except for escaped_image
, offset_x
, offset_y
, and px
.
1// Prefix all sprite names with `sprite-` (e.g. `home` -> `sprite-home`) 2cssVarMap: function (sprite) { 3 sprite.name = 'sprite_' + sprite.name; 4} 5 6// Generates: 7// $sprite_fork_x = 0px; 8// $sprite_fork_y = 0px; 9 10// As oppposed to default: 11// $fork_x = 0px; 12// $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:
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
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
:
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
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.
In this example, we are using the alt-diagonal
algorithm to guarantee no overlap if images overflow.
Configuration:
1{ 2 imgName: 'sprite.png', 3 cssName: 'sprite.styl', 4 algorithm: 'alt-diagonal' 5}
Output:
In this example, we are using the phantomjssmith
engine as an alternative to the pixelsmith
default.
Requirements:
Install phantomjssmith
to our node_modules
via npm install
.
1npm install phantomjssmith
Alternatively, we can use --save
or --save-dev
to save to our package.json's dependencies
or devDependenices
.
1npm install phantomjssmith --save # Updates {"dependencies": {"phantomjssmith": "1.2.3"}} 2npm install phantomjssmith --save-dev # Updates {"devDependencies": {"phantomjssmith": "1.2.3"}}
Configuration:
1// var phantomjssmith = require('phantomjssmith'); 2{ 3 imgName: 'sprite.png', 4 cssName: 'sprite.styl', 5 engine: phantomjssmith 6}
Output:
The padding
option allows for inserting spacing between images.
Configuration:
1{ 2 imgName: 'sprite.png', 3 cssName: 'sprite.styl', 4 padding: 20 // Exaggerated for visibility, normal usage is 1 or 2 5}
Output:
In this example, we will use generate a normal and retina spritesheet via the retinaSrcFilter
and retinaImgName
parameters.
Configuration:
1{ 2 // This will filter out `fork@2x.png`, `github@2x.png`, ... for our retina spritesheet 3 // The normal spritesheet will now receive `fork.png`, `github.png`, ... 4 retinaSrcFilter: ['images/*@2x.png'], 5 imgName: 'sprite.png', 6 retinaImgName: 'sprite@2x.png', 7 cssName: 'sprite.styl' 8}
Normal spritesheet:
Retina spritesheet:
In this example, we will use cssTemplate
with a handlebars
template to generate CSS that uses :before
selectors.
Template:
1{{#sprites}} 2.icon-{{name}}:before { 3 display: block; 4 background-image: url({{{escaped_image}}}); 5 background-position: {{px.offset_x}} {{px.offset_y}}; 6 width: {{px.width}}; 7 height: {{px.height}}; 8} 9{{/sprites}}
Configuration:
1{ 2 imgName: 'sprite.png', 3 cssName: 'sprite.css', 4 cssTemplate: 'handlebarsStr.css.handlebars' 5}
Output:
1.icon-fork:before { 2 display: block; 3 background-image: url(sprite.png); 4 background-position: 0px 0px; 5 width: 32px; 6 height: 32px; 7} 8.icon-github:before { 9/* ... */
In this example, we will extend the SCSS template to provide minimal variables. The JSON at the front comes from the original template and is required to provide consistent casing and default options.
Different block sections for each template are documented in:
https://github.com/twolfson/spritesheet-templates
Template:
1{ 2 // Default options 3 'functions': true, 4 'variableNameTransforms': ['dasherize'] 5} 6 7{{#extend "scss"}} 8{{#content "sprites"}} 9{{#each sprites}} 10${{strings.name}}: ({{px.x}}, {{px.y}}, {{px.offset_x}}, {{px.offset_y}}, {{px.width}}, {{px.height}}, {{px.total_width}}, {{px.total_height}}, '{{{escaped_image}}}', '{{name}}', ); 11{{/each}} 12{{/content}} 13{{#content "spritesheet"}} 14${{spritesheet_info.strings.name_sprites}}: ({{#each sprites}}${{strings.name}}, {{/each}}); 15${{spritesheet_info.strings.name}}: ({{spritesheet.px.width}}, {{spritesheet.px.height}}, '{{{spritesheet.escaped_image}}}', ${{spritesheet_info.strings.name_sprites}}, ); 16{{/content}} 17{{/extend}}
Configuration:
1{ 2 imgName: 'sprite.png', 3 cssName: 'sprite.scss', 4 cssTemplate: 'handlebarsInheritance.scss.handlebars' 5}
Output:
1$fork: (0px, 0px, 0px, 0px, 32px, 32px, 64px, 64px, 'sprite.png', 'fork', ); 2$github: (32px, 0px, -32px, 0px, 32px, 32px, 64px, 64px, 'sprite.png', 'github', ); 3$twitter: (0px, 32px, 0px, -32px, 32px, 32px, 64px, 64px, 'sprite.png', 'twitter', ); 4$spritesheet-sprites: ($fork, $github, $twitter, ); 5$spritesheet: (64px, 64px, 'sprite.png', $spritesheet-sprites, ); 6/* ... */
In this example, we will use cssTemplate
with a custom function that generates YAML.
Configuration:
1// var yaml = require('js-yaml'); 2{ 3 imgName: 'sprite.png', 4 cssName: 'sprite.yml', 5 cssTemplate: function (data) { 6 // Convert sprites from an array into an object 7 var spriteObj = {}; 8 data.sprites.forEach(function (sprite) { 9 // Grab the name and store the sprite under it 10 var name = sprite.name; 11 spriteObj[name] = sprite; 12 13 // Delete the name from the sprite 14 delete sprite.name; 15 }); 16 17 // Return stringified spriteObj 18 return yaml.safeDump(spriteObj); 19 } 20}
Output:
1fork: 2 x: 0 3 'y': 0 4 width: 32 5 height: 32 6 source_image: /home/todd/github/gulp.spritesmith/docs/images/fork.png 7 image: sprite.png 8 total_width: 64 9 total_height: 64 10 escaped_image: sprite.png 11 offset_x: -0.0 12 offset_y: -0.0 13 px: 14 x: 0px 15 'y': 0px 16 offset_x: 0px 17 offset_y: 0px 18 height: 32px 19 width: 32px 20 total_height: 64px 21 total_width: 64px 22github: 23# ...
gulp.spritesmith
doesn't directly support cache busting but gulp-spritesmash
is a plugin that takes gulp.spritesmith's
output and generates cache busted filenames and CSS URLs. Here's an example usage:
https://github.com/MasterOfMalt/gulp-spritesmash
1var gulp = require('gulp'); 2var buffer = require('vinyl-buffer'); 3var spritesmash = require('gulp-spritesmash'); 4var spritesmith = require('gulp.spritesmith'); 5 6gulp.task('sprite', function () { 7 return gulp.src('images/*.png', {encoding: false}) 8 .pipe(spritesmith({ 9 imgName: 'sprite.png', 10 cssName: 'sprite.css' 11 })) 12 .pipe(buffer()) 13 .pipe(spritesmash()); 14 .pipe(gulp.dest('path/to/output/', {encoding: false})); 15});
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.
As of Feb 09 2014, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
0 existing vulnerabilities detected
Reason
license file detected
Details
Reason
Found 1/29 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
SAST tool is not run on all commits -- score normalized to 0
Details
Score
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