2012-12-01 12:04:10 -08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* Sprite constructor
|
|
|
|
* @constructor
|
|
|
|
* @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
|
2013-01-02 23:55:56 -08:00
|
|
|
* @param {Integer} [config.index] animation index
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {String} [config.fill] fill color
|
|
|
|
*
|
|
|
|
* @param {Image} [config.fillPatternImage] fill pattern image
|
|
|
|
* @param {Number} [config.fillPatternX]
|
|
|
|
* @param {Number} [config.fillPatternY]
|
|
|
|
* @param {Array|Object} [config.fillPatternOffset] array with two elements or object with x and y component
|
|
|
|
* @param {Array|Object} [config.fillPatternScale] array with two elements or object with x and y component
|
|
|
|
* @param {Number} [config.fillPatternRotation]
|
|
|
|
* @param {String} [config.fillPatternRepeat] can be 'repeat', 'repeat-x', 'repeat-y', or 'no-repeat'. The default is 'no-repeat'
|
|
|
|
*
|
|
|
|
* @param {Array|Object} [config.fillLinearGradientStartPoint] array with two elements or object with x and y component
|
|
|
|
* @param {Array|Object} [config.fillLinearGradientEndPoint] array with two elements or object with x and y component
|
|
|
|
* @param {Array} [config.fillLinearGradientColorStops] array of color stops
|
|
|
|
*
|
|
|
|
* @param {Array|Object} [config.fillRadialGradientStartPoint] array with two elements or object with x and y component
|
|
|
|
* @param {Array|Object} [config.fillRadialGradientEndPoint] array with two elements or object with x and y component
|
|
|
|
* @param {Number} [config.fillRadialGradientStartRadius]
|
|
|
|
* @param {Number} [config.fillRadialGradientEndRadius]
|
|
|
|
* @param {Array} [config.fillRadialGradientColorStops] array of color stops
|
|
|
|
*
|
|
|
|
* @param {String} [config.stroke] stroke color
|
|
|
|
* @param {Number} [config.strokeWidth] stroke width
|
|
|
|
* @param {String} [config.lineJoin] can be miter, round, or bevel. The default
|
|
|
|
* is miter
|
|
|
|
* @param {String} [config.lineCap] can be butt, round, or sqare. The default
|
|
|
|
* is butt
|
|
|
|
* @param {String} [config.shadowColor]
|
|
|
|
* @param {Number} [config.shadowBlur]
|
|
|
|
* @param {Obect} [config.shadowOffset]
|
|
|
|
* @param {Number} [config.shadowOffset.x]
|
|
|
|
* @param {Number} [config.shadowOffset.y]
|
|
|
|
* @param {Number} [config.shadowOpacity] shadow opacity. Can be any real number
|
|
|
|
* between 0 and 1
|
|
|
|
* @param {Array} [config.dashArray]
|
|
|
|
*
|
|
|
|
*
|
|
|
|
*
|
|
|
|
* @param {Number} [config.x]
|
|
|
|
* @param {Number} [config.y]
|
|
|
|
* @param {Number} [config.width]
|
|
|
|
* @param {Number} [config.height]
|
|
|
|
* @param {Boolean} [config.visible]
|
|
|
|
* @param {Boolean} [config.listening] whether or not the node is listening for events
|
|
|
|
* @param {String} [config.id] unique id
|
|
|
|
* @param {String} [config.name] non-unique name
|
|
|
|
* @param {Number} [config.opacity] determines node opacity. Can be any number between 0 and 1
|
|
|
|
* @param {Object} [config.scale]
|
|
|
|
* @param {Number} [config.scale.x]
|
|
|
|
* @param {Number} [config.scale.y]
|
|
|
|
* @param {Number} [config.rotation] rotation in radians
|
|
|
|
* @param {Number} [config.rotationDeg] rotation in degrees
|
|
|
|
* @param {Object} [config.offset] offset from center point and rotation point
|
|
|
|
* @param {Number} [config.offset.x]
|
|
|
|
* @param {Number} [config.offset.y]
|
|
|
|
* @param {Boolean} [config.draggable]
|
|
|
|
* @param {Function} [config.dragBoundFunc]
|
2012-12-01 12:04:10 -08:00
|
|
|
*/
|
|
|
|
Kinetic.Sprite = function(config) {
|
|
|
|
this._initSprite(config);
|
|
|
|
};
|
|
|
|
|
|
|
|
Kinetic.Sprite.prototype = {
|
|
|
|
_initSprite: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
index: 0,
|
|
|
|
frameRate: 17
|
|
|
|
});
|
2013-01-01 00:41:13 -08:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
// call super constructor
|
|
|
|
Kinetic.Shape.call(this, config);
|
2013-01-01 00:41:13 -08:00
|
|
|
this.shapeType = 'Sprite';
|
2012-12-01 12:04:10 -08:00
|
|
|
this._setDrawFuncs();
|
|
|
|
|
|
|
|
this.anim = new Kinetic.Animation();
|
|
|
|
var that = this;
|
|
|
|
this.on('animationChange', function() {
|
|
|
|
// reset index when animation changes
|
|
|
|
that.setIndex(0);
|
|
|
|
});
|
|
|
|
},
|
2012-12-09 09:52:33 -08:00
|
|
|
drawFunc: function(canvas) {
|
|
|
|
var anim = this.attrs.animation, index = this.attrs.index, f = this.attrs.animations[anim][index], context = canvas.getContext(), image = this.attrs.image;
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2012-12-09 09:52:33 -08:00
|
|
|
if(image) {
|
|
|
|
context.drawImage(image, f.x, f.y, f.width, f.height, 0, 0, f.width, f.height);
|
2012-12-01 12:04:10 -08:00
|
|
|
}
|
|
|
|
},
|
2012-12-09 09:52:33 -08:00
|
|
|
drawHitFunc: function(canvas) {
|
|
|
|
var anim = this.attrs.animation, index = this.attrs.index, f = this.attrs.animations[anim][index], context = canvas.getContext();
|
2012-12-06 20:23:18 -08:00
|
|
|
|
2012-07-28 10:46:16 -07:00
|
|
|
context.beginPath();
|
2012-12-06 20:23:18 -08:00
|
|
|
context.rect(0, 0, f.width, f.height);
|
2012-07-28 10:46:16 -07:00
|
|
|
context.closePath();
|
2012-12-09 09:52:33 -08:00
|
|
|
canvas.fill(this);
|
2012-12-01 12:04:10 -08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* start sprite animation
|
|
|
|
* @name start
|
|
|
|
* @methodOf 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.node = layer;
|
|
|
|
|
|
|
|
this.interval = setInterval(function() {
|
|
|
|
var index = that.attrs.index;
|
|
|
|
that._updateIndex();
|
|
|
|
if(that.afterFrameFunc && index === that.afterFrameIndex) {
|
|
|
|
that.afterFrameFunc();
|
|
|
|
delete that.afterFrameFunc;
|
|
|
|
delete that.afterFrameIndex;
|
|
|
|
}
|
|
|
|
}, 1000 / this.attrs.frameRate);
|
2012-07-28 10:46:16 -07:00
|
|
|
|
2012-12-01 12:04:10 -08:00
|
|
|
this.anim.start();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* stop sprite animation
|
|
|
|
* @name stop
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
stop: function() {
|
|
|
|
this.anim.stop();
|
|
|
|
clearInterval(this.interval);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after frame event handler
|
|
|
|
* @name afterFrame
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
* @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;
|
|
|
|
}
|
2012-07-28 10:46:16 -07:00
|
|
|
}
|
2012-12-01 12:04:10 -08:00
|
|
|
};
|
|
|
|
Kinetic.Global.extend(Kinetic.Sprite, Kinetic.Shape);
|
|
|
|
|
|
|
|
// add getters setters
|
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Sprite, ['animation', 'animations', 'index']);
|
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* set animation key
|
|
|
|
* @name setAnimation
|
2012-07-26 22:58:38 -07:00
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @param {String} anim animation key
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* set animations object
|
|
|
|
* @name setAnimations
|
2012-07-26 22:58:38 -07:00
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
2012-12-01 12:04:10 -08:00
|
|
|
* @param {Object} animations
|
2012-07-26 22:58:38 -07:00
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
2012-07-26 22:58:38 -07:00
|
|
|
/**
|
2012-12-01 12:04:10 -08:00
|
|
|
* set animation frame index
|
|
|
|
* @name setIndex
|
2012-07-26 22:58:38 -07:00
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
* @param {Integer} index frame index
|
|
|
|
*/
|
2012-12-01 12:04:10 -08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* get animation key
|
|
|
|
* @name getAnimation
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animations object
|
|
|
|
* @name getAnimations
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get animation frame index
|
|
|
|
* @name getIndex
|
|
|
|
* @methodOf Kinetic.Sprite.prototype
|
|
|
|
*/
|
|
|
|
})();
|