2012-12-01 12:04:10 -08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Sprite constructor
|
|
|
|
* @constructor
|
2013-05-16 00:28:49 -07:00
|
|
|
* @memberof Kinetic
|
2012-12-01 12:04:10 -08:00
|
|
|
* @augments Kinetic.Shape
|
|
|
|
* @param {Object} config
|
2012-12-22 23:08:03 -08:00
|
|
|
* @param {String} config.animation animation key
|
|
|
|
* @param {Object} config.animations animation map
|
2014-01-07 23:56:49 -08:00
|
|
|
* @param {Integer} [config.frameIndex] animation frame index
|
2013-05-17 15:35:21 -07:00
|
|
|
* @param {Image} config.image image object
|
2013-06-01 10:27:44 -07:00
|
|
|
* @@shapeParams
|
|
|
|
* @@nodeParams
|
2013-05-17 15:35:21 -07:00
|
|
|
* @example
|
2014-04-03 20:17:09 -07:00
|
|
|
* var imageObj = new Image();
|
|
|
|
* imageObj.onload = function() {
|
|
|
|
* var sprite = new Kinetic.Sprite({
|
|
|
|
* x: 200,
|
|
|
|
* y: 100,
|
|
|
|
* image: imageObj,
|
|
|
|
* animation: 'standing',
|
|
|
|
* animations: {
|
|
|
|
* standing: [
|
|
|
|
* // x, y, width, height (6 frames)
|
|
|
|
* 0, 0, 49, 109,
|
|
|
|
* 52, 0, 49, 109,
|
|
|
|
* 105, 0, 49, 109,
|
|
|
|
* 158, 0, 49, 109,
|
|
|
|
* 210, 0, 49, 109,
|
|
|
|
* 262, 0, 49, 109
|
|
|
|
* ],
|
|
|
|
* kicking: [
|
|
|
|
* // x, y, width, height (6 frames)
|
|
|
|
* 0, 109, 45, 98,
|
|
|
|
* 45, 109, 45, 98,
|
|
|
|
* 95, 109, 63, 98,
|
|
|
|
* 156, 109, 70, 98,
|
|
|
|
* 229, 109, 60, 98,
|
|
|
|
* 287, 109, 41, 98
|
|
|
|
* ]
|
|
|
|
* },
|
|
|
|
* frameRate: 7,
|
|
|
|
* frameIndex: 0
|
|
|
|
* });
|
|
|
|
* };
|
2013-05-17 15:35:21 -07:00
|
|
|
* imageObj.src = '/path/to/image.jpg'
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
Kinetic.Sprite = function(config) {
|
2013-07-22 21:41:41 -07:00
|
|
|
this.___init(config);
|
2013-06-01 22:17:18 -07:00
|
|
|
};
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
Kinetic.Sprite.prototype = {
|
2013-07-22 21:41:41 -07:00
|
|
|
___init: function(config) {
|
2012-12-01 12:04:10 -08:00
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.call(this, config);
|
2013-05-19 21:48:48 -07:00
|
|
|
this.className = 'Sprite';
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2014-08-23 18:11:41 +08:00
|
|
|
this._updated = true;
|
|
|
|
var that = this;
|
|
|
|
this.anim = new Kinetic.Animation(function() {
|
|
|
|
// if we don't need to redraw layer we should return false
|
|
|
|
var updated = that._updated;
|
|
|
|
that._updated = false;
|
|
|
|
return updated;
|
|
|
|
});
|
2013-07-24 22:56:21 -07:00
|
|
|
this.on('animationChange.kinetic', function() {
|
2012-12-01 12:04:10 -08:00
|
|
|
// reset index when animation changes
|
2014-01-07 00:43:31 -08:00
|
|
|
this.frameIndex(0);
|
2012-12-01 12:04:10 -08:00
|
|
|
});
|
2014-08-23 18:11:41 +08:00
|
|
|
this.on('frameIndexChange.kinetic', function() {
|
|
|
|
this._updated = true;
|
|
|
|
});
|
2014-02-25 00:40:35 +08:00
|
|
|
// smooth change for frameRate
|
|
|
|
this.on('frameRateChange.kinetic', function() {
|
|
|
|
if (!this.anim.isRunning()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
clearInterval(this.interval);
|
2014-02-25 07:30:10 +08:00
|
|
|
this._setInterval();
|
2014-02-25 00:40:35 +08:00
|
|
|
});
|
2013-12-13 11:02:07 -08:00
|
|
|
|
2014-01-04 23:34:01 -08:00
|
|
|
this.sceneFunc(this._sceneFunc);
|
|
|
|
this.hitFunc(this._hitFunc);
|
2012-12-01 12:04:10 -08:00
|
|
|
},
|
2014-01-04 23:34:01 -08:00
|
|
|
_sceneFunc: function(context) {
|
2013-07-22 21:41:41 -07:00
|
|
|
var anim = this.getAnimation(),
|
2014-01-07 00:43:31 -08:00
|
|
|
index = this.frameIndex(),
|
|
|
|
ix4 = index * 4,
|
|
|
|
set = this.getAnimations()[anim],
|
2014-11-10 16:20:11 -05:00
|
|
|
offsets = this.getOffsets(),
|
2014-01-07 00:43:31 -08:00
|
|
|
x = set[ix4 + 0],
|
|
|
|
y = set[ix4 + 1],
|
|
|
|
width = set[ix4 + 2],
|
|
|
|
height = set[ix4 + 3],
|
2013-04-02 22:07:04 -07:00
|
|
|
image = this.getImage();
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2012-12-09 09:52:33 -08:00
|
|
|
if(image) {
|
2014-11-10 16:20:11 -05:00
|
|
|
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);
|
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
},
|
2014-01-04 23:34:01 -08:00
|
|
|
_hitFunc: function(context) {
|
2013-07-22 21:41:41 -07:00
|
|
|
var anim = this.getAnimation(),
|
2014-01-07 00:43:31 -08:00
|
|
|
index = this.frameIndex(),
|
|
|
|
ix4 = index * 4,
|
|
|
|
set = this.getAnimations()[anim],
|
|
|
|
width = set[ix4 + 2],
|
|
|
|
height = set[ix4 + 3];
|
2012-12-06 20:23:18 -08:00
|
|
|
|
2013-09-07 20:55:03 -07:00
|
|
|
context.beginPath();
|
2014-01-07 00:43:31 -08:00
|
|
|
context.rect(0, 0, width, height);
|
2013-09-07 20:55:03 -07:00
|
|
|
context.closePath();
|
2013-09-25 07:58:35 -07:00
|
|
|
context.fillShape(this);
|
2012-12-01 12:04:10 -08:00
|
|
|
},
|
2013-09-29 13:01:13 -07:00
|
|
|
_useBufferCanvas: function() {
|
|
|
|
return (this.hasShadow() || this.getAbsoluteOpacity() !== 1) && this.hasStroke();
|
|
|
|
},
|
2014-02-25 07:30:10 +08:00
|
|
|
_setInterval: function() {
|
|
|
|
var that = this;
|
|
|
|
this.interval = setInterval(function() {
|
|
|
|
that._updateIndex();
|
|
|
|
}, 1000 / this.getFrameRate());
|
|
|
|
},
|
2012-12-01 12:04:10 -08:00
|
|
|
/**
|
|
|
|
* start sprite animation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
start: function() {
|
|
|
|
var layer = this.getLayer();
|
|
|
|
|
|
|
|
/*
|
|
|
|
* animation object has no executable function because
|
|
|
|
* the updates are done with a fixed FPS with the setInterval
|
|
|
|
* below. The anim object only needs the layer reference for
|
|
|
|
* redraw
|
|
|
|
*/
|
2013-05-05 20:57:31 -07:00
|
|
|
this.anim.setLayers(layer);
|
2014-02-25 07:30:10 +08:00
|
|
|
this._setInterval();
|
2012-12-01 12:04:10 -08:00
|
|
|
this.anim.start();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop sprite animation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
this.anim.stop();
|
|
|
|
clearInterval(this.interval);
|
|
|
|
},
|
2014-03-02 10:46:45 +08:00
|
|
|
/**
|
|
|
|
* determine if animation of sprite is running or not. returns true or false
|
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Animation.prototype
|
|
|
|
* @returns {Boolean}
|
|
|
|
*/
|
|
|
|
isRunning: function() {
|
|
|
|
return this.anim.isRunning();
|
|
|
|
},
|
2012-12-01 12:04:10 -08:00
|
|
|
_updateIndex: function() {
|
2014-01-07 00:43:31 -08:00
|
|
|
var index = this.frameIndex(),
|
2013-04-02 22:07:04 -07:00
|
|
|
animation = this.getAnimation(),
|
|
|
|
animations = this.getAnimations(),
|
2013-07-22 21:41:41 -07:00
|
|
|
anim = animations[animation],
|
2014-01-07 00:43:31 -08:00
|
|
|
len = anim.length / 4;
|
2013-07-22 21:41:41 -07:00
|
|
|
|
2013-04-02 22:07:04 -07:00
|
|
|
if(index < len - 1) {
|
2014-01-07 00:43:31 -08:00
|
|
|
this.frameIndex(index + 1);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
else {
|
2014-01-07 00:43:31 -08:00
|
|
|
this.frameIndex(0);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
2012-07-28 10:46:16 -07:00
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
};
|
2013-05-07 23:51:02 -07:00
|
|
|
Kinetic.Util.extend(Kinetic.Sprite, Kinetic.Shape);
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
// add getters setters
|
2013-08-10 21:11:34 -07:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'animation');
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2014-01-07 00:43:31 -08:00
|
|
|
* get/set animation key
|
|
|
|
* @name animation
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @param {String} anim animation key
|
2013-09-16 22:05:28 -07:00
|
|
|
* @returns {String}
|
2014-01-07 00:43:31 -08:00
|
|
|
* @example
|
2014-04-03 20:17:09 -07:00
|
|
|
* // get animation key
|
|
|
|
* var animation = sprite.animation();
|
2014-01-07 00:43:31 -08:00
|
|
|
*
|
2014-04-03 20:17:09 -07:00
|
|
|
* // set animation key
|
2014-01-07 00:43:31 -08:00
|
|
|
* sprite.animation('kicking');
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2013-08-10 21:11:34 -07:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'animations');
|
2013-04-02 22:07:04 -07:00
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2014-01-07 00:43:31 -08:00
|
|
|
* get/set animations map
|
|
|
|
* @name animations
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-02 10:22:21 -07:00
|
|
|
* @param {Object} animations
|
2013-09-16 22:05:28 -07:00
|
|
|
* @returns {Object}
|
2014-01-07 00:43:31 -08:00
|
|
|
* @example
|
2014-04-03 20:17:09 -07:00
|
|
|
* // get animations map
|
|
|
|
* var animations = sprite.animations();
|
2014-01-07 00:43:31 -08:00
|
|
|
*
|
2014-04-03 20:17:09 -07:00
|
|
|
* // set animations map
|
|
|
|
* sprite.animations({
|
|
|
|
* standing: [
|
|
|
|
* // x, y, width, height (6 frames)
|
|
|
|
* 0, 0, 49, 109,
|
|
|
|
* 52, 0, 49, 109,
|
|
|
|
* 105, 0, 49, 109,
|
|
|
|
* 158, 0, 49, 109,
|
|
|
|
* 210, 0, 49, 109,
|
|
|
|
* 262, 0, 49, 109
|
|
|
|
* ],
|
|
|
|
* kicking: [
|
|
|
|
* // x, y, width, height (6 frames)
|
|
|
|
* 0, 109, 45, 98,
|
|
|
|
* 45, 109, 45, 98,
|
|
|
|
* 95, 109, 63, 98,
|
|
|
|
* 156, 109, 70, 98,
|
|
|
|
* 229, 109, 60, 98,
|
|
|
|
* 287, 109, 41, 98
|
|
|
|
* ]
|
2014-01-07 00:43:31 -08:00
|
|
|
* });
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
|
2014-11-10 16:20:11 -05:00
|
|
|
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
|
|
|
|
* ]
|
|
|
|
* });
|
|
|
|
*/
|
|
|
|
|
2013-08-10 21:11:34 -07:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'image');
|
2013-05-02 10:22:21 -07:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
/**
|
2014-01-07 00:43:31 -08:00
|
|
|
* get/set image
|
|
|
|
* @name image
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-07-22 21:41:41 -07:00
|
|
|
* @param {Image} image
|
2014-01-07 00:43:31 -08:00
|
|
|
* @returns {Image}
|
|
|
|
* @example
|
|
|
|
* // get image
|
2014-04-03 20:17:09 -07:00
|
|
|
* var image = sprite.image();
|
2014-01-07 00:43:31 -08:00
|
|
|
*
|
2014-04-03 20:17:09 -07:00
|
|
|
* // set image
|
2014-01-07 00:43:31 -08:00
|
|
|
* sprite.image(imageObj);
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
|
2014-01-07 00:43:31 -08:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameIndex', 0);
|
2013-05-02 10:22:21 -07:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
/**
|
2014-01-07 00:43:31 -08:00
|
|
|
* set/set animation frame index
|
|
|
|
* @name frameIndex
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2014-01-07 00:43:31 -08:00
|
|
|
* @param {Integer} frameIndex
|
2013-09-16 22:05:28 -07:00
|
|
|
* @returns {Integer}
|
2014-01-07 00:43:31 -08:00
|
|
|
* @example
|
2014-04-03 20:17:09 -07:00
|
|
|
* // get animation frame index
|
|
|
|
* var frameIndex = sprite.frameIndex();
|
2014-01-07 00:43:31 -08:00
|
|
|
*
|
2014-04-03 20:17:09 -07:00
|
|
|
* // set animation frame index
|
2014-01-07 00:43:31 -08:00
|
|
|
* sprite.frameIndex(3);
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
2013-05-02 10:22:21 -07:00
|
|
|
|
2013-08-10 21:11:34 -07:00
|
|
|
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameRate', 17);
|
2013-05-02 10:22:21 -07:00
|
|
|
|
2013-05-07 10:19:54 -07:00
|
|
|
/**
|
2014-01-07 00:43:31 -08:00
|
|
|
* get/set frame rate in frames per second. Increase this number to make the sprite
|
2013-05-07 10:19:54 -07:00
|
|
|
* animation run faster, and decrease the number to make the sprite animation run slower
|
2014-01-07 00:43:31 -08:00
|
|
|
* The default is 17 frames per second
|
|
|
|
* @name frameRate
|
2013-05-16 00:28:49 -07:00
|
|
|
* @method
|
|
|
|
* @memberof Kinetic.Sprite.prototype
|
2013-05-07 10:19:54 -07:00
|
|
|
* @param {Integer} frameRate
|
2014-01-07 00:43:31 -08:00
|
|
|
* @returns {Integer}
|
|
|
|
* @example
|
2014-04-03 20:17:09 -07:00
|
|
|
* // get frame rate
|
|
|
|
* var frameRate = sprite.frameRate();
|
2014-01-07 00:43:31 -08:00
|
|
|
*
|
2014-04-03 20:17:09 -07:00
|
|
|
* // set frame rate to 2 frames per second
|
2014-01-07 00:43:31 -08:00
|
|
|
* sprite.frameRate(2);
|
2013-05-07 10:19:54 -07:00
|
|
|
*/
|
2014-01-10 22:58:55 -08:00
|
|
|
|
|
|
|
Kinetic.Factory.backCompat(Kinetic.Sprite, {
|
|
|
|
index: 'frameIndex',
|
|
|
|
getIndex: 'getFrameIndex',
|
|
|
|
setIndex: 'setFrameIndex'
|
|
|
|
});
|
2014-01-19 21:35:28 -08:00
|
|
|
|
|
|
|
Kinetic.Collection.mapMethods(Kinetic.Sprite);
|
2012-12-01 12:04:10 -08:00
|
|
|
})();
|