konva/src/shapes/Sprite.js

121 lines
3.0 KiB
JavaScript
Raw Normal View History

///////////////////////////////////////////////////////////////////////
// Sprite
///////////////////////////////////////////////////////////////////////
Kinetic.Sprite = Kinetic.Shape.extend({
/**
* Sprite constructor
* @constructor
* @augments Kinetic.Shape
* @param {Object} config
*/
init: function(config) {
this.setDefaultAttrs({
index: 0,
frameRate: 17
});
config.drawFunc = function() {
if(!!this.attrs.image) {
var context = this.getContext();
var anim = this.attrs.animation;
var index = this.attrs.index;
var f = this.attrs.animations[anim][index];
context.beginPath();
context.rect(0, 0, f.width, f.height);
context.closePath();
this.drawImage(this.attrs.image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height);
}
};
// call super constructor
this._super(config);
var that = this;
this.on('animationChange', function() {
// reset index when animation changes
that.setIndex(0);
});
},
/**
* start sprite animation
*/
start: function() {
var that = this;
var layer = this.getLayer();
this.interval = setInterval(function() {
that._updateIndex();
layer.draw();
if(that.afterFrameFunc && that.attrs.index === that.afterFrameIndex) {
that.afterFrameFunc();
}
}, 1000 / this.attrs.frameRate)
},
/**
* stop sprite animation
*/
stop: function() {
clearInterval(this.interval);
},
/**
* set after frame event handler
* @param {Integer} index frame index
* @param {Function} func function to be executed after frame has been drawn
*/
afterFrame: function(index, func) {
this.afterFrameIndex = index;
this.afterFrameFunc = func;
},
_updateIndex: function() {
var i = this.attrs.index;
var a = this.attrs.animation;
if(i < this.attrs.animations[a].length - 1) {
this.attrs.index++;
}
else {
this.attrs.index = 0;
}
}
});
// add getters setters
Kinetic.Node.addGettersSetters(Kinetic.Sprite, ['animation', 'animations', 'index']);
/**
* set animation key
2012-06-14 17:19:51 +08:00
* @name setAnimation
* @methodOf Kinetic.Sprite.prototype
* @param {String} anim animation key
*/
/**
* set animations obect
2012-06-14 17:19:51 +08:00
* @name setAnimations
* @methodOf Kinetic.Sprite.prototype
* @param {Object} animations
*/
/**
* set animation frame index
2012-06-14 17:19:51 +08:00
* @name setIndex
* @methodOf Kinetic.Sprite.prototype
* @param {Integer} index frame index
*/
/**
* get animation key
2012-06-14 17:19:51 +08:00
* @name getAnimation
* @methodOf Kinetic.Sprite.prototype
*/
/**
* get animations object
2012-06-14 17:19:51 +08:00
* @name getAnimations
* @methodOf Kinetic.Sprite.prototype
*/
/**
* get animation frame index
2012-06-14 17:19:51 +08:00
* @name getIndex
* @methodOf Kinetic.Sprite.prototype
*/