mirror of
https://github.com/konvajs/konva.git
synced 2026-01-02 20:42:42 +08:00
moved Path back to the plugins, and added Geometry utility methods to the Path namespace so that they can be called as a utility methods without instantiating a Path shape
This commit is contained in:
4
Thorfile
4
Thorfile
@@ -4,9 +4,9 @@ class Build < Thor
|
|||||||
# This is the list of files to concatenate. The first file will appear at the top of the final file. All files are relative to the lib directory.
|
# This is the list of files to concatenate. The first file will appear at the top of the final file. All files are relative to the lib directory.
|
||||||
FILES = [
|
FILES = [
|
||||||
"license.js", "src/Global.js", "src/Transition.js", "src/filters/Grayscale.js",
|
"license.js", "src/Global.js", "src/Transition.js", "src/filters/Grayscale.js",
|
||||||
"src/util/Type.js", "src/util/Canvas.js", "src/util/Class.js", "src/util/Tween.js", "src/util/Transform.js", "src/util/Geometry.js",
|
"src/util/Type.js", "src/util/Canvas.js", "src/util/Class.js", "src/util/Tween.js", "src/util/Transform.js",
|
||||||
"src/Animation.js", "src/Node.js", "src/Container.js", "src/Stage.js", "src/Layer.js", "src/Group.js", "src/Shape.js",
|
"src/Animation.js", "src/Node.js", "src/Container.js", "src/Stage.js", "src/Layer.js", "src/Group.js", "src/Shape.js",
|
||||||
"src/shapes/Rect.js", "src/shapes/Ellipse.js", "src/shapes/Image.js", "src/shapes/Polygon.js", "src/shapes/Text.js", "src/shapes/Line.js", "src/shapes/Sprite.js", "src/shapes/Path.js"
|
"src/shapes/Rect.js", "src/shapes/Ellipse.js", "src/shapes/Image.js", "src/shapes/Polygon.js", "src/shapes/Text.js", "src/shapes/Line.js", "src/shapes/Sprite.js"
|
||||||
]
|
]
|
||||||
|
|
||||||
desc "dev", "Concatenate all the js files into /dist/kinetic-VERSION.js."
|
desc "dev", "Concatenate all the js files into /dist/kinetic-VERSION.js."
|
||||||
|
|||||||
556
dist/kinetic-core.js
vendored
556
dist/kinetic-core.js
vendored
@@ -1083,474 +1083,6 @@ Kinetic.Transform.prototype = {
|
|||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
/*
|
|
||||||
* Utility methods written by jfollas to
|
|
||||||
* handle length and point measurements
|
|
||||||
*/
|
|
||||||
Kinetic.Geometry = {
|
|
||||||
getLineLength: function(x1, y1, x2, y2) {
|
|
||||||
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
||||||
},
|
|
||||||
getPointOnLine: function(dist, P1x, P1y, P2x, P2y, fromX, fromY) {
|
|
||||||
if(fromX === undefined) {
|
|
||||||
fromX = P1x;
|
|
||||||
}
|
|
||||||
if(fromY === undefined) {
|
|
||||||
fromY = P1y;
|
|
||||||
}
|
|
||||||
|
|
||||||
var m = (P2y - P1y) / ((P2x - P1x) + 0.00000001);
|
|
||||||
var run = Math.sqrt(dist * dist / (1 + m * m));
|
|
||||||
var rise = m * run;
|
|
||||||
var pt;
|
|
||||||
|
|
||||||
if((fromY - P1y) / ((fromX - P1x) + 0.00000001) === m) {
|
|
||||||
pt = {
|
|
||||||
x: fromX + run,
|
|
||||||
y: fromY + rise
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var ix, iy;
|
|
||||||
|
|
||||||
var len = this.getLineLength(P1x, P1y, P2x, P2y);
|
|
||||||
if(len < 0.00000001) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
var u = (((fromX - P1x) * (P2x - P1x)) + ((fromY - P1y) * (P2y - P1y)));
|
|
||||||
u = u / (len * len);
|
|
||||||
ix = P1x + u * (P2x - P1x);
|
|
||||||
iy = P1y + u * (P2y - P1y);
|
|
||||||
|
|
||||||
var pRise = this.getLineLength(fromX, fromY, ix, iy);
|
|
||||||
var pRun = Math.sqrt(dist * dist - pRise * pRise);
|
|
||||||
run = Math.sqrt(pRun * pRun / (1 + m * m));
|
|
||||||
rise = m * run;
|
|
||||||
pt = {
|
|
||||||
x: ix + run,
|
|
||||||
y: iy + rise
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return pt;
|
|
||||||
},
|
|
||||||
getPointOnCubicBezier: function(pct, P1x, P1y, P2x, P2y, P3x, P3y, P4x, P4y) {
|
|
||||||
function CB1(t) {
|
|
||||||
return t * t * t;
|
|
||||||
}
|
|
||||||
function CB2(t) {
|
|
||||||
return 3 * t * t * (1 - t);
|
|
||||||
}
|
|
||||||
function CB3(t) {
|
|
||||||
return 3 * t * (1 - t) * (1 - t);
|
|
||||||
}
|
|
||||||
function CB4(t) {
|
|
||||||
return (1 - t) * (1 - t) * (1 - t);
|
|
||||||
}
|
|
||||||
var x = P4x * CB1(pct) + P3x * CB2(pct) + P2x * CB3(pct) + P1x * CB4(pct);
|
|
||||||
var y = P4y * CB1(pct) + P3y * CB2(pct) + P2y * CB3(pct) + P1y * CB4(pct);
|
|
||||||
|
|
||||||
return {
|
|
||||||
x: x,
|
|
||||||
y: y
|
|
||||||
};
|
|
||||||
},
|
|
||||||
getPointOnQuadraticBezier: function(pct, P1x, P1y, P2x, P2y, P3x, P3y) {
|
|
||||||
function QB1(t) {
|
|
||||||
return t * t;
|
|
||||||
}
|
|
||||||
function QB2(t) {
|
|
||||||
return 2 * t * (1 - t);
|
|
||||||
}
|
|
||||||
function QB3(t) {
|
|
||||||
return (1 - t) * (1 - t);
|
|
||||||
}
|
|
||||||
var x = P3x * QB1(pct) + P2x * QB2(pct) + P1x * QB3(pct);
|
|
||||||
var y = P3y * QB1(pct) + P2y * QB2(pct) + P1y * QB3(pct);
|
|
||||||
|
|
||||||
return {
|
|
||||||
x: x,
|
|
||||||
y: y
|
|
||||||
};
|
|
||||||
},
|
|
||||||
getPointOnEllipticalArc: function(cx, cy, rx, ry, theta, psi) {
|
|
||||||
var cosPsi = Math.cos(psi), sinPsi = Math.sin(psi);
|
|
||||||
var pt = {
|
|
||||||
x: rx * Math.cos(theta),
|
|
||||||
y: ry * Math.sin(theta)
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
x: cx + (pt.x * cosPsi - pt.y * sinPsi),
|
|
||||||
y: cy + (pt.x * sinPsi + pt.y * cosPsi)
|
|
||||||
};
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* get parsed data array from the data
|
|
||||||
* string. V, v, H, h, and l data are converted to
|
|
||||||
* L data for the purpose of high performance Path
|
|
||||||
* rendering
|
|
||||||
*/
|
|
||||||
parsePathData: function(data) {
|
|
||||||
// Path Data Segment must begin with a moveTo
|
|
||||||
//m (x y)+ Relative moveTo (subsequent points are treated as lineTo)
|
|
||||||
//M (x y)+ Absolute moveTo (subsequent points are treated as lineTo)
|
|
||||||
//l (x y)+ Relative lineTo
|
|
||||||
//L (x y)+ Absolute LineTo
|
|
||||||
//h (x)+ Relative horizontal lineTo
|
|
||||||
//H (x)+ Absolute horizontal lineTo
|
|
||||||
//v (y)+ Relative vertical lineTo
|
|
||||||
//V (y)+ Absolute vertical lineTo
|
|
||||||
//z (closepath)
|
|
||||||
//Z (closepath)
|
|
||||||
//c (x1 y1 x2 y2 x y)+ Relative Bezier curve
|
|
||||||
//C (x1 y1 x2 y2 x y)+ Absolute Bezier curve
|
|
||||||
//q (x1 y1 x y)+ Relative Quadratic Bezier
|
|
||||||
//Q (x1 y1 x y)+ Absolute Quadratic Bezier
|
|
||||||
//t (x y)+ Shorthand/Smooth Relative Quadratic Bezier
|
|
||||||
//T (x y)+ Shorthand/Smooth Absolute Quadratic Bezier
|
|
||||||
//s (x2 y2 x y)+ Shorthand/Smooth Relative Bezier curve
|
|
||||||
//S (x2 y2 x y)+ Shorthand/Smooth Absolute Bezier curve
|
|
||||||
//a (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Relative Elliptical Arc
|
|
||||||
//A (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Absolute Elliptical Arc
|
|
||||||
|
|
||||||
// return early if data is not defined
|
|
||||||
if(!data) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// command string
|
|
||||||
var cs = data;
|
|
||||||
|
|
||||||
// command chars
|
|
||||||
var cc = ['m', 'M', 'l', 'L', 'v', 'V', 'h', 'H', 'z', 'Z', 'c', 'C', 'q', 'Q', 't', 'T', 's', 'S', 'a', 'A'];
|
|
||||||
// convert white spaces to commas
|
|
||||||
cs = cs.replace(new RegExp(' ', 'g'), ',');
|
|
||||||
// create pipes so that we can split the data
|
|
||||||
for(var n = 0; n < cc.length; n++) {
|
|
||||||
cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
|
||||||
}
|
|
||||||
// create array
|
|
||||||
var arr = cs.split('|');
|
|
||||||
var ca = [];
|
|
||||||
// init context point
|
|
||||||
var cpx = 0;
|
|
||||||
var cpy = 0;
|
|
||||||
for(var n = 1; n < arr.length; n++) {
|
|
||||||
var str = arr[n];
|
|
||||||
var c = str.charAt(0);
|
|
||||||
str = str.slice(1);
|
|
||||||
// remove ,- for consistency
|
|
||||||
str = str.replace(new RegExp(',-', 'g'), '-');
|
|
||||||
// add commas so that it's easy to split
|
|
||||||
str = str.replace(new RegExp('-', 'g'), ',-');
|
|
||||||
str = str.replace(new RegExp('e,-', 'g'), 'e-');
|
|
||||||
var p = str.split(',');
|
|
||||||
if(p.length > 0 && p[0] === '') {
|
|
||||||
p.shift();
|
|
||||||
}
|
|
||||||
// convert strings to floats
|
|
||||||
for(var i = 0; i < p.length; i++) {
|
|
||||||
p[i] = parseFloat(p[i]);
|
|
||||||
}
|
|
||||||
while(p.length > 0) {
|
|
||||||
if(isNaN(p[0]))// case for a trailing comma before next command
|
|
||||||
break;
|
|
||||||
|
|
||||||
var cmd = null;
|
|
||||||
var points = [];
|
|
||||||
var startX = cpx, startY = cpy;
|
|
||||||
|
|
||||||
// convert l, H, h, V, and v to L
|
|
||||||
switch (c) {
|
|
||||||
|
|
||||||
// Note: Keep the lineTo's above the moveTo's in this switch
|
|
||||||
case 'l':
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'L':
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Note: lineTo handlers need to be above this point
|
|
||||||
case 'm':
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'M';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
c = 'l';
|
|
||||||
// subsequent points are treated as relative lineTo
|
|
||||||
break;
|
|
||||||
case 'M':
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'M';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
c = 'L';
|
|
||||||
// subsequent points are treated as absolute lineTo
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'h':
|
|
||||||
cpx += p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'H':
|
|
||||||
cpx = p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'V':
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
points.push(p.shift(), p.shift(), p.shift(), p.shift());
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'c':
|
|
||||||
points.push(cpx + p.shift(), cpy + p.shift(), cpx + p.shift(), cpy + p.shift());
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'C';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'C') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
|
||||||
}
|
|
||||||
points.push(ctlPtx, ctlPty, p.shift(), p.shift());
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'C';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'C') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
|
||||||
}
|
|
||||||
points.push(ctlPtx, ctlPty, cpx + p.shift(), cpy + p.shift());
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'C';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'Q':
|
|
||||||
points.push(p.shift(), p.shift());
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
points.push(cpx + p.shift(), cpy + p.shift());
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'Q';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'T':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'Q') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
|
||||||
}
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'Q';
|
|
||||||
points.push(ctlPtx, ctlPty, cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'Q') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
|
||||||
}
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'Q';
|
|
||||||
points.push(ctlPtx, ctlPty, cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'A':
|
|
||||||
var rx = p.shift(), ry = p.shift(), psi = p.shift(), fa = p.shift(), fs = p.shift();
|
|
||||||
var x1 = cpx, y1 = cpy;
|
|
||||||
cpx = p.shift(), cpy = p.shift();
|
|
||||||
cmd = 'A';
|
|
||||||
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
|
||||||
break;
|
|
||||||
case 'a':
|
|
||||||
var rx = p.shift(), ry = p.shift(), psi = p.shift(), fa = p.shift(), fs = p.shift();
|
|
||||||
var x1 = cpx, y1 = cpy;
|
|
||||||
cpx += p.shift(), cpy += p.shift();
|
|
||||||
cmd = 'A';
|
|
||||||
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ca.push({
|
|
||||||
command: cmd || c,
|
|
||||||
points: points,
|
|
||||||
start: {
|
|
||||||
x: startX,
|
|
||||||
y: startY
|
|
||||||
},
|
|
||||||
pathLength: this.calcLength(startX, startY, cmd || c, points)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if(c === 'z' || c === 'Z') {
|
|
||||||
ca.push({
|
|
||||||
command: 'z',
|
|
||||||
points: [],
|
|
||||||
start: undefined,
|
|
||||||
pathLength: 0
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ca;
|
|
||||||
},
|
|
||||||
calcLength: function(x, y, cmd, points) {
|
|
||||||
var len, p1, p2;
|
|
||||||
var g = Kinetic.Geometry;
|
|
||||||
|
|
||||||
switch (cmd) {
|
|
||||||
case 'L':
|
|
||||||
return g.getLineLength(x, y, points[0], points[1]);
|
|
||||||
case 'C':
|
|
||||||
// Approximates by breaking curve into 100 line segments
|
|
||||||
len = 0.0;
|
|
||||||
p1 = g.getPointOnCubicBezier(0, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
|
||||||
for( t = 0.01; t <= 1; t += 0.01) {
|
|
||||||
p2 = g.getPointOnCubicBezier(t, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
case 'Q':
|
|
||||||
// Approximates by breaking curve into 100 line segments
|
|
||||||
len = 0.0;
|
|
||||||
p1 = g.getPointOnQuadraticBezier(0, x, y, points[0], points[1], points[2], points[3]);
|
|
||||||
for( t = 0.01; t <= 1; t += 0.01) {
|
|
||||||
p2 = g.getPointOnQuadraticBezier(t, x, y, points[0], points[1], points[2], points[3]);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
case 'A':
|
|
||||||
// Approximates by breaking curve into line segments
|
|
||||||
len = 0.0;
|
|
||||||
var start = points[4];
|
|
||||||
// 4 = theta
|
|
||||||
var dTheta = points[5];
|
|
||||||
// 5 = dTheta
|
|
||||||
var end = points[4] + dTheta;
|
|
||||||
var inc = Math.PI / 180.0;
|
|
||||||
// 1 degree resolution
|
|
||||||
if(Math.abs(start - end) < inc) {
|
|
||||||
inc = Math.abs(start - end);
|
|
||||||
}
|
|
||||||
// Note: for purpose of calculating arc length, not going to worry about rotating X-axis by angle psi
|
|
||||||
p1 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], start, 0);
|
|
||||||
if(dTheta < 0) {// clockwise
|
|
||||||
for( t = start - inc; t > end; t -= inc) {
|
|
||||||
p2 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {// counter-clockwise
|
|
||||||
for( t = start + inc; t < end; t += inc) {
|
|
||||||
p2 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p2 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], end, 0);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
},
|
|
||||||
convertEndpointToCenterParameterization: function(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg) {
|
|
||||||
// Derived from: http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
|
|
||||||
var psi = psiDeg * (Math.PI / 180.0);
|
|
||||||
var xp = Math.cos(psi) * (x1 - x2) / 2.0 + Math.sin(psi) * (y1 - y2) / 2.0;
|
|
||||||
var yp = -1 * Math.sin(psi) * (x1 - x2) / 2.0 + Math.cos(psi) * (y1 - y2) / 2.0;
|
|
||||||
|
|
||||||
var lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
|
|
||||||
|
|
||||||
if(lambda > 1) {
|
|
||||||
rx *= Math.sqrt(lambda);
|
|
||||||
ry *= Math.sqrt(lambda);
|
|
||||||
}
|
|
||||||
|
|
||||||
var f = Math.sqrt((((rx * rx) * (ry * ry)) - ((rx * rx) * (yp * yp)) - ((ry * ry) * (xp * xp))) / ((rx * rx) * (yp * yp) + (ry * ry) * (xp * xp)));
|
|
||||||
|
|
||||||
if(fa == fs) {
|
|
||||||
f *= -1;
|
|
||||||
}
|
|
||||||
if(isNaN(f)) {
|
|
||||||
f = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
var cxp = f * rx * yp / ry;
|
|
||||||
var cyp = f * -ry * xp / rx;
|
|
||||||
|
|
||||||
var cx = (x1 + x2) / 2.0 + Math.cos(psi) * cxp - Math.sin(psi) * cyp;
|
|
||||||
var cy = (y1 + y2) / 2.0 + Math.sin(psi) * cxp + Math.cos(psi) * cyp;
|
|
||||||
|
|
||||||
var vMag = function(v) {
|
|
||||||
return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
|
|
||||||
};
|
|
||||||
var vRatio = function(u, v) {
|
|
||||||
return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
|
|
||||||
};
|
|
||||||
var vAngle = function(u, v) {
|
|
||||||
return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
|
|
||||||
};
|
|
||||||
var theta = vAngle([1, 0], [(xp - cxp) / rx, (yp - cyp) / ry]);
|
|
||||||
var u = [(xp - cxp) / rx, (yp - cyp) / ry];
|
|
||||||
var v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
|
|
||||||
var dTheta = vAngle(u, v);
|
|
||||||
|
|
||||||
if(vRatio(u, v) <= -1) {
|
|
||||||
dTheta = Math.PI;
|
|
||||||
}
|
|
||||||
if(vRatio(u, v) >= 1) {
|
|
||||||
dTheta = 0;
|
|
||||||
}
|
|
||||||
if(fs === 0 && dTheta > 0) {
|
|
||||||
dTheta = dTheta - 2 * Math.PI;
|
|
||||||
}
|
|
||||||
if(fs == 1 && dTheta < 0) {
|
|
||||||
dTheta = dTheta + 2 * Math.PI;
|
|
||||||
}
|
|
||||||
return [cx, cy, rx, ry, theta, dTheta, psi, fs];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
// Animation
|
// Animation
|
||||||
///////////////////////////////////////////////////////////////////////
|
///////////////////////////////////////////////////////////////////////
|
||||||
@@ -6006,91 +5538,3 @@ Kinetic.Node.addGettersSetters(Kinetic.Sprite, ['animation', 'animations', 'inde
|
|||||||
* @name getIndex
|
* @name getIndex
|
||||||
* @methodOf Kinetic.Sprite.prototype
|
* @methodOf Kinetic.Sprite.prototype
|
||||||
*/
|
*/
|
||||||
///////////////////////////////////////////////////////////////////////
|
|
||||||
// SVG Path
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
|
||||||
/**
|
|
||||||
* Path constructor.
|
|
||||||
* @author Jason Follas
|
|
||||||
* @constructor
|
|
||||||
* @augments Kinetic.Shape
|
|
||||||
* @param {Object} config
|
|
||||||
*/
|
|
||||||
Kinetic.Plugins.Path = Kinetic.Shape.extend({
|
|
||||||
init: function(config) {
|
|
||||||
this.shapeType = "Path";
|
|
||||||
this.dataArray = [];
|
|
||||||
var that = this;
|
|
||||||
|
|
||||||
config.drawFunc = this.drawFunc;
|
|
||||||
// call super constructor
|
|
||||||
this._super(config);
|
|
||||||
this.dataArray = Kinetic.Geometry.parsePathData(this.attrs.data);
|
|
||||||
this.on('dataChange', function() {
|
|
||||||
that.dataArray = Kinetic.Geometry.parsePathData(that.attrs.data);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
drawFunc: function(context) {
|
|
||||||
var ca = this.dataArray;
|
|
||||||
// context position
|
|
||||||
context.beginPath();
|
|
||||||
for(var n = 0; n < ca.length; n++) {
|
|
||||||
var c = ca[n].command;
|
|
||||||
var p = ca[n].points;
|
|
||||||
switch (c) {
|
|
||||||
case 'L':
|
|
||||||
context.lineTo(p[0], p[1]);
|
|
||||||
break;
|
|
||||||
case 'M':
|
|
||||||
context.moveTo(p[0], p[1]);
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
context.bezierCurveTo(p[0], p[1], p[2], p[3], p[4], p[5]);
|
|
||||||
break;
|
|
||||||
case 'Q':
|
|
||||||
context.quadraticCurveTo(p[0], p[1], p[2], p[3]);
|
|
||||||
break;
|
|
||||||
case 'A':
|
|
||||||
var cx = p[0], cy = p[1], rx = p[2], ry = p[3], theta = p[4], dTheta = p[5], psi = p[6], fs = p[7];
|
|
||||||
|
|
||||||
var r = (rx > ry) ? rx : ry;
|
|
||||||
var scaleX = (rx > ry) ? 1 : rx / ry;
|
|
||||||
var scaleY = (rx > ry) ? ry / rx : 1;
|
|
||||||
|
|
||||||
context.translate(cx, cy);
|
|
||||||
context.rotate(psi);
|
|
||||||
context.scale(scaleX, scaleY);
|
|
||||||
context.arc(0, 0, r, theta, theta + dTheta, 1 - fs);
|
|
||||||
context.scale(1 / scaleX, 1 / scaleY);
|
|
||||||
context.rotate(-psi);
|
|
||||||
context.translate(-cx, -cy);
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'z':
|
|
||||||
context.closePath();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.fill(context);
|
|
||||||
this.stroke(context);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// add getters setters
|
|
||||||
Kinetic.Node.addGettersSetters(Kinetic.Plugins.Path, ['data']);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set SVG path data string. This method
|
|
||||||
* also automatically parses the data string
|
|
||||||
* into a data array. Currently supported SVG data:
|
|
||||||
* M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
|
|
||||||
* @name setData
|
|
||||||
* @methodOf Kinetic.Plugins.Path.prototype
|
|
||||||
* @param {String} SVG path command string
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get SVG path data string
|
|
||||||
* @name getData
|
|
||||||
* @methodOf Kinetic.Plugins.Path.prototype
|
|
||||||
*/
|
|
||||||
|
|||||||
4
dist/kinetic-core.min.js
vendored
4
dist/kinetic-core.min.js
vendored
File diff suppressed because one or more lines are too long
555
src/plugins/shapes/Path.js
Normal file
555
src/plugins/shapes/Path.js
Normal file
@@ -0,0 +1,555 @@
|
|||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
// SVG Path
|
||||||
|
///////////////////////////////////////////////////////////////////////
|
||||||
|
/**
|
||||||
|
* Path constructor.
|
||||||
|
* @author Jason Follas
|
||||||
|
* @constructor
|
||||||
|
* @augments Kinetic.Shape
|
||||||
|
* @param {Object} config
|
||||||
|
*/
|
||||||
|
Kinetic.Plugins.Path = Kinetic.Shape.extend({
|
||||||
|
init: function(config) {
|
||||||
|
this.shapeType = "Path";
|
||||||
|
this.dataArray = [];
|
||||||
|
var that = this;
|
||||||
|
|
||||||
|
config.drawFunc = this.drawFunc;
|
||||||
|
// call super constructor
|
||||||
|
this._super(config);
|
||||||
|
this.dataArray = Kinetic.Plugins.Path.parsePathData(this.attrs.data);
|
||||||
|
this.on('dataChange', function() {
|
||||||
|
that.dataArray = Kinetic.Plugins.Path.parsePathData(that.attrs.data);
|
||||||
|
});
|
||||||
|
},
|
||||||
|
drawFunc: function(context) {
|
||||||
|
var ca = this.dataArray;
|
||||||
|
// context position
|
||||||
|
context.beginPath();
|
||||||
|
for(var n = 0; n < ca.length; n++) {
|
||||||
|
var c = ca[n].command;
|
||||||
|
var p = ca[n].points;
|
||||||
|
switch (c) {
|
||||||
|
case 'L':
|
||||||
|
context.lineTo(p[0], p[1]);
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
context.moveTo(p[0], p[1]);
|
||||||
|
break;
|
||||||
|
case 'C':
|
||||||
|
context.bezierCurveTo(p[0], p[1], p[2], p[3], p[4], p[5]);
|
||||||
|
break;
|
||||||
|
case 'Q':
|
||||||
|
context.quadraticCurveTo(p[0], p[1], p[2], p[3]);
|
||||||
|
break;
|
||||||
|
case 'A':
|
||||||
|
var cx = p[0], cy = p[1], rx = p[2], ry = p[3], theta = p[4], dTheta = p[5], psi = p[6], fs = p[7];
|
||||||
|
|
||||||
|
var r = (rx > ry) ? rx : ry;
|
||||||
|
var scaleX = (rx > ry) ? 1 : rx / ry;
|
||||||
|
var scaleY = (rx > ry) ? ry / rx : 1;
|
||||||
|
|
||||||
|
context.translate(cx, cy);
|
||||||
|
context.rotate(psi);
|
||||||
|
context.scale(scaleX, scaleY);
|
||||||
|
context.arc(0, 0, r, theta, theta + dTheta, 1 - fs);
|
||||||
|
context.scale(1 / scaleX, 1 / scaleY);
|
||||||
|
context.rotate(-psi);
|
||||||
|
context.translate(-cx, -cy);
|
||||||
|
|
||||||
|
break;
|
||||||
|
case 'z':
|
||||||
|
context.closePath();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
this.fill(context);
|
||||||
|
this.stroke(context);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Utility methods written by jfollas to
|
||||||
|
* handle length and point measurements
|
||||||
|
*/
|
||||||
|
Kinetic.Plugins.Path.getLineLength = function(x1, y1, x2, y2) {
|
||||||
|
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
||||||
|
};
|
||||||
|
Kinetic.Plugins.Path.getPointOnLine = function(dist, P1x, P1y, P2x, P2y, fromX, fromY) {
|
||||||
|
if(fromX === undefined) {
|
||||||
|
fromX = P1x;
|
||||||
|
}
|
||||||
|
if(fromY === undefined) {
|
||||||
|
fromY = P1y;
|
||||||
|
}
|
||||||
|
|
||||||
|
var m = (P2y - P1y) / ((P2x - P1x) + 0.00000001);
|
||||||
|
var run = Math.sqrt(dist * dist / (1 + m * m));
|
||||||
|
var rise = m * run;
|
||||||
|
var pt;
|
||||||
|
|
||||||
|
if((fromY - P1y) / ((fromX - P1x) + 0.00000001) === m) {
|
||||||
|
pt = {
|
||||||
|
x: fromX + run,
|
||||||
|
y: fromY + rise
|
||||||
|
};
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
var ix, iy;
|
||||||
|
|
||||||
|
var len = this.getLineLength(P1x, P1y, P2x, P2y);
|
||||||
|
if(len < 0.00000001) {
|
||||||
|
return undefined;
|
||||||
|
}
|
||||||
|
var u = (((fromX - P1x) * (P2x - P1x)) + ((fromY - P1y) * (P2y - P1y)));
|
||||||
|
u = u / (len * len);
|
||||||
|
ix = P1x + u * (P2x - P1x);
|
||||||
|
iy = P1y + u * (P2y - P1y);
|
||||||
|
|
||||||
|
var pRise = this.getLineLength(fromX, fromY, ix, iy);
|
||||||
|
var pRun = Math.sqrt(dist * dist - pRise * pRise);
|
||||||
|
run = Math.sqrt(pRun * pRun / (1 + m * m));
|
||||||
|
rise = m * run;
|
||||||
|
pt = {
|
||||||
|
x: ix + run,
|
||||||
|
y: iy + rise
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
return pt;
|
||||||
|
};
|
||||||
|
|
||||||
|
Kinetic.Plugins.Path.getPointOnCubicBezier = function(pct, P1x, P1y, P2x, P2y, P3x, P3y, P4x, P4y) {
|
||||||
|
function CB1(t) {
|
||||||
|
return t * t * t;
|
||||||
|
}
|
||||||
|
function CB2(t) {
|
||||||
|
return 3 * t * t * (1 - t);
|
||||||
|
}
|
||||||
|
function CB3(t) {
|
||||||
|
return 3 * t * (1 - t) * (1 - t);
|
||||||
|
}
|
||||||
|
function CB4(t) {
|
||||||
|
return (1 - t) * (1 - t) * (1 - t);
|
||||||
|
}
|
||||||
|
var x = P4x * CB1(pct) + P3x * CB2(pct) + P2x * CB3(pct) + P1x * CB4(pct);
|
||||||
|
var y = P4y * CB1(pct) + P3y * CB2(pct) + P2y * CB3(pct) + P1y * CB4(pct);
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: x,
|
||||||
|
y: y
|
||||||
|
};
|
||||||
|
};
|
||||||
|
Kinetic.Plugins.Path.getPointOnQuadraticBezier = function(pct, P1x, P1y, P2x, P2y, P3x, P3y) {
|
||||||
|
function QB1(t) {
|
||||||
|
return t * t;
|
||||||
|
}
|
||||||
|
function QB2(t) {
|
||||||
|
return 2 * t * (1 - t);
|
||||||
|
}
|
||||||
|
function QB3(t) {
|
||||||
|
return (1 - t) * (1 - t);
|
||||||
|
}
|
||||||
|
var x = P3x * QB1(pct) + P2x * QB2(pct) + P1x * QB3(pct);
|
||||||
|
var y = P3y * QB1(pct) + P2y * QB2(pct) + P1y * QB3(pct);
|
||||||
|
|
||||||
|
return {
|
||||||
|
x: x,
|
||||||
|
y: y
|
||||||
|
};
|
||||||
|
};
|
||||||
|
Kinetic.Plugins.Path.getPointOnEllipticalArc = function(cx, cy, rx, ry, theta, psi) {
|
||||||
|
var cosPsi = Math.cos(psi), sinPsi = Math.sin(psi);
|
||||||
|
var pt = {
|
||||||
|
x: rx * Math.cos(theta),
|
||||||
|
y: ry * Math.sin(theta)
|
||||||
|
};
|
||||||
|
return {
|
||||||
|
x: cx + (pt.x * cosPsi - pt.y * sinPsi),
|
||||||
|
y: cy + (pt.x * sinPsi + pt.y * cosPsi)
|
||||||
|
};
|
||||||
|
};
|
||||||
|
/**
|
||||||
|
* get parsed data array from the data
|
||||||
|
* string. V, v, H, h, and l data are converted to
|
||||||
|
* L data for the purpose of high performance Path
|
||||||
|
* rendering
|
||||||
|
*/
|
||||||
|
Kinetic.Plugins.Path.parsePathData = function(data) {
|
||||||
|
// Path Data Segment must begin with a moveTo
|
||||||
|
//m (x y)+ Relative moveTo (subsequent points are treated as lineTo)
|
||||||
|
//M (x y)+ Absolute moveTo (subsequent points are treated as lineTo)
|
||||||
|
//l (x y)+ Relative lineTo
|
||||||
|
//L (x y)+ Absolute LineTo
|
||||||
|
//h (x)+ Relative horizontal lineTo
|
||||||
|
//H (x)+ Absolute horizontal lineTo
|
||||||
|
//v (y)+ Relative vertical lineTo
|
||||||
|
//V (y)+ Absolute vertical lineTo
|
||||||
|
//z (closepath)
|
||||||
|
//Z (closepath)
|
||||||
|
//c (x1 y1 x2 y2 x y)+ Relative Bezier curve
|
||||||
|
//C (x1 y1 x2 y2 x y)+ Absolute Bezier curve
|
||||||
|
//q (x1 y1 x y)+ Relative Quadratic Bezier
|
||||||
|
//Q (x1 y1 x y)+ Absolute Quadratic Bezier
|
||||||
|
//t (x y)+ Shorthand/Smooth Relative Quadratic Bezier
|
||||||
|
//T (x y)+ Shorthand/Smooth Absolute Quadratic Bezier
|
||||||
|
//s (x2 y2 x y)+ Shorthand/Smooth Relative Bezier curve
|
||||||
|
//S (x2 y2 x y)+ Shorthand/Smooth Absolute Bezier curve
|
||||||
|
//a (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Relative Elliptical Arc
|
||||||
|
//A (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Absolute Elliptical Arc
|
||||||
|
|
||||||
|
// return early if data is not defined
|
||||||
|
if(!data) {
|
||||||
|
return [];
|
||||||
|
}
|
||||||
|
|
||||||
|
// command string
|
||||||
|
var cs = data;
|
||||||
|
|
||||||
|
// command chars
|
||||||
|
var cc = ['m', 'M', 'l', 'L', 'v', 'V', 'h', 'H', 'z', 'Z', 'c', 'C', 'q', 'Q', 't', 'T', 's', 'S', 'a', 'A'];
|
||||||
|
// convert white spaces to commas
|
||||||
|
cs = cs.replace(new RegExp(' ', 'g'), ',');
|
||||||
|
// create pipes so that we can split the data
|
||||||
|
for(var n = 0; n < cc.length; n++) {
|
||||||
|
cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
||||||
|
}
|
||||||
|
// create array
|
||||||
|
var arr = cs.split('|');
|
||||||
|
var ca = [];
|
||||||
|
// init context point
|
||||||
|
var cpx = 0;
|
||||||
|
var cpy = 0;
|
||||||
|
for(var n = 1; n < arr.length; n++) {
|
||||||
|
var str = arr[n];
|
||||||
|
var c = str.charAt(0);
|
||||||
|
str = str.slice(1);
|
||||||
|
// remove ,- for consistency
|
||||||
|
str = str.replace(new RegExp(',-', 'g'), '-');
|
||||||
|
// add commas so that it's easy to split
|
||||||
|
str = str.replace(new RegExp('-', 'g'), ',-');
|
||||||
|
str = str.replace(new RegExp('e,-', 'g'), 'e-');
|
||||||
|
var p = str.split(',');
|
||||||
|
if(p.length > 0 && p[0] === '') {
|
||||||
|
p.shift();
|
||||||
|
}
|
||||||
|
// convert strings to floats
|
||||||
|
for(var i = 0; i < p.length; i++) {
|
||||||
|
p[i] = parseFloat(p[i]);
|
||||||
|
}
|
||||||
|
while(p.length > 0) {
|
||||||
|
if(isNaN(p[0]))// case for a trailing comma before next command
|
||||||
|
break;
|
||||||
|
|
||||||
|
var cmd = null;
|
||||||
|
var points = [];
|
||||||
|
var startX = cpx, startY = cpy;
|
||||||
|
|
||||||
|
// convert l, H, h, V, and v to L
|
||||||
|
switch (c) {
|
||||||
|
|
||||||
|
// Note: Keep the lineTo's above the moveTo's in this switch
|
||||||
|
case 'l':
|
||||||
|
cpx += p.shift();
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'L';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'L':
|
||||||
|
cpx = p.shift();
|
||||||
|
cpy = p.shift();
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
|
||||||
|
// Note: lineTo handlers need to be above this point
|
||||||
|
case 'm':
|
||||||
|
cpx += p.shift();
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'M';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
c = 'l';
|
||||||
|
// subsequent points are treated as relative lineTo
|
||||||
|
break;
|
||||||
|
case 'M':
|
||||||
|
cpx = p.shift();
|
||||||
|
cpy = p.shift();
|
||||||
|
cmd = 'M';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
c = 'L';
|
||||||
|
// subsequent points are treated as absolute lineTo
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'h':
|
||||||
|
cpx += p.shift();
|
||||||
|
cmd = 'L';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'H':
|
||||||
|
cpx = p.shift();
|
||||||
|
cmd = 'L';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'v':
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'L';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'V':
|
||||||
|
cpy = p.shift();
|
||||||
|
cmd = 'L';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'C':
|
||||||
|
points.push(p.shift(), p.shift(), p.shift(), p.shift());
|
||||||
|
cpx = p.shift();
|
||||||
|
cpy = p.shift();
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'c':
|
||||||
|
points.push(cpx + p.shift(), cpy + p.shift(), cpx + p.shift(), cpy + p.shift());
|
||||||
|
cpx += p.shift();
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'C';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'S':
|
||||||
|
var ctlPtx = cpx, ctlPty = cpy;
|
||||||
|
var prevCmd = ca[ca.length - 1];
|
||||||
|
if(prevCmd.command === 'C') {
|
||||||
|
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
||||||
|
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
||||||
|
}
|
||||||
|
points.push(ctlPtx, ctlPty, p.shift(), p.shift());
|
||||||
|
cpx = p.shift();
|
||||||
|
cpy = p.shift();
|
||||||
|
cmd = 'C';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 's':
|
||||||
|
var ctlPtx = cpx, ctlPty = cpy;
|
||||||
|
var prevCmd = ca[ca.length - 1];
|
||||||
|
if(prevCmd.command === 'C') {
|
||||||
|
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
||||||
|
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
||||||
|
}
|
||||||
|
points.push(ctlPtx, ctlPty, cpx + p.shift(), cpy + p.shift());
|
||||||
|
cpx += p.shift();
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'C';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'Q':
|
||||||
|
points.push(p.shift(), p.shift());
|
||||||
|
cpx = p.shift();
|
||||||
|
cpy = p.shift();
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'q':
|
||||||
|
points.push(cpx + p.shift(), cpy + p.shift());
|
||||||
|
cpx += p.shift();
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'Q';
|
||||||
|
points.push(cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'T':
|
||||||
|
var ctlPtx = cpx, ctlPty = cpy;
|
||||||
|
var prevCmd = ca[ca.length - 1];
|
||||||
|
if(prevCmd.command === 'Q') {
|
||||||
|
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
||||||
|
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
||||||
|
}
|
||||||
|
cpx = p.shift();
|
||||||
|
cpy = p.shift();
|
||||||
|
cmd = 'Q';
|
||||||
|
points.push(ctlPtx, ctlPty, cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 't':
|
||||||
|
var ctlPtx = cpx, ctlPty = cpy;
|
||||||
|
var prevCmd = ca[ca.length - 1];
|
||||||
|
if(prevCmd.command === 'Q') {
|
||||||
|
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
||||||
|
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
||||||
|
}
|
||||||
|
cpx += p.shift();
|
||||||
|
cpy += p.shift();
|
||||||
|
cmd = 'Q';
|
||||||
|
points.push(ctlPtx, ctlPty, cpx, cpy);
|
||||||
|
break;
|
||||||
|
case 'A':
|
||||||
|
var rx = p.shift(), ry = p.shift(), psi = p.shift(), fa = p.shift(), fs = p.shift();
|
||||||
|
var x1 = cpx, y1 = cpy;
|
||||||
|
cpx = p.shift(), cpy = p.shift();
|
||||||
|
cmd = 'A';
|
||||||
|
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
||||||
|
break;
|
||||||
|
case 'a':
|
||||||
|
var rx = p.shift(), ry = p.shift(), psi = p.shift(), fa = p.shift(), fs = p.shift();
|
||||||
|
var x1 = cpx, y1 = cpy;
|
||||||
|
cpx += p.shift(), cpy += p.shift();
|
||||||
|
cmd = 'A';
|
||||||
|
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
ca.push({
|
||||||
|
command: cmd || c,
|
||||||
|
points: points,
|
||||||
|
start: {
|
||||||
|
x: startX,
|
||||||
|
y: startY
|
||||||
|
},
|
||||||
|
pathLength: this.calcLength(startX, startY, cmd || c, points)
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
if(c === 'z' || c === 'Z') {
|
||||||
|
ca.push({
|
||||||
|
command: 'z',
|
||||||
|
points: [],
|
||||||
|
start: undefined,
|
||||||
|
pathLength: 0
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ca;
|
||||||
|
};
|
||||||
|
Kinetic.Plugins.Path.calcLength = function(x, y, cmd, points) {
|
||||||
|
var len, p1, p2;
|
||||||
|
var path = Kinetic.Plugins.Path;
|
||||||
|
|
||||||
|
switch (cmd) {
|
||||||
|
case 'L':
|
||||||
|
return path.getLineLength(x, y, points[0], points[1]);
|
||||||
|
case 'C':
|
||||||
|
// Approximates by breaking curve into 100 line segments
|
||||||
|
len = 0.0;
|
||||||
|
p1 = path.getPointOnCubicBezier(0, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
||||||
|
for( t = 0.01; t <= 1; t += 0.01) {
|
||||||
|
p2 = path.getPointOnCubicBezier(t, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
||||||
|
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
||||||
|
p1 = p2;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
case 'Q':
|
||||||
|
// Approximates by breaking curve into 100 line segments
|
||||||
|
len = 0.0;
|
||||||
|
p1 = path.getPointOnQuadraticBezier(0, x, y, points[0], points[1], points[2], points[3]);
|
||||||
|
for( t = 0.01; t <= 1; t += 0.01) {
|
||||||
|
p2 = path.getPointOnQuadraticBezier(t, x, y, points[0], points[1], points[2], points[3]);
|
||||||
|
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
||||||
|
p1 = p2;
|
||||||
|
}
|
||||||
|
return len;
|
||||||
|
case 'A':
|
||||||
|
// Approximates by breaking curve into line segments
|
||||||
|
len = 0.0;
|
||||||
|
var start = points[4];
|
||||||
|
// 4 = theta
|
||||||
|
var dTheta = points[5];
|
||||||
|
// 5 = dTheta
|
||||||
|
var end = points[4] + dTheta;
|
||||||
|
var inc = Math.PI / 180.0;
|
||||||
|
// 1 degree resolution
|
||||||
|
if(Math.abs(start - end) < inc) {
|
||||||
|
inc = Math.abs(start - end);
|
||||||
|
}
|
||||||
|
// Note: for purpose of calculating arc length, not going to worry about rotating X-axis by angle psi
|
||||||
|
p1 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], start, 0);
|
||||||
|
if(dTheta < 0) {// clockwise
|
||||||
|
for( t = start - inc; t > end; t -= inc) {
|
||||||
|
p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
||||||
|
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
||||||
|
p1 = p2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {// counter-clockwise
|
||||||
|
for( t = start + inc; t < end; t += inc) {
|
||||||
|
p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
||||||
|
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
||||||
|
p1 = p2;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
p2 = path.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], end, 0);
|
||||||
|
len += path.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
||||||
|
|
||||||
|
return len;
|
||||||
|
}
|
||||||
|
|
||||||
|
return 0;
|
||||||
|
};
|
||||||
|
Kinetic.Plugins.Path.convertEndpointToCenterParameterization = function(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg) {
|
||||||
|
// Derived from: http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
|
||||||
|
var psi = psiDeg * (Math.PI / 180.0);
|
||||||
|
var xp = Math.cos(psi) * (x1 - x2) / 2.0 + Math.sin(psi) * (y1 - y2) / 2.0;
|
||||||
|
var yp = -1 * Math.sin(psi) * (x1 - x2) / 2.0 + Math.cos(psi) * (y1 - y2) / 2.0;
|
||||||
|
|
||||||
|
var lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
|
||||||
|
|
||||||
|
if(lambda > 1) {
|
||||||
|
rx *= Math.sqrt(lambda);
|
||||||
|
ry *= Math.sqrt(lambda);
|
||||||
|
}
|
||||||
|
|
||||||
|
var f = Math.sqrt((((rx * rx) * (ry * ry)) - ((rx * rx) * (yp * yp)) - ((ry * ry) * (xp * xp))) / ((rx * rx) * (yp * yp) + (ry * ry) * (xp * xp)));
|
||||||
|
|
||||||
|
if(fa == fs) {
|
||||||
|
f *= -1;
|
||||||
|
}
|
||||||
|
if(isNaN(f)) {
|
||||||
|
f = 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
var cxp = f * rx * yp / ry;
|
||||||
|
var cyp = f * -ry * xp / rx;
|
||||||
|
|
||||||
|
var cx = (x1 + x2) / 2.0 + Math.cos(psi) * cxp - Math.sin(psi) * cyp;
|
||||||
|
var cy = (y1 + y2) / 2.0 + Math.sin(psi) * cxp + Math.cos(psi) * cyp;
|
||||||
|
|
||||||
|
var vMag = function(v) {
|
||||||
|
return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
|
||||||
|
};
|
||||||
|
var vRatio = function(u, v) {
|
||||||
|
return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
|
||||||
|
};
|
||||||
|
var vAngle = function(u, v) {
|
||||||
|
return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
|
||||||
|
};
|
||||||
|
var theta = vAngle([1, 0], [(xp - cxp) / rx, (yp - cyp) / ry]);
|
||||||
|
var u = [(xp - cxp) / rx, (yp - cyp) / ry];
|
||||||
|
var v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
|
||||||
|
var dTheta = vAngle(u, v);
|
||||||
|
|
||||||
|
if(vRatio(u, v) <= -1) {
|
||||||
|
dTheta = Math.PI;
|
||||||
|
}
|
||||||
|
if(vRatio(u, v) >= 1) {
|
||||||
|
dTheta = 0;
|
||||||
|
}
|
||||||
|
if(fs === 0 && dTheta > 0) {
|
||||||
|
dTheta = dTheta - 2 * Math.PI;
|
||||||
|
}
|
||||||
|
if(fs == 1 && dTheta < 0) {
|
||||||
|
dTheta = dTheta + 2 * Math.PI;
|
||||||
|
}
|
||||||
|
return [cx, cy, rx, ry, theta, dTheta, psi, fs];
|
||||||
|
};
|
||||||
|
|
||||||
|
// add getters setters
|
||||||
|
Kinetic.Node.addGettersSetters(Kinetic.Plugins.Path, ['data']);
|
||||||
|
|
||||||
|
/**
|
||||||
|
* set SVG path data string. This method
|
||||||
|
* also automatically parses the data string
|
||||||
|
* into a data array. Currently supported SVG data:
|
||||||
|
* M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
|
||||||
|
* @name setData
|
||||||
|
* @methodOf Kinetic.Plugins.Path.prototype
|
||||||
|
* @param {String} SVG path command string
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* get SVG path data string
|
||||||
|
* @name getData
|
||||||
|
* @methodOf Kinetic.Plugins.Path.prototype
|
||||||
|
*/
|
||||||
@@ -26,9 +26,9 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
config.drawFunc = this.drawFunc;
|
config.drawFunc = this.drawFunc;
|
||||||
// call super constructor
|
// call super constructor
|
||||||
this._super(config);
|
this._super(config);
|
||||||
this.dataArray = Kinetic.Geometry.parsePathData(this.attrs.data);
|
this.dataArray = Kinetic.Plugins.Path.parsePathData(this.attrs.data);
|
||||||
this.on('dataChange', function() {
|
this.on('dataChange', function() {
|
||||||
that.dataArray = Kinetic.Geometry.parsePathData(this.attrs.data);
|
that.dataArray = Kinetic.Plugins.Path.parsePathData(this.attrs.data);
|
||||||
});
|
});
|
||||||
// update text data for certain attr changes
|
// update text data for certain attr changes
|
||||||
var attrs = ['text', 'textStroke', 'textStrokeWidth'];
|
var attrs = ['text', 'textStroke', 'textStrokeWidth'];
|
||||||
@@ -176,8 +176,8 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
|
|
||||||
switch (pathCmd.command) {
|
switch (pathCmd.command) {
|
||||||
case 'L':
|
case 'L':
|
||||||
if(Kinetic.Geometry.getLineLength(p0.x, p0.y, pathCmd.points[0], pathCmd.points[1]) > glyphWidth) {
|
if(Kinetic.Plugins.Path.getLineLength(p0.x, p0.y, pathCmd.points[0], pathCmd.points[1]) > glyphWidth) {
|
||||||
p1 = Kinetic.Geometry.getPointOnLine(glyphWidth, p0.x, p0.y, pathCmd.points[0], pathCmd.points[1], p0.x, p0.y);
|
p1 = Kinetic.Plugins.Path.getPointOnLine(glyphWidth, p0.x, p0.y, pathCmd.points[0], pathCmd.points[1], p0.x, p0.y);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
pathCmd = undefined;
|
pathCmd = undefined;
|
||||||
@@ -202,7 +202,7 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
currentT = end;
|
currentT = end;
|
||||||
needNewSegment = true;
|
needNewSegment = true;
|
||||||
}
|
}
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], currentT, pathCmd.points[6]);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], currentT, pathCmd.points[6]);
|
||||||
break;
|
break;
|
||||||
case 'C':
|
case 'C':
|
||||||
if(currentT === 0) {
|
if(currentT === 0) {
|
||||||
@@ -220,7 +220,7 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
currentT = 1.0;
|
currentT = 1.0;
|
||||||
needNewSegment = true;
|
needNewSegment = true;
|
||||||
}
|
}
|
||||||
p1 = Kinetic.Geometry.getPointOnCubicBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], pathCmd.points[4], pathCmd.points[5]);
|
p1 = Kinetic.Plugins.Path.getPointOnCubicBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3], pathCmd.points[4], pathCmd.points[5]);
|
||||||
break;
|
break;
|
||||||
case 'Q':
|
case 'Q':
|
||||||
if(currentT === 0)
|
if(currentT === 0)
|
||||||
@@ -234,13 +234,13 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
currentT = 1.0;
|
currentT = 1.0;
|
||||||
needNewSegment = true;
|
needNewSegment = true;
|
||||||
}
|
}
|
||||||
p1 = Kinetic.Geometry.getPointOnQuadraticBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3]);
|
p1 = Kinetic.Plugins.Path.getPointOnQuadraticBezier(currentT, pathCmd.start.x, pathCmd.start.y, pathCmd.points[0], pathCmd.points[1], pathCmd.points[2], pathCmd.points[3]);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(p1 !== undefined) {
|
if(p1 !== undefined) {
|
||||||
currLen = Kinetic.Geometry.getLineLength(p0.x, p0.y, p1.x, p1.y);
|
currLen = Kinetic.Plugins.Path.getLineLength(p0.x, p0.y, p1.x, p1.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
if(needNewSegment) {
|
if(needNewSegment) {
|
||||||
@@ -257,7 +257,7 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
if(p0 === undefined || p1 === undefined)
|
if(p0 === undefined || p1 === undefined)
|
||||||
break;
|
break;
|
||||||
|
|
||||||
var width = Kinetic.Geometry.getLineLength(p0.x, p0.y, p1.x, p1.y);
|
var width = Kinetic.Plugins.Path.getLineLength(p0.x, p0.y, p1.x, p1.y);
|
||||||
|
|
||||||
// Note: Since glyphs are rendered one at a time, any kerning pair data built into the font will not be used.
|
// Note: Since glyphs are rendered one at a time, any kerning pair data built into the font will not be used.
|
||||||
// Can foresee having a rough pair table built in that the developer can override as needed.
|
// Can foresee having a rough pair table built in that the developer can override as needed.
|
||||||
@@ -265,7 +265,7 @@ Kinetic.Plugins.TextPath = Kinetic.Shape.extend({
|
|||||||
var kern = 0;
|
var kern = 0;
|
||||||
// placeholder for future implementation
|
// placeholder for future implementation
|
||||||
|
|
||||||
var midpoint = Kinetic.Geometry.getPointOnLine(kern + width / 2.0, p0.x, p0.y, p1.x, p1.y);
|
var midpoint = Kinetic.Plugins.Path.getPointOnLine(kern + width / 2.0, p0.x, p0.y, p1.x, p1.y);
|
||||||
|
|
||||||
var rotation = Math.atan2((p1.y - p0.y), (p1.x - p0.x));
|
var rotation = Math.atan2((p1.y - p0.y), (p1.x - p0.x));
|
||||||
this.glyphInfo.push({
|
this.glyphInfo.push({
|
||||||
|
|||||||
@@ -1,88 +0,0 @@
|
|||||||
///////////////////////////////////////////////////////////////////////
|
|
||||||
// SVG Path
|
|
||||||
///////////////////////////////////////////////////////////////////////
|
|
||||||
/**
|
|
||||||
* Path constructor.
|
|
||||||
* @author Jason Follas
|
|
||||||
* @constructor
|
|
||||||
* @augments Kinetic.Shape
|
|
||||||
* @param {Object} config
|
|
||||||
*/
|
|
||||||
Kinetic.Plugins.Path = Kinetic.Shape.extend({
|
|
||||||
init: function(config) {
|
|
||||||
this.shapeType = "Path";
|
|
||||||
this.dataArray = [];
|
|
||||||
var that = this;
|
|
||||||
|
|
||||||
config.drawFunc = this.drawFunc;
|
|
||||||
// call super constructor
|
|
||||||
this._super(config);
|
|
||||||
this.dataArray = Kinetic.Geometry.parsePathData(this.attrs.data);
|
|
||||||
this.on('dataChange', function() {
|
|
||||||
that.dataArray = Kinetic.Geometry.parsePathData(that.attrs.data);
|
|
||||||
});
|
|
||||||
},
|
|
||||||
drawFunc: function(context) {
|
|
||||||
var ca = this.dataArray;
|
|
||||||
// context position
|
|
||||||
context.beginPath();
|
|
||||||
for(var n = 0; n < ca.length; n++) {
|
|
||||||
var c = ca[n].command;
|
|
||||||
var p = ca[n].points;
|
|
||||||
switch (c) {
|
|
||||||
case 'L':
|
|
||||||
context.lineTo(p[0], p[1]);
|
|
||||||
break;
|
|
||||||
case 'M':
|
|
||||||
context.moveTo(p[0], p[1]);
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
context.bezierCurveTo(p[0], p[1], p[2], p[3], p[4], p[5]);
|
|
||||||
break;
|
|
||||||
case 'Q':
|
|
||||||
context.quadraticCurveTo(p[0], p[1], p[2], p[3]);
|
|
||||||
break;
|
|
||||||
case 'A':
|
|
||||||
var cx = p[0], cy = p[1], rx = p[2], ry = p[3], theta = p[4], dTheta = p[5], psi = p[6], fs = p[7];
|
|
||||||
|
|
||||||
var r = (rx > ry) ? rx : ry;
|
|
||||||
var scaleX = (rx > ry) ? 1 : rx / ry;
|
|
||||||
var scaleY = (rx > ry) ? ry / rx : 1;
|
|
||||||
|
|
||||||
context.translate(cx, cy);
|
|
||||||
context.rotate(psi);
|
|
||||||
context.scale(scaleX, scaleY);
|
|
||||||
context.arc(0, 0, r, theta, theta + dTheta, 1 - fs);
|
|
||||||
context.scale(1 / scaleX, 1 / scaleY);
|
|
||||||
context.rotate(-psi);
|
|
||||||
context.translate(-cx, -cy);
|
|
||||||
|
|
||||||
break;
|
|
||||||
case 'z':
|
|
||||||
context.closePath();
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
this.fill(context);
|
|
||||||
this.stroke(context);
|
|
||||||
}
|
|
||||||
});
|
|
||||||
|
|
||||||
// add getters setters
|
|
||||||
Kinetic.Node.addGettersSetters(Kinetic.Plugins.Path, ['data']);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* set SVG path data string. This method
|
|
||||||
* also automatically parses the data string
|
|
||||||
* into a data array. Currently supported SVG data:
|
|
||||||
* M, m, L, l, H, h, V, v, Q, q, T, t, C, c, S, s, A, a, Z, z
|
|
||||||
* @name setData
|
|
||||||
* @methodOf Kinetic.Plugins.Path.prototype
|
|
||||||
* @param {String} SVG path command string
|
|
||||||
*/
|
|
||||||
|
|
||||||
/**
|
|
||||||
* get SVG path data string
|
|
||||||
* @name getData
|
|
||||||
* @methodOf Kinetic.Plugins.Path.prototype
|
|
||||||
*/
|
|
||||||
@@ -1,467 +0,0 @@
|
|||||||
/*
|
|
||||||
* Utility methods written by jfollas to
|
|
||||||
* handle length and point measurements
|
|
||||||
*/
|
|
||||||
Kinetic.Geometry = {
|
|
||||||
getLineLength: function(x1, y1, x2, y2) {
|
|
||||||
return Math.sqrt((x2 - x1) * (x2 - x1) + (y2 - y1) * (y2 - y1));
|
|
||||||
},
|
|
||||||
getPointOnLine: function(dist, P1x, P1y, P2x, P2y, fromX, fromY) {
|
|
||||||
if(fromX === undefined) {
|
|
||||||
fromX = P1x;
|
|
||||||
}
|
|
||||||
if(fromY === undefined) {
|
|
||||||
fromY = P1y;
|
|
||||||
}
|
|
||||||
|
|
||||||
var m = (P2y - P1y) / ((P2x - P1x) + 0.00000001);
|
|
||||||
var run = Math.sqrt(dist * dist / (1 + m * m));
|
|
||||||
var rise = m * run;
|
|
||||||
var pt;
|
|
||||||
|
|
||||||
if((fromY - P1y) / ((fromX - P1x) + 0.00000001) === m) {
|
|
||||||
pt = {
|
|
||||||
x: fromX + run,
|
|
||||||
y: fromY + rise
|
|
||||||
};
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
var ix, iy;
|
|
||||||
|
|
||||||
var len = this.getLineLength(P1x, P1y, P2x, P2y);
|
|
||||||
if(len < 0.00000001) {
|
|
||||||
return undefined;
|
|
||||||
}
|
|
||||||
var u = (((fromX - P1x) * (P2x - P1x)) + ((fromY - P1y) * (P2y - P1y)));
|
|
||||||
u = u / (len * len);
|
|
||||||
ix = P1x + u * (P2x - P1x);
|
|
||||||
iy = P1y + u * (P2y - P1y);
|
|
||||||
|
|
||||||
var pRise = this.getLineLength(fromX, fromY, ix, iy);
|
|
||||||
var pRun = Math.sqrt(dist * dist - pRise * pRise);
|
|
||||||
run = Math.sqrt(pRun * pRun / (1 + m * m));
|
|
||||||
rise = m * run;
|
|
||||||
pt = {
|
|
||||||
x: ix + run,
|
|
||||||
y: iy + rise
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
return pt;
|
|
||||||
},
|
|
||||||
getPointOnCubicBezier: function(pct, P1x, P1y, P2x, P2y, P3x, P3y, P4x, P4y) {
|
|
||||||
function CB1(t) {
|
|
||||||
return t * t * t;
|
|
||||||
}
|
|
||||||
function CB2(t) {
|
|
||||||
return 3 * t * t * (1 - t);
|
|
||||||
}
|
|
||||||
function CB3(t) {
|
|
||||||
return 3 * t * (1 - t) * (1 - t);
|
|
||||||
}
|
|
||||||
function CB4(t) {
|
|
||||||
return (1 - t) * (1 - t) * (1 - t);
|
|
||||||
}
|
|
||||||
var x = P4x * CB1(pct) + P3x * CB2(pct) + P2x * CB3(pct) + P1x * CB4(pct);
|
|
||||||
var y = P4y * CB1(pct) + P3y * CB2(pct) + P2y * CB3(pct) + P1y * CB4(pct);
|
|
||||||
|
|
||||||
return {
|
|
||||||
x: x,
|
|
||||||
y: y
|
|
||||||
};
|
|
||||||
},
|
|
||||||
getPointOnQuadraticBezier: function(pct, P1x, P1y, P2x, P2y, P3x, P3y) {
|
|
||||||
function QB1(t) {
|
|
||||||
return t * t;
|
|
||||||
}
|
|
||||||
function QB2(t) {
|
|
||||||
return 2 * t * (1 - t);
|
|
||||||
}
|
|
||||||
function QB3(t) {
|
|
||||||
return (1 - t) * (1 - t);
|
|
||||||
}
|
|
||||||
var x = P3x * QB1(pct) + P2x * QB2(pct) + P1x * QB3(pct);
|
|
||||||
var y = P3y * QB1(pct) + P2y * QB2(pct) + P1y * QB3(pct);
|
|
||||||
|
|
||||||
return {
|
|
||||||
x: x,
|
|
||||||
y: y
|
|
||||||
};
|
|
||||||
},
|
|
||||||
getPointOnEllipticalArc: function(cx, cy, rx, ry, theta, psi) {
|
|
||||||
var cosPsi = Math.cos(psi), sinPsi = Math.sin(psi);
|
|
||||||
var pt = {
|
|
||||||
x: rx * Math.cos(theta),
|
|
||||||
y: ry * Math.sin(theta)
|
|
||||||
};
|
|
||||||
return {
|
|
||||||
x: cx + (pt.x * cosPsi - pt.y * sinPsi),
|
|
||||||
y: cy + (pt.x * sinPsi + pt.y * cosPsi)
|
|
||||||
};
|
|
||||||
},
|
|
||||||
/**
|
|
||||||
* get parsed data array from the data
|
|
||||||
* string. V, v, H, h, and l data are converted to
|
|
||||||
* L data for the purpose of high performance Path
|
|
||||||
* rendering
|
|
||||||
*/
|
|
||||||
parsePathData: function(data) {
|
|
||||||
// Path Data Segment must begin with a moveTo
|
|
||||||
//m (x y)+ Relative moveTo (subsequent points are treated as lineTo)
|
|
||||||
//M (x y)+ Absolute moveTo (subsequent points are treated as lineTo)
|
|
||||||
//l (x y)+ Relative lineTo
|
|
||||||
//L (x y)+ Absolute LineTo
|
|
||||||
//h (x)+ Relative horizontal lineTo
|
|
||||||
//H (x)+ Absolute horizontal lineTo
|
|
||||||
//v (y)+ Relative vertical lineTo
|
|
||||||
//V (y)+ Absolute vertical lineTo
|
|
||||||
//z (closepath)
|
|
||||||
//Z (closepath)
|
|
||||||
//c (x1 y1 x2 y2 x y)+ Relative Bezier curve
|
|
||||||
//C (x1 y1 x2 y2 x y)+ Absolute Bezier curve
|
|
||||||
//q (x1 y1 x y)+ Relative Quadratic Bezier
|
|
||||||
//Q (x1 y1 x y)+ Absolute Quadratic Bezier
|
|
||||||
//t (x y)+ Shorthand/Smooth Relative Quadratic Bezier
|
|
||||||
//T (x y)+ Shorthand/Smooth Absolute Quadratic Bezier
|
|
||||||
//s (x2 y2 x y)+ Shorthand/Smooth Relative Bezier curve
|
|
||||||
//S (x2 y2 x y)+ Shorthand/Smooth Absolute Bezier curve
|
|
||||||
//a (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Relative Elliptical Arc
|
|
||||||
//A (rx ry x-axis-rotation large-arc-flag sweep-flag x y)+ Absolute Elliptical Arc
|
|
||||||
|
|
||||||
// return early if data is not defined
|
|
||||||
if(!data) {
|
|
||||||
return [];
|
|
||||||
}
|
|
||||||
|
|
||||||
// command string
|
|
||||||
var cs = data;
|
|
||||||
|
|
||||||
// command chars
|
|
||||||
var cc = ['m', 'M', 'l', 'L', 'v', 'V', 'h', 'H', 'z', 'Z', 'c', 'C', 'q', 'Q', 't', 'T', 's', 'S', 'a', 'A'];
|
|
||||||
// convert white spaces to commas
|
|
||||||
cs = cs.replace(new RegExp(' ', 'g'), ',');
|
|
||||||
// create pipes so that we can split the data
|
|
||||||
for(var n = 0; n < cc.length; n++) {
|
|
||||||
cs = cs.replace(new RegExp(cc[n], 'g'), '|' + cc[n]);
|
|
||||||
}
|
|
||||||
// create array
|
|
||||||
var arr = cs.split('|');
|
|
||||||
var ca = [];
|
|
||||||
// init context point
|
|
||||||
var cpx = 0;
|
|
||||||
var cpy = 0;
|
|
||||||
for(var n = 1; n < arr.length; n++) {
|
|
||||||
var str = arr[n];
|
|
||||||
var c = str.charAt(0);
|
|
||||||
str = str.slice(1);
|
|
||||||
// remove ,- for consistency
|
|
||||||
str = str.replace(new RegExp(',-', 'g'), '-');
|
|
||||||
// add commas so that it's easy to split
|
|
||||||
str = str.replace(new RegExp('-', 'g'), ',-');
|
|
||||||
str = str.replace(new RegExp('e,-', 'g'), 'e-');
|
|
||||||
var p = str.split(',');
|
|
||||||
if(p.length > 0 && p[0] === '') {
|
|
||||||
p.shift();
|
|
||||||
}
|
|
||||||
// convert strings to floats
|
|
||||||
for(var i = 0; i < p.length; i++) {
|
|
||||||
p[i] = parseFloat(p[i]);
|
|
||||||
}
|
|
||||||
while(p.length > 0) {
|
|
||||||
if(isNaN(p[0]))// case for a trailing comma before next command
|
|
||||||
break;
|
|
||||||
|
|
||||||
var cmd = null;
|
|
||||||
var points = [];
|
|
||||||
var startX = cpx, startY = cpy;
|
|
||||||
|
|
||||||
// convert l, H, h, V, and v to L
|
|
||||||
switch (c) {
|
|
||||||
|
|
||||||
// Note: Keep the lineTo's above the moveTo's in this switch
|
|
||||||
case 'l':
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'L':
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
|
|
||||||
// Note: lineTo handlers need to be above this point
|
|
||||||
case 'm':
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'M';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
c = 'l';
|
|
||||||
// subsequent points are treated as relative lineTo
|
|
||||||
break;
|
|
||||||
case 'M':
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'M';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
c = 'L';
|
|
||||||
// subsequent points are treated as absolute lineTo
|
|
||||||
break;
|
|
||||||
|
|
||||||
case 'h':
|
|
||||||
cpx += p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'H':
|
|
||||||
cpx = p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'v':
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'V':
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'L';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'C':
|
|
||||||
points.push(p.shift(), p.shift(), p.shift(), p.shift());
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'c':
|
|
||||||
points.push(cpx + p.shift(), cpy + p.shift(), cpx + p.shift(), cpy + p.shift());
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'C';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'S':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'C') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
|
||||||
}
|
|
||||||
points.push(ctlPtx, ctlPty, p.shift(), p.shift());
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'C';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 's':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'C') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[2]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[3]);
|
|
||||||
}
|
|
||||||
points.push(ctlPtx, ctlPty, cpx + p.shift(), cpy + p.shift());
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'C';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'Q':
|
|
||||||
points.push(p.shift(), p.shift());
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'q':
|
|
||||||
points.push(cpx + p.shift(), cpy + p.shift());
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'Q';
|
|
||||||
points.push(cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'T':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'Q') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
|
||||||
}
|
|
||||||
cpx = p.shift();
|
|
||||||
cpy = p.shift();
|
|
||||||
cmd = 'Q';
|
|
||||||
points.push(ctlPtx, ctlPty, cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 't':
|
|
||||||
var ctlPtx = cpx, ctlPty = cpy;
|
|
||||||
var prevCmd = ca[ca.length - 1];
|
|
||||||
if(prevCmd.command === 'Q') {
|
|
||||||
ctlPtx = cpx + (cpx - prevCmd.points[0]);
|
|
||||||
ctlPty = cpy + (cpy - prevCmd.points[1]);
|
|
||||||
}
|
|
||||||
cpx += p.shift();
|
|
||||||
cpy += p.shift();
|
|
||||||
cmd = 'Q';
|
|
||||||
points.push(ctlPtx, ctlPty, cpx, cpy);
|
|
||||||
break;
|
|
||||||
case 'A':
|
|
||||||
var rx = p.shift(), ry = p.shift(), psi = p.shift(), fa = p.shift(), fs = p.shift();
|
|
||||||
var x1 = cpx, y1 = cpy;
|
|
||||||
cpx = p.shift(), cpy = p.shift();
|
|
||||||
cmd = 'A';
|
|
||||||
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
|
||||||
break;
|
|
||||||
case 'a':
|
|
||||||
var rx = p.shift(), ry = p.shift(), psi = p.shift(), fa = p.shift(), fs = p.shift();
|
|
||||||
var x1 = cpx, y1 = cpy;
|
|
||||||
cpx += p.shift(), cpy += p.shift();
|
|
||||||
cmd = 'A';
|
|
||||||
points = this.convertEndpointToCenterParameterization(x1, y1, cpx, cpy, fa, fs, rx, ry, psi);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
ca.push({
|
|
||||||
command: cmd || c,
|
|
||||||
points: points,
|
|
||||||
start: {
|
|
||||||
x: startX,
|
|
||||||
y: startY
|
|
||||||
},
|
|
||||||
pathLength: this.calcLength(startX, startY, cmd || c, points)
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
if(c === 'z' || c === 'Z') {
|
|
||||||
ca.push({
|
|
||||||
command: 'z',
|
|
||||||
points: [],
|
|
||||||
start: undefined,
|
|
||||||
pathLength: 0
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return ca;
|
|
||||||
},
|
|
||||||
calcLength: function(x, y, cmd, points) {
|
|
||||||
var len, p1, p2;
|
|
||||||
var g = Kinetic.Geometry;
|
|
||||||
|
|
||||||
switch (cmd) {
|
|
||||||
case 'L':
|
|
||||||
return g.getLineLength(x, y, points[0], points[1]);
|
|
||||||
case 'C':
|
|
||||||
// Approximates by breaking curve into 100 line segments
|
|
||||||
len = 0.0;
|
|
||||||
p1 = g.getPointOnCubicBezier(0, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
|
||||||
for( t = 0.01; t <= 1; t += 0.01) {
|
|
||||||
p2 = g.getPointOnCubicBezier(t, x, y, points[0], points[1], points[2], points[3], points[4], points[5]);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
case 'Q':
|
|
||||||
// Approximates by breaking curve into 100 line segments
|
|
||||||
len = 0.0;
|
|
||||||
p1 = g.getPointOnQuadraticBezier(0, x, y, points[0], points[1], points[2], points[3]);
|
|
||||||
for( t = 0.01; t <= 1; t += 0.01) {
|
|
||||||
p2 = g.getPointOnQuadraticBezier(t, x, y, points[0], points[1], points[2], points[3]);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
return len;
|
|
||||||
case 'A':
|
|
||||||
// Approximates by breaking curve into line segments
|
|
||||||
len = 0.0;
|
|
||||||
var start = points[4];
|
|
||||||
// 4 = theta
|
|
||||||
var dTheta = points[5];
|
|
||||||
// 5 = dTheta
|
|
||||||
var end = points[4] + dTheta;
|
|
||||||
var inc = Math.PI / 180.0;
|
|
||||||
// 1 degree resolution
|
|
||||||
if(Math.abs(start - end) < inc) {
|
|
||||||
inc = Math.abs(start - end);
|
|
||||||
}
|
|
||||||
// Note: for purpose of calculating arc length, not going to worry about rotating X-axis by angle psi
|
|
||||||
p1 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], start, 0);
|
|
||||||
if(dTheta < 0) {// clockwise
|
|
||||||
for( t = start - inc; t > end; t -= inc) {
|
|
||||||
p2 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
else {// counter-clockwise
|
|
||||||
for( t = start + inc; t < end; t += inc) {
|
|
||||||
p2 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], t, 0);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
p1 = p2;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
p2 = g.getPointOnEllipticalArc(points[0], points[1], points[2], points[3], end, 0);
|
|
||||||
len += g.getLineLength(p1.x, p1.y, p2.x, p2.y);
|
|
||||||
|
|
||||||
return len;
|
|
||||||
}
|
|
||||||
|
|
||||||
return 0;
|
|
||||||
},
|
|
||||||
convertEndpointToCenterParameterization: function(x1, y1, x2, y2, fa, fs, rx, ry, psiDeg) {
|
|
||||||
// Derived from: http://www.w3.org/TR/SVG/implnote.html#ArcImplementationNotes
|
|
||||||
var psi = psiDeg * (Math.PI / 180.0);
|
|
||||||
var xp = Math.cos(psi) * (x1 - x2) / 2.0 + Math.sin(psi) * (y1 - y2) / 2.0;
|
|
||||||
var yp = -1 * Math.sin(psi) * (x1 - x2) / 2.0 + Math.cos(psi) * (y1 - y2) / 2.0;
|
|
||||||
|
|
||||||
var lambda = (xp * xp) / (rx * rx) + (yp * yp) / (ry * ry);
|
|
||||||
|
|
||||||
if(lambda > 1) {
|
|
||||||
rx *= Math.sqrt(lambda);
|
|
||||||
ry *= Math.sqrt(lambda);
|
|
||||||
}
|
|
||||||
|
|
||||||
var f = Math.sqrt((((rx * rx) * (ry * ry)) - ((rx * rx) * (yp * yp)) - ((ry * ry) * (xp * xp))) / ((rx * rx) * (yp * yp) + (ry * ry) * (xp * xp)));
|
|
||||||
|
|
||||||
if(fa == fs) {
|
|
||||||
f *= -1;
|
|
||||||
}
|
|
||||||
if(isNaN(f)) {
|
|
||||||
f = 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
var cxp = f * rx * yp / ry;
|
|
||||||
var cyp = f * -ry * xp / rx;
|
|
||||||
|
|
||||||
var cx = (x1 + x2) / 2.0 + Math.cos(psi) * cxp - Math.sin(psi) * cyp;
|
|
||||||
var cy = (y1 + y2) / 2.0 + Math.sin(psi) * cxp + Math.cos(psi) * cyp;
|
|
||||||
|
|
||||||
var vMag = function(v) {
|
|
||||||
return Math.sqrt(v[0] * v[0] + v[1] * v[1]);
|
|
||||||
};
|
|
||||||
var vRatio = function(u, v) {
|
|
||||||
return (u[0] * v[0] + u[1] * v[1]) / (vMag(u) * vMag(v));
|
|
||||||
};
|
|
||||||
var vAngle = function(u, v) {
|
|
||||||
return (u[0] * v[1] < u[1] * v[0] ? -1 : 1) * Math.acos(vRatio(u, v));
|
|
||||||
};
|
|
||||||
var theta = vAngle([1, 0], [(xp - cxp) / rx, (yp - cyp) / ry]);
|
|
||||||
var u = [(xp - cxp) / rx, (yp - cyp) / ry];
|
|
||||||
var v = [(-1 * xp - cxp) / rx, (-1 * yp - cyp) / ry];
|
|
||||||
var dTheta = vAngle(u, v);
|
|
||||||
|
|
||||||
if(vRatio(u, v) <= -1) {
|
|
||||||
dTheta = Math.PI;
|
|
||||||
}
|
|
||||||
if(vRatio(u, v) >= 1) {
|
|
||||||
dTheta = 0;
|
|
||||||
}
|
|
||||||
if(fs === 0 && dTheta > 0) {
|
|
||||||
dTheta = dTheta - 2 * Math.PI;
|
|
||||||
}
|
|
||||||
if(fs == 1 && dTheta < 0) {
|
|
||||||
dTheta = dTheta + 2 * Math.PI;
|
|
||||||
}
|
|
||||||
return [cx, cy, rx, ry, theta, dTheta, psi, fs];
|
|
||||||
}
|
|
||||||
};
|
|
||||||
@@ -4,6 +4,7 @@
|
|||||||
<link rel="stylesheet" type="text/css"href="../base.css">
|
<link rel="stylesheet" type="text/css"href="../base.css">
|
||||||
<script src="../../dist/kinetic-core.js"></script>
|
<script src="../../dist/kinetic-core.js"></script>
|
||||||
<!-- plugins -->
|
<!-- plugins -->
|
||||||
|
<script src="../../src/plugins/shapes/Path.js"></script>
|
||||||
<script src="../../src/plugins/shapes/RegularPolygon.js"></script>
|
<script src="../../src/plugins/shapes/RegularPolygon.js"></script>
|
||||||
<script src="../../src/plugins/shapes/Star.js"></script>
|
<script src="../../src/plugins/shapes/Star.js"></script>
|
||||||
<script src="../../src/plugins/shapes/TextPath.js"></script>
|
<script src="../../src/plugins/shapes/TextPath.js"></script>
|
||||||
|
|||||||
@@ -4,6 +4,7 @@
|
|||||||
<link rel="stylesheet" type="text/css"href="../base.css">
|
<link rel="stylesheet" type="text/css"href="../base.css">
|
||||||
<script src="../../dist/kinetic-core.js"></script>
|
<script src="../../dist/kinetic-core.js"></script>
|
||||||
<!-- plugins -->
|
<!-- plugins -->
|
||||||
|
<script src="../../src/plugins/shapes/Path.js"></script>
|
||||||
<script src="../../src/plugins/shapes/RegularPolygon.js"></script>
|
<script src="../../src/plugins/shapes/RegularPolygon.js"></script>
|
||||||
<script src="../../src/plugins/shapes/Star.js"></script>
|
<script src="../../src/plugins/shapes/Star.js"></script>
|
||||||
<script src="../../src/plugins/shapes/TextPath.js"></script>
|
<script src="../../src/plugins/shapes/TextPath.js"></script>
|
||||||
|
|||||||
@@ -6,7 +6,9 @@
|
|||||||
<!-- plugins -->
|
<!-- plugins -->
|
||||||
<script src="../../src/plugins/shapes/RegularPolygon.js"></script>
|
<script src="../../src/plugins/shapes/RegularPolygon.js"></script>
|
||||||
<script src="../../src/plugins/shapes/Star.js"></script>
|
<script src="../../src/plugins/shapes/Star.js"></script>
|
||||||
|
<script src="../../src/plugins/shapes/Path.js"></script>
|
||||||
<script src="../../src/plugins/shapes/TextPath.js"></script>
|
<script src="../../src/plugins/shapes/TextPath.js"></script>
|
||||||
|
<script src="../../src/plugins/util/PathHelper.js"></script>
|
||||||
<!-- assets -->
|
<!-- assets -->
|
||||||
<script src="../assets/worldMap.js"></script>
|
<script src="../assets/worldMap.js"></script>
|
||||||
<script src="../assets/tiger.js"></script>
|
<script src="../assets/tiger.js"></script>
|
||||||
|
|||||||
@@ -5076,7 +5076,7 @@ Test.prototype.tests = {
|
|||||||
fill: 'black'
|
fill: 'black'
|
||||||
}));
|
}));
|
||||||
|
|
||||||
var p1 = Kinetic.Geometry.getPointOnLine(125, 10, 10, 210, 160);
|
var p1 = Kinetic.Plugins.Path.getPointOnLine(125, 10, 10, 210, 160);
|
||||||
// should be 1/2 way, or (110,85)
|
// should be 1/2 way, or (110,85)
|
||||||
test(Math.round(p1.x) === 110, 'point X value should be 110');
|
test(Math.round(p1.x) === 110, 'point X value should be 110');
|
||||||
test(Math.round(p1.y) === 85, 'point Y value should be 85');
|
test(Math.round(p1.y) === 85, 'point Y value should be 85');
|
||||||
@@ -5116,12 +5116,12 @@ Test.prototype.tests = {
|
|||||||
c = 'M 100 200';
|
c = 'M 100 200';
|
||||||
|
|
||||||
for( t = 0.25; t <= 1; t += 0.25) {
|
for( t = 0.25; t <= 1; t += 0.25) {
|
||||||
var p1 = Kinetic.Geometry.getPointOnCubicBezier(t, 100, 200, 100, 100, 250, 100, 250, 200);
|
var p1 = Kinetic.Plugins.Path.getPointOnCubicBezier(t, 100, 200, 100, 100, 250, 100, 250, 200);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
for( t = 0.25; t <= 1; t += 0.25) {
|
for( t = 0.25; t <= 1; t += 0.25) {
|
||||||
var p1 = Kinetic.Geometry.getPointOnCubicBezier(t, 250, 200, 250, 300, 400, 300, 400, 200);
|
var p1 = Kinetic.Plugins.Path.getPointOnCubicBezier(t, 250, 200, 250, 300, 400, 300, 400, 200);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5159,12 +5159,12 @@ Test.prototype.tests = {
|
|||||||
c = 'M 200 300';
|
c = 'M 200 300';
|
||||||
|
|
||||||
for( t = 0.333; t <= 1; t += 0.333) {
|
for( t = 0.333; t <= 1; t += 0.333) {
|
||||||
var p1 = Kinetic.Geometry.getPointOnQuadraticBezier(t, 200, 300, 400, 50, 600, 300);
|
var p1 = Kinetic.Plugins.Path.getPointOnQuadraticBezier(t, 200, 300, 400, 50, 600, 300);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
for( t = 0.333; t <= 1; t += 0.333) {
|
for( t = 0.333; t <= 1; t += 0.333) {
|
||||||
var p1 = Kinetic.Geometry.getPointOnQuadraticBezier(t, 600, 300, 800, 550, 1000, 300);
|
var p1 = Kinetic.Plugins.Path.getPointOnQuadraticBezier(t, 600, 300, 800, 550, 1000, 300);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -5200,7 +5200,7 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
layer.add(path);
|
layer.add(path);
|
||||||
|
|
||||||
var centerParamPoints = Kinetic.Geometry.convertEndpointToCenterParameterization(50, 100, 150, 150, 1, 1, 100, 50, 0);
|
var centerParamPoints = Kinetic.Plugins.Path.convertEndpointToCenterParameterization(50, 100, 150, 150, 1, 1, 100, 50, 0);
|
||||||
|
|
||||||
var start = centerParamPoints[4];
|
var start = centerParamPoints[4];
|
||||||
// 4 = theta
|
// 4 = theta
|
||||||
@@ -5210,24 +5210,24 @@ Test.prototype.tests = {
|
|||||||
var inc = Math.PI / 6.0;
|
var inc = Math.PI / 6.0;
|
||||||
// 30 degree resolution
|
// 30 degree resolution
|
||||||
|
|
||||||
var p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], start, 0);
|
var p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], start, 0);
|
||||||
c = 'M ' + p1.x.toString() + ' ' + p1.y.toString();
|
c = 'M ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
|
|
||||||
if(dTheta < 0)// clockwise
|
if(dTheta < 0)// clockwise
|
||||||
{
|
{
|
||||||
for( t = start - inc; t > end; t -= inc) {
|
for( t = start - inc; t > end; t -= inc) {
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else// counter-clockwise
|
else// counter-clockwise
|
||||||
{
|
{
|
||||||
for( t = start + inc; t < end; t += inc) {
|
for( t = start + inc; t < end; t += inc) {
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], end, 0);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], end, 0);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
|
|
||||||
var testpath = new Kinetic.Plugins.Path({
|
var testpath = new Kinetic.Plugins.Path({
|
||||||
@@ -5262,7 +5262,7 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
layer.add(path);
|
layer.add(path);
|
||||||
|
|
||||||
var centerParamPoints = Kinetic.Geometry.convertEndpointToCenterParameterization(250, 100, 150, 150, 1, 0, 100, 50, 0);
|
var centerParamPoints = Kinetic.Plugins.Path.convertEndpointToCenterParameterization(250, 100, 150, 150, 1, 0, 100, 50, 0);
|
||||||
|
|
||||||
var start = centerParamPoints[4];
|
var start = centerParamPoints[4];
|
||||||
// 4 = theta
|
// 4 = theta
|
||||||
@@ -5272,24 +5272,24 @@ Test.prototype.tests = {
|
|||||||
var inc = Math.PI / 6.0;
|
var inc = Math.PI / 6.0;
|
||||||
// 30 degree resolution
|
// 30 degree resolution
|
||||||
|
|
||||||
var p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], start, 0);
|
var p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], start, 0);
|
||||||
c = 'M ' + p1.x.toString() + ' ' + p1.y.toString();
|
c = 'M ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
|
|
||||||
if(dTheta < 0)// clockwise
|
if(dTheta < 0)// clockwise
|
||||||
{
|
{
|
||||||
for( t = start - inc; t > end; t -= inc) {
|
for( t = start - inc; t > end; t -= inc) {
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else// counter-clockwise
|
else// counter-clockwise
|
||||||
{
|
{
|
||||||
for( t = start + inc; t < end; t += inc) {
|
for( t = start + inc; t < end; t += inc) {
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, 0);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], end, 0);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], end, 0);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
|
|
||||||
var testpath = new Kinetic.Plugins.Path({
|
var testpath = new Kinetic.Plugins.Path({
|
||||||
@@ -5324,7 +5324,7 @@ Test.prototype.tests = {
|
|||||||
|
|
||||||
layer.add(path);
|
layer.add(path);
|
||||||
|
|
||||||
var centerParamPoints = Kinetic.Geometry.convertEndpointToCenterParameterization(250, 100, 150, 150, 1, 0, 100, 50, 30);
|
var centerParamPoints = Kinetic.Plugins.Path.convertEndpointToCenterParameterization(250, 100, 150, 150, 1, 0, 100, 50, 30);
|
||||||
|
|
||||||
var start = centerParamPoints[4];
|
var start = centerParamPoints[4];
|
||||||
// 4 = theta
|
// 4 = theta
|
||||||
@@ -5336,24 +5336,24 @@ Test.prototype.tests = {
|
|||||||
var psi = centerParamPoints[6];
|
var psi = centerParamPoints[6];
|
||||||
// 6 = psi radians
|
// 6 = psi radians
|
||||||
|
|
||||||
var p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], start, psi);
|
var p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], start, psi);
|
||||||
c = 'M ' + p1.x.toString() + ' ' + p1.y.toString();
|
c = 'M ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
|
|
||||||
if(dTheta < 0)// clockwise
|
if(dTheta < 0)// clockwise
|
||||||
{
|
{
|
||||||
for( t = start - inc; t > end; t -= inc) {
|
for( t = start - inc; t > end; t -= inc) {
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, psi);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, psi);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else// counter-clockwise
|
else// counter-clockwise
|
||||||
{
|
{
|
||||||
for( t = start + inc; t < end; t += inc) {
|
for( t = start + inc; t < end; t += inc) {
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, psi);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], t, psi);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
p1 = Kinetic.Geometry.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], end, psi);
|
p1 = Kinetic.Plugins.Path.getPointOnEllipticalArc(centerParamPoints[0], centerParamPoints[1], centerParamPoints[2], centerParamPoints[3], end, psi);
|
||||||
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
c += ' ' + p1.x.toString() + ' ' + p1.y.toString();
|
||||||
|
|
||||||
var testpath = new Kinetic.Plugins.Path({
|
var testpath = new Kinetic.Plugins.Path({
|
||||||
|
|||||||
Reference in New Issue
Block a user