pixi-spine
Spine implementation for pixi v3
Usage
Browserify
If you use browserify you can use pixi-spine like this:
var PIXI = require('pixi.js'),
spine = require('pixi-spine');
PIXI.loader
.add('spineCharacter', 'spine-data-1/HERO.json');
.load(function (loader, resources) {
var animation = new spine.Spine(resources.spineCharacter.spineData);
});
Prebuilt Files
If you are just including the built files, pixi spine adds itself to a pixi namespace:
PIXI.loader
.add('spineCharacter', 'spine-data-1/HERO.json');
.load(function (loader, resources) {
var animation = new PIXI.spine.Spine(resources.spineCharacter.spineData);
});
How to use spine events
animation.state.onEvent = function(i, event) {
console.log('event fired!', i, event);
};
How to choose resolution
Use with pixi-compressed-textures.js
PIXI.loader.before(PIXI.compressedTextures.extensionChooser(["@2x.atlas"]));
var options = { metadata: { spineMetadata: { choice: ["@.5x.atlas", "@2x.atlas"] } } };
PIXI.loader
.add('spineCharacter', 'spine-data-1/HERO.json', options);
.load(function (loader, resources) {
var animation = new PIXI.spine.Spine(resources.spineCharacter.spineData);
});
How to use pre-loaded json and atlas files
var rawSkeletonData = JSON.parse("$jsondata");
var rawAtlasData = "$atlasdata";
var spineAtlas = new spine.Atlas(rawAtlasData, function(line, callback) {
callback(PIXI.BaseTexture.fromImage(line));
});
var spineJsonParser = new PIXI.spine.SkeletonJsonParser(new PIXI.spine.AtlasAttachmentParser(spineAtlas));
var skeletonData = spineJsonParser.readSkeletonData(rawSkeletonData);
var spine = new PIXI.spine(skeletonData);
How to use pixi spritesheet with it
It's possible to load each image separately as opposed to loading in just one spritesheet. This can be useful if SVGs are needed instead of providing many PNG files. Simply create an Atlas object and pass in an object of image names and PIXI textures, like so:
var spine = PIXI.spine;
var loader = new PIXI.loaders.Loader();
var atlas = new spine.SpineRuntime.Atlas();
var allTextures = {
'head': PIXI.Texture.fromImage('head.svg'),
'left-eye': PIXI.Texture.fromImage('left-eye.svg')
};
atlas.addTextureHash(allTextures, true);
PIXI.loader
.add('spineboy', 'spineboy.json', {metadata: {spineAtlas: atlas}})
.load(function(response) {
var mySpineBoy = new PIXI.spine.Spine(response.resources.boy.spineData);
stage.addChild(mySpineBoy);
});
How to run animation
var spineBoy = new PIXI.spine.Spine(spineBoyData);
if (spineBoy.state.hasAnimationByName('run')) {
spineBoy.state.setAnimationByName(0, 'run', true);
spineBoy.state.timeScale = 0.1;
}
How to use compressed textures
PIXI.loader.before(PIXI.compressedTextures.extensionChooser(["@2x.atlas", ".dds"]));
var options = { metadata: { spineMetadata: { choice: ["@.5x.atlas", "@2x.atlas"] }, imageMetadata: { choice: [".dds", ".pvr"] } } };
PIXI.loader
.add('spineCharacter', 'spine-data-1/HERO.json', options);
.load(function (loader, resources) {
var animation = new PIXI.spine.Spine(resources.spineCharacter.spineData);
});
Building
You will need to have node and gulp setup on your machine.
Then you can install dependencies and build:
npm i && npm run build
That will output the built distributables to ./dist
.
Typescript
Typescript definition file for pixi-spine is available in pixi-typescript