From 6a483fa8be18c40968b8d4a28a7d32ce8aa4ac01 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 27 Apr 2011 21:40:52 +0100 Subject: [PATCH] Rename CurveLocation related functions to *At(offset), and add optional isParameter argument that defines whether offset is a length or a description of curve index / curve parameter. --- examples/Animated/Extruded.html | 4 +-- examples/Animated/Flock.html | 2 +- src/path/Path.js | 64 +++++++++++++++++---------------- 3 files changed, 37 insertions(+), 33 deletions(-) diff --git a/examples/Animated/Extruded.html b/examples/Animated/Extruded.html index 9a9a185d..c489eac6 100644 --- a/examples/Animated/Extruded.html +++ b/examples/Animated/Extruded.html @@ -61,8 +61,8 @@ var path = lineGroup.children[i], l1 = (length1 / lineCount * (i + count / 10)) % length1, l2 = (length2 / lineCount * (i + count / 10)) % length2; - path.segments[0].point = path1.getLocation(l1).point, - path.segments[1].point = path2.getLocation(l2).point; + path.segments[0].point = path1.getPointAt(l1), + path.segments[1].point = path2.getPointAt(l2); } count++; } diff --git a/examples/Animated/Flock.html b/examples/Animated/Flock.html index 38d27f3d..a868e373 100644 --- a/examples/Animated/Flock.html +++ b/examples/Animated/Flock.html @@ -251,7 +251,7 @@ count++; for(var i = 0, l = boids.length; i < l; i++) { if (groupTogether) { - var point = heartPath.getLocation(((i + count / 30) % l) / l * pathLength).point; + var point = heartPath.getPointAt(((i + count / 30) % l) / l * pathLength); boids[i].arrive(point); } boids[i].run(boids); diff --git a/src/path/Path.js b/src/path/Path.js index 4e780fd2..dd7e5d10 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -210,30 +210,6 @@ var Path = this.Path = PathItem.extend({ return false; }, - // TODO: getLocation(point, precision) - getLocation: function(offset) { - var curves = this.getCurves(), - currentLength = 0; - for (var i = 0, l = curves.length; i < l; i++) { - var startLength = currentLength, - curve = curves[i]; - currentLength += curve.getLength(); - if (currentLength >= length) { - // found the segment within which the length lies - var t = curve.getParameter(length - startLength); - return new CurveLocation(curve, t); - } - } - // TODO: is this the case for paper.js too? - // it may be that through impreciseness of getLength, that the end - // of the curves was missed: - if (length <= this.getLength()) { - var curve = curves[curves.length - 1]; - return new CurveLocation(curve, 1); - } - return null; - }, - getLength: function() { var curves = this.getCurves(); var length = 0; @@ -255,11 +231,39 @@ var Path = this.Path = PathItem.extend({ return null; }, + // TODO: getLocationAt(point, precision) + // TODO: Port back renaming and new isParameter argument to Scriptographer + getLocationAt: function(offset, isParameter) { + var curves = this.getCurves(), + length = 0; + if (isParameter) { + // offset consists of curve index and curve parameter, before and + // after the fractional digit. + var index = ~~offset; // = Math.floor() + return new CurveLocation(curves[index], offset - index); + } + for (var i = 0, l = curves.length; i < l; i++) { + var start = length, + curve = curves[i]; + length += curve.getLength(); + if (length >= offset) { + // Found the segment within which the length lies + return new CurveLocation(curve, + curve.getParameter(offset - start)); + } + } + // It may be that through impreciseness of getLength, that the end + // of the curves was missed: + if (offset <= this.getLength()) + return new CurveLocation(curves[curves.length - 1], 1); + return null; + }, + /** * Returns the point of the path at the given offset. */ - getPoint: function(offset) { - var loc = this.getLocation(offset); + getPointAt: function(offset, isParameter) { + var loc = this.getLocationAt(offset, isParameter); return loc && loc.getPoint(); }, @@ -267,16 +271,16 @@ var Path = this.Path = PathItem.extend({ * Returns the tangent to the path at the given offset as a vector * point. */ - getTangent: function(offset) { - var loc = this.getLocation(offset); + getTangentAt: function(offset, isParameter) { + var loc = this.getLocationAt(offset, isParameter); return loc && loc.getTangent(); }, /** * Returns the normal to the path at the given offset as a vector point. */ - getNormal: function(offset) { - var loc = this.getLocation(offset); + getNormalAt: function(offset, isParameter) { + var loc = this.getLocationAt(offset, isParameter); return loc && loc.getNormal(); } }, new function() { // Scope for drawing