2012-12-02 04:04:10 +08:00
|
|
|
(function() {
|
2013-05-08 14:51:02 +08:00
|
|
|
Kinetic.Util.addMethods(Kinetic.Layer, {
|
2012-12-02 04:04:10 +08:00
|
|
|
_initLayer: function(config) {
|
|
|
|
this.nodeType = 'Layer';
|
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);
|
2013-04-12 15:48:41 +08:00
|
|
|
|
2013-04-13 14:45:22 +08:00
|
|
|
this.canvas = new Kinetic.SceneCanvas();
|
2013-04-12 15:48:41 +08:00
|
|
|
this.canvas.getElement().style.position = 'absolute';
|
|
|
|
this.hitCanvas = new Kinetic.HitCanvas();
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
2013-04-12 14:51:21 +08:00
|
|
|
/**
|
|
|
|
* get intersection object that contains shape and pixel data
|
|
|
|
* @name getIntersection
|
2013-05-06 13:09:32 +08:00
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2013-04-12 14:51:21 +08:00
|
|
|
*/
|
|
|
|
getIntersection: function() {
|
2013-05-08 14:51:02 +08:00
|
|
|
var pos = Kinetic.Util._getXY(Array.prototype.slice.call(arguments)),
|
2013-04-12 14:51:21 +08:00
|
|
|
p, colorKey, shape;
|
|
|
|
|
|
|
|
if(this.isVisible() && this.isListening()) {
|
|
|
|
p = this.hitCanvas.context.getImageData(pos.x | 0, pos.y | 0, 1, 1).data;
|
|
|
|
// this indicates that a hit pixel may have been found
|
|
|
|
if(p[3] === 255) {
|
2013-05-08 14:51:02 +08:00
|
|
|
colorKey = Kinetic.Util._rgbToHex(p[0], p[1], p[2]);
|
2013-04-12 14:51:21 +08:00
|
|
|
shape = Kinetic.Global.shapes[colorKey];
|
|
|
|
return {
|
|
|
|
shape: shape,
|
|
|
|
pixel: p
|
|
|
|
};
|
|
|
|
}
|
|
|
|
// if no shape mapped to that pixel, return pixel array
|
|
|
|
else if(p[0] > 0 || p[1] > 0 || p[2] > 0 || p[3] > 0) {
|
|
|
|
return {
|
|
|
|
pixel: p
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
},
|
2013-04-05 14:17:20 +08:00
|
|
|
drawScene: function(canvas) {
|
2013-04-15 00:41:59 +08:00
|
|
|
var canvas = canvas || this.getCanvas();
|
2013-04-05 14:17:20 +08:00
|
|
|
|
2013-04-15 00:41:59 +08:00
|
|
|
if(this.getClearBeforeDraw()) {
|
|
|
|
canvas.clear();
|
2013-04-05 14:17:20 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
Kinetic.Container.prototype.drawScene.call(this, canvas);
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
2013-04-05 14:17:20 +08:00
|
|
|
drawHit: function() {
|
|
|
|
var layer = this.getLayer();
|
|
|
|
|
|
|
|
if(layer && layer.getClearBeforeDraw()) {
|
|
|
|
layer.getHitCanvas().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
Kinetic.Container.prototype.drawHit.call(this);
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get layer canvas
|
|
|
|
* @name getCanvas
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
2013-03-24 12:47:15 +08:00
|
|
|
return this.canvas;
|
2012-12-02 04:04:10 +08:00
|
|
|
},
|
2013-03-24 11:02:11 +08:00
|
|
|
/**
|
|
|
|
* get layer hit canvas
|
|
|
|
* @name getHitCanvas
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
getHitCanvas: function() {
|
|
|
|
return this.hitCanvas;
|
|
|
|
},
|
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
|
|
|
|
2013-05-08 14:51:02 +08:00
|
|
|
if(stage && canvas && Kinetic.Util._isInDocument(element)) {
|
2013-01-13 14:01:12 +08:00
|
|
|
stage.content.removeChild(element);
|
2012-09-26 06:57:57 +08:00
|
|
|
}
|
|
|
|
}
|
2013-05-08 14:17:57 +08:00
|
|
|
});
|
2013-05-08 14:51:02 +08:00
|
|
|
Kinetic.Util.extend(Kinetic.Layer, Kinetic.Container);
|
2012-12-02 04:04:10 +08:00
|
|
|
|
|
|
|
// 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
|
|
|
})();
|