From 294904eac7c1bba616f162b3559053c0fe9a7de8 Mon Sep 17 00:00:00 2001 From: sapics Date: Sat, 24 Oct 2015 10:20:45 +0900 Subject: [PATCH 1/2] Faster curve.getNearestParameter when curve has no handles --- src/path/Curve.js | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/path/Curve.js b/src/path/Curve.js index f1e7720a..f903c902 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -645,6 +645,20 @@ statics: { }, getNearestParameter: function(v, point) { + if (!this.hasHandles(v)) { + var p1x = v[0], p1y = v[1], + p2x = v[6], p2y = v[7], + v12x = p2x - p1x, v12y = p2y - p1x, + t = ((point.x - p1x) * v12x + (point.y - p1y) * v12y) / + (v12x * v12x + v12y * v12y); + if (t <= 0) return 0; + if (t >= 1) return 1; + if (!t) return 0.5; + var roots = []; + if (Numerical.solveCubic(2, -3, 0, t, roots, 0, 1) === 1) + return roots[0]; + } + var count = 100, minDist = Infinity, minT = 0; From 89a45a668227767375b5c054027ed68b4689526f Mon Sep 17 00:00:00 2001 From: sapics Date: Sat, 24 Oct 2015 14:42:03 +0900 Subject: [PATCH 2/2] Use cubic equation solution for faster calculation --- src/path/Curve.js | 12 +++++------- 1 file changed, 5 insertions(+), 7 deletions(-) diff --git a/src/path/Curve.js b/src/path/Curve.js index f903c902..d3d5a81b 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -650,13 +650,11 @@ statics: { p2x = v[6], p2y = v[7], v12x = p2x - p1x, v12y = p2y - p1x, t = ((point.x - p1x) * v12x + (point.y - p1y) * v12y) / - (v12x * v12x + v12y * v12y); - if (t <= 0) return 0; - if (t >= 1) return 1; - if (!t) return 0.5; - var roots = []; - if (Numerical.solveCubic(2, -3, 0, t, roots, 0, 1) === 1) - return roots[0]; + (v12x * v12x + v12y * v12y), + epsilon = /*#=*/Numerical.EPSILON; + if (t < epsilon) return 0; + if (t + epsilon > 1) return 1; + return 0.5 - Math.cos(Math.acos(2 * t - 1) / 3 + Math.PI * 4 / 3) || 0.5; } var count = 100,