Home

Awesome

Deprecated module

This module has been deprecated in favor of stylus-lemonade.

Stylus-Sprite

Stylus-Sprite is an extension for Stylus which makes sprite images from Stylus tags. Actually it takes a image file, places it to a sprite image and replaces the original pointer in the CSS file with position coordinates according to the sprite image.

Installation

npm install stylus-sprite

Dependencies

Install libgd on Mac with homebrew

brew update
brew install libgd

Install libgd on Debian/Ubuntu

apt-get install libgd2-xpm-dev

I had problems using node-gd on mac but on Debian it worked perfectly

Usage

Consider the following Stylus CSS

.block-elm
    background: url(sprite.png) no-repeat sprite("star.png");
    width: 25px;
    height: 25px;
    

After running Stylus-Sprite the resulting CSS would be something like

.block_elm{
    background: url(sprite.png) no-repeat -25px -78px;
    width: 25px;
    height: 25px;
}

And the image sprite.png would have star.png placed on position 25x78 px.

CSS API

Function sprite(filename[, options]) includes the filename in the sprite image and replaces sprite(...) with the coordinates of it.

If options param is left empty, no special behavior is added.

background-position: sprite("tag.png");

options is a string similar to html style param, keys and values separated with colons and key/value pairs with semicolons.

background-position: sprite("tag.png","height: 120; repeat: x");

Possible keys are

NB! All numeric values are plain numeric, no measurement units (defaults to pixels).

JavaScript API

Creating the sprite consists of two phases - preparation and rendering.

The first step is to define StylusSprite object with required params

var sprite = new StylusSprite({
    image_root: "./images",
    output_file:"sprite.png"
});

Second step would be hooking to the Stylus parsing phase with Stylus.define

stylus...define('sprite', function(filename, option_val){
        // preparation phase
        return sprite.spritefunc(filename, option_val);
    });

A more sane version would be using bound function

sprite.spritefunc.bind(sprite, filename, option_val)

but as Stylus checks for function parameters proxying anonymous function is needed.

Finally when Stylus is finished rendering the CSS sprite.build must be run with it.

sprite.build(rendered_css, function(err, final_css){
    console.log(final_css);
});

Somewhat complete example:

var stylus = require("stylus"),
    StylusSprite = require("stylus-sprite")
    sprite = new StylusSprite({output_file:"sprite.png"});

var css = "body.....";

stylus(css).
    set('filename', 'test.css').
    define('sprite', function(filename, option_val){
        // preparation phase
        return sprite.spritefunc(filename, option_val);
    }).
    render(function(err, css){
        if (err) throw err;
        
        // rendering phase
        sprite.build(css, function(err, css){
            if (err) throw err;
            console.log(css);
        });
    });

Pngcrush

If you have Pngcrush installed in your system, you can use it to optimize generated PNG images.

var sprite = new StylusSprite({
    image_root: "./images",
    output_file:"sprite.png",
    pngcrush: "pngcrush" // path to pngcrush command
});