From 67d30f004248f1df6b49b09f2ac1600ffac2e811 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Thu, 11 Sep 2014 15:20:04 +0200 Subject: [PATCH] Actually use maxRecursion and tolerance arguments in PathIterator, and increase precision in #flatten() a bit. --- src/path/Path.js | 5 +++-- src/path/PathIterator.js | 19 +++++++++---------- 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/src/path/Path.js b/src/path/Path.js index 38cbe050..14d3f36d 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -952,7 +952,7 @@ var Path = PathItem.extend(/** @lends Path# */{ * copy.flatten(20); */ flatten: function(maxDistance) { - var iterator = new PathIterator(this), + var iterator = new PathIterator(this, 64, 0.1), pos = 0, // Adapt step = maxDistance so the points distribute evenly. step = iterator.length / Math.ceil(iterator.length / maxDistance), @@ -2117,7 +2117,8 @@ var Path = PathItem.extend(/** @lends Path# */{ // native dashes. if (!dontStart) ctx.beginPath(); - var iterator = new PathIterator(this, strokeMatrix), + var iterator = new PathIterator(this, 32, 0.25, + strokeMatrix), length = iterator.length, from = -style.getDashOffset(), to, i = 0; diff --git a/src/path/PathIterator.js b/src/path/PathIterator.js index da9fd811..401a73e7 100644 --- a/src/path/PathIterator.js +++ b/src/path/PathIterator.js @@ -22,24 +22,22 @@ var PathIterator = Base.extend({ * Creates a path iterator for the given path. * * @param {Path} path the path to iterate over. - * @param {Matrix} [matrix] the matrix by which to transform the path's - * coordinates without modifying the actual path. * @param {Number} [maxRecursion=32] the maximum amount of recursion in * curve subdivision when mapping offsets to curve parameters. * @param {Number} [tolerance=0.25] the error tolerance at which the * recursion is interrupted before the maximum number of iterations is * reached. + * @param {Matrix} [matrix] the matrix by which to transform the path's + * coordinates without modifying the actual path. * @return {PathIterator} the newly created path iterator. */ - initialize: function(path, matrix, maxRecursion, tolerance) { - if (!tolerance) - tolerance = 0.25; - + initialize: function(path, maxRecursion, tolerance, matrix) { // Instead of relying on path.curves, we only use segments here and // get the curve values from them. var curves = [], // The curve values as returned by getValues() parts = [], // The calculated, subdivided parts of the path length = 0, // The total length of the path + // By default, we're not subdividing more than 32 times. minDifference = 1 / (maxRecursion || 32), segments = path._segments, segment1 = segments[0], @@ -55,10 +53,11 @@ var PathIterator = Base.extend({ function computeParts(curve, index, minT, maxT) { // Check if the t-span is big enough for subdivision. - // We're not subdividing more than 32 times... - // After quite a bit of testing, a tolerance of 0.25 appears to be a - // good trade-off between speed and precision. - if ((maxT - minT) > 1 / 32 && !Curve.isFlatEnough(curve, 0.25)) { + if ((maxT - minT) > minDifference + // After quite a bit of testing, a default tolerance of 0.25 + // appears to offer a good trade-off between speed and + // precision for display purposes. + && !Curve.isFlatEnough(curve, tolerance || 0.25)) { var split = Curve.subdivide(curve), halfT = (minT + maxT) / 2; // Recursively subdivide and compute parts again.