diff --git a/examples/Scripts/CurveTimeParametrization.html b/examples/Scripts/CurveTimeParametrization.html index fd0bc840..d2c1bb52 100644 --- a/examples/Scripts/CurveTimeParametrization.html +++ b/examples/Scripts/CurveTimeParametrization.html @@ -23,18 +23,32 @@ var length = curve.length; var step = 10; var iteratively = false; + var curvesToPoints = true; var num = Math.floor(length / step); var prev = 0; - for (var i = 0, pos = 0; i <= num; i++, pos += step) { - var t = iteratively - ? curve.getParameter(step, prev) - : curve.getParameter(pos); - var point = curve.getPoint(t); - var circle = new Path.Circle(point, step / 2); - circle.strokeColor = 'red'; - if (remove) - circle.removeOnMove(); - prev = t; + if (curvesToPoints) { + var clone = path.clone(); + clone.curvesToPoints(step); + for (var i = 0; i < clone.segments.length; i++) { + var point = clone.segments[i].point; + var circle = new Path.Circle(point, step / 2); + circle.strokeColor = 'red'; + if (remove) + circle.removeOnMove(); + } + clone.remove(); + } else { + for (var i = 0, pos = 0; i <= num; i++, pos += step) { + var t = iteratively + ? curve.getParameter(step, prev) + : curve.getParameter(pos); + var point = curve.getPoint(t); + var circle = new Path.Circle(point, step / 2); + circle.strokeColor = 'red'; + if (remove) + circle.removeOnMove(); + prev = t; + } } } diff --git a/src/path/Curve.js b/src/path/Curve.js index 69d4eebb..f2b8d6ec 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -302,19 +302,6 @@ var Curve = this.Curve = Base.extend({ // TODO: getIntersections // TODO: adjustThroughPoint - // DOCS: document Curve#transform(matrix) - /** - * @param {Matrix} matrix - * @return {Curve} - */ - transform: function(matrix) { - return new Curve( - matrix._transformPoint(this._segment1._point), - matrix._transformPoint(this._segment1._handleOut), - matrix._transformPoint(this._segment2._handleIn), - matrix._transformPoint(this._segment2._point)); - }, - /** * Returns a reversed version of the curve, without modifying the curve * itself. diff --git a/src/path/Path.js b/src/path/Path.js index 0b4ec87a..152c058c 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -458,9 +458,22 @@ var Path = this.Path = PathItem.extend({ setFullySelected: function(selected) { this.setSelected(selected); }, - + + curvesToPoints: function(maxDistance) { + var flattener = new PathFlattener(this), + pos = 0; + var step = flattener.length / Math.ceil(flattener.length / maxDistance); + this.segments = []; + // Iterate over path and evaluate and add points at given offsets + do { + this._add([ new Segment(flattener.evaluate(pos, 0)) ]); + } while ((pos += step) < flattener.length); + // Add last one + this._add([ new Segment(flattener.evaluate(flattener.length, 0)) ]); + this._changed(ChangeFlags.GEOMETRY); + }, + // TODO: pointsToCurves([tolerance[, threshold[, cornerRadius[, scale]]]]) - // TODO: curvesToPoints([maxPointDistance[, flatness]]) // TODO: reduceSegments([flatness]) // TODO: split(offset) / split(location) / split(index[, parameter]) diff --git a/src/path/PathFlattener.js b/src/path/PathFlattener.js index 9f6f6a71..bb62046c 100644 --- a/src/path/PathFlattener.js +++ b/src/path/PathFlattener.js @@ -113,6 +113,12 @@ var PathFlattener = Base.extend({ }; }, + evaluate: function(offset, type) { + var param = this.getParameter(offset); + return Curve.evaluate.apply(Curve, + this.curves[param.index].concat([param.value, type])); + }, + drawPart: function(ctx, from, to) { from = this.getParameter(from); to = this.getParameter(to);