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
|
2012-05-20 12:14:04 +08:00
|
|
|
* @config {String|Object} [fill] can be a string color, a linear gradient object, a radial
|
2012-05-14 01:46:49 +08:00
|
|
|
* gradient object, or a pattern object.
|
2012-03-31 14:57:10 +08:00
|
|
|
* @config {String} [stroke] stroke color
|
|
|
|
* @config {Number} [strokeWidth] stroke width
|
2012-05-14 01:46:49 +08:00
|
|
|
* @config {String} [lineJoin] line join can be "miter", "round", or "bevel". The default
|
2012-03-31 14:57:10 +08:00
|
|
|
* is "miter"
|
2012-05-20 12:14:04 +08:00
|
|
|
* @config {Object} [shadow] shadow object
|
2012-04-01 16:08:33 +08:00
|
|
|
* @config {String} [detectionType] shape detection type. Can be "path" or "pixel".
|
|
|
|
* The default is "path" because it performs better
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
Kinetic.Shape = function(config) {
|
2012-04-29 03:55:18 +08:00
|
|
|
this.setDefaultAttrs({
|
|
|
|
fill: undefined,
|
|
|
|
stroke: undefined,
|
|
|
|
strokeWidth: undefined,
|
|
|
|
lineJoin: undefined,
|
2012-05-20 13:11:33 +08:00
|
|
|
detectionType: 'path',
|
|
|
|
shadow: {
|
2012-05-21 01:47:28 +08:00
|
|
|
blur: 10,
|
|
|
|
alpha: 1,
|
2012-05-21 07:42:37 +08:00
|
|
|
offset: {
|
2012-05-27 01:57:56 +08:00
|
|
|
x: 0,
|
|
|
|
y: 0
|
2012-05-21 07:42:37 +08:00
|
|
|
}
|
2012-05-20 13:11:33 +08:00
|
|
|
}
|
2012-04-29 03:55:18 +08:00
|
|
|
});
|
2012-04-06 14:48:58 +08:00
|
|
|
|
2012-04-02 10:38:30 +08:00
|
|
|
this.data = [];
|
2012-04-08 05:39:31 +08:00
|
|
|
this.nodeType = 'Shape';
|
2012-03-07 13:45:48 +08:00
|
|
|
|
|
|
|
// 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,
|
2012-04-01 15:31:02 +08:00
|
|
|
* .getContext() returns the context of the invisible path layer.
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
|
|
|
getContext: function() {
|
2012-05-13 06:32:27 +08:00
|
|
|
if(this.tempLayer === undefined) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return this.tempLayer.getContext();
|
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get shape temp layer canvas
|
|
|
|
*/
|
|
|
|
getCanvas: function() {
|
|
|
|
return this.tempLayer.getCanvas();
|
|
|
|
},
|
2012-04-29 12:12:01 +08:00
|
|
|
/**
|
|
|
|
* helper method to stroke shape
|
|
|
|
*/
|
|
|
|
stroke: function() {
|
|
|
|
var context = this.getContext();
|
|
|
|
|
|
|
|
if(!!this.attrs.stroke || !!this.attrs.strokeWidth) {
|
|
|
|
var stroke = !!this.attrs.stroke ? this.attrs.stroke : 'black';
|
|
|
|
var strokeWidth = !!this.attrs.strokeWidth ? this.attrs.strokeWidth : 2;
|
|
|
|
|
|
|
|
context.lineWidth = strokeWidth;
|
|
|
|
context.strokeStyle = stroke;
|
|
|
|
context.stroke();
|
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:06:13 +08:00
|
|
|
/**
|
2012-05-20 13:11:33 +08:00
|
|
|
* applies shadow, fill, and stroke
|
2012-05-09 14:06:13 +08:00
|
|
|
*/
|
2012-05-10 13:31:55 +08:00
|
|
|
applyStyles: function() {
|
2012-05-09 14:06:13 +08:00
|
|
|
var context = this.getContext();
|
2012-05-10 10:15:49 +08:00
|
|
|
/*
|
|
|
|
* if fill is defined, apply shadow to
|
|
|
|
* fill only and not the stroke
|
|
|
|
*/
|
|
|
|
if(!!this.attrs.fill) {
|
2012-05-21 01:47:28 +08:00
|
|
|
this.applyShadow(this.fill);
|
2012-05-10 10:15:49 +08:00
|
|
|
this.stroke();
|
|
|
|
}
|
|
|
|
/*
|
2012-05-20 13:11:33 +08:00
|
|
|
* if fill is not defined, apply the shadow
|
2012-05-10 10:15:49 +08:00
|
|
|
* to the stroke
|
|
|
|
*/
|
|
|
|
else {
|
2012-05-21 01:47:28 +08:00
|
|
|
this.applyShadow(this.stroke);
|
2012-05-10 10:15:49 +08:00
|
|
|
}
|
2012-05-09 14:06:13 +08:00
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
2012-05-14 01:46:49 +08:00
|
|
|
* helper method to fill the shape with a color, linear gradient,
|
|
|
|
* radial gradient, or pattern
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
2012-05-09 14:06:13 +08:00
|
|
|
fill: function() {
|
2012-03-07 13:45:48 +08:00
|
|
|
var context = this.getContext();
|
2012-05-04 05:20:50 +08:00
|
|
|
var fill = this.attrs.fill;
|
|
|
|
if(!!fill) {
|
2012-05-13 09:18:06 +08:00
|
|
|
var s = fill.start;
|
|
|
|
var e = fill.end;
|
|
|
|
var f = null;
|
|
|
|
|
2012-05-04 05:20:50 +08:00
|
|
|
// color fill
|
|
|
|
if( typeof fill == 'string') {
|
|
|
|
f = this.attrs.fill;
|
2012-05-14 01:27:40 +08:00
|
|
|
context.fillStyle = f;
|
|
|
|
context.fill();
|
2012-05-04 05:20:50 +08:00
|
|
|
}
|
2012-05-21 02:12:07 +08:00
|
|
|
// pattern
|
2012-05-13 09:18:06 +08:00
|
|
|
else if(fill.image !== undefined) {
|
|
|
|
var repeat = fill.repeat === undefined ? 'repeat' : fill.repeat;
|
|
|
|
f = context.createPattern(fill.image, repeat);
|
2012-05-14 01:27:40 +08:00
|
|
|
|
2012-05-14 01:46:49 +08:00
|
|
|
context.save();
|
2012-05-27 01:57:56 +08:00
|
|
|
|
|
|
|
if(fill.offset !== undefined) {
|
|
|
|
context.translate(fill.offset.x, fill.offset.y);
|
|
|
|
}
|
|
|
|
|
2012-05-14 01:27:40 +08:00
|
|
|
context.fillStyle = f;
|
|
|
|
context.fill();
|
|
|
|
context.restore();
|
2012-05-13 09:18:06 +08:00
|
|
|
}
|
2012-05-21 02:12:07 +08:00
|
|
|
// linear gradient
|
|
|
|
else if(s.radius === undefined && e.radius === undefined) {
|
2012-05-13 09:18:06 +08:00
|
|
|
var context = this.getContext();
|
|
|
|
var grd = context.createLinearGradient(s.x, s.y, e.x, e.y);
|
2012-05-21 02:12:07 +08:00
|
|
|
var colorStops = fill.colorStops;
|
|
|
|
|
|
|
|
// build color stops
|
|
|
|
for(var n = 0; n < colorStops.length; n += 2) {
|
|
|
|
grd.addColorStop(colorStops[n], colorStops[n + 1]);
|
|
|
|
}
|
2012-05-13 09:18:06 +08:00
|
|
|
f = grd;
|
2012-05-14 01:27:40 +08:00
|
|
|
context.fillStyle = f;
|
|
|
|
context.fill();
|
2012-05-13 09:18:06 +08:00
|
|
|
}
|
|
|
|
// radial gradient
|
|
|
|
else if(s.radius !== undefined && e.radius !== undefined) {
|
|
|
|
var context = this.getContext();
|
2012-05-21 02:12:07 +08:00
|
|
|
var grd = context.createRadialGradient(s.x, s.y, s.radius, e.x, e.y, e.radius);
|
|
|
|
var colorStops = fill.colorStops;
|
|
|
|
|
|
|
|
// build color stops
|
|
|
|
for(var n = 0; n < colorStops.length; n += 2) {
|
|
|
|
grd.addColorStop(colorStops[n], colorStops[n + 1]);
|
|
|
|
}
|
2012-05-13 09:18:06 +08:00
|
|
|
f = grd;
|
2012-05-14 01:27:40 +08:00
|
|
|
context.fillStyle = f;
|
|
|
|
context.fill();
|
2012-05-13 09:18:06 +08:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
f = 'black';
|
2012-05-14 01:27:40 +08:00
|
|
|
context.fillStyle = f;
|
|
|
|
context.fill();
|
2012-05-04 05:20:50 +08:00
|
|
|
}
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
|
|
|
},
|
2012-03-31 14:57:10 +08:00
|
|
|
/**
|
|
|
|
* helper method to set the line join of a shape
|
|
|
|
* based on the lineJoin property
|
|
|
|
*/
|
|
|
|
applyLineJoin: function() {
|
|
|
|
var context = this.getContext();
|
2012-04-06 14:48:58 +08:00
|
|
|
if(this.attrs.lineJoin !== undefined) {
|
|
|
|
context.lineJoin = this.attrs.lineJoin;
|
2012-03-31 14:57:10 +08:00
|
|
|
}
|
|
|
|
},
|
2012-05-09 14:06:13 +08:00
|
|
|
/**
|
2012-05-20 12:14:04 +08:00
|
|
|
* apply shadow based on shadow color, blur,
|
|
|
|
* and offset properties
|
2012-05-21 01:47:28 +08:00
|
|
|
* @param {Function} draw function. Will typically be this.fill(), this.stroke(),
|
|
|
|
* this.fillText(), or this.strokeText()
|
2012-05-09 14:06:13 +08:00
|
|
|
*/
|
2012-05-21 01:47:28 +08:00
|
|
|
applyShadow: function(drawFunc) {
|
|
|
|
var context = this.getContext();
|
|
|
|
var s = this.attrs.shadow;
|
|
|
|
var aa = this.getAbsoluteAlpha();
|
|
|
|
var sa = this.attrs.shadow.alpha;
|
|
|
|
|
|
|
|
context.save();
|
|
|
|
|
|
|
|
if(sa === 1) {
|
|
|
|
this._applyShadow();
|
|
|
|
drawFunc.call(this);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
context.save();
|
|
|
|
context.globalAlpha = sa * aa;
|
|
|
|
this._applyShadow();
|
|
|
|
drawFunc.call(this);
|
|
|
|
context.restore();
|
|
|
|
drawFunc.call(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
context.restore();
|
|
|
|
},
|
|
|
|
_applyShadow: function() {
|
2012-05-09 14:06:13 +08:00
|
|
|
var context = this.getContext();
|
2012-05-20 12:14:04 +08:00
|
|
|
var s = this.attrs.shadow;
|
|
|
|
|
2012-05-20 13:11:33 +08:00
|
|
|
if(s.color !== undefined) {
|
2012-05-20 12:14:04 +08:00
|
|
|
context.shadowColor = s.color;
|
|
|
|
context.shadowBlur = s.blur;
|
|
|
|
context.shadowOffsetX = s.offset.x;
|
|
|
|
context.shadowOffsetY = s.offset.y;
|
2012-05-09 14:06:13 +08:00
|
|
|
}
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
2012-05-14 01:46:49 +08:00
|
|
|
* set fill which can be a color, linear gradient object,
|
|
|
|
* radial gradient object, or pattern object
|
|
|
|
* @param {String|Object} fill
|
2012-03-07 13:45:48 +08:00
|
|
|
*/
|
2012-05-27 01:57:56 +08:00
|
|
|
setFill: function(config) {
|
|
|
|
this.setAttrs({
|
|
|
|
fill: config
|
|
|
|
});
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get fill
|
|
|
|
*/
|
|
|
|
getFill: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.fill;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set stroke color
|
|
|
|
* @param {String} stroke
|
|
|
|
*/
|
|
|
|
setStroke: function(stroke) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.stroke = stroke;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get stroke color
|
|
|
|
*/
|
|
|
|
getStroke: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.stroke;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
2012-03-31 14:57:10 +08:00
|
|
|
/**
|
|
|
|
* set line join
|
2012-05-14 01:46:49 +08:00
|
|
|
* @param {String} lineJoin. Can be miter, round, or bevel. The
|
|
|
|
* default is miter
|
2012-03-31 14:57:10 +08:00
|
|
|
*/
|
|
|
|
setLineJoin: function(lineJoin) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.lineJoin = lineJoin;
|
2012-03-31 14:57:10 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get line join
|
|
|
|
*/
|
|
|
|
getLineJoin: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.lineJoin;
|
2012-03-31 14:57:10 +08:00
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* set stroke width
|
|
|
|
* @param {Number} strokeWidth
|
|
|
|
*/
|
|
|
|
setStrokeWidth: function(strokeWidth) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.strokeWidth = strokeWidth;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get stroke width
|
|
|
|
*/
|
|
|
|
getStrokeWidth: function() {
|
2012-04-06 14:48:58 +08:00
|
|
|
return this.attrs.strokeWidth;
|
2012-03-07 13:45:48 +08:00
|
|
|
},
|
2012-05-09 14:06:13 +08:00
|
|
|
/**
|
2012-05-20 12:14:04 +08:00
|
|
|
* set shadow object
|
|
|
|
* @param {Object} config
|
2012-05-09 14:06:13 +08:00
|
|
|
*/
|
2012-05-26 11:18:05 +08:00
|
|
|
setShadow: function(config) {
|
2012-05-27 01:57:56 +08:00
|
|
|
this.setAttrs({
|
|
|
|
shadow: config
|
|
|
|
});
|
2012-05-09 14:06:13 +08:00
|
|
|
},
|
|
|
|
/**
|
2012-05-20 12:14:04 +08:00
|
|
|
* get shadow object
|
2012-05-09 14:06:13 +08:00
|
|
|
*/
|
2012-05-20 12:14:04 +08:00
|
|
|
getShadow: function() {
|
|
|
|
return this.attrs.shadow;
|
2012-05-09 14:06:13 +08:00
|
|
|
},
|
2012-03-31 15:14:18 +08:00
|
|
|
/**
|
|
|
|
* set draw function
|
|
|
|
* @param {Function} func drawing function
|
|
|
|
*/
|
|
|
|
setDrawFunc: function(func) {
|
2012-05-20 12:14:04 +08:00
|
|
|
this.attrs.drawFunc = func;
|
2012-03-31 15:14:18 +08:00
|
|
|
},
|
2012-04-02 10:38:30 +08:00
|
|
|
/**
|
2012-04-05 10:53:11 +08:00
|
|
|
* save shape data when using pixel detection.
|
2012-04-02 10:38:30 +08:00
|
|
|
*/
|
2012-04-09 11:25:31 +08:00
|
|
|
saveData: function() {
|
2012-04-02 10:38:30 +08:00
|
|
|
var stage = this.getStage();
|
2012-04-06 14:48:58 +08:00
|
|
|
var w = stage.attrs.width;
|
|
|
|
var h = stage.attrs.height;
|
2012-04-02 10:38:30 +08:00
|
|
|
|
|
|
|
var bufferLayer = stage.bufferLayer;
|
|
|
|
var bufferLayerContext = bufferLayer.getContext();
|
|
|
|
|
|
|
|
bufferLayer.clear();
|
|
|
|
this._draw(bufferLayer);
|
|
|
|
|
|
|
|
var imageData = bufferLayerContext.getImageData(0, 0, w, h);
|
|
|
|
this.data = imageData.data;
|
|
|
|
},
|
2012-04-09 11:25:31 +08:00
|
|
|
/**
|
|
|
|
* clear shape data
|
|
|
|
*/
|
|
|
|
clearData: function() {
|
|
|
|
this.data = [];
|
|
|
|
},
|
2012-04-16 00:18:30 +08:00
|
|
|
/**
|
2012-04-28 11:54:49 +08:00
|
|
|
* determines if point is in the shape
|
2012-04-16 00:18:30 +08:00
|
|
|
*/
|
2012-04-28 14:57:01 +08:00
|
|
|
intersects: function() {
|
2012-05-14 01:46:49 +08:00
|
|
|
var pos = Kinetic.GlobalObject._getXY(arguments);
|
2012-04-16 00:18:30 +08:00
|
|
|
var stage = this.getStage();
|
|
|
|
|
|
|
|
if(this.attrs.detectionType === 'path') {
|
|
|
|
var pathLayer = stage.pathLayer;
|
|
|
|
var pathLayerContext = pathLayer.getContext();
|
|
|
|
|
|
|
|
this._draw(pathLayer);
|
|
|
|
|
|
|
|
return pathLayerContext.isPointInPath(pos.x, pos.y);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
var w = stage.attrs.width;
|
|
|
|
var alpha = this.data[((w * pos.y) + pos.x) * 4 + 3];
|
|
|
|
return (alpha !== undefined && alpha !== 0);
|
|
|
|
}
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
_draw: function(layer) {
|
2012-05-20 12:14:04 +08:00
|
|
|
if(layer !== undefined && this.attrs.drawFunc !== undefined) {
|
2012-03-07 13:45:48 +08:00
|
|
|
var stage = layer.getStage();
|
|
|
|
var context = layer.getContext();
|
2012-04-15 09:27:06 +08:00
|
|
|
var family = [];
|
|
|
|
var parent = this.parent;
|
|
|
|
|
|
|
|
family.unshift(this);
|
|
|
|
while(parent) {
|
|
|
|
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-04-15 09:27:06 +08:00
|
|
|
for(var n = 0; n < family.length; n++) {
|
|
|
|
var node = family[n];
|
|
|
|
var t = node.getTransform();
|
2012-04-15 07:27:00 +08:00
|
|
|
|
2012-04-15 09:27:06 +08:00
|
|
|
// center offset
|
|
|
|
if(node.attrs.centerOffset.x !== 0 || node.attrs.centerOffset.y !== 0) {
|
|
|
|
t.translate(-1 * node.attrs.centerOffset.x, -1 * node.attrs.centerOffset.y);
|
|
|
|
}
|
|
|
|
|
|
|
|
var m = t.getMatrix();
|
|
|
|
context.transform(m[0], m[1], m[2], m[3], m[4], m[5]);
|
|
|
|
}
|
2012-04-15 07:27:00 +08:00
|
|
|
|
2012-05-10 13:31:55 +08:00
|
|
|
this.tempLayer = layer;
|
|
|
|
|
|
|
|
/*
|
|
|
|
* pre styles include alpha, linejoin, and line cap
|
|
|
|
*/
|
2012-04-15 07:27:00 +08:00
|
|
|
if(this.getAbsoluteAlpha() !== 1) {
|
|
|
|
context.globalAlpha = this.getAbsoluteAlpha();
|
2012-03-07 13:45:48 +08:00
|
|
|
}
|
2012-05-10 13:31:55 +08:00
|
|
|
this.applyLineJoin();
|
2012-04-15 07:27:00 +08:00
|
|
|
|
2012-05-10 13:31:55 +08:00
|
|
|
// draw the shape
|
2012-05-20 12:14:04 +08:00
|
|
|
this.attrs.drawFunc.call(this);
|
2012-03-07 13:45:48 +08:00
|
|
|
context.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// extend Node
|
2012-03-12 07:05:47 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Shape, Kinetic.Node);
|