npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

grunt-sprite-packer

v0.1.7

Published

Sprite packer converts multiple png files into single sprite file using efficient bin packer algorithm.

Downloads

99

Readme

grunt-sprite-packer

Sprite packer converts multiple png files into single sprite file using efficient packer algorithm. Metadata (names, dimensions and positions of individual sprites) are written into separate template-based text file.

Getting Started

This plugin requires Grunt ~0.4.1 and ImageMagick library.

If you haven't used Grunt before, be sure to check out the Getting Started guide, as it explains how to create a Gruntfile as well as install and use Grunt plugins. Once you're familiar with that process, you may install this plugin with this command:

npm install grunt-sprite-packer --save-dev

Once the plugin has been installed, it may be enabled inside your Gruntfile with this line of JavaScript:

grunt.loadNpmTasks('grunt-sprite-packer');

ImageMagick library have to be installed in your system. See www.imagemagick.org for installation instructions.

The "spritepacker" task

Overview

This task converts multiple images into single image using efficient packing algorithm. It outputs two files: resulting sprite image and a text file with information about positions and dimensions of individual sprites. This text file has no fixed format or structure, instead it is generated using a handlebars template, so that you have full control of its format. It can be plain CSS with classes or some declarations for additional processing (LESS, SASS, Stylus or JSON).

NEW: Since version 0.1.7, Spritepacker can now work with SVG files. See svg option for details. This is an experimental feature, please report any issues you may encounter.

In your project's Gruntfile, add a section named spritepacker to the data object passed into grunt.initConfig().

grunt.initConfig({
  spritepacker: {
    default_options: {
      options: {
        // Path to the template for generating metafile:
        template: 'css/sprites.css.tpl',

        // Destination metafile:
        destCss: 'css/sprites.css',

        // Base URL for sprite image, used in template
        baseUrl: '../img/'
      },
      files: {
        'img/sprites.png': ['img/sprites/*.png']
      }
    }
  }
})

Options

options.template

Type: String

A path to the template for generating metafile. Template is processed through Handlebars with the following variables:

  • sprites - an array of object with metainfo for each sprite
  • baseUrl - base URL from task options
  • fileName - file name of generated sprite file (i.e. task destination)
  • width - width of generated sprite file
  • height - height of generated sprite file
  • timestamp - timestamp (can be used in template as image url parameter to prevent caching issues) - deprecated, use checksum instead
  • checksum - md5 hash of the generated sprite image file (can be used in template as image url parameter to prevent caching issues)

Each item in sprites array contains the following properties:

  • name - name of the sprite (= original file name without extension)
  • x - x position of the sprite in generated sprite file
  • y - y position of the sprite in generated sprite file
  • width - width of the sprite
  • height - height of the sprite

Example of template file for generating simple CSS:

{{#sprites}}.{{name}} = { background: url('{{../baseUrl}}{{../fileName}}') no-repeat -{{x}}px -{{y}}px; width: {{width}}px; height: {{height}}px; }
{{/sprites}}

Example of template file for generating Stylus declarations to be processed later by some Stylus function:

{{#sprites}}{{name}} = ('{{../baseUrl}}{{../fileName}}' -{{x}}px -{{y}}px {{width}}px {{height}}px {{../name}})
{{/sprites}}

options.template

Type: String

A path to the template file.

options.destCss

Type: String

A path to the metafile that will be generated from the template.

options.baseUrl

Type: String

URL path to the generated sprite file.

options.padding

Type: Number Default: 0

White space that will be added as a padding around every sprite (in px). It is useful to prevent bleeding pixels from one sprite to another when scaled / interpolated in the browser.

options.format

Type: String Default: 'png32'

Output file format. Any image format supported by ImageMagick should work (tested 'png32' and 'jpg').

options.background

Type: String Default: 'none'

Background color of output image. 'none' = transparent, '#ffffff' = white, '#ff0000' = red etc.

options.quality

Type: String Default: '100%'

Compression quality of output file. Has no effect for file formats other than jpg.

options.evenPixels

Type: Boolean Default: false

When set to true, dimensions of sprites will be aligned to even pixels (useful when scaled down by factor of 2 in CSS for mobile/retina/nonretina optimization). Note that padding must be set to (multiple of) 2 as well.

options.svg

Type: Boolean Default: false Version: 0.1.7

When set to true, the packer will work in experimental SVG mode. In this mode, all of the input sprites have to be valid SVG files. These SVGs will be merged into a single SVG sprite file by some XML processing. Individual sprites will be included as SVG elements (SVG root element can contain another SVG elements) and positioned using the same packer algorithm as in normal "bitmap" mode. Note that each SVG sprite should have correctly set width and height attributes that define its bounding box. The viewBox attribute is ignored. To prevent problems with duplicate ID attributes from different files, any IDs are prefixed with the name of the sprite. All xlink:href and url(#) references are modified accordingly.

Credits

This plugin uses binary tree packer algorithm by Jake Gordon (https://github.com/jakesgordon/bin-packing/).