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

speet

v1.0.1

Published

Simple dynamic spritesheet generator for web app

Downloads

2

Readme

speet.js: Dynamic Spritesheet Generator for web

Minimum module to create spritesheet dynamically.
Currently, it is mostly for creating pixi.js spritesheet.

Install

npm install speet

Usage

Sample

Basic

Traditional browser style

<script src="path/to/speet.js"></script>
<script>
const imageList = {
  player: "./assets/player.png",
  enemy: "./assets/enemy.png",
}

speet.generate(imageList)
.then((ss)=> {
  console.log(ss.data); // json of sprite frame data
  document.body.appendChild(ss.image);
})
</script>

Modern ES style

import {generate} from "speet";

const imageList = {
  player: "./assets/player.png",
  enemy: "./assets/enemy.png",
};

(async ()=> {
  const { image: ssImage, data: ssData } = await generate(imageList)
  console.log(ssData); // json of sprite frame data
  document.body.appendChild(ssImage);
})();

Combination with pixi.js

import {generate} from "speet";
function createPixiSpriteSheet(image, dataJson) {
  return new Promise((resolve)=> {
    const baseTexture = PIXI.BaseTexture.from(image);
    const ss = new PIXI.Spritesheet(baseTexture, dataJson);
    ss.parse(()=> resolve(ss))
  })
}

const imageList = {
  player: "./assets/player.png",
  enemy: "./assets/enemy.png",
};

(async ()=> {
  // pixi app
  const app = new PIXI.Application();
  document.body.appendChild(app.view);
  const interaction = app.renderer.plugins.interaction;

  // Create pixi spritesheet
  const baseSS = await speet.generate(imageList, { forcePOT: true });
  const pixiSS = await createPixiSpriteSheet(baseSS.image, baseSS.data);

  // Add sprite using spritesheet
  const spr = new PIXI.Sprite(pixiSS.textures["player"]);
  app.stage.addChild(spr);
  interaction.on('pointerdown', (e) => {
    spr.position.set(e.data.global.x, e.data.global.y)
  });
})();

Setting options

(async ()=> {
  const ss = await speet.generate(imageList, {
    padding: 4,
    border: 4,
    forcePOT: true,
    formatter: (sprites) => {
      return sprites.map((s)=> {
        return {
          x: s.x,
          y: s.y,
          w: s.width,
          h: s.height,
        }
      })
    }
  });
  
  console.log(ss.data); // array of objects with x, y, w, h property
})();

| Name | Type | Description | Default | |-----------|----------|----------------------------------------------------------------------------------------------------------------------------------------|---------| | padding | number | Sets padding between sprites. | 2 | | border | number | Sets border padding at the edges of spritesheet. | 0 | | forcePOT | boolean | Force spritesheet size to be power of two (e.g. 512x512). This option is for optimization of GPU-powered graphic system (mostly WebGL). | false | | formatter | function | Sets your own formatting function for spritesheet data json. | undefined |

Misc

Supported types of image

Instead of image path string, you can also pass dataURI string, HTMLImageElement, or HTMLCanvasElement.

function createCanvasRect(w, h) {
  const canvas = document.createElement('canvas');
  const ctx = canvas.getContext('2d');
  canvas.width = w;
  canvas.height = h;
  ctx.fillStyle = "#D93788";
  ctx.fillRect(0, 0, w, h);
  return canvas;
}

const imageList = {
  eggplant: "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADcAAAAtCAMAAAA9SAOJAAAAYFBMVEUAAAAiIDRFKDxmOTGPVjvfcSbZoGbuw5r78jaZ5VBqvjA3lG5LaS9SSyQyPDk/P3QwYIJbbuFjm/9fzeTL2/z///+brbeEfodpampZVlJ2QoqsMjLZV2PXe7qPl0qKbzD7Y7zPAAAAIHRSTlMA/////////////////////////////////////////5KarXYAAACqSURBVEiJ7dOxDsMgDEVRZhg8ZXn//6F1qFAqgm2eo3bqDVOUI1tCKeXfs2prv3W15sbpk2LnybBOM+N4V6EEtGutO4XkPOBktMOIu3pcJRnhgITDHPu9JpFbIWW+W6PImUqZBOw49Gj7DrYTx9lLdha5ecPBxF/TdPyaknOPGOvkYowL2cqJxGx28hkcVywUsGKgt/J+2OW88WrL3XOYB11mykgt7R76ei8wAEus0oBkTgAAAABJRU5ErkJggg=="
  canvasRect: createCanvasRect(128, 128),
}

speet.generate(imageList)

Other

Sprite packing algorithm is inspired by bertrand_a's idea.