Actually use maxRecursion and tolerance arguments in PathIterator, and increase precision in #flatten() a bit.

This commit is contained in:
Jürg Lehni 2014-09-11 15:20:04 +02:00
parent 73765e6654
commit 67d30f0042
2 changed files with 12 additions and 12 deletions

View file

@ -952,7 +952,7 @@ var Path = PathItem.extend(/** @lends Path# */{
* copy.flatten(20); * copy.flatten(20);
*/ */
flatten: function(maxDistance) { flatten: function(maxDistance) {
var iterator = new PathIterator(this), var iterator = new PathIterator(this, 64, 0.1),
pos = 0, pos = 0,
// Adapt step = maxDistance so the points distribute evenly. // Adapt step = maxDistance so the points distribute evenly.
step = iterator.length / Math.ceil(iterator.length / maxDistance), step = iterator.length / Math.ceil(iterator.length / maxDistance),
@ -2117,7 +2117,8 @@ var Path = PathItem.extend(/** @lends Path# */{
// native dashes. // native dashes.
if (!dontStart) if (!dontStart)
ctx.beginPath(); ctx.beginPath();
var iterator = new PathIterator(this, strokeMatrix), var iterator = new PathIterator(this, 32, 0.25,
strokeMatrix),
length = iterator.length, length = iterator.length,
from = -style.getDashOffset(), to, from = -style.getDashOffset(), to,
i = 0; i = 0;

View file

@ -22,24 +22,22 @@ var PathIterator = Base.extend({
* Creates a path iterator for the given path. * Creates a path iterator for the given path.
* *
* @param {Path} path the path to iterate over. * @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 * @param {Number} [maxRecursion=32] the maximum amount of recursion in
* curve subdivision when mapping offsets to curve parameters. * curve subdivision when mapping offsets to curve parameters.
* @param {Number} [tolerance=0.25] the error tolerance at which the * @param {Number} [tolerance=0.25] the error tolerance at which the
* recursion is interrupted before the maximum number of iterations is * recursion is interrupted before the maximum number of iterations is
* reached. * 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. * @return {PathIterator} the newly created path iterator.
*/ */
initialize: function(path, matrix, maxRecursion, tolerance) { initialize: function(path, maxRecursion, tolerance, matrix) {
if (!tolerance)
tolerance = 0.25;
// Instead of relying on path.curves, we only use segments here and // Instead of relying on path.curves, we only use segments here and
// get the curve values from them. // get the curve values from them.
var curves = [], // The curve values as returned by getValues() var curves = [], // The curve values as returned by getValues()
parts = [], // The calculated, subdivided parts of the path parts = [], // The calculated, subdivided parts of the path
length = 0, // The total length 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), minDifference = 1 / (maxRecursion || 32),
segments = path._segments, segments = path._segments,
segment1 = segments[0], segment1 = segments[0],
@ -55,10 +53,11 @@ var PathIterator = Base.extend({
function computeParts(curve, index, minT, maxT) { function computeParts(curve, index, minT, maxT) {
// Check if the t-span is big enough for subdivision. // Check if the t-span is big enough for subdivision.
// We're not subdividing more than 32 times... if ((maxT - minT) > minDifference
// After quite a bit of testing, a tolerance of 0.25 appears to be a // After quite a bit of testing, a default tolerance of 0.25
// good trade-off between speed and precision. // appears to offer a good trade-off between speed and
if ((maxT - minT) > 1 / 32 && !Curve.isFlatEnough(curve, 0.25)) { // precision for display purposes.
&& !Curve.isFlatEnough(curve, tolerance || 0.25)) {
var split = Curve.subdivide(curve), var split = Curve.subdivide(curve),
halfT = (minT + maxT) / 2; halfT = (minT + maxT) / 2;
// Recursively subdivide and compute parts again. // Recursively subdivide and compute parts again.