From 5fe8effbf880037c6ef4ad29062561c85c75f632 Mon Sep 17 00:00:00 2001 From: "Mr.doob" Date: Thu, 22 Dec 2016 16:59:37 +0900 Subject: [PATCH] ShapeUtils: Moved b2 and b3 to it's own file. --- src/extras/ShapeUtils.js | 75 +--------------------- src/extras/core/Bezier.js | 70 ++++++++++++++++++++ src/extras/core/Font.js | 4 +- src/extras/curves/CubicBezierCurve.js | 4 +- src/extras/curves/CubicBezierCurve3.js | 6 +- src/extras/curves/QuadraticBezierCurve.js | 4 +- src/extras/curves/QuadraticBezierCurve3.js | 6 +- 7 files changed, 79 insertions(+), 90 deletions(-) create mode 100644 src/extras/core/Bezier.js diff --git a/src/extras/ShapeUtils.js b/src/extras/ShapeUtils.js index 847277dad7..ec891a2b54 100644 --- a/src/extras/ShapeUtils.js +++ b/src/extras/ShapeUtils.js @@ -687,80 +687,7 @@ var ShapeUtils = { return ShapeUtils.area( pts ) < 0; - }, - - // Bezier Curves formulas obtained from - // http://en.wikipedia.org/wiki/B%C3%A9zier_curve - - // Quad Bezier Functions - - b2: ( function () { - - function b2p0( t, p ) { - - var k = 1 - t; - return k * k * p; - - } - - function b2p1( t, p ) { - - return 2 * ( 1 - t ) * t * p; - - } - - function b2p2( t, p ) { - - return t * t * p; - - } - - return function b2( t, p0, p1, p2 ) { - - return b2p0( t, p0 ) + b2p1( t, p1 ) + b2p2( t, p2 ); - - }; - - } )(), - - // Cubic Bezier Functions - - b3: ( function () { - - function b3p0( t, p ) { - - var k = 1 - t; - return k * k * k * p; - - } - - function b3p1( t, p ) { - - var k = 1 - t; - return 3 * k * k * t * p; - - } - - function b3p2( t, p ) { - - var k = 1 - t; - return 3 * k * t * t * p; - - } - - function b3p3( t, p ) { - - return t * t * t * p; - - } - - return function b3( t, p0, p1, p2, p3 ) { - - return b3p0( t, p0 ) + b3p1( t, p1 ) + b3p2( t, p2 ) + b3p3( t, p3 ); - - }; - - } )() + } }; diff --git a/src/extras/core/Bezier.js b/src/extras/core/Bezier.js new file mode 100644 index 0000000000..4907cd97ed --- /dev/null +++ b/src/extras/core/Bezier.js @@ -0,0 +1,70 @@ +/** + * @author zz85 / http://www.lab4games.net/zz85/blog + * + * Bezier Curves formulas obtained from + * http://en.wikipedia.org/wiki/Bézier_curve + */ + +// Quad Bezier Functions + +function b2p0( t, p ) { + + var k = 1 - t; + return k * k * p; + +} + +function b2p1( t, p ) { + + return 2 * ( 1 - t ) * t * p; + +} + +function b2p2( t, p ) { + + return t * t * p; + +} + +function b2( t, p0, p1, p2 ) { + + return b2p0( t, p0 ) + b2p1( t, p1 ) + b2p2( t, p2 ); + +} + +// Cubic Bezier Functions + +function b3p0( t, p ) { + + var k = 1 - t; + return k * k * k * p; + +} + +function b3p1( t, p ) { + + var k = 1 - t; + return 3 * k * k * t * p; + +} + +function b3p2( t, p ) { + + var k = 1 - t; + return 3 * k * t * t * p; + +} + +function b3p3( t, p ) { + + return t * t * t * p; + +} + +function b3( t, p0, p1, p2, p3 ) { + + return b3p0( t, p0 ) + b3p1( t, p1 ) + b3p2( t, p2 ) + b3p3( t, p3 ); + +} + +export { b2, b3 }; diff --git a/src/extras/core/Font.js b/src/extras/core/Font.js index cc36b859fb..21c8979353 100644 --- a/src/extras/core/Font.js +++ b/src/extras/core/Font.js @@ -1,4 +1,4 @@ -import { ShapeUtils } from '../ShapeUtils'; +import { b2, b3 } from './Bezier'; import { ShapePath } from './ShapePath'; /** @@ -47,7 +47,7 @@ Object.assign( Font.prototype, { var path = new ShapePath(); - var pts = [], b2 = ShapeUtils.b2, b3 = ShapeUtils.b3; + var pts = []; var x, y, cpx, cpy, cpx0, cpy0, cpx1, cpy1, cpx2, cpy2, laste; if ( glyph.o ) { diff --git a/src/extras/curves/CubicBezierCurve.js b/src/extras/curves/CubicBezierCurve.js index ab50f7b0f4..14bdef986d 100644 --- a/src/extras/curves/CubicBezierCurve.js +++ b/src/extras/curves/CubicBezierCurve.js @@ -1,7 +1,7 @@ +import { b3 } from '../core/Bezier'; import { Curve } from '../core/Curve'; import { Vector2 } from '../../math/Vector2'; import { CurveUtils } from '../CurveUtils'; -import { ShapeUtils } from '../ShapeUtils'; /************************************************************** * Cubic Bezier curve @@ -21,8 +21,6 @@ CubicBezierCurve.prototype.constructor = CubicBezierCurve; CubicBezierCurve.prototype.getPoint = function ( t ) { - var b3 = ShapeUtils.b3; - return new Vector2( b3( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x ), b3( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y ) diff --git a/src/extras/curves/CubicBezierCurve3.js b/src/extras/curves/CubicBezierCurve3.js index 8dc1a3eea6..0af246ca0c 100644 --- a/src/extras/curves/CubicBezierCurve3.js +++ b/src/extras/curves/CubicBezierCurve3.js @@ -1,6 +1,6 @@ -import { Vector3 } from '../../math/Vector3'; -import { ShapeUtils } from '../ShapeUtils'; +import { b3 } from '../core/Bezier'; import { Curve } from '../core/Curve'; +import { Vector3 } from '../../math/Vector3'; /************************************************************** * Cubic Bezier 3D curve @@ -19,8 +19,6 @@ var CubicBezierCurve3 = Curve.create( function ( t ) { - var b3 = ShapeUtils.b3; - return new Vector3( b3( t, this.v0.x, this.v1.x, this.v2.x, this.v3.x ), b3( t, this.v0.y, this.v1.y, this.v2.y, this.v3.y ), diff --git a/src/extras/curves/QuadraticBezierCurve.js b/src/extras/curves/QuadraticBezierCurve.js index f28d42c828..1c53f31682 100644 --- a/src/extras/curves/QuadraticBezierCurve.js +++ b/src/extras/curves/QuadraticBezierCurve.js @@ -1,7 +1,7 @@ +import { b2 } from '../core/Bezier'; import { Curve } from '../core/Curve'; import { Vector2 } from '../../math/Vector2'; import { CurveUtils } from '../CurveUtils'; -import { ShapeUtils } from '../ShapeUtils'; /************************************************************** * Quadratic Bezier curve @@ -22,8 +22,6 @@ QuadraticBezierCurve.prototype.constructor = QuadraticBezierCurve; QuadraticBezierCurve.prototype.getPoint = function ( t ) { - var b2 = ShapeUtils.b2; - return new Vector2( b2( t, this.v0.x, this.v1.x, this.v2.x ), b2( t, this.v0.y, this.v1.y, this.v2.y ) diff --git a/src/extras/curves/QuadraticBezierCurve3.js b/src/extras/curves/QuadraticBezierCurve3.js index 40a27f84b3..75cb8d6244 100644 --- a/src/extras/curves/QuadraticBezierCurve3.js +++ b/src/extras/curves/QuadraticBezierCurve3.js @@ -1,6 +1,6 @@ -import { Vector3 } from '../../math/Vector3'; -import { ShapeUtils } from '../ShapeUtils'; +import { b2 } from '../core/Bezier'; import { Curve } from '../core/Curve'; +import { Vector3 } from '../../math/Vector3'; /************************************************************** * Quadratic Bezier 3D curve @@ -18,8 +18,6 @@ var QuadraticBezierCurve3 = Curve.create( function ( t ) { - var b2 = ShapeUtils.b2; - return new Vector3( b2( t, this.v0.x, this.v1.x, this.v2.x ), b2( t, this.v0.y, this.v1.y, this.v2.y ), -- GitLab