Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Src/extras/core: move to es6 classes [3] #21206

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 32 additions & 34 deletions src/extras/core/CurvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,26 @@ 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() {

this.type = 'CurvePath';
super();

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

}

CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {
this.curves = [];
this.autoClose = false; // Automatically closes the path

constructor: CurvePath,
}

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 +37,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 +48,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 +78,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 +130,9 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

return lengths;

},
}

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

const points = [];

Expand All @@ -152,9 +150,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 +188,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 +208,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 +226,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 @@ -248,7 +246,7 @@ CurvePath.prototype = Object.assign( Object.create( Curve.prototype ), {

}

} );
}


export { CurvePath };
79 changes: 38 additions & 41 deletions src/extras/core/Path.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,27 +6,24 @@ 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 ) {

this.type = 'Path';
super();
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 +35,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 +54,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 +70,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 +87,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 +100,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 +112,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 +131,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 +157,39 @@ 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