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
|
2013-01-08 12:03:44 +08:00
|
|
|
* @param {Boolean} [config.clearBeforeDraw] set this property to false if you don't want
|
|
|
|
* to clear the canvas before each layer draw. The default value is true.
|
2013-01-27 12:42:19 +08:00
|
|
|
* {{NodeParams}}
|
2013-02-16 10:20:34 +08:00
|
|
|
* {{ContainerParams}}
|
2012-12-02 04:04:10 +08:00
|
|
|
*/
|
|
|
|
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.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
|
|
|
|
2013-03-15 23:33:05 +08:00
|
|
|
this.createAttrs();
|
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);
|
|
|
|
}
|
2013-02-13 01:58:47 +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();
|
2013-03-15 23:33:05 +08:00
|
|
|
if(this.getClearBeforeDraw()) {
|
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
|
|
|
},
|
2013-01-03 14:02:00 +08:00
|
|
|
toDataURL: function(config) {
|
|
|
|
config = config || {};
|
2013-03-15 23:33:05 +08:00
|
|
|
var mimeType = config.mimeType || null,
|
|
|
|
quality = config.quality || null,
|
|
|
|
canvas, context,
|
|
|
|
x = config.x || 0,
|
|
|
|
y = config.y || 0;
|
2013-01-03 14:02:00 +08:00
|
|
|
|
|
|
|
// if dimension or position is defined, use Node toDataURL
|
|
|
|
if(config.width || config.height || config.x || config.y) {
|
|
|
|
return Kinetic.Node.prototype.toDataURL.call(this, config);
|
|
|
|
}
|
|
|
|
// otherwise get data url of the currently drawn layer
|
|
|
|
else {
|
2013-01-13 14:01:12 +08:00
|
|
|
return this.getCanvas().toDataURL(mimeType, quality);
|
2013-01-03 14:02:00 +08:00
|
|
|
}
|
2013-03-15 23:33:05 +08:00
|
|
|
|
2013-01-03 14:02:00 +08:00
|
|
|
},
|
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;
|
2013-03-15 06:07:35 +08:00
|
|
|
return this;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set after draw handler
|
|
|
|
* @name afterDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
* @param {Function} handler
|
|
|
|
*/
|
|
|
|
afterDraw: function(func) {
|
|
|
|
this.afterDrawFunc = func;
|
2013-03-15 06:07:35 +08:00
|
|
|
return this;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer canvas
|
|
|
|
* @name getCanvas
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
2013-03-22 15:46:41 +08:00
|
|
|
var stage = this.getStage();
|
|
|
|
return (stage && stage._isTempDDLayerActive()) ? stage.dragLayer.canvas : this.canvas;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer canvas context
|
|
|
|
* @name getContext
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
getContext: function() {
|
2013-03-22 15:46:41 +08:00
|
|
|
return this.getCanvas().getContext();
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* 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) {
|
2013-03-22 15:46:41 +08:00
|
|
|
this.getCanvas().element.style.display = 'block';
|
2012-12-02 04:04:10 +08:00
|
|
|
this.hitCanvas.element.style.display = 'block';
|
2012-10-06 09:59:03 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-03-22 15:46:41 +08:00
|
|
|
this.getCanvas().element.style.display = 'none';
|
2012-12-02 04:04:10 +08:00
|
|
|
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) {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.removeChild(this.getCanvas().element);
|
2012-09-26 06:57:57 +08:00
|
|
|
|
2012-12-02 04:04:10 +08:00
|
|
|
if(index < stage.getChildren().length - 1) {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.insertBefore(this.getCanvas().element, stage.getChildren()[index + 1].getCanvas().element);
|
2012-09-26 06:57:57 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.appendChild(this.getCanvas().element);
|
2012-09-26 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
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) {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.removeChild(this.getCanvas().element);
|
|
|
|
stage.content.appendChild(this.getCanvas().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) {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.removeChild(this.getCanvas().element);
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
if(this.index < stage.getChildren().length - 1) {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.insertBefore(this.getCanvas().element, stage.getChildren()[this.index + 1].getCanvas().element);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
else {
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.appendChild(this.getCanvas().element);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
moveDown: function() {
|
|
|
|
if(Kinetic.Node.prototype.moveDown.call(this)) {
|
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
var children = stage.getChildren();
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.removeChild(this.getCanvas().element);
|
|
|
|
stage.content.insertBefore(this.getCanvas().element, children[this.index + 1].getCanvas().element);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
moveToBottom: function() {
|
|
|
|
if(Kinetic.Node.prototype.moveToBottom.call(this)) {
|
|
|
|
var stage = this.getStage();
|
|
|
|
if(stage) {
|
|
|
|
var children = stage.getChildren();
|
2013-03-22 15:46:41 +08:00
|
|
|
stage.content.removeChild(this.getCanvas().element);
|
|
|
|
stage.content.insertBefore(this.getCanvas().element, children[1].getCanvas().element);
|
2012-12-02 04:04:10 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
},
|
|
|
|
getLayer: function() {
|
|
|
|
return this;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* remove layer from stage
|
|
|
|
*/
|
|
|
|
remove: function() {
|
2013-03-22 15:46:41 +08:00
|
|
|
var stage = this.getStage(), canvas = this.getCanvas(), element = canvas.element;
|
2012-12-02 04:04:10 +08:00
|
|
|
Kinetic.Node.prototype.remove.call(this);
|
2013-01-13 14:01:12 +08:00
|
|
|
|
|
|
|
if(stage && canvas && Kinetic.Type._isInDocument(element)) {
|
|
|
|
stage.content.removeChild(element);
|
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
|
2013-03-15 23:33:05 +08:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Layer, 'clearBeforeDraw', true);
|
2012-12-02 04:04:10 +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
|
|
|
/**
|
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
|
|
|
})();
|