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

canvas-sprite

v4.0.1

Published

Render sprite animation with canvas, (0 dependencies)

Downloads

35

Readme

canvas-sprite

Render sprite animation with canvas

demo: https://codesandbox.io/s/red-dawn-m706x

Why?

Because why not ?

Even simple sprite sheet animation can be done with pure CSS, but:

  • the animation looks "shaking" on Android device when the element is using repsonsive unit in some case.
  • the animation looks "flashing" on iOS device when the sprite image's dimension is too large.

Install!

npm i -S canvas-sprite

or you can include dist/main.js with a <script /> tag, it exposes CanvasSprite to global

Usage!

Simple Case

import CanvasSprite from 'canvas-sprite';

var cs = CanvasSprite({
  canvas: document.getElementById('canvas'),
  imageUrl: './sprite.png',
  frames: 25,
  fps: 12,
  loop: true,
  onLoop: function (count) {
    console.log('have looped', count, 'times');
  },
  onEnd: function () {
    console.log('end');
  }
});

// call destroy when the canvas element is removed from DOM
cs.destroy();

Cache Management

If an url string is passed to options.imageUrl, the library has to fetch the image from the URL before display the animation, and when the image is too large, it may take too long to fetching it, this will be the case when trying to switch sprite sheet on the same canvas element. So we can pass a loaded Image to options.imageUrl to get a seamlessly switching.

import CanvasSprite, { loadImage } from 'canvas-sprite';

let cs = null;
function createAnim(image) {
  cs.destroy();
  cs = CanvasSprite({
    canvas: document.getElementById('canvas'),
    imageUrl: image,
    frames: 25,
    fps: 12,
    loop: true
  });
}

// prepare image cache
let cache = [];
Promise.all([
  loadImage('./sprite_sheet_1.png'),
  loadImage('./sprite_sheet_2.png')
])
  .then(imgs => {
    cache = imgs;
    console.log('cache is ready');
  })
  .then(() => {
    createAnim(cache[0]);
    // some time later...
    setTimeout(() => {
      createAnim(cache[1]);
    }, 10 * 1000);
  });

API!

options:

  • canvas (HTMLCanvasElement): the canvas we want to render the sprite, required
  • imageUrl (String|Image): the sprite image url or image element, required
  • frames (Number): the number of sprite frames, required
  • fps (Number): frames rendered per second, required
  • loop (Boolean): should sprite animation loop, default to true
  • onEnd (Function): function invoked when animation ends if options.loop is false
  • onLoop (Function): function invoked every time aniamtion loops if options.loop is true

instance methods:

  • play(): play animation, can be called after pause/stop
  • pause(): pause animation, stay in curren frame
  • stop(): stop animation, reset to first frame
  • destroy(): destroy animation, should be called when canvas element is removed(unmounted) from DOM, in case of memory leak

static methods:

  • loadImage(url): load image from url, return a Promise which resolve the loaded image

Migrate from v1.x

Basically nothing, width and height are removed from options since they can be calculated, automagically.

Caveats

  • we support single row sprite image only for now
  • make sure the width & height of every frame is integer for performance boost