Pixi.js: Parse sprite sheet image using object

Created on 2 Sep 2016  路  6Comments  路  Source: pixijs/pixi.js

Loading the JSON for a spritesheet loads the image and creates frames automatically, but when using something like Webpack, it would be great to save a roundtrip request and build the spritesheet json data into the bundle.

Unfortunately, there's no easy way to use PIXI's built in spritesheet parser to make frames from the data when the image is loaded. A built-in method along the lines of PIXI.utils.framesFromData( jsonData, image or Texture ) would be quite helpful!

@danielberndt built a method to do this, and I adapted it use the built in spritesheetParser middleware, but this is easily broken if spritesheetParser changes.

import PIXI from "pixi.js";

export default function loadFromJson(name, pathToImage, data, resolution = parseInt(data.meta.scale, 10)) {

  var loader = new PIXI.loaders.Loader();

  PIXI.loaders.spritesheetParser().call(loader, {
      name: name,
      url: pathToImage,
      data: data,
      isJson: true,
      metadata: {}
    }, function(){ console.log('next', arguments, PIXI.utils.TextureCache); });

  return loader;
}

Usage:

import loadFromJson from "./loadFromJson.js";

var loader = loadFromJson(
  'spritesheet', 
  require("../spritesheets/spritesheet.png"), 
  require("../spritesheets/spritesheet.json")
);

function onAssetsLoaded(){
  var body = new PIXI.Sprite.fromFrame('body.png');
}
loader.load(onAssetsLoaded);
馃捑 v4.x (Legacy) 馃摙 Accepting PRs

Most helpful comment

good shout @shshaw ! I think separating the parsers from the loaders is something we should look into!

All 6 comments

good shout @shshaw ! I think separating the parsers from the loaders is something we should look into!

The method from above doesn't work anymore on 4.3.2 with webpack; and Pixi.js docs doesn't have any reference to Pixi.loaders.spritesheetParser.

Is there any way to make this work for webpack?

Hey,
I ran into the same problem today, did you find any solutions ?

Yeah, with new resourceloader its bad.

I propose to make the algorithm not depend on PIXI.loader

Looks to be resolved by #3676

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

lucap86 picture lucap86  路  3Comments

zcr1 picture zcr1  路  3Comments

SebastienFPRousseau picture SebastienFPRousseau  路  3Comments

sntiagomoreno picture sntiagomoreno  路  3Comments

gigamesh picture gigamesh  路  3Comments