mirror of
https://github.com/konvajs/konva.git
synced 2025-09-18 18:27:58 +08:00
67 lines
2.6 KiB
JavaScript
67 lines
2.6 KiB
JavaScript
(function() {
|
|
/**
|
|
* Blob constructor. Blobs are defined by an array of points and
|
|
* a tension
|
|
* @constructor
|
|
* @augments Kinetic.Spline
|
|
* @param {Object} config
|
|
* @param {Array} config.points can be a flattened array of points, an array of point arrays, or an array of point objects.
|
|
* e.g. [0,1,2,3], [[0,1],[2,3]] and [{x:0,y:1},{x:2,y:3}] are equivalent
|
|
* @param {Number} [config.tension] default value is 1. Higher values will result in a more curvy line. A value of 0 will result in no interpolation.
|
|
* {{ShapeParams}}
|
|
* {{NodeParams}}
|
|
*/
|
|
Kinetic.Blob = function(config) {
|
|
this._initBlob(config);
|
|
};
|
|
|
|
Kinetic.Blob.prototype = {
|
|
_initBlob: function(config) {
|
|
// call super constructor
|
|
Kinetic.Spline.call(this, config);
|
|
this.shapeType = 'Blob';
|
|
},
|
|
drawFunc: function(canvas) {
|
|
var points = this.getPoints(), length = points.length, context = canvas.getContext(), tension = this.getTension();
|
|
context.beginPath();
|
|
context.moveTo(points[0].x, points[0].y);
|
|
|
|
// tension
|
|
if(tension !== 0 && length > 2) {
|
|
var ap = this.allPoints, len = ap.length;
|
|
var n = 0;
|
|
while(n < len-1) {
|
|
context.bezierCurveTo(ap[n].x, ap[n++].y, ap[n].x, ap[n++].y, ap[n].x, ap[n++].y);
|
|
}
|
|
}
|
|
// no tension
|
|
else {
|
|
for(var n = 1; n < length; n++) {
|
|
var point = points[n];
|
|
context.lineTo(point.x, point.y);
|
|
}
|
|
}
|
|
|
|
context.closePath();
|
|
canvas.fillStroke(this);
|
|
},
|
|
_setAllPoints: function() {
|
|
var points = this.getPoints(), length = points.length, tension = this.getTension(), firstControlPoints = Kinetic.Spline._getControlPoints(points[length - 1], points[0], points[1], tension), lastControlPoints = Kinetic.Spline._getControlPoints(points[length - 2], points[length - 1], points[0], tension);
|
|
|
|
Kinetic.Spline.prototype._setAllPoints.call(this);
|
|
|
|
// prepend control point
|
|
this.allPoints.unshift(firstControlPoints[1]);
|
|
|
|
// append cp, point, cp, cp, first point
|
|
this.allPoints.push(lastControlPoints[0]);
|
|
this.allPoints.push(points[length - 1]);
|
|
this.allPoints.push(lastControlPoints[1]);
|
|
this.allPoints.push(firstControlPoints[0]);
|
|
this.allPoints.push(points[0]);
|
|
}
|
|
};
|
|
|
|
Kinetic.Util.extend(Kinetic.Blob, Kinetic.Spline);
|
|
})();
|