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) {
|
2013-04-12 15:48:41 +08:00
|
|
|
var contextType = '';
|
2012-12-02 04:04:10 +08:00
|
|
|
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
|
|
|
|
|
|
|
contextType = this.getContextType();
|
|
|
|
if (contextType === '2d') {
|
|
|
|
this.canvas = new Kinetic.SceneCanvas();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.canvas = new Kinetic.GenericCanvas({
|
|
|
|
contextType: contextType
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
this.canvas.getElement().style.position = 'absolute';
|
|
|
|
this.hitCanvas = new Kinetic.HitCanvas();
|
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
|
|
|
},
|
2013-04-12 14:51:21 +08:00
|
|
|
/**
|
|
|
|
* get intersection object that contains shape and pixel data
|
|
|
|
* @name getIntersection
|
|
|
|
* @methodOf Kinetic.Stage.prototype
|
|
|
|
* @param {Object} pos point object
|
|
|
|
*/
|
|
|
|
getIntersection: function() {
|
|
|
|
var pos = Kinetic.Type._getXY(Array.prototype.slice.call(arguments)),
|
|
|
|
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) {
|
|
|
|
colorKey = Kinetic.Type._rgbToHex(p[0], p[1], p[2]);
|
|
|
|
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) {
|
|
|
|
var layer = this.getLayer();
|
|
|
|
|
|
|
|
if(layer && layer.getClearBeforeDraw()) {
|
|
|
|
layer.getCanvas().clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
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);
|
2013-04-12 15:48:41 +08:00
|
|
|
Kinetic.Node.addGetterSetter(Kinetic.Layer, 'contextType', '2d');
|
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
|
|
|
})();
|