2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Layer
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
2012-07-09 12:56:52 +08:00
|
|
|
/**
|
|
|
|
* Layer constructor. Layers are tied to their own canvas element and are used
|
|
|
|
* to contain groups or shapes
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Container
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
2012-07-04 03:07:27 +08:00
|
|
|
Kinetic.Layer = Kinetic.Container.extend({
|
|
|
|
init: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
throttle: 80,
|
|
|
|
clearBeforeDraw: true
|
|
|
|
});
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
this.nodeType = 'Layer';
|
|
|
|
this.lastDrawTime = 0;
|
|
|
|
this.beforeDrawFunc = undefined;
|
|
|
|
this.afterDrawFunc = undefined;
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-07-19 14:28:45 +08:00
|
|
|
this.canvas = new Kinetic.Canvas();
|
|
|
|
this.canvas.getElement().style.position = 'absolute';
|
2012-03-20 12:09:13 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
// call super constructor
|
|
|
|
this._super(config);
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
2012-03-24 14:39:54 +08:00
|
|
|
* draw children nodes. this includes any groups
|
|
|
|
* or shapes
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name draw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
2012-07-19 14:28:45 +08:00
|
|
|
draw: function(canvas) {
|
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-07-19 14:28:45 +08:00
|
|
|
this._draw(canvas);
|
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() {
|
2012-07-19 14:28:45 +08:00
|
|
|
that.draw(canvas);
|
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
|
|
|
},
|
2012-04-29 14:03:58 +08:00
|
|
|
/**
|
|
|
|
* set before draw function handler
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name beforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-04-29 14:03:58 +08:00
|
|
|
*/
|
|
|
|
beforeDraw: function(func) {
|
|
|
|
this.beforeDrawFunc = func;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after draw function handler
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name afterDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-04-29 14:03:58 +08:00
|
|
|
*/
|
|
|
|
afterDraw: function(func) {
|
|
|
|
this.afterDrawFunc = func;
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* get layer canvas
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name getCanvas
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
|
|
|
return this.canvas;
|
|
|
|
},
|
|
|
|
/**
|
2012-07-19 14:28:45 +08:00
|
|
|
* get layer canvas context
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name getContext
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
getContext: function() {
|
2012-07-19 14:28:45 +08:00
|
|
|
return this.canvas.context;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
toDataURL() is now synchronous, and works with all nodes, including the stage, layers, groups, and shapes. This also sets things up nicely for node caching. You can now cache anything, including the whole stage, layers, groups, or shapes, manifested as Kinetic Images that were instantiated with data urls
2012-07-15 09:10:37 +08:00
|
|
|
/**
|
|
|
|
* Creates a composite data URL. If MIME type is not
|
|
|
|
* specified, then "image/png" will result. For "image/jpeg", specify a quality
|
|
|
|
* level as quality (range 0.0 - 1.0). Note that this method works
|
|
|
|
* differently from toDataURL() for other nodes because it generates an absolute dataURL
|
|
|
|
* based on what's draw on the layer, rather than drawing
|
|
|
|
* the current state of each child node
|
|
|
|
* @name toDataURL
|
2012-07-19 14:28:45 +08:00
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-07-22 06:38:25 +08:00
|
|
|
* @param {Object} config
|
toDataURL() is now synchronous, and works with all nodes, including the stage, layers, groups, and shapes. This also sets things up nicely for node caching. You can now cache anything, including the whole stage, layers, groups, or shapes, manifested as Kinetic Images that were instantiated with data urls
2012-07-15 09:10:37 +08:00
|
|
|
*/
|
2012-07-22 06:38:25 +08:00
|
|
|
toDataURL: function(config) {
|
|
|
|
var canvas;
|
|
|
|
var mimeType = config && config.mimeType ? config.mimeType : null;
|
|
|
|
var quality = config && config.quality ? config.quality : null;
|
|
|
|
|
|
|
|
if(config && config.width && config.height) {
|
|
|
|
canvas = new Kinetic.Canvas(config.width, config.height);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
canvas = this.getCanvas();
|
|
|
|
}
|
|
|
|
return canvas.toDataURL(mimeType, quality);
|
toDataURL() is now synchronous, and works with all nodes, including the stage, layers, groups, and shapes. This also sets things up nicely for node caching. You can now cache anything, including the whole stage, layers, groups, or shapes, manifested as Kinetic Images that were instantiated with data urls
2012-07-15 09:10:37 +08:00
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* private draw children
|
|
|
|
*/
|
2012-07-19 14:28:45 +08:00
|
|
|
_draw: function(canvas) {
|
|
|
|
/*
|
|
|
|
* if canvas is not defined, then use the canvas
|
|
|
|
* tied to the layer
|
|
|
|
*/
|
|
|
|
if(!canvas) {
|
|
|
|
canvas = this.getCanvas();
|
|
|
|
}
|
|
|
|
|
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-06-24 07:11:58 +08:00
|
|
|
if(this.attrs.clearBeforeDraw) {
|
2012-07-19 14:28:45 +08:00
|
|
|
canvas.clear();
|
2012-06-24 07:11:58 +08:00
|
|
|
}
|
|
|
|
|
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-07-19 14:28:45 +08:00
|
|
|
this._drawChildren(canvas);
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
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
|
|
|
}
|
2012-07-04 03:07:27 +08:00
|
|
|
});
|
2012-06-24 07:11:58 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
// add getters and setters
|
2012-07-06 15:27:55 +08:00
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Layer, ['clearBeforeDraw', 'throttle']);
|
2012-06-24 07:11:58 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
* set flag which determines if the layer is cleared or not
|
|
|
|
* before drawing
|
|
|
|
* @name setClearBeforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
* @param {Boolean} clearBeforeDraw
|
|
|
|
*/
|
|
|
|
|
2012-07-06 15:27:55 +08:00
|
|
|
/**
|
|
|
|
* set throttle
|
|
|
|
* @name setThrottle
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
* @param {Number} throttle
|
|
|
|
*/
|
|
|
|
|
2012-06-24 07:11:58 +08:00
|
|
|
/**
|
|
|
|
* get flag which determines if the layer is cleared or not
|
|
|
|
* before drawing
|
|
|
|
* @name getClearBeforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-07-06 15:27:55 +08:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get throttle
|
|
|
|
* @name getThrottle
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-07-04 03:07:27 +08:00
|
|
|
*/
|