From 84b202fd79da625e9162b77ca0813ba7c1496d02 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?J=C3=BCrg=20Lehni?= Date: Wed, 19 Aug 2015 17:26:52 +0200 Subject: [PATCH] Expose and reuse Curve evaluation method names. --- src/path/Curve.js | 42 +++++++++++++++++++++++---------------- src/path/CurveLocation.js | 17 ++++++++-------- src/path/Path.js | 3 +-- src/path/PathIterator.js | 3 +-- 4 files changed, 36 insertions(+), 29 deletions(-) diff --git a/src/path/Curve.js b/src/path/Curve.js index d2c4df95..40226fec 100644 --- a/src/path/Curve.js +++ b/src/path/Curve.js @@ -719,7 +719,8 @@ statics: { padding); } } -}}, Base.each(['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], +}}, Base.each( + ['getBounds', 'getStrokeBounds', 'getHandleBounds', 'getRoughBounds'], // Note: Although Curve.getBounds() exists, we are using Path.getBounds() to // determine the bounds of Curve objects with defined segment1 and segment2 // values Curve.getBounds() can be used directly on curve arrays, without @@ -769,21 +770,7 @@ statics: { * @type Rectangle * @ignore */ -}), Base.each(['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', - 'getWeightedNormal', 'getCurvature'], - // Note: Although Curve.getBounds() exists, we are using Path.getBounds() to - // determine the bounds of Curve objects with defined segment1 and segment2 - // values Curve.getBounds() can be used directly on curve arrays, without - // the need to create a Curve object first, as required by the code that - // finds path interesections. - function(name) { - this[name + 'At'] = function(offset, isParameter) { - var values = this.getValues(); - return Curve[name](values, isParameter ? offset - : Curve.getParameterAt(values, offset, 0)); - }; - }, -/** @lends Curve# */{ +}), /** @lends Curve# */{ // Explicitly deactivate the creation of beans, as we have functions here // that look like bean getters but actually read arguments. // See #getParameterOf(), #getLocationOf(), #getNearestLocation(), ... @@ -970,7 +957,28 @@ statics: { * is a curve time parameter * @return {Number} the curvature of the curve at the given offset */ -}), +}, +new function() { // // Scope to inject various evaluate methods + var methods = ['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', + 'getWeightedNormal', 'getCurvature']; + return Base.each(methods, + // Note: Although Curve.getBounds() exists, we are using Path.getBounds() to + // determine the bounds of Curve objects with defined segment1 and segment2 + // values Curve.getBounds() can be used directly on curve arrays, without + // the need to create a Curve object first, as required by the code that + // finds path interesections. + function(name) { + this[name + 'At'] = function(offset, isParameter) { + var values = this.getValues(); + return Curve[name](values, isParameter ? offset + : Curve.getParameterAt(values, offset, 0)); + }; + }, { + statics: { + evaluateMethods: methods + } + }) +}, new function() { // Scope for methods that require private functions function getLengthIntegrand(v) { diff --git a/src/path/CurveLocation.js b/src/path/CurveLocation.js index 40fa63d3..7a47ede0 100644 --- a/src/path/CurveLocation.js +++ b/src/path/CurveLocation.js @@ -307,13 +307,14 @@ var CurveLocation = Base.extend(/** @lends CurveLocation# */{ parts.push('distance: ' + f.number(this._distance)); return '{ ' + parts.join(', ') + ' }'; } -}, Base.each(['getTangent', 'getNormal', 'getWeightedTangent', - 'getWeightedNormal', 'getCurvature'], function(name) { +}, Base.each(Curve.evaluateMethods, function(name) { // Produce getters for #getTangent() / #getNormal() / #getCurvature() - var get = name + 'At'; - this[name] = function() { - var parameter = this.getParameter(), - curve = this.getCurve(); - return parameter != null && curve && curve[get](parameter, true); - }; + if (name !== 'getPoint') { + var get = name + 'At'; + this[name] = function() { + var parameter = this.getParameter(), + curve = this.getCurve(); + return parameter != null && curve && curve[get](parameter, true); + }; + } }, {})); diff --git a/src/path/Path.js b/src/path/Path.js index 7aaa63bb..ac1005f1 100644 --- a/src/path/Path.js +++ b/src/path/Path.js @@ -1638,8 +1638,7 @@ var Path = PathItem.extend(/** @lends Path# */{ // TODO: intersects(item) // TODO: contains(item) -}, Base.each(['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', - 'getWeightedNormal', 'getCurvature'], +}, Base.each(Curve.evaluateMethods, function(name) { this[name + 'At'] = function(offset, isParameter) { var loc = this.getLocationAt(offset, isParameter); diff --git a/src/path/PathIterator.js b/src/path/PathIterator.js index 4548d510..1a997798 100644 --- a/src/path/PathIterator.js +++ b/src/path/PathIterator.js @@ -146,8 +146,7 @@ var PathIterator = Base.extend({ ctx.bezierCurveTo.apply(ctx, curve.slice(2)); } } -}, Base.each(['getPoint', 'getTangent', 'getNormal', 'getWeightedTangent', - 'getWeightedNormal', 'getCurvature'], +}, Base.each(Curve.evaluateMethods, function(name) { this[name + 'At'] = function(offset, weighted) { var param = this.getParameterAt(offset);