2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
* @param {Boolean} [config.clearBeforeDraw] set this property to true if you'd like to disable
|
|
|
|
* canvas clearing before each new layer draw
|
|
|
|
*/
|
|
|
|
Kinetic.Layer = function(config) {
|
|
|
|
this._initLayer(config);
|
|
|
|
};
|
2012-08-23 14:13:09 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Layer.prototype = {
|
|
|
|
_initLayer: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
clearBeforeDraw: true
|
|
|
|
});
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
this.nodeType = 'Layer';
|
|
|
|
this.beforeDrawFunc = undefined;
|
|
|
|
this.afterDrawFunc = undefined;
|
2012-12-10 01:52:33 +08:00
|
|
|
this.canvas = new Kinetic.SceneCanvas();
|
2012-12-02 04:04:10 +08:00
|
|
|
this.canvas.getElement().style.position = 'absolute';
|
2012-12-22 14:51:57 +08:00
|
|
|
this.hitCanvas = new Kinetic.HitCanvas();
|
2012-03-20 12:09:13 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
// call super constructor
|
|
|
|
Kinetic.Container.call(this, config);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* draw children nodes. this includes any groups
|
|
|
|
* or shapes
|
|
|
|
* @name draw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
draw: function() {
|
|
|
|
// before draw handler
|
|
|
|
if(this.beforeDrawFunc !== undefined) {
|
|
|
|
this.beforeDrawFunc.call(this);
|
|
|
|
}
|
2012-08-20 11:44:45 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Container.prototype.draw.call(this);
|
2012-08-20 11:44:45 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
// after draw handler
|
|
|
|
if(this.afterDrawFunc !== undefined) {
|
|
|
|
this.afterDrawFunc.call(this);
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* draw children nodes on hit. this includes any groups
|
|
|
|
* or shapes
|
|
|
|
* @name drawHit
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
drawHit: function() {
|
|
|
|
this.hitCanvas.clear();
|
|
|
|
Kinetic.Container.prototype.drawHit.call(this);
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* draw children nodes on scene. this includes any groups
|
|
|
|
* or shapes
|
|
|
|
* @name drawScene
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
* @param {Kinetic.Canvas} [canvas]
|
|
|
|
*/
|
2012-12-11 16:08:59 +08:00
|
|
|
drawScene: function(canvas) {
|
|
|
|
canvas = canvas || this.getCanvas();
|
2012-12-02 04:04:10 +08:00
|
|
|
if(this.attrs.clearBeforeDraw) {
|
2012-12-11 16:08:59 +08:00
|
|
|
canvas.clear();
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
2012-12-11 16:08:59 +08:00
|
|
|
Kinetic.Container.prototype.drawScene.call(this, canvas);
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set before draw handler
|
|
|
|
* @name beforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
* @param {Function} handler
|
|
|
|
*/
|
|
|
|
beforeDraw: function(func) {
|
|
|
|
this.beforeDrawFunc = func;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after draw handler
|
|
|
|
* @name afterDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
* @param {Function} handler
|
|
|
|
*/
|
|
|
|
afterDraw: function(func) {
|
|
|
|
this.afterDrawFunc = func;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer canvas
|
|
|
|
* @name getCanvas
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
|
|
|
return this.canvas;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer canvas context
|
|
|
|
* @name getContext
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
getContext: function() {
|
|
|
|
return this.canvas.context;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* clear canvas tied to the layer
|
|
|
|
* @name clear
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
clear: function() {
|
2012-11-19 11:50:50 +08:00
|
|
|
this.getCanvas().clear();
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
// extenders
|
|
|
|
setVisible: function(visible) {
|
|
|
|
Kinetic.Node.prototype.setVisible.call(this, visible);
|
|
|
|
if(visible) {
|
|
|
|
this.canvas.element.style.display = 'block';
|
|
|
|
this.hitCanvas.element.style.display = 'block';
|
2012-10-06 09:59:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2012-12-02 04:04:10 +08:00
|
|
|
this.canvas.element.style.display = 'none';
|
|
|
|
this.hitCanvas.element.style.display = 'none';
|
2012-10-06 09:59:03 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
setZIndex: function(index) {
|
|
|
|
Kinetic.Node.prototype.setZIndex.call(this, index);
|
2012-09-26 06:57:57 +08:00
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
stage.content.removeChild(this.canvas.element);
|
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
if(index < stage.getChildren().length - 1) {
|
|
|
|
stage.content.insertBefore(this.canvas.element, stage.getChildren()[index + 1].canvas.element);
|
2012-09-26 06:57:57 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
stage.content.appendChild(this.canvas.element);
|
|
|
|
}
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
moveToTop: function() {
|
|
|
|
Kinetic.Node.prototype.moveToTop.call(this);
|
2012-09-26 06:57:57 +08:00
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
stage.content.removeChild(this.canvas.element);
|
2012-12-02 04:04:10 +08:00
|
|
|
stage.content.appendChild(this.canvas.element);
|
2012-09-26 06:57:57 +08:00
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
moveUp: function() {
|
|
|
|
if(Kinetic.Node.prototype.moveUp.call(this)) {
|
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
stage.content.removeChild(this.canvas.element);
|
|
|
|
|
|
|
|
if(this.index < stage.getChildren().length - 1) {
|
|
|
|
stage.content.insertBefore(this.canvas.element, stage.getChildren()[this.index + 1].canvas.element);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
stage.content.appendChild(this.canvas.element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
moveDown: function() {
|
|
|
|
if(Kinetic.Node.prototype.moveDown.call(this)) {
|
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
var children = stage.getChildren();
|
|
|
|
stage.content.removeChild(this.canvas.element);
|
|
|
|
stage.content.insertBefore(this.canvas.element, children[this.index + 1].canvas.element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
moveToBottom: function() {
|
|
|
|
if(Kinetic.Node.prototype.moveToBottom.call(this)) {
|
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
var children = stage.getChildren();
|
|
|
|
stage.content.removeChild(this.canvas.element);
|
|
|
|
stage.content.insertBefore(this.canvas.element, children[1].canvas.element);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getLayer: function() {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* remove layer from stage
|
|
|
|
*/
|
|
|
|
remove: function() {
|
2012-09-26 06:57:57 +08:00
|
|
|
var stage = this.getStage();
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Node.prototype.remove.call(this);
|
|
|
|
/*
|
|
|
|
* remove canvas DOM from the document if
|
|
|
|
* it exists
|
|
|
|
*/
|
|
|
|
try {
|
2012-09-26 06:57:57 +08:00
|
|
|
stage.content.removeChild(this.canvas.element);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
catch(e) {
|
|
|
|
Kinetic.Global.warn('unable to remove layer scene canvas element from the document');
|
2012-09-26 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
2012-12-02 04:04:10 +08:00
|
|
|
};
|
|
|
|
Kinetic.Global.extend(Kinetic.Layer, Kinetic.Container);
|
|
|
|
|
|
|
|
// add getters and setters
|
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Layer, ['clearBeforeDraw']);
|
|
|
|
|
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-12-02 04:04:10 +08:00
|
|
|
* set flag which determines if the layer is cleared or not
|
|
|
|
* before drawing
|
|
|
|
* @name setClearBeforeDraw
|
2012-07-19 14:28:45 +08:00
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-12-02 04:04:10 +08:00
|
|
|
* @param {Boolean} clearBeforeDraw
|
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
|
|
|
|
2012-08-27 09:25:51 +08:00
|
|
|
/**
|
2012-12-02 04:04:10 +08:00
|
|
|
* get flag which determines if the layer is cleared or not
|
|
|
|
* before drawing
|
|
|
|
* @name getClearBeforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-08-27 09:25:51 +08:00
|
|
|
*/
|
2012-12-02 04:04:10 +08:00
|
|
|
})();
|