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-03-31 14:57:10 +08:00
|
|
|
* @config {String|CanvasGradient|CanvasPattern} [fill] fill
|
|
|
|
* @config {String} [stroke] stroke color
|
|
|
|
* @config {Number} [strokeWidth] stroke width
|
|
|
|
* @config {String} [lineJoin] line join. Can be "miter", "round", or "bevel". The default
|
|
|
|
* is "miter"
|
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-09 14:06:13 +08:00
|
|
|
detectionType: 'path',
|
|
|
|
shadowColor: undefined,
|
2012-05-10 09:53:20 +08:00
|
|
|
shadowBlur: 5,
|
2012-05-09 14:06:13 +08:00
|
|
|
shadowOffset: {
|
|
|
|
x: 0,
|
|
|
|
y: 0
|
|
|
|
}
|
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-10 13:31:55 +08:00
|
|
|
* applies shadows, fills, and styles
|
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) {
|
|
|
|
context.save();
|
|
|
|
this.applyShadow();
|
|
|
|
this.fill();
|
|
|
|
context.restore();
|
|
|
|
this.stroke();
|
|
|
|
}
|
|
|
|
/*
|
|
|
|
* if fill is not defined, try applying the shadow
|
|
|
|
* to the stroke
|
|
|
|
*/
|
|
|
|
else {
|
|
|
|
this.applyShadow();
|
|
|
|
this.stroke();
|
|
|
|
}
|
2012-05-09 14:06:13 +08:00
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
2012-03-31 14:57:10 +08:00
|
|
|
* helper method to fill and stroke a shape
|
|
|
|
* based on its fill, stroke, and strokeWidth, properties
|
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-13 09:18:06 +08:00
|
|
|
// pattern fill
|
|
|
|
else if(fill.image !== undefined) {
|
2012-05-14 01:27:40 +08:00
|
|
|
var o = {};
|
|
|
|
|
|
|
|
// set offset o
|
|
|
|
if(fill.offset !== undefined) {
|
|
|
|
Kinetic.GlobalObject._setXY(o, 'pos', fill.offset);
|
|
|
|
}
|
2012-05-04 05:20:50 +08:00
|
|
|
|
2012-05-13 09:18:06 +08:00
|
|
|
var repeat = fill.repeat === undefined ? 'repeat' : fill.repeat;
|
|
|
|
f = context.createPattern(fill.image, repeat);
|
2012-05-14 01:27:40 +08:00
|
|
|
|
|
|
|
context.save();
|
|
|
|
context.translate(o.pos.x, o.pos.y);
|
|
|
|
context.fillStyle = f;
|
|
|
|
context.fill();
|
|
|
|
context.restore();
|
2012-05-13 09:18:06 +08:00
|
|
|
}
|
|
|
|
// gradient fill
|
|
|
|
else if(s.x !== undefined && s.y !== undefined && e.x !== undefined && e.y !== undefined) {
|
|
|
|
var context = this.getContext();
|
|
|
|
var grd = context.createLinearGradient(s.x, s.y, e.x, e.y);
|
|
|
|
grd.addColorStop(0, s.color);
|
|
|
|
grd.addColorStop(1, e.color);
|
|
|
|
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();
|
|
|
|
var grd = context.createRadialGradient(s.x, s.y, s.radius, s.x, s.y, e.radius);
|
|
|
|
grd.addColorStop(0, s.color);
|
|
|
|
grd.addColorStop(1, e.color);
|
|
|
|
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-05-14 01:27:40 +08:00
|
|
|
// TODO: if using fill offset, save context, translate offset, fill(), then restore
|
|
|
|
|
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-10 09:53:20 +08:00
|
|
|
* apply shadow based on shadowColor, shadowBlur,
|
|
|
|
* and shadowOffset properties
|
2012-05-09 14:06:13 +08:00
|
|
|
*/
|
|
|
|
applyShadow: function() {
|
|
|
|
var context = this.getContext();
|
|
|
|
if(this.attrs.shadowColor !== undefined) {
|
|
|
|
context.shadowColor = this.attrs.shadowColor;
|
|
|
|
context.shadowBlur = this.attrs.shadowBlur;
|
|
|
|
context.shadowOffsetX = this.attrs.shadowOffset.x;
|
|
|
|
context.shadowOffsetY = this.attrs.shadowOffset.y;
|
|
|
|
}
|
|
|
|
},
|
2012-03-07 13:45:48 +08:00
|
|
|
/**
|
|
|
|
* set fill which can be a color, gradient object,
|
2012-03-31 14:57:10 +08:00
|
|
|
* or pattern object
|
2012-03-07 13:45:48 +08:00
|
|
|
* @param {String|CanvasGradient|CanvasPattern} fill
|
|
|
|
*/
|
|
|
|
setFill: function(fill) {
|
2012-04-06 14:48:58 +08:00
|
|
|
this.attrs.fill = fill;
|
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
|
|
|
|
* @param {String} lineJoin. Can be "miter", "round", or "bevel". The
|
|
|
|
* default is "miter"
|
|
|
|
*/
|
|
|
|
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
|
|
|
/**
|
|
|
|
* set shadow color
|
|
|
|
* @param {String} color
|
|
|
|
*/
|
|
|
|
setShadowColor: function(color) {
|
|
|
|
this.attrs.shadowColor = color;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get shadow color
|
|
|
|
*/
|
|
|
|
getShadowColor: function() {
|
|
|
|
return this.attrs.shadowColor;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set shadow blur
|
|
|
|
* @param {Integer}
|
|
|
|
*/
|
|
|
|
setShadowBlur: function(blur) {
|
|
|
|
this.attrs.shadowBlur = blur;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get shadow blur
|
|
|
|
*/
|
|
|
|
getShadowblur: function() {
|
|
|
|
return this.attrs.shadowBlur;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* set shadow offset
|
|
|
|
* @param {Object} offset
|
|
|
|
*/
|
|
|
|
setShadowOffset: function(offset) {
|
|
|
|
this.attrs.shadowOffset = offset;
|
|
|
|
},
|
|
|
|
/**
|
|
|
|
* get shadow offset
|
|
|
|
*/
|
|
|
|
getShadowOffset: function() {
|
|
|
|
return this.attrs.shadowOffset;
|
|
|
|
},
|
2012-03-31 15:14:18 +08:00
|
|
|
/**
|
|
|
|
* set draw function
|
|
|
|
* @param {Function} func drawing function
|
|
|
|
*/
|
|
|
|
setDrawFunc: function(func) {
|
|
|
|
this.drawFunc = func;
|
|
|
|
},
|
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() {
|
|
|
|
var pos = Kinetic.GlobalObject._getPoint(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 shape
|
|
|
|
* @param {Layer} layer Layer that the shape will be drawn on
|
|
|
|
*/
|
|
|
|
_draw: function(layer) {
|
2012-04-15 03:04:45 +08:00
|
|
|
if(layer !== undefined && this.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-03-07 13:45:48 +08:00
|
|
|
this.drawFunc.call(this);
|
|
|
|
context.restore();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
// extend Node
|
2012-03-12 07:05:47 +08:00
|
|
|
Kinetic.GlobalObject.extend(Kinetic.Shape, Kinetic.Node);
|