Skip to content

Commit

Permalink
old changes from PR mrdoob#20656 minus font.js
Browse files Browse the repository at this point in the history
  • Loading branch information
DefinitelyMaybe committed Feb 4, 2021
1 parent 60d51c3 commit 9ad8182
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 126 deletions.
69 changes: 32 additions & 37 deletions src/extras/core/CurvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,24 @@ import * as Curves from '../curves/Curves.js';
* curves, but retains the api of a curve
**************************************************************/

function CurvePath() {
class CurvePath extends Curve {

Curve.call( this );
constructor() {
super();

this.type = 'CurvePath';
this.type = 'CurvePath';

this.curves = [];
this.autoClose = false; // Automatically closes the path

}

CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

constructor: CurvePath,
this.curves = [];
this.autoClose = false; // Automatically closes the path
}

add: function ( curve ) {
add( curve ) {

this.curves.push( curve );

},
}

closePath: function () {
closePath() {

// Add a line curve if start and end of lines are not connected
const startPoint = this.curves[ 0 ].getPoint( 0 );
Expand All @@ -39,7 +35,7 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

}

},
}

// To get accurate point with reference to
// entire path distance at time t,
Expand All @@ -50,7 +46,7 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
// 3. Get t for the curve
// 4. Return curve.getPointAt(t')

getPoint: function ( t ) {
getPoint( t ) {

const d = t * this.getLength();
const curveLengths = this.getCurveLengths();
Expand Down Expand Up @@ -80,32 +76,32 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

// loop where sum != 0, sum > d , sum+1 <d

},
}

// We cannot use the default THREE.Curve getPoint() with getLength() because in
// THREE.Curve, getLength() depends on getPoint() but in THREE.CurvePath
// getPoint() depends on getLength

getLength: function () {
getLength() {

const lens = this.getCurveLengths();
return lens[ lens.length - 1 ];

},
}

// cacheLengths must be recalculated.
updateArcLengths: function () {
updateArcLengths() {

this.needsUpdate = true;
this.cacheLengths = null;
this.getCurveLengths();

},
}

// Compute lengths and cache them
// We cannot overwrite getLengths() because UtoT mapping uses it.

getCurveLengths: function () {
getCurveLengths() {

// We use cache values if curves and cache array are same length

Expand All @@ -132,9 +128,9 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

return lengths;

},
}

getSpacedPoints: function ( divisions = 40 ) {
getSpacedPoints( divisions = 40 ) {

const points = [];

Expand All @@ -152,9 +148,9 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

return points;

},
}

getPoints: function ( divisions = 12 ) {
getPoints( divisions = 12 ) {

const points = [];
let last;
Expand Down Expand Up @@ -190,11 +186,11 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

return points;

},
}

copy: function ( source ) {
copy( source ) {

Curve.prototype.copy.call( this, source );
super.copy( source );

this.curves = [];

Expand All @@ -210,11 +206,11 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

return this;

},
}

toJSON: function () {
toJSON() {

const data = Curve.prototype.toJSON.call( this );
const data = super.toJSON( );

data.autoClose = this.autoClose;
data.curves = [];
Expand All @@ -228,11 +224,11 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

return data;

},
}

fromJSON: function ( json ) {
fromJSON( json ) {

Curve.prototype.fromJSON.call( this, json );
super.fromJSON( json );

this.autoClose = json.autoClose;
this.curves = [];
Expand All @@ -247,8 +243,7 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
return this;

}

} );
}


export { CurvePath };
80 changes: 37 additions & 43 deletions src/extras/core/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,22 @@ import { CubicBezierCurve } from '../curves/CubicBezierCurve.js';
import { QuadraticBezierCurve } from '../curves/QuadraticBezierCurve.js';
import { LineCurve } from '../curves/LineCurve.js';

function Path( points ) {
class Path extends CurvePath {

CurvePath.call( this );
constructor ( points ) {
super()
this.type = 'Path';

this.type = 'Path';
this.currentPoint = new Vector2();

this.currentPoint = new Vector2();
if ( points ) {

if ( points ) {

this.setFromPoints( points );
this.setFromPoints( points );

}
}

}

Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

constructor: Path,

setFromPoints: function ( points ) {
setFromPoints( points ) {

this.moveTo( points[ 0 ].x, points[ 0 ].y );

Expand All @@ -38,17 +33,17 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

moveTo: function ( x, y ) {
moveTo( x, y ) {

this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?

return this;

},
}

lineTo: function ( x, y ) {
lineTo( x, y ) {

const curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
this.curves.push( curve );
Expand All @@ -57,9 +52,9 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

quadraticCurveTo: function ( aCPx, aCPy, aX, aY ) {
quadraticCurveTo( aCPx, aCPy, aX, aY ) {

const curve = new QuadraticBezierCurve(
this.currentPoint.clone(),
Expand All @@ -73,9 +68,9 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

bezierCurveTo: function ( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {

const curve = new CubicBezierCurve(
this.currentPoint.clone(),
Expand All @@ -90,9 +85,9 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

splineThru: function ( pts /*Array of Vector*/ ) {
splineThru( pts /*Array of Vector*/ ) {

const npts = [ this.currentPoint.clone() ].concat( pts );

Expand All @@ -103,9 +98,9 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

arc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
arc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {

const x0 = this.currentPoint.x;
const y0 = this.currentPoint.y;
Expand All @@ -115,17 +110,17 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

absarc: function ( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
absarc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {

this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );

return this;

},
}

ellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
ellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {

const x0 = this.currentPoint.x;
const y0 = this.currentPoint.y;
Expand All @@ -134,9 +129,9 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

absellipse: function ( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
absellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {

const curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );

Expand All @@ -160,39 +155,38 @@ Path.prototype = Object.assign( Object.create( CurvePath.prototype ), {

return this;

},
}

copy: function ( source ) {
copy( source ) {

CurvePath.prototype.copy.call( this, source );
super.copy( source );

this.currentPoint.copy( source.currentPoint );

return this;

},
}

toJSON: function () {
toJSON() {

const data = CurvePath.prototype.toJSON.call( this );
const data = super.toJSON( );

data.currentPoint = this.currentPoint.toArray();

return data;

},
}

fromJSON: function ( json ) {
fromJSON( json ) {

CurvePath.prototype.fromJSON.call( this, json );
super.fromJSON( json );

this.currentPoint.fromArray( json.currentPoint );

return this;

}

} );
}


export { Path };
Loading

0 comments on commit 9ad8182

Please sign in to comment.