Files
konva/src/shapes/Sprite.js

260 lines
7.6 KiB
JavaScript
Raw Normal View History

(function() {
/**
* Sprite constructor
* @constructor
2013-05-16 00:28:49 -07:00
* @memberof Kinetic
* @augments Kinetic.Shape
* @param {Object} config
* @param {String} config.animation animation key
* @param {Object} config.animations animation map
* @param {Integer} [config.frameIndex] animation frame index
2013-05-17 15:35:21 -07:00
* @param {Image} config.image image object
* @@shapeParams
* @@nodeParams
2013-05-17 15:35:21 -07:00
* @example
* var imageObj = new Image();<br>
* imageObj.onload = function() {<br>
* var sprite = new Kinetic.Sprite({<br>
* x: 200,<br>
* y: 100,<br>
* image: imageObj,<br>
* animation: 'standing',<br>
* animations: {<br>
* standing: [<br>
* // x, y, width, height (6 frames)<br>
* 0, 0, 49, 109,<br>
* 52, 0, 49, 109,<br>
* 105, 0, 49, 109,<br>
* 158, 0, 49, 109,<br>
* 210, 0, 49, 109,<br>
* 262, 0, 49, 109<br>
* ],<br>
* kicking: [<br>
* // x, y, width, height (6 frames)<br>
* 0, 109, 45, 98,<br>
* 45, 109, 45, 98,<br>
* 95, 109, 63, 98,<br>
* 156, 109, 70, 98,<br>
* 229, 109, 60, 98,<br>
* 287, 109, 41, 98<br>
* ]<br>
* },<br>
2013-05-17 15:35:21 -07:00
* frameRate: 7,<br>
* frameIndex: 0<br>
2013-05-17 15:35:21 -07:00
* });<br>
* };<br>
* imageObj.src = '/path/to/image.jpg'
*/
Kinetic.Sprite = function(config) {
this.___init(config);
};
Kinetic.Sprite.prototype = {
___init: function(config) {
// call super constructor
Kinetic.Shape.call(this, config);
this.className = 'Sprite';
this.anim = new Kinetic.Animation();
this.on('animationChange.kinetic', function() {
// reset index when animation changes
this.frameIndex(0);
});
this.sceneFunc(this._sceneFunc);
this.hitFunc(this._hitFunc);
},
_sceneFunc: function(context) {
var anim = this.getAnimation(),
index = this.frameIndex(),
ix4 = index * 4,
set = this.getAnimations()[anim],
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();
if(image) {
context.drawImage(image, x, y, width, height, 0, 0, width, height);
}
},
_hitFunc: function(context) {
var anim = this.getAnimation(),
index = this.frameIndex(),
ix4 = index * 4,
set = this.getAnimations()[anim],
width = set[ix4 + 2],
height = set[ix4 + 3];
context.beginPath();
context.rect(0, 0, width, height);
context.closePath();
2013-09-25 07:58:35 -07:00
context.fillShape(this);
},
_useBufferCanvas: function() {
return (this.hasShadow() || this.getAbsoluteOpacity() !== 1) && this.hasStroke();
},
/**
* start sprite animation
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Sprite.prototype
*/
start: function() {
var that = this;
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
*/
this.anim.setLayers(layer);
this.interval = setInterval(function() {
that._updateIndex();
2013-04-02 22:07:04 -07:00
}, 1000 / this.getFrameRate());
this.anim.start();
},
/**
* stop sprite animation
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Sprite.prototype
*/
stop: function() {
this.anim.stop();
clearInterval(this.interval);
},
_updateIndex: function() {
var index = this.frameIndex(),
2013-04-02 22:07:04 -07:00
animation = this.getAnimation(),
animations = this.getAnimations(),
anim = animations[animation],
len = anim.length / 4;
2013-04-02 22:07:04 -07:00
if(index < len - 1) {
this.frameIndex(index + 1);
}
else {
this.frameIndex(0);
}
}
};
Kinetic.Util.extend(Kinetic.Sprite, Kinetic.Shape);
// add getters setters
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'animation');
/**
* get/set animation key
* @name animation
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Sprite.prototype
* @param {String} anim animation key
* @returns {String}
* @example
* // get animation key<br>
* var animation = sprite.animation();<br><br>
*
* // set animation key<br>
* sprite.animation('kicking');
*/
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'animations');
2013-04-02 22:07:04 -07:00
/**
* get/set animations map
* @name animations
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Sprite.prototype
* @param {Object} animations
* @returns {Object}
* @example
* // get animations map<br>
* var animations = sprite.animations();<br><br>
*
* // set animations map<br>
* sprite.animations({<br>
* standing: [<br>
* // x, y, width, height (6 frames)<br>
* 0, 0, 49, 109,<br>
* 52, 0, 49, 109,<br>
* 105, 0, 49, 109,<br>
* 158, 0, 49, 109,<br>
* 210, 0, 49, 109,<br>
* 262, 0, 49, 109<br>
* ],<br>
* kicking: [<br>
* // x, y, width, height (6 frames)<br>
* 0, 109, 45, 98,<br>
* 45, 109, 45, 98,<br>
* 95, 109, 63, 98,<br>
* 156, 109, 70, 98,<br>
* 229, 109, 60, 98,<br>
* 287, 109, 41, 98<br>
* ]<br>
* });
*/
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'image');
/**
* get/set image
* @name image
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Sprite.prototype
* @param {Image} image
* @returns {Image}
* @example
* // get image
* var image = sprite.image();<br><br>
*
* // set image<br>
* sprite.image(imageObj);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameIndex', 0);
/**
* set/set animation frame index
* @name frameIndex
2013-05-16 00:28:49 -07:00
* @method
* @memberof Kinetic.Sprite.prototype
* @param {Integer} frameIndex
* @returns {Integer}
* @example
* // get animation frame index<br>
* var frameIndex = sprite.frameIndex();<br><br>
*
* // set animation frame index<br>
* sprite.frameIndex(3);
*/
Kinetic.Factory.addGetterSetter(Kinetic.Sprite, 'frameRate', 17);
2013-05-07 10:19:54 -07: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
* 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
* @returns {Integer}
* @example
* // get frame rate<br>
* var frameRate = sprite.frameRate();<br><br>
*
* // set frame rate to 2 frames per second<br>
* sprite.frameRate(2);
2013-05-07 10:19:54 -07:00
*/
Kinetic.Factory.backCompat(Kinetic.Sprite, {
index: 'frameIndex',
getIndex: 'getFrameIndex',
setIndex: 'setFrameIndex'
});
})();