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

game-sprite

v0.0.1

Published

The professional library for sprite of javascript

Downloads

8

Readme

Game Sprite

The professional library for sprite of javascript.

HOME PAGE

Feature

  • Easy to use

  • Small -> 21kb

  • Support TexturePacker tool

  • Multiple render support

#Compatibility

| Render Context | IE | Chrome | Safari | FireFox | |-----------------|:------------:|--------:|--------:|--------:| | DOM | 6+(no transform support) 9+(all functional) | * | * | * | | CANVAS | 9+ | * | * | * | | SVG(snapsvg.js) | 9+ | * | * | * |

How to use

1: Import

<script src="./game-sprite.min.js"></script>

//use
var mySprite = new Sprite({...});

2:Setting render context

GameSprite Support Canvas、DOM、SnapSVG, you need tell GameSprite which one you need.

//DOM: the dom which as sprite's container
//Snap: Snap paper
//Canvas: canvas.getContext('2d')
var sister = new Sprite({
    ctx: 'set the render context here'
});

3:Setting Sprite data resource

GameSprite Support 2 kind of data resource

  • The image that every frames is in same size, ex:image
  • TexturePacker Tool, ex:image data

We recommend use the TexturePacker Tool.

4: Creating Sprite Instance

No TexturePacker tool

var sister = new Sprite({
    //dom render example
    //render context
    ctx: document.getElementById('sprite-container'),

	//image url
	//if in canvas render context, here is the image object(new Image());
	res: 'http://gtms04.alicdn.com/tps/i4/TB11wd2FVXXXXcbXXXXo12dMVXX-6555-285.png',

	//total frame count
	count: 23,

	//every frame size
	width: 285,
	height: 285,

	//the resource image is multi row?
	row: 1,

	//default 100
	spf: 50,

	//setting the animation[beginFrame, endFrame]
	anim: {
		'runRight': [15, 22],
		'runLeft': [7, 14],
		'static': [1],
		'jump': [0],
		'falling': [2, 6]
	}
});

Using TexturePacker tool

var girl = new Sprite({
    ctx: document.getElementById('dom-sprite'),
	res: {
		image: '../res/gril.png',
		json: SPRITE_DATA
	}
});

When you using the TexturePacker, the rule of animation is the image's name.

For example, if you have 5 picture for running animation, before you drag them into TexturePacker, you need name them like: run-0.png run-1.png...run-4.png。

5: Running Sprite Animation

//run-left is the name of animation
sister.run('run-left');

//when you want to switch the animation, just run
sister.run('jump');

//if you want to run a series of animation
sister.run(['run-left', 'run-right', 'jump', 'run-left', 'jump']);

//you can set the count that animation play
sister.run('run-left', 4, function(){
    //do something
});

//if first argument is number, it means the spf of this animation
sister.run(20, 'run-left');

if you have the timer ticker

//you can use play, ‘play’ method do not running a timer auto
sister.play('run-left');

//custom ticker
requestAnimationFrame(function tick() {
    //to update the sprite's frames
    sister.update();
    requestAnimationFrame(tick);
});

In a general way, in canvas render context, we recommend replace play+update for run method, because you need 'ctx.clearRect'.

The difference between play and run

Sprite is running frame one to one, so it need timer ticker.

Run method will setting a timer by default.

But in some condition, you need play and manual updating.

API

run(name [,count] [,callback])

  • name

    • [String] default animation
    • [Array] a series of animation
    • [Number] spf of animation
  • count [optional]

    • [Number] the iteration count of animation
    • [Function] callback
  • callback [Function] [optional]

play(name [,count] [,callback]) just like run, but no timer ticker

draw(name [, crender])

  • name [String] animation name
  • crender [Function] [optional] custom render function

destroy

start start the animation

pause pause the animation

update update frames of animation

stop stop the sprite's timer

Property

cFrame: current frame

cAnim: current animation object

  • getName() get animation's name

rowNum:

row:

ctx: render context

view: the view of sprite

Using TexturePacker

TexturePacker JSON HASH Type

1: Download TexturePacker

google...

2: Splice images

TexturePacker using the image's name for json's key, so if you want to create a animation for running, you need named images like: girl-run-0.png girl-run-1.png girl-run-2.png, and then:

var girl = new Sprite({
    ctx: document.getElementById('dom-sprite'),
	res: {
		//the TexturePacker's image
		image: '../res/gril.png',
		//the TexturePacker's data
		json: SPRITE_DATA
	},
	sfp: 50
});

//the order is 0-1-2
girl.run('girl-run');

IMPORTANT the name must not has "." & must begin from "0".

if you only has one frame, you can name 'girl-run.png', not necessary 'girl-run-0.png'.

Advanced

custom render function

sprite.draw('custom render context',

function(){

	this._render = function(){};
	this._update = function(){};

});

//for example
//dom render
//this._render = domRender;
function domRender() {

    var s,
        offsetX,
        offsetY;

    function setPos() {
        offsetX = -((this.cFrame % this.rowNum) * this.width);
        offsetY = -(Math.floor(this.cFrame / this.rowNum) * this.height);
        this.view.style.backgroundPosition = offsetX + 'px ' + offsetY + 'px';
    };

    this._render = function () {
        this.view = document.createElement('div');

        s = this.view.style;
        s.width = this.width + 'px';
        s.height = this.height + 'px';

        s.backgroundImage = 'url("' + this.res + '")';
        s.backgroundRepeat = 'no-repeat';

        setPos.call(this);

        this.ctx.appendChild(this.view);

    };

    this._update = function () {
        setPos.call(this);
    };

}

ISSUE

If you have any questions with GameSprite, Just open an issue.