From 366fe5df3d18b9469639ecf62b1585bb5a361a4c Mon Sep 17 00:00:00 2001 From: leohenning Date: Mon, 10 Nov 2014 16:20:11 -0500 Subject: [PATCH] Add in offset for packed sprites Many sprite animation tools pack the sprites in such a way that the size of the image is optimized. In order to play the sprite properly you need to change the x and y coordinate of the image so the sprite plays properly. Currently in Kineticjs there is no place to store this offset data. I added the offset data optionally to the sprite object and changed the scene function to use it if it exists. --- src/shapes/Sprite.js | 45 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 44 insertions(+), 1 deletion(-) diff --git a/src/shapes/Sprite.js b/src/shapes/Sprite.js index b680b744..4267b83d 100644 --- a/src/shapes/Sprite.js +++ b/src/shapes/Sprite.js @@ -87,6 +87,7 @@ index = this.frameIndex(), ix4 = index * 4, set = this.getAnimations()[anim], + offsets = this.getOffsets(), x = set[ix4 + 0], y = set[ix4 + 1], width = set[ix4 + 2], @@ -94,7 +95,13 @@ image = this.getImage(); if(image) { - context.drawImage(image, x, y, width, height, 0, 0, width, height); + if (offsets) { + var offset = offsets[anim], + ix2 = index * 2; + context.drawImage(image, x, y, width, height, offset[ix2 + 0], offset[ix2 + 1], width, height); + } else { + context.drawImage(image, x, y, width, height, 0, 0, width, height); + } } }, _hitFunc: function(context) { @@ -226,6 +233,42 @@ * }); */ + Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'offsets'); + + /** + * get/set offsets map + * @name offsets + * @method + * @memberof Kinetic.Sprite.prototype + * @param {Object} offsets + * @returns {Object} + * @example + * // get offsets map + * var offsets = sprite.offsets(); + * + * // set offsets map + * sprite.offsets({ + * standing: [ + * // x, y (6 frames) + * 0, 0, + * 0, 0, + * 5, 0, + * 0, 0, + * 0, 3, + * 2, 0 + * ], + * kicking: [ + * // x, y (6 frames) + * 0, 5, + * 5, 0, + * 10, 0, + * 0, 0, + * 2, 1, + * 0, 0 + * ] + * }); + */ + Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'image'); /**