Sign Up for Free

RunKit +

Try any Node.js package right in your browser

This is a playground to test code. It runs a full Node.js environment and already has all of npm’s 400,000 packages pre-installed, including spritify with all npm packages installed. Try it out:

var spritify = require("spritify")

This service is provided by RunKit and is not affiliated with npm, Inc or the package authors.

spritify v1.1.0

Create image sprite(s) from images and update css file(s) with new image sprite(s) details

spritify

NPM Version NPM Downloads Build

spritify

SPRITIFY is a node.js package to create image sprite(s) from images and update css file(s) with new image sprite(s) details.


Features:

  • Create image sprite(s), and search for background, background-image css properties in the css file, and update the value with the new sprite image url and dimension by adding/overwrite css properties like background-size, background-position, width ...etc.

  • Option to optimize sprite css by comine class names to one background-image css property.

  • Supports SVG sprites.

  • Supports Retina sprites.

  • Can inline sprites using base64 encoding.

  • Can check for unused images in the sprite(s).

  • Can check for missing images, by searching for image path in the background, background-image css properties and can't find the image.

  • Can be used in grunt, gulp or any build tool.

spritify console log

Demo

Clone or download Spritify example repo and run it.

Install

npm install spritify --save-dev

OR

npm install -g spritify

CLI

Usage:
  spritify [OPTIONS] [ARGS]


Options:
  -h, --help : Help
  -v, --version : Version
  -c CONFIG, --config=CONFIG : JSON Config file path (It should be relative path)
  -s SRC, --src=SRC : Source directory of the images for the sprite image (It should be relative path).
  -d DEST, --dest=DEST : Destination file path of the sprite image (It should be relative path).
  -l LAYOUT, --layout=LAYOUT : layout of the sprite image e.g: 'top-down', 'left-right'.
  -p PADDING, --padding=PADDING : Padding pixels around the sprite image. e.g: 10.
  -svg, --svg : Flag to build svg sprite image

CLI usage example

./node_modules/.bin/spritify -c CONFIG/FILE/PATH.json

OR create only image sprite

./node_modules/.bin/spritify -s SOURCE/DIRECTORY/PATH/ -d DESTINATION/FILE/PATH.png -p 15 -l left-right

OR create only svg image sprite

./node_modules/.bin/spritify -s SOURCE/DIRECTORY/PATH/ -d DESTINATION/FILE/PATH.svg -svg

CLI config file schema example

{
    "css": [
        {
            "src": "__tests__/fixtures/css/style-a.css",
            "dest": "build/css/style-a.css"
        },
        {
            "optimize": false,
            "inline": true,
            "src": "__tests__/fixtures/css/style-inline.css",
            "dest": "build/css/style-inline.css"
        },
        {
            "optimize": false,
            "src": "__tests__/fixtures/css/style-svg-a.css",
            "dest": "build/css/style-svg-a.css"
        }
    ],
    "sprites": [
        {
            "layout": "top-down",
            "src": [
                "public/images/common",
                "public/images/other-icons"
            ],
            "dest": "build/img/sprite-site.png",
            "relative": "../img"
        },
        {
            "padding": 10,
            "layout": "top-down",
            "src": "__tests__/fixtures/img/icons",
            "dest": "build/images/sprite.png",
            "relative": "../images"
        },
        {
            "retina": 2,
            "src": "__tests__/fixtures/img/icons@2x",
            "dest": "build/images/sprite@2x.png",
            "relative": "../images"
        },
        {
            "svg": true,
            "src": "__tests__/fixtures/img/svg",
            "dest": "build/images/sprite-svg.svg",
            "relative": "../images"
        }
    ]
}

Programatic usage

const Spritify = require('spritify');
Spritify.build(params, callback);

Programatic usage example

/* 
    File: ./bin/run-spritify.js
    Run: node ./bin/run-spritify.js
*/
const Spritify = require('spritify');

const params = {
    debug: true,
    showIgnoredRules: true,
    css: [
        {
            optimize: false,
            inline: true,
            src: 'public/stylesheets/site.css',
            dest: 'build/css/site.css'
        },
        {
            src: 'build/css/theme.css',
            dest: 'build/css/theme.css'
        }
    ],
    sprites: [
        {
            layout: 'top-down',
            src: [
                'public/images/common',
                'public/images/other-icons'
            ],
            dest: 'build/img/sprite-site.png',
            relative: '../img'
        },
        {
            layout: 'top-down',
            src: 'public/images/icons',
            dest: 'build/img/sprite.png',
            relative: '../img'
        },
        {
            padding: 15,
            retina: 2,
            layout: 'left-right',
            src: 'public/img/icons@2x',
            dest: 'build/img/sprite@2x.png',
            relative: '../img'
        },
        {
            svg: true,
            src: 'public/img/svg',
            dest: 'build/img/svg-sprite.svg',
            relative: '../img'
        }
    ]
};

Spritify.build(params, (err) => {

    if (err) {
        console.error(err.stack || err);
        return;
    }
});

Spritify CSS Directives

NAMESyntaxDescription
Ignore/* spritify: ignore */Ignore statement, to ignore css rule or css declaration

Documentation

Spritify.build(params, callback)

Generates sprite(s) based on the provided images, and search for background, background-image css properties in the css files based on the provided css, and it will update the value(s) with the new sprite image(s) and dimensions by adding/overwrite background-size, background-position, width ...etc.

params Object:

NAMETYPEDEFAULTREQUIREDDESCRIPTION
cssArray of ObjectsYESArray of spritify css object.
spritesArray of ObjectsYESArray of spritify sprites object.
debugBooleanfalseNOOption to show debug info.
showIgnoredRulesBooleanfalseNOOption to show ignored css rules.

callback Function:

Is a callback function which should have signature function (err), err is Error|null.

css object:

NAMETYPEDEFAULTREQUIREDDESCRIPTION
srcStringYESSource path of the css file.
destStringYESDestination path of the css file.
optimizeBooleantrueNOOption to optimize sprite css by comine class names to one background-image css property.
inlineBooleanfalseNOOption inline sprites using base64 encoded.

sprites object:

NAMETYPEDEFAULTREQUIREDDESCRIPTION
srcString Or Array of StringsYESSource directory path(s) of the images.
destStringYESDestination path of the sprite file.
relativeStringYESRelative destination directory path of the sprite in the css file.
svgBooleanfalseNOOption to create svg sprite for svg images.
paddingNumber0NOOption to add more padding around the images in the css.
retinaNumber1NOOption to support retina images by setting value. e.g: to use 2X retina, set retina: 2.
layoutStringbinary-treeNOOption to pack images with. please layouts table.
engineStringpixelsmithNOOption to use different engine for spritesmith.

Layouts

top-downleft-rightdiagonalalt-diagonalbinary-tree
top-downleft-rightdiagonalalt-diagonalbinary-tree

License

Copyright 2017, Yahoo Holdings.

Copyrights licensed under the New BSD License. See the accompanying LICENSE file for terms.

Metadata

RunKit is a free, in-browser JavaScript dev environment for prototyping Node.js code, with every npm package installed. Sign up to share your code.
Sign Up for Free