Skip to content

Commit

Permalink
✨ Introducing new features.
Browse files Browse the repository at this point in the history
Spritesheet class expose a file property and a "get" function that return some frames
  • Loading branch information
GMartigny committed Nov 27, 2019
1 parent 636e2f8 commit 820a71e
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 11 deletions.
10 changes: 9 additions & 1 deletion modules/sprite/readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,15 @@ Sprite.sheet("spritesheet.json")
.then((sheet) => {
const position = scene.center;
const selector = "image_*.png";
const sprite = sheet.extract(position, selector);
const options = {
speed: 1, // Change frame every draw call (~60 fps)
loop: true, // Repeat the animation indefinitely
};

const sprite = sheet.extract(position, selector, options);
// Equivalent to
const sprite = new Sprite(position, sheet.file, sheet.get(selector), options);

scene.add(sprite).startLoop();
});
```
Expand Down
33 changes: 25 additions & 8 deletions modules/sprite/sprite.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,13 +207,19 @@ class Spritesheet {
}

/**
* Group images from the spritesheet into a single sprite
* @param {PositionDefinition} position - Position of the sprite
* @param {String|Function|RegExp} selector - Match against the spritesheet images name
* @param {ImageOptions} options - Options of the sprite
* @returns {Sprite}
* Getter for the image file
* @returns {Image}
*/
get file () {
return this.json.meta.file;
}

/**
* Return all the frames corresponding to a selector
* @param {String|Function|RegExp} [selector="*"] - Match against the spritesheet images name using a glob pattern, a validation function or a regular expression
* @returns {Array}
*/
extract (position, selector = "*", options) {
get (selector = "*") {
const filter = ((matcher) => {
if (typeof matcher === "function") {
return matcher;
Expand All @@ -229,8 +235,19 @@ class Spritesheet {
})(selector);

const { frames } = this.json;
const selected = Object.keys(frames).filter(filter).map(key => frames[key]);
return Object.keys(frames)
.filter(filter)
.map(key => frames[key]);
}

return new Sprite(position, this.json.meta.file, selected, options);
/**
* Group images from the spritesheet into a single sprite
* @param {PositionDefinition} position - Position of the sprite
* @param {String|Function|RegExp} [selector="*"] - Match against the spritesheet images name
* @param {ImageOptions} [options] - Options of the sprite
* @returns {Sprite}
*/
extract (position, selector, options) {
return new Sprite(position, this.file, this.get(selector), options);
}
}
5 changes: 3 additions & 2 deletions modules/sprite/sprite.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,11 @@ test("sheet", async (t) => {

const sheet = await Sprite.sheet("url");
t.true(typeof sheet.json === "object");
t.true(typeof sheet.extract === "function");

const sprite = sheet.extract([4, 5], "any");
const frames = sheet.get("any");
t.true(Array.isArray(frames));

const sprite = sheet.extract([4, 5], "any");
t.true(sprite instanceof Sprite);
});

Expand Down

0 comments on commit 820a71e

Please sign in to comment.