Gathering detailed insights and metrics for spritesheet-templates
Gathering detailed insights and metrics for spritesheet-templates
Gathering detailed insights and metrics for spritesheet-templates
Gathering detailed insights and metrics for spritesheet-templates
@pixi/spritesheet
Spritesheets maintain a collection of Textures on a single image
webpack-spritesmith
Webpack plugin that converts set of images into a spritesheet and SASS/LESS/Stylus mixins
grunt-spritesmith
Grunt task for converting a set of images into a spritesheet and corresponding CSS variables.
vite-plugin-icons-spritesheet
Vite plugin that generates a spritesheet and types out of your icons folder.
Convert spritesheet data into CSS or CSS pre-processor data
npm install spritesheet-templates
Module System
Min. Node Version
Typescript Support
Node Version
NPM Version
107 Stars
883 Commits
48 Forks
3 Watching
3 Branches
15 Contributors
Updated on 16 Mar 2024
Minified
Minified + Gzipped
JavaScript (32.44%)
Handlebars (22.47%)
SCSS (15.22%)
Less (10.75%)
Sass (8.66%)
Stylus (6.77%)
CSS (2.39%)
Mustache (1.3%)
Cumulative downloads
Total Downloads
Last day
1.1%
7,848
Compared to previous day
Last week
-1.1%
41,500
Compared to previous week
Last month
2.3%
188,238
Compared to previous month
Last year
-46.3%
2,266,484
Compared to previous year
Convert spritesheet data into CSS or CSS pre-processor data
spritesheet-templates
, formerly json2css
, was built as part of spritesmith
, a tool that converts images into spritesheets and CSS variables.
Install the module with: npm install spritesheet-templates
1// Compilation 2var templater = require('spritesheet-templates'); 3templater({ 4 sprites: [{ 5 name: 'github', x: 0, y: 0, width: 10, height: 20 6 }, { 7 name: 'twitter', x: 10, y: 20, width: 20, height: 30 8 }, { 9 name: 'rss', x: 30, y: 50, width: 50, height: 50 10 }], 11 spritesheet: { 12 width: 80, height: 100, image: 'url/path/to/spritesheet.png' 13 } 14}, {format: 'stylus'}); /* 15// Result stylus 16$github_x = 0px; 17$github_y = 0px; 18... 19$github = 0px 0px 0px 0px 10px 20px 80px 100px 'url/path/to/spritesheet.png' 'github'; 20... 21$twitter = 10px 20px -10px -20px 20px 30px 80px 100px 'url/path/to/spritesheet.png' 'twitter'; 22... 23$rss = 30px 50px -30px -50px 50px 50px 80px 100px 'url/path/to/spritesheet.png' 'rss'; 24... 25spriteWidth($sprite) { 26 width: $sprite[0]; 27} 28... 29sprite($sprite) { 30 spriteImage($sprite) 31 spritePosition($sprite) 32 spriteWidth($sprite) 33 spriteHeight($sprite) 34} 35 36// Inside of your Stylus 37.github-logo { 38 sprite($github); 39} 40*/
spritesheet-templates
exports the function templater
as its module.exports
.
templater(data, options)
Converter for spritesheet/sprite info into spritesheet
Object
- Container for data for template
Object[]
- Deprecated alternative key to define data.sprites
Object[]
- Array of objects with coordinate data about each sprite on the spritesheet
Object
- Container for sprite coordinate data
*
symbolizes any index (e.g. data.sprites[0]
)String
- Name to use for the imageNumber
- Horizontal coordinate of top-left corner of imageNumber
- Vertical coordinate of top-left corner of imageNumber
- Horizontal length of image in pixelsNumber
- Vertical length of image in pixelsObject
- Information about spritesheet
Number
- Horizontal length of image in pixelsNumber
- Vertical length of image in pixelsString
- URL to use for spritesheet
background-image
background-image: url({{spritesheet.image}});
Object
- Optional container for metadata about spritesheet
and its representation
String
- Prefix to use for all spritesheet variables
icons
will generate $icons-width
/$icons-image
/etc in a SCSS templatespritesheet
(e.g. $spritesheet-width
, $spritesheet-image
)retina
templates are documented in the Retina parameters sectionObject
- Optional settings
String
- Deprecated alternative for spritesheet_info.name
String
- Format to generate output in
css
formatMixed
- Options to pass through to the formatterReturns:
String
- Result from specified formatterretina
templates require additional parameters data.retina_sprites
, data.retina_spritesheet
and data.retina_groups
to be passed in.
For the variables to be useful, the retina spritesheet should be a 2x scale image of the original spritesheet. Similarly, retina sprites should be positioned in the same layout and order as their normal counterparts (e.g. [{x: 0, y: 0}, {x: 20, y: 20}]
should correspond to [{x: 0, y: 0}, {x: 40, y: 40}]
).
Object
- Same container as defined above
Object[]
- Array of objects with coordinate data about each retina sprite for the retina spritesheet
data.sprites
Object
- Information about retina spritesheet
data.spritesheet
Object
- Optional container for metadata about retina_spritesheet
and its representation
String
- Prefix to use for all retina spritesheet variables
retina-icons
will generate $retina-icons-width
/$retina-icons-image
/etc in a SCSS templateretina-spritesheet
(e.g. $retina-spritesheet-width
, $retina-spritesheet-image
)Object[]
- Array of objects that maps to normal and retina sprites
Object
- Container for data about sprite mapping
String
- Name to refer to mapping by
Number
- Index to look up corresponding normal/retina sprites from data.sprites
/data.retina_sprites
Object
- Optional container for metadata about retina_groups
and its representation
String
- Name to use for retina_groups
variable
icon-groups
will generate $icons-groups
in a SCSS templateretina-groups
(e.g. $retina-groups
)Below are our template options for options.format
.
Handlebars-based templates support inheritance via handlebars-layouts
(e.g. {{#extend "css"}}
). Inherited templates must copy/paste JSON front matter. An example can be found in the Examples section.
Retina templates have the same setup but are located in the Retina templates section for convenience.
css
Ouput CSS variables as CSS rules.
Options:
Function
- Override mapping for CSS selector
cssSelector
should have signature function (sprite) { return 'selector'; }
'.icon-' + sprite.name
sprite
with all parameters designed for templateHandlebars blocks:
css
is a Handlebars based template. We allow for overriding the following sections:
{{#content "sprites-comment"}}
- Comment before CSS rules{{#content "sprites"}}
- CSS rulesExample:
1.icon-sprite1 { 2 background-image: url(nested/dir/spritesheet.png); 3 background-position: 0px 0px; 4 width: 10px; 5 height: 20px; 6} 7.icon-sprite2 { 8/* ... */
json
Output CSS variables in JSON format.
Example:
1{ 2 "sprite1": { 3 "x": 0, 4 "y": 0, 5 "width": 10, 6 "height": 20, 7 "total_width": 80, 8 "total_height": 100, 9 "image": "nested/dir/spritesheet.png", 10 "offset_x": 0, 11 "offset_y": 0, 12 "px": { 13 "x": "0px", 14 "y": "0px", 15 "offset_x": "0px", 16 "offset_y": "0px", 17 "height": "20px", 18 "width": "10px", 19 "total_height": "100px", 20 "total_width": "80px" 21 }, 22 "escaped_image": "nested/dir/spritesheet.png" 23 }, 24 "sprite2": { 25 // ...
json_array
Output CSS variables as an array of objects.
Example:
1[ 2 { 3 "name": "sprite1", 4 "x": 0, 5 "y": 0, 6 "width": 10, 7 "height": 20, 8 "total_width": 80, 9 "total_height": 100, 10 "image": "nested/dir/spritesheet.png", 11 "offset_x": 0, 12 "offset_y": 0, 13 "px": { 14 "x": "0px", 15 "y": "0px", 16 "offset_x": "0px", 17 "offset_y": "0px", 18 "height": "20px", 19 "width": "10px", 20 "total_height": "100px", 21 "total_width": "80px" 22 }, 23 "escaped_image": "nested/dir/spritesheet.png" 24 }, 25 { 26 "name": "sprite2", 27 // ...
json_texture
Output CSS variables as an object in format similar to that of TexturePacker. Useful for game frameworks, such as Phaser, Pixi.js, and others.
For consistency with TexturePacker, we will use the basename of a given image. spritesmith
provides this via sprite.source_image
. If you would like to provide a custom name, then please define sprite.frame_name
:
1// Input 2{ 3 sprites: [{ 4 frame_name: 'hello', name: 'github', x: 0, y: 0, width: 10, height: 20 5 }] 6} 7 8// Output 9{ 10 frames: { 11 hello: {x: 0, y: 0, w: 10, h: 20} 12 } 13}
If neither sprite.source_image
nor spriteframe
is used, then sprite.name
will be used.
For integration in grunt-spritesmith
/gulp.spritesmith
, please see their cssVarMap
documentation.
Example:
1{ 2 "frames": { 3 "mysprite.png": { 4 "frame": { 5 "x": 10, 6 "y": 20, 7 "w": 20, 8 "h": 30 9 } 10 }, 11 // ... 12 }, 13 "meta": { 14 "app": "spritesheet-templates", 15 // ... 16 "image": "nested/dir/spritesheet.png", 17 "scale": 1, 18 "size": { 19 "w": 80, 20 "h": 100 21 } 22 } 23}
less
Output CSS variables as LESS variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['dasherize']
which yields a dash-case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
less
is a Handlebars based template. We allow for overriding the following sections:
{{#content "sprites-comment"}}
- Comment before LESS variable declarations{{#content "sprites"}}
- LESS variable declarations for sprites{{#content "spritesheet"}}
- LESS variable declarations for spritesheet{{#content "sprite-functions-comment"}}
- Comment before LESS functions for sprite variables{{#content "sprite-functions"}}
- LESS functions for sprite variables{{#content "spritesheet-functions-comment"}}
- Comment before LESS functions for spritesheet variables{{#content "spritesheet-functions"}}
- LESS functions for spritesheet variablesExample:
1@sprite1-name: 'sprite1'; 2@sprite1-x: 0px; 3@sprite1-y: 0px; 4@sprite1-offset-x: 0px; 5@sprite1-offset-y: 0px; 6@sprite1-width: 10px; 7@sprite1-height: 20px; 8@sprite1-total-width: 80px; 9@sprite1-total-height: 100px; 10@sprite1-image: 'nested/dir/spritesheet.png'; 11@sprite1: 0px 0px 0px 0px 10px 20px 80px 100px 'nested/dir/spritesheet.png' 'sprite1'; 12@sprite2-name: 'sprite2'; 13// ...
sass
Output CSS variables as SASS variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['dasherize']
which yields a dash-case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
sass
is a Handlebars based template. We allow for overriding the following sections:
{{#content "sprites-comment"}}
- Comment before SASS variable declarations{{#content "sprites"}}
- SASS variable declarations for sprites{{#content "spritesheet"}}
- SASS variable declarations for spritesheet{{#content "sprite-functions-comment"}}
- Comment before SASS functions for sprite variables{{#content "sprite-functions"}}
- SASS functions for sprite variables{{#content "spritesheet-functions-comment"}}
- Comment before SASS functions for spritesheet variables{{#content "spritesheet-functions"}}
- SASS functions for spritesheet variablesExample:
1$sprite1-name: 'sprite1' 2$sprite1-x: 0px 3$sprite1-y: 0px 4$sprite1-offset-x: 0px 5$sprite1-offset-y: 0px 6$sprite1-width: 10px 7$sprite1-height: 20px 8$sprite1-total-width: 80px 9$sprite1-total-height: 100px 10$sprite1-image: 'nested/dir/spritesheet.png' 11$sprite1: 0px 0px 0px 0px 10px 20px 80px 100px 'nested/dir/spritesheet.png' 'sprite1' 12$sprite2-name: 'sprite2' 13// ...
scss
Output CSS variables as SCSS variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['dasherize']
which yields a dash-case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
scss
is a Handlebars based template. We allow for overriding the following sections:
{{#content "sprites-comment"}}
- Comment before SCSS variable declarations{{#content "sprites"}}
- SCSS variable declarations for sprites{{#content "spritesheet"}}
- SCSS variable declarations for spritesheet{{#content "sprite-functions-comment"}}
- Comment before SCSS functions for sprite variables{{#content "sprite-functions"}}
- SCSS functions for sprite variables{{#content "spritesheet-functions-comment"}}
- Comment before SCSS functions for spritesheet variables{{#content "spritesheet-functions"}}
- SCSS functions for spritesheet variablesExample:
1$sprite1-name: 'sprite1'; 2$sprite1-x: 0px; 3$sprite1-y: 0px; 4$sprite1-offset-x: 0px; 5$sprite1-offset-y: 0px; 6$sprite1-width: 10px; 7$sprite1-height: 20px; 8$sprite1-total-width: 80px; 9$sprite1-total-height: 100px; 10$sprite1-image: 'nested/dir/spritesheet.png'; 11$sprite1: 0px 0px 0px 0px 10px 20px 80px 100px 'nested/dir/spritesheet.png' 'sprite1'; 12$sprite2-name: 'sprite2'; 13// ...
scss_maps
Output CSS variables as SCSS maps variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['underscored']
which yields a snake_case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
scss_maps
is a Handlebars based template. We allow for overriding the following sections:
{{#content "sprites-comment"}}
- Comment before SCSS variable declarations{{#content "sprites"}}
- SCSS variable declarations for sprites{{#content "spritesheet"}}
- SCSS variable declaration for spritesheet{{#content "sprite-functions-comment"}}
- Comment before SCSS functions for sprite variables{{#content "sprite-functions"}}
- SCSS functions for sprite variables{{#content "spritesheet-functions-comment"}}
- Comment before SCSS functions for spritesheet variables{{#content "spritesheet-functions"}}
- SCSS functions for spritesheet variablesExample:
1$sprite1: ( 2 name: 'sprite1', 3 x: 0px, 4 y: 0px, 5 offset_x: 0px, 6 offset_y: 0px, 7 width: 10px, 8 height: 20px, 9 total_width: 80px, 10 total_height: 100px, 11 image: 'nested/dir/spritesheet.png' 12); 13$sprite2: ( 14// ...
stylus
Output CSS variables as Stylus variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['underscored']
which yields a snake_case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
stylus
is a Handlebars based template. We allow for overriding the following sections:
{{#content "sprites-comment"}}
- Comment before Stylus variable declarations{{#content "sprites"}}
- Stylus variable declarations for sprites{{#content "spritesheet"}}
- Stylus variable declarations for spritesheet{{#content "sprite-functions-comment"}}
- Comment before Stylus functions for sprite variables{{#content "sprite-functions"}}
- Stylus functions for sprite variables{{#content "spritesheet-functions-comment"}}
- Comment before Stylus functions for spritesheet variables{{#content "spritesheet-functions"}}
- Stylus functions for spritesheet variablesExample:
1$sprite1_name = 'sprite1'; 2$sprite1_x = 0px; 3$sprite1_y = 0px; 4$sprite1_offset_x = 0px; 5$sprite1_offset_y = 0px; 6$sprite1_width = 10px; 7$sprite1_height = 20px; 8$sprite1_total_width = 80px; 9$sprite1_total_height = 100px; 10$sprite1_image = 'nested/dir/spritesheet.png'; 11$sprite1 = 0px 0px 0px 0px 10px 20px 80px 100px 'nested/dir/spritesheet.png'; 12$sprite2_name = 'sprite2'; 13// ...
These are a subset of templates that support retina spritesheets. These require retina parameters like retina_sprites
are provided in order to work properly.
css_retina
Ouput CSS variables as CSS rules with media query and additional rules for retina support.
Options:
Function
- Override mapping for CSS selector
cssSelector
should have signature function (retinaGroup) { return 'selector'; }
'.icon-' + retinaGroup.name
retinaGroup
with all parameters designed for retina_groups[*]
in templates (e.g. name
, normal
, retina
)Handlebars blocks:
We extend from the css
template and have its blocks. There are no new sections for retina data.
Example:
1.icon-sprite1 { 2 background-image: url(nested/dir/spritesheet.png); 3 background-position: 0px 0px; 4 width: 10px; 5 height: 20px; 6} 7/* ... */ 8@media (-webkit-min-device-pixel-ratio: 2), 9 (min-resolution: 192dpi) { 10 .icon-sprite1 { 11 background-image: url(nested/dir/spritesheet@2x.png); 12 background-size: 80px 100px; 13 } 14}
json_retina
Output retina CSS variables in JSON format.
Example:
1{ 2 "sprite1": { 3 "normal": { 4 "x": 0, 5 "y": 0, 6 "width": 10, 7 "height": 20, 8 "image": "nested/dir/spritesheet.png", 9 "escaped_image": "nested/dir/spritesheet.png", 10 "total_width": 80, 11 "total_height": 100, 12 "offset_x": 0, 13 "offset_y": 0, 14 "px": { 15 "x": "0px", 16 "y": "0px", 17 "offset_x": "0px", 18 "offset_y": "0px", 19 "height": "20px", 20 "width": "10px", 21 "total_height": "100px", 22 "total_width": "80px" 23 } 24 }, 25 "retina": { 26 "x": 0, 27 "y": 0, 28 // ... 29 }, 30 "sprite2": { 31 // ...
json_array_retina
Output retina CSS variables as an array of objects.
Example:
1[ 2 { 3 "name": "sprite1", 4 "normal": { 5 "x": 0, 6 "y": 0, 7 "width": 10, 8 "height": 20, 9 "total_width": 80, 10 "total_height": 100, 11 // ... 12 }, 13 "retina": { 14 "x": 0, 15 "y": 0, 16 "width": 20, 17 "height": 40, 18 "total_width": 160, 19 "total_height": 200, 20 // ... 21 } 22 }, 23 { 24 "name": "sprite2", 25 // ...
less_retina
Output retina CSS variables as LESS variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['dasherize']
which yields a dash-case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
We extend from the less
template and have its blocks. There are no new sections for retina data.
Example:
1@sprite1-name: 'sprite1'; 2@sprite1-x: 0px; 3@sprite1-y: 0px; 4@sprite1-offset-x: 0px; 5@sprite1-offset-y: 0px; 6@sprite1-total-width: 80px; 7@sprite1-total-height: 100px; 8// ... 9@sprite2-2x-total-width: 160px; 10@sprite2-2x-total-height: 200px; 11@sprite2-2x-image: 'nested/dir/spritesheet@2x.png'; 12@sprite2-2x: 0px 0px 0px 0px 20px 40px 160px 200px 'nested/dir/spritesheet@2x.png' 'sprite2@2x'; 13// ... 14@sprite3-group: 'sprite3' @sprite3 @sprite3-2x; 15@retina-groups: @sprite1-group @sprite2-group @sprite3-group; 16// ...
sass_retina
Output retina CSS variables as SASS variables and mixins.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['dasherize']
which yields a dash-case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
We extend from the sass
template and have its blocks. There are no new sections for retina data.
Example:
1$sprite1-name: 'sprite1' 2$sprite1-x: 0px 3$sprite1-y: 0px 4$sprite1-offset-x: 0px 5$sprite1-total-width: 80px 6$sprite1-total-height: 100px 7// ... 8$sprite2-2x-total-width: 160px 9$sprite2-2x-total-height: 200px 10$sprite2-2x-image: 'nested/dir/spritesheet@2x.png' 11$sprite2-2x: (20px, 40px, -20px, -40px, 40px, 60px, 160px, 200px, 'nested/dir/spritesheet@2x.png', 'sprite2@2x', ) 12// ... 13$sprite3-group: ('sprite3', $sprite3, $sprite3-2x, ) 14$retina-groups: ($sprite1-group, $sprite2-group, $sprite3-group, ) 15// ...
scss_retina
Output retina CSS variables as SCSS variables and mixins.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['dasherize']
which yields a dash-case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
We extend from the scss
template and have its blocks. There are no new sections for retina data.
Example:
1$sprite1-name: 'sprite1'; 2$sprite1-x: 0px; 3$sprite1-y: 0px; 4$sprite1-offset-x: 0px; 5$sprite1-total-width: 80px; 6$sprite1-total-height: 100px; 7// ... 8$sprite2-2x-total-width: 160px; 9$sprite2-2x-total-height: 200px; 10$sprite2-2x-image: 'nested/dir/spritesheet@2x.png'; 11$sprite2-2x: (20px, 40px, -20px, -40px, 40px, 60px, 160px, 200px, 'nested/dir/spritesheet@2x.png', 'sprite2@2x', ); 12// ... 13$sprite3-group: ('sprite3', $sprite3, $sprite3-2x, ); 14$retina-groups: ($sprite1-group, $sprite2-group, $sprite3-group, ); 15// ...
scss_maps_retina
Output retina CSS variables as SCSS maps variables.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['underscored']
which yields a snake_case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
We extend from the scss_maps
template and have its blocks. There are no new sections for retina data.
Example:
1$sprite1: ( 2 name: 'sprite1', 3 x: 0px, 4 y: 0px, 5 offset_x: 0px, 6 offset_y: 0px, 7 total_width: 80px, 8 total_height: 100px, 9 // ... 10); 11$sprite2: ( 12 // ... 13 total-width: 160px, 14 total-height: 200px, 15 image: 'nested/dir/spritesheet@2x.png' 16); 17// ... 18$sprite3-group: ( 19 name: 'sprite3', 20 normal: $sprite3, 21 retina: $sprite3-2x 22); 23$retina-groups: ($sprite1-group, $sprite2-group, $sprite3-group, ); 24// ...
stylus_retina
Output retina CSS variables as Stylus variables and mixins.
Options:
Boolean
- Flag to include mixins or not
true
(mixins will be included)String[]
- Array of underscore.string
methods to run on variable names
['camelize']
would transform icon-home-x
to iconHomeX
['underscored']
which yields a snake_case
nameunderscore.string
: http://epeli.github.io/underscore.string/#api
chain
which allows for toUpperCase
and toLowerCase
Handlebars blocks:
We extend from the stylus
template and have its blocks. There are no new sections for retina data.
Example:
1$sprite1_name = 'sprite1'; 2$sprite1_x = 0px; 3$sprite1_y = 0px; 4$sprite1_offset_x = 0px; 5$sprite1_offset_y = 0px; 6$sprite1_total_width = 80px; 7$sprite1_total_height = 100px; 8// ... 9$sprite2_2x_total_width = 160px; 10$sprite2_2x_total_height = 200px; 11$sprite2_2x_image = 'nested/dir/spritesheet@2x.png'; 12$sprite2_2x = 20px 40px -20px -40px 40px 60px 160px 200px 'nested/dir/spritesheet@2x.png' 'sprite2@2x'; 13// ... 14$sprite3_group = 'sprite3' $sprite3 $sprite3_2x; 15$retina_groups = $sprite1_group $sprite2_group $sprite3_group; 16// ...
Custom templates can be added dynamically via templater.addTemplate
and templater.addHandlebarsTemplate
.
The parameters passed into your template are known as data
. These are a cloned copy of the data
originally passed in. We add some normalized properties for your convenience.
Object
- Data available to template
Object[]
- Deprecated alias for data.sprites
Object[]
- Array of objects with coordinate data about each sprite on the spritesheet
Object
- Container for sprite coordinate data
*
symbolizes any index (e.g. data.sprites[0]
)String
- Name to use for the imageNumber
- Horizontal coordinate of top-left corner of imageNumber
- Vertical coordinate of top-left corner of imageNumber
- Horizontal length of image in pixelsNumber
- Vertical length of image in pixelsNumber
- Width of entire spritesheetNumber
- Height of entire spritesheetString
- URL path to spritesheetString
- URL encoded image
Number
- 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
String
- Deprecated alias for spritesheet_info.name
Number
- Horizontal length of image in pixelsNumber
- Vertical length of image in pixelsString
- URL to use for spritesheet
background-image
background-image: url({{spritesheet.image}});
String
- URL encoded image
Object
container for numeric values including px
String
- width
suffixed with px
String
- height
suffixed with px
String
- Deprecated alias for spritesheet_info.name
Object
- Container for information about spritesheet
and its representation
String
- Name for spritesheet
Mixed
- Options to passed through via options.formatOpts
We provide an extra set of data for handlebars
templates for variable/string names.
Object
- Container for sprite-relevant variable/string names
variableNameTransforms
String
- Transformed name of sprite (e.g. icon-home
)String
- Transformed combination of sprite name and -name
string (e.g. icon-home-name
)String
- Transformed combination of sprite name and -x
string (e.g. icon-home-x
)String
- Transformed combination of sprite name and -y
string (e.g. icon-home-y
)String
- Transformed combination of sprite name and -offset-x
string (e.g. icon-home-offset-x
)String
- Transformed combination of sprite name and -offset-y
string (e.g. icon-home-offset-y
)String
- Transformed combination of sprite name and -width
string (e.g. icon-home-width
)String
- Transformed combination of sprite name and -height
string (e.g. icon-home-height
)String
- Transformed combination of sprite name and -total-width
string (e.g. icon-home-total-width
)String
- Transformed combination of sprite name and -total-height
string (e.g. icon-home-total-height
)String
- Transformed combination of sprite name and -image
string (e.g. icon-home-image
)String
- Transformed combination of sprite name and -sprites
string (e.g. icon-home-sprites
)String
- Transformed combination of sprite name and -group
string (e.g. icon-home-group
)String
- Transformed combination of sprite name and -group-name
string (e.g. icon-home-group-name
)String
- Transformed combination of sprite name and -normal
string (e.g. icon-home-normal
)String
- Transformed combination of sprite name and -retina
string (e.g. icon-home-retina
)String
- Transformed word for name
String
- Transformed word for x
String
- Transformed word for y
String
- Transformed word for offset-x
String
- Transformed word for offset-y
String
- Transformed word for width
String
- Transformed word for height
String
- Transformed word for total-width
String
- Transformed word for total-height
String
- Transformed word for image
String
- Transformed word for sprites
String
- Transformed word for group
String
- Transformed word for group-name
String
- Transformed word for normal
String
- Transformed word for retina
Object
- Deprecated container for spritesheet-relevant variable/string names
data.spritesheet_info.strings
Object
- Container for spritesheet-relevant variable/string names
variableNameTransforms
String
- Transformed name of spritesheet (e.g. icon-home
)String
- Transformed combination of spritesheet name and -name
string (e.g. icon-home-name
)String
- Transformed combination of spritesheet name and -x
string (e.g. icon-home-x
)String
- Transformed combination of spritesheet name and -y
string (e.g. icon-home-y
)String
- Transformed combination of spritesheet name and -offset-x
string (e.g. icon-home-offset-x
)String
- Transformed combination of spritesheet name and -offset-y
string (e.g. icon-home-offset-y
)String
- Transformed combination of spritesheet name and -width
string (e.g. icon-home-width
)String
- Transformed combination of spritesheet name and -height
string (e.g. icon-home-height
)String
- Transformed combination of spritesheet name and -total-width
string (e.g. icon-home-total-width
)String
- Transformed combination of spritesheet name and -total-height
string (e.g. icon-home-total-height
)String
- Transformed combination of spritesheet name and -image
string (e.g. icon-home-image
)String
- Transformed combination of spritesheet name and -sprites
string (e.g. icon-home-sprites
)String
- Transformed combination of spritesheet name and -group
string (e.g. icon-home-group
)String
- Transformed combination of spritesheet name and -group-name
string (e.g. icon-home-group-name
)String
- Transformed combination of spritesheet and -normal
string (e.g. icon-home-normal
)String
- Transformed combination of spritesheet and -retina
string (e.g. icon-home-retina
)String
- Transformed word for name
String
- Transformed word for x
String
- Transformed word for y
String
- Transformed word for offset-x
String
- Transformed word for offset-y
String
- Transformed word for width
String
- Transformed word for height
String
- Transformed word for total-width
String
- Transformed word for total-height
String
- Transformed word for image
String
- Transformed word for sprites
String
- Transformed word for group
String
- Transformed word for group-name
String
- Transformed word for normal
String
- Transformed word for retina
Object
- Container for generic strings
variableNameTransforms
String
- Transformed word for name
String
- Transformed word for x
String
- Transformed word for y
String
- Transformed word for offset-x
String
- Transformed word for offset-y
String
- Transformed word for width
String
- Transformed word for height
String
- Transformed word for total-width
String
- Transformed word for total-height
String
- Transformed word for image
String
- Transformed word for sprites
String
- Transformed word for group
String
- Transformed word for group-name
String
- Transformed word for normal
String
- Transformed word for retina
These are additional properties of the template data when retina parameters have been passed in (e.g. retina_sprites
, retina_groups
). As with the normal data, it is cloned copy of the original data with additional properties for convenience.
Object
- Same container as defined above
Object[]
- Array of objects with coordinate data about each retina sprite for the retina spritesheet
data.sprites
(e.g. name
, x
, offset_y
, px
)Object
- Information about retina spritesheet
data.spritesheet
(e.g. width
, image
, px
)
retina_spritesheet.name
as name
is deprecatedObject
- Optional container for metadata about retina_spritesheet
and its representation
data.spritesheet_info
(e.g. name
)Object[]
- 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
Retina specific properties will have the same corresponding new data for Handlebars templates
Object
- Container for retina sprite-relevant variable/string names
variableNameTransforms
data.sprites[*].strings
(e.g. name
, name_name
, bare_name
)Object
- Container for retina spritesheet-relevant variable/string names
variableNameTransforms
data.spritesheet_info.strings
(e.g. name
, name_sprites
, bare_name
)Object
- Container for group-relevant variable/string names
variableNameTransforms
String
- Transformed name of retina group (e.g. icon-home
)String
- Transformed combination of retina group name and -name
string (e.g. icon-home-name
)String
- Transformed combination of retina group name and -x
string (e.g. icon-home-x
)String
- Transformed combination of retina group name and -y
string (e.g. icon-home-y
)String
- Transformed combination of retina group name and -offset-x
string (e.g. icon-home-offset-x
)String
- Transformed combination of retina group name and -offset-y
string (e.g. icon-home-offset-y
)String
- Transformed combination of retina group name and -width
string (e.g. icon-home-width
)String
- Transformed combination of retina group name and -height
string (e.g. icon-home-height
)String
- Transformed combination of retina group name and -total-width
string (e.g. icon-home-total-width
)String
- Transformed combination of retina group name and -total-height
string (e.g. icon-home-total-height
)String
- Transformed combination of retina group name and -image
string (e.g. icon-home-image
)String
- Transformed combination of retina group name and -sprites
string (e.g. icon-home-sprites
)String
- Transformed combination of retina group name and -group
string (e.g. icon-home-group
)String
- Transformed combination of retina group name and -group-name
string (e.g. icon-home-group-name
)String
- Transformed combination of retina group name and -normal
string (e.g. icon-home-normal
)String
- Transformed combination of retina group name and -retina
string (e.g. icon-home-retina
)String
- Transformed word for name
String
- Transformed word for x
String
- Transformed word for y
String
- Transformed word for offset-x
String
- Transformed word for offset-y
String
- Transformed word for width
String
- Transformed word for height
String
- Transformed word for total-width
String
- Transformed word for total-height
String
- Transformed word for image
String
- Transformed word for sprites
String
- Transformed word for group
String
- Transformed word for group-name
String
- Transformed word for normal
String
- Transformed word for retina
Object
- Container for retina groups relevant variable/string names
variableNameTransforms
String
- Transformed name of retina groups (e.g. icon-home
)String
- Transformed combination of retina groups name and -name
string (e.g. icon-home-name
)String
- Transformed combination of retina groups name and -x
string (e.g. icon-home-x
)String
- Transformed combination of retina groups name and -y
string (e.g. icon-home-y
)String
- Transformed combination of retina groups name and -offset-x
string (e.g. icon-home-offset-x
)String
- Transformed combination of retina groups name and -offset-y
string (e.g. icon-home-offset-y
)String
- Transformed combination of retina groups name and -width
string (e.g. icon-home-width
)String
- Transformed combination of retina groups name and -height
string (e.g. icon-home-height
)String
- Transformed combination of retina groups name and -total-width
string (e.g. icon-home-total-width
)String
- Transformed combination of retina groups name and -total-height
string (e.g. icon-home-total-height
)String
- Transformed combination of retina groups name and -image
string (e.g. icon-home-image
)String
- Transformed combination of retina groups name and -sprites
string (e.g. icon-home-sprites
)String
- Transformed combination of retina groups name and -group
string (e.g. icon-home-group
)String
- Transformed combination of retina groups name and -group-name
string (e.g. icon-home-group-name
)String
- Transformed combination of retina groups name and -normal
string (e.g. icon-home-normal
)String
- Transformed combination of retina groups name and -retina
string (e.g. icon-home-retina
)String
- Transformed word for name
String
- Transformed word for x
String
- Transformed word for y
String
- Transformed word for offset-x
String
- Transformed word for offset-y
String
- Transformed word for width
String
- Transformed word for height
String
- Transformed word for total-width
String
- Transformed word for total-height
String
- Transformed word for image
String
- Transformed word for sprites
String
- Transformed word for group
String
- Transformed word for group-name
String
- Transformed word for normal
String
- Transformed word for retina
templater.addTemplate(name, fn)
Method to define a custom template under the format of name
.
String
- Key to store template under for reference via options.format
Function
- Template function
function (data)
and return a String
outputtemplater.addHandlebarsTemplate(name, tmplStr)
Method to define a custom handlebars template under the format of name
.
As noted in the Templates section, these can inherit from existing templates via handlebars-layouts
conventions (e.g. {{#extend "scss"}}
). An example can be found in the Examples section.
String
- Key to store template under for reference via options.format
String
- Handlebars template to use for formatting
data
as its data
(e.g. {{sprites}}
is data.sprites
)templater.addMustacheTemplate(name, tmplStr)
Deprecated alias for templater.addHandlebarsTemplate
In this example, we will process a template with retina data.
1var templater = require('spritesheet-templates'); 2templater({ 3 sprites: [{ 4 name: 'github', x: 0, y: 0, width: 10, height: 20 5 }, { 6 name: 'twitter', x: 10, y: 20, width: 20, height: 30 7 }, { 8 name: 'rss', x: 30, y: 50, width: 50, height: 50 9 }], 10 // Note that the retina sprites are in the same order as `sprites` 11 retina_sprites: [{ 12 name: 'github@2x', x: 0, y: 0, width: 20, height: 40 13 }, { 14 name: 'twitter@2x', x: 20, y: 40, width: 40, height: 60 15 }, { 16 name: 'rss@2x', x: 60, y: 100, width: 100, height: 100 17 }], 18 spritesheet: { 19 width: 80, height: 100, image: 'url/path/to/spritesheet.png' 20 }, 21 retina_spritesheet: { 22 width: 160, height: 200, image: 'url/path/to/spritesheet@2x.png' 23 }, 24 retina_groups: [{ 25 name: 'github', index: 0 26 }, { 27 name: 'twitter', index: 1 28 }, { 29 name: 'rss', index: 2 30 }] 31}, {format: 'scss_retina'}); /* 32$github-name: 'github'; 33$github-x: 0px; 34... 35$twitter-2x-name: 'twitter@2x'; 36$twitter-2x-x: 20px; 37... 38$rss-group-name: 'rss'; 39$rss-group: ('rss', $rss, $rss-2x, ); 40$retina-groups: ($github-group, $twitter-group, $rss-group, ); 41*/
In this example, we will extend the SCSS template to output a minimal set of template data.
It should be noted that we must include the JSON front matter from the original template we are inheriting from to preserve default casing and options.
scss-minimal.handlebars:
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}}
index.js:
1// Load in our dependencies 2var fs = require('fs'); 3var templater = require('spritesheet-templates'); 4 5// Register our new template 6var scssMinimalHandlebars = fs.readFileSync('scss-minimal.handlebars', 'utf8'); 7templater.addHandlebarsTemplate('scss-minimal', scssMinimalHandlebars); 8 9// Run our templater 10templater({ 11 sprites: [{ 12 name: 'github', x: 0, y: 0, width: 10, height: 20 13 }, { 14 name: 'twitter', x: 10, y: 20, width: 20, height: 30 15 }, { 16 name: 'rss', x: 30, y: 50, width: 50, height: 50 17 }], 18 spritesheet: { 19 width: 80, height: 100, image: 'url/path/to/spritesheet.png' 20 } 21}, {format: 'scss-minimal'}); /* 22$github: (0px, 0px, 0px, 0px, 10px, 20px, 80px, 100px, 'url/path/to/spritesheet.png', 'github', ); 23$twitter: (10px, 20px, -10px, -20px, 20px, 30px, 80px, 100px, 'url/path/to/spritesheet.png', 'twitter', ); 24$rss: (30px, 50px, -30px, -50px, 50px, 50px, 80px, 100px, 'url/path/to/spritesheet.png', 'rss', ); 25$spritesheet-sprites: ($github, $twitter, $rss, ); 26$spritesheet: (80px, 100px, 'url/path/to/spritesheet.png', $spritesheet-sprites, ); 27*/
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
.
Support this project and others by twolfson via donations.
http://twolfson.com/support-me
As of Sep 08 2013, Todd Wolfson has released this repository and its contents to the public domain.
It has been released under the UNLICENSE.
Prior to Sep 08 2013, this repository and its contents were 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
Reason
Found 2/30 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