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-24 13:39:55 +08:00
|
|
|
* @param {Boolean} [config.clearBeforeDraw] set this property to true if you'd like to disable
|
|
|
|
* canvas clearing before each new layer draw
|
|
|
|
* @param {Number} [config.x]
|
|
|
|
* @param {Number} [config.y]
|
|
|
|
* @param {Boolean} [config.visible]
|
|
|
|
* @param {Boolean} [config.listening] whether or not the node is listening for events
|
|
|
|
* @param {String} [config.id] unique id
|
|
|
|
* @param {String} [config.name] non-unique name
|
2012-08-14 14:06:29 +08:00
|
|
|
* @param {Number} [config.opacity] determines node opacity. Can be any number between 0 and 1
|
2012-07-24 13:39:55 +08:00
|
|
|
* @param {Object} [config.scale]
|
|
|
|
* @param {Number} [config.scale.x]
|
|
|
|
* @param {Number} [config.scale.y]
|
|
|
|
* @param {Number} [config.rotation] rotation in radians
|
|
|
|
* @param {Number} [config.rotationDeg] rotation in degrees
|
|
|
|
* @param {Object} [config.offset] offsets default position point and rotation point
|
|
|
|
* @param {Number} [config.offset.x]
|
|
|
|
* @param {Number} [config.offset.y]
|
|
|
|
* @param {Boolean} [config.draggable]
|
|
|
|
* @param {String} [config.dragConstraint] can be vertical, horizontal, or none. The default
|
|
|
|
* is none
|
|
|
|
* @param {Object} [config.dragBounds]
|
|
|
|
* @param {Number} [config.dragBounds.top]
|
|
|
|
* @param {Number} [config.dragBounds.right]
|
|
|
|
* @param {Number} [config.dragBounds.bottom]
|
|
|
|
* @param {Number} [config.dragBounds.left]
|
2012-07-09 12:56:52 +08:00
|
|
|
*/
|
2012-07-04 03:07:27 +08:00
|
|
|
Kinetic.Layer = Kinetic.Container.extend({
|
|
|
|
init: function(config) {
|
|
|
|
this.setDefaultAttrs({
|
|
|
|
clearBeforeDraw: true
|
|
|
|
});
|
2012-04-29 02:18:40 +08:00
|
|
|
|
2012-07-04 03:07:27 +08:00
|
|
|
this.nodeType = 'Layer';
|
|
|
|
this.beforeDrawFunc = undefined;
|
|
|
|
this.afterDrawFunc = undefined;
|
2012-07-19 14:28:45 +08:00
|
|
|
this.canvas = new Kinetic.Canvas();
|
|
|
|
this.canvas.getElement().style.position = 'absolute';
|
2012-08-12 07:22:01 +08:00
|
|
|
this.bufferCanvas = new Kinetic.Canvas();
|
|
|
|
this.bufferCanvas.name = 'buffer';
|
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-08-20 11:44:45 +08:00
|
|
|
// before draw handler
|
|
|
|
if(this.beforeDrawFunc !== undefined) {
|
|
|
|
this.beforeDrawFunc.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(canvas) {
|
|
|
|
this._draw(canvas);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this._draw(this.getCanvas());
|
|
|
|
this._draw(this.bufferCanvas);
|
|
|
|
}
|
|
|
|
|
|
|
|
// after draw handler
|
|
|
|
if(this.afterDrawFunc !== undefined) {
|
|
|
|
this.afterDrawFunc.call(this);
|
|
|
|
}
|
2012-04-29 02:18:40 +08:00
|
|
|
},
|
2012-08-14 14:06:29 +08:00
|
|
|
/**
|
|
|
|
* draw children nodes on buffer. this includes any groups
|
|
|
|
* or shapes
|
|
|
|
* @name drawBuffer
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
drawBuffer: function() {
|
|
|
|
this.draw(this.bufferCanvas);
|
|
|
|
},
|
2012-08-17 14:22:07 +08:00
|
|
|
/**
|
|
|
|
* draw children nodes on scene. this includes any groups
|
|
|
|
* or shapes
|
|
|
|
* @name drawScene
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
drawScene: function() {
|
|
|
|
this.draw(this.getCanvas());
|
|
|
|
},
|
2012-04-29 14:03:58 +08:00
|
|
|
/**
|
2012-07-24 13:39:55 +08:00
|
|
|
* set before draw handler
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name beforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-07-24 13:39:55 +08:00
|
|
|
* @param {Function} handler
|
2012-04-29 14:03:58 +08:00
|
|
|
*/
|
|
|
|
beforeDraw: function(func) {
|
|
|
|
this.beforeDrawFunc = func;
|
|
|
|
},
|
|
|
|
/**
|
2012-07-24 13:39:55 +08:00
|
|
|
* set after draw handler
|
2012-07-09 12:56:52 +08:00
|
|
|
* @name afterDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-07-24 13:39:55 +08:00
|
|
|
* @param {Function} handler
|
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
|
|
|
},
|
2012-07-22 12:09:02 +08:00
|
|
|
/**
|
|
|
|
* clear canvas tied to the layer
|
|
|
|
* @name clear
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
|
|
|
*/
|
|
|
|
clear: function() {
|
|
|
|
this.getCanvas().clear();
|
|
|
|
},
|
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
|
2012-07-24 13:39:55 +08:00
|
|
|
* 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
|
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
|
|
|
* @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
|
2012-07-23 14:30:56 +08:00
|
|
|
* @param {String} [config.mimeType] mime type. can be "image/png" or "image/jpeg".
|
|
|
|
* "image/png" is the default
|
|
|
|
* @param {Number} [config.width] data url image width
|
|
|
|
* @param {Number} [config.height] data url image height
|
|
|
|
* @param {Number} [config.quality] jpeg quality. If using an "image/jpeg" mimeType,
|
|
|
|
* you can specify the quality from 0 to 1, where 0 is very poor quality and 1
|
|
|
|
* is very high 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-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-08-21 13:11:13 +08:00
|
|
|
__draw: function(canvas) {
|
2012-08-20 11:44:45 +08:00
|
|
|
if(this.attrs.clearBeforeDraw) {
|
|
|
|
canvas.clear();
|
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-27 14:26:58 +08:00
|
|
|
Kinetic.Node.addGettersSetters(Kinetic.Layer, ['clearBeforeDraw']);
|
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
|
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* get flag which determines if the layer is cleared or not
|
|
|
|
* before drawing
|
|
|
|
* @name getClearBeforeDraw
|
|
|
|
* @methodOf Kinetic.Layer.prototype
|
2012-07-04 03:07:27 +08:00
|
|
|
*/
|