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

devkit-spriter

v0.0.9

Published

a 2D spriter for the DevKit game engine

Downloads

3

Readme

devkit-spriter

The devkit-spriter package provides three functions for the devkit build process:

  1. loading images
  2. spriting images into spritesheets
  3. caching spritesheets

Loading Images

To load images, use the loadImages(filenames : String[]) : Promise<ImageInfo[]> function:

var spriter = require('devkit-spriter');
spriter.loadImages(['image1.png', 'image2.png'])
    .then(function (images) {
        // images is an array of ImageInfo objects
    });

An ImageInfo object is a representation of an image that the spriter can use to create spritesheets. It has the following properites:

  • x : int the location of the image in a spritesheet
  • y : int the location of the image in a spritesheet
  • filename : string the full path to the image on disk
  • hash : string an md5 hash of the image bitmap
  • buffer : ImageBuffer the pixels of the image wrapped in a Jimp object (see https://github.com/oliver-moran/jimp). An ImageBuffer extends the Jimp base class with a drawImage call (based on the HTML5 canvas drawImage call and promisifies getBuffer and write.
  • width : int the original image width
  • height : int the original image height
  • area : int the image's area
  • data : Buffer the raw data in a node Buffer, alias for buffer.bitmap.data
  • depth : int the number of channels for the image, e.g. 4 for rgba
  • hasAlphaChannel : boolean true if the depth is 4
  • margin : {top : int, right : int, bottom : int, left : int} the size of the margins around the image (transparent regions in original image)
  • contentWidth : int the width minus the margins
  • contentHeight : int the height minus the margins
  • contentArea : int the content area, the number of pixels this image will occupy on a spritesheet (excluding spritesheet padding)

Spriting images

To sprite images, first load them into ImageInfo objects. Then call layout(ImageInfo[], opts) : Spritesheet[]:

var spritesheets = spriter.layout(images);

Note that this is a synchronous call since no IO is performed. This call maybe expensive, depending on how many images you provide.

opts can include:

  • padding : int number of pixels on each side of an image to pad out the image. Padding works by extending the border of the image. Defaults to 2, which prevents most texture artifacts in OpenGL.
  • maxSize : int the largest dimension of a spritesheet. Spritesheets cannot exceed this value in either width or height. Defaults to 1024.
  • powerOfTwoSheets : boolean whether sheets should round their dimensions up to the nearest power of two. Defaults to true.
  • name : string a prefix for spritesheet names
  • ext : string a postfix for all spritesheet names

Caching spritesheets

The spriter provides utility functions for reading and writing to a disk cache. It verifies the integrity of the disk cache before returning cached results. Call loadCache(cacheFile : string, outputDirectory : string) : Promise<DiskCache> to load a disk cache file:

var group = 'group1';
var filenames = ['image1.png', 'image2.png'];

spriter.loadCache('.my-cache-file', 'build/spritesheets/')
    .get(group, filenames)
    .then(function () {
        // cached
    })
    .catch(Spriter.NotCachedError, function () {
        // should resprite
    });

The spriter is commonly used with groups of spritesheets, since a common use case is to sprite images that will probably be used together into the same spritesheet(s). For example, all sprites in a common animation should be placed in a single group. Compression is another example of grouping: images compressed as jpgs should be sprited separately from images compressed as pngs so that the resulting spritesheets can be compressed correctly.

Calling get on DiskCache verifies the following:

  • no files were added to the group
  • no files were removed from the group
  • no files have changed in the group (comparing modified times)
  • the generated spritesheet files still exist
  • the spritesheet data looks valid (every sheet has a name and images, every image in the spritesheet is in the group)