2012-03-07 13:45:48 +08:00
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
// Shape
|
|
|
|
///////////////////////////////////////////////////////////////////////
|
|
|
|
/**
|
|
|
|
* Shape constructor. Shapes are used to objectify drawing bits of a KineticJS
|
|
|
|
* application
|
|
|
|
* @constructor
|
|
|
|
* @augments Kinetic.Node
|
|
|
|
* @param {Object} config
|
|
|
|
*/
|
|
|
|
Kinetic.Shape = function(config) {
|
2012-03-18 01:28:25 +08:00
|
|
|
this.className = 'Shape';
|
2012-03-07 13:45:48 +08:00
|
|
|
|
|
|
|
// defaults
|
|
|
|
if(config.stroke !== undefined || config.strokeWidth !== undefined) {
|
|
|
|
if(config.stroke === undefined) {
|
2012-03-18 01:28:25 +08:00
|
|
|
config.stroke = 'black';
|
2012-03-14 12:16:25 +08:00
|
|
|
}
|
2012-03-18 05:35:34 +08:00
|
|
|
else if(config.strokeWidth === undefined) {
|
2012-03-07 13:45:48 +08:00
|
|
|
config.strokeWidth = 2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// required
|
|
|
|
this.drawFunc = config.drawFunc;
|
|
|
|
|
|
|
|
// call super constructor
|
|
|
|
Kinetic.Node.apply(this, [config]);
|
|
|
|
};
|
|
|
|
/*
|
|
|
|
* Shape methods
|
|
|
|
*/
|
|
|
|
Kinetic.Shape.prototype = {
|
|
|
|
/**
|
2012-03-14 12:16:25 +08:00
|
|
|
* get layer context where the shape is being drawn. When
|
|
|
|
* the shape is being rendered, .getContext() returns the context of the
|
|
|
|
* user created layer that contains the shape. When the event detection
|
|
|
|
* engine is determining whether or not an event has occured on that shape,
|
|
|
|
* .getContext() returns the context of the invisible backstage layer.
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
getContext: function() {
|
|
|
|
return this.tempLayer.getContext();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get shape temp layer canvas
|
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
|
|
|
return this.tempLayer.getCanvas();
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* helper method to fill and stroke a shape based on its
|
|
|
|
* fill, stroke, and strokeWidth properties
|
|
|
|
*/
|
|
|
|
fillStroke: function() {
|
|
|
|
var context = this.getContext();
|
|
|
|
|
|
|
|
if(this.fill !== undefined) {
|
|
|
|
context.fillStyle = this.fill;
|
|
|
|
context.fill();
|
|
|
|
}
|
|
|
|
if(this.stroke !== undefined) {
|
|
|
|
context.lineWidth = this.strokeWidth === undefined ? 1 : this.strokeWidth;
|
|
|
|
context.strokeStyle = this.stroke;
|
|
|
|
context.stroke();
|
|
|
|
}
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set fill which can be a color, gradient object,
|
|
|
|
* or pattern object
|
|
|
|
* @param {String|CanvasGradient|CanvasPattern} fill
|
|
|
|
*/
|
|
|
|
setFill: function(fill) {
|
|
|
|
this.fill = fill;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get fill
|
|
|
|
*/
|
|
|
|
getFill: function() {
|
|
|
|
return this.fill;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set stroke color
|
|
|
|
* @param {String} stroke
|
|
|
|
*/
|
|
|
|
setStroke: function(stroke) {
|
|
|
|
this.stroke = stroke;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get stroke color
|
|
|
|
*/
|
|
|
|
getStroke: function() {
|
|
|
|
return this.stroke;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set stroke width
|
|
|
|
* @param {Number} strokeWidth
|
|
|
|
*/
|
|
|
|
setStrokeWidth: function(strokeWidth) {
|
|
|
|
this.strokeWidth = strokeWidth;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get stroke width
|
|
|
|
*/
|
|
|
|
getStrokeWidth: function() {
|
|
|
|
return this.strokeWidth;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* draw shape
|
|
|
|
* @param {Layer} layer Layer that the shape will be drawn on
|
|
|
|
*/
|
|
|
|
_draw: function(layer) {
|
|
|
|
if(this.visible) {
|
|
|
|
var stage = layer.getStage();
|
|
|
|
var context = layer.getContext();
|
|
|
|
var family = [];
|
2012-03-23 04:47:37 +08:00
|
|
|
var parent = this.parent;
|
2012-03-07 13:45:48 +08:00
|
|
|
|
|
|
|
family.unshift(this);
|
2012-03-23 04:47:37 +08:00
|
|
|
while(parent) {
|
2012-03-07 13:45:48 +08:00
|
|
|
family.unshift(parent);
|
|
|
|
parent = parent.parent;
|
|
|
|
}
|
2012-03-24 14:39:54 +08:00
|
|
|
|
2012-03-23 04:47:37 +08:00
|
|
|
context.save();
|
2012-03-07 13:45:48 +08:00
|
|
|
for(var n = 0; n < family.length; n++) {
|
2012-03-23 04:47:37 +08:00
|
|
|
var node = family[n];
|
2012-03-25 01:03:28 +08:00
|
|
|
var m = node.getTransform().getMatrix();
|
2012-03-24 14:39:54 +08:00
|
|
|
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
|
|
|
2012-03-23 04:47:37 +08:00
|
|
|
if(node.getAbsoluteAlpha() !== 1) {
|
|
|
|
context.globalAlpha = node.getAbsoluteAlpha();
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
this.tempLayer = layer;
|
|
|
|
this.drawFunc.call(this);
|
|
|
|
context.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// extend Node
|
2012-03-12 07:05:47 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Shape, Kinetic.Node);
|