2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Layer
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Layer constructor. Layers are tied to their own canvas element and are used
|
|
|
|
* to contain groups or shapes
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Container
|
|
|
|
* @augments Kinetic.Node
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Layer = function(config) {
|
2012-04-29 03:55:18 +08:00
|
|
|
this.setDefaultAttrs({
|
2012-05-04 03:05:54 +08:00
|
|
|
throttle: 80
|
2012-04-29 03:55:18 +08:00
|
|
|
});
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-04-05 13:57:36 +08:00
|
|
|
this.nodeType = 'Layer';
|
2012-04-29 02:18:40 +08:00
|
|
|
this.lastDrawTime = 0;
|
2012-04-29 14:03:58 +08:00
|
|
|
this.beforeDrawFunc = undefined;
|
|
|
|
this.afterDrawFunc = undefined;
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
this.canvas = document.createElement('canvas');
|
|
|
|
this.context = this.canvas.getContext('2d');
|
|
|
|
this.canvas.style.position = 'absolute';
|
2012-03-20 12:09:13 +08:00
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
// call super constructors
|
|
|
|
Kinetic.Container.apply(this, []);
|
|
|
|
Kinetic.Node.apply(this, [config]);
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Layer methods
|
|
|
|
*/
|
|
|
|
Kinetic.Layer.prototype = {
|
|
|
|
/**
|
2012-03-24 14:39:54 +08:00
|
|
|
* draw children nodes. this includes any groups
|
|
|
|
* or shapes
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
draw: function() {
|
2012-04-29 02:18:40 +08:00
|
|
|
var throttle = this.attrs.throttle;
|
|
|
|
var date = new Date();
|
|
|
|
var time = date.getTime();
|
|
|
|
var timeDiff = time - this.lastDrawTime;
|
2012-05-20 13:18:33 +08:00
|
|
|
var tt = 1000 / throttle;
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-06-19 08:56:12 +08:00
|
|
|
if(timeDiff >= tt || throttle > 200) {
|
2012-04-29 02:18:40 +08:00
|
|
|
this._draw();
|
2012-05-20 13:18:33 +08:00
|
|
|
|
2012-04-29 04:57:43 +08:00
|
|
|
if(this.drawTimeout !== undefined) {
|
|
|
|
clearTimeout(this.drawTimeout);
|
2012-04-29 10:52:45 +08:00
|
|
|
this.drawTimeout = undefined;
|
2012-04-29 04:57:43 +08:00
|
|
|
}
|
2012-04-29 02:18:40 +08:00
|
|
|
}
|
2012-04-29 04:46:54 +08:00
|
|
|
/*
|
|
|
|
* if we cannot draw the layer due to throttling,
|
|
|
|
* try to redraw the layer in the near future
|
|
|
|
*/
|
|
|
|
else if(this.drawTimeout === undefined) {
|
|
|
|
var that = this;
|
2012-05-20 13:18:33 +08:00
|
|
|
/*
|
|
|
|
* wait 17ms before trying again (60fps)
|
|
|
|
*/
|
2012-04-29 04:46:54 +08:00
|
|
|
this.drawTimeout = setTimeout(function() {
|
|
|
|
that.draw();
|
2012-05-20 13:18:33 +08:00
|
|
|
}, 17);
|
2012-04-29 04:46:54 +08:00
|
|
|
}
|
2012-04-29 02:18:40 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set throttle
|
|
|
|
* @param {Number} throttle in ms
|
|
|
|
*/
|
|
|
|
setThrottle: function(throttle) {
|
2012-04-29 03:55:18 +08:00
|
|
|
this.attrs.throttle = throttle;
|
2012-04-29 02:18:40 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get throttle
|
|
|
|
*/
|
|
|
|
getThrottle: function() {
|
2012-04-29 03:55:18 +08:00
|
|
|
return this.attrs.throttle;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
2012-04-29 14:03:58 +08:00
|
|
|
/**
|
|
|
|
* set before draw function handler
|
|
|
|
*/
|
|
|
|
beforeDraw: function(func) {
|
|
|
|
this.beforeDrawFunc = func;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after draw function handler
|
|
|
|
*/
|
|
|
|
afterDraw: function(func) {
|
|
|
|
this.afterDrawFunc = func;
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
2012-03-24 14:39:54 +08:00
|
|
|
* clears the canvas context tied to the layer. Clearing
|
|
|
|
* a layer does not remove its children. The nodes within
|
|
|
|
* the layer will be redrawn whenever the .draw() method
|
|
|
|
* is used again.
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
clear: function() {
|
|
|
|
var context = this.getContext();
|
|
|
|
var canvas = this.getCanvas();
|
|
|
|
context.clearRect(0, 0, canvas.width, canvas.height);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer canvas
|
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
|
|
|
return this.canvas;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer context
|
|
|
|
*/
|
|
|
|
getContext: function() {
|
|
|
|
return this.context;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* private draw children
|
|
|
|
*/
|
|
|
|
_draw: function() {
|
2012-05-20 13:18:33 +08:00
|
|
|
var date = new Date();
|
2012-05-04 03:05:54 +08:00
|
|
|
var time = date.getTime();
|
2012-05-20 13:18:33 +08:00
|
|
|
this.lastDrawTime = time;
|
|
|
|
|
2012-04-29 14:03:58 +08:00
|
|
|
// before draw handler
|
|
|
|
if(this.beforeDrawFunc !== undefined) {
|
2012-05-20 13:18:33 +08:00
|
|
|
this.beforeDrawFunc.call(this);
|
2012-04-29 14:03:58 +08:00
|
|
|
}
|
|
|
|
|
2012-03-07 13:45:48 +08:00
|
|
|
this.clear();
|
2012-06-03 10:12:06 +08:00
|
|
|
if(this.isVisible()) {
|
2012-05-20 13:18:33 +08:00
|
|
|
// draw custom func
|
|
|
|
if(this.attrs.drawFunc !== undefined) {
|
|
|
|
this.attrs.drawFunc.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
// draw children
|
2012-03-07 13:45:48 +08:00
|
|
|
this._drawChildren();
|
|
|
|
}
|
2012-04-29 14:03:58 +08:00
|
|
|
|
|
|
|
// after draw handler
|
|
|
|
if(this.afterDrawFunc !== undefined) {
|
2012-05-20 13:18:33 +08:00
|
|
|
this.afterDrawFunc.call(this);
|
2012-04-29 14:03:58 +08:00
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
};
|
|
|
|
// Extend Container and Node
|
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Layer, Kinetic.Container);
|
2012-03-13 13:41:09 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Layer, Kinetic.Node);
|