CubicBezierCurve.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. import { Curve } from '../core/Curve.js';
  2. import { CubicBezier } from '../core/Interpolations.js';
  3. import { Vector2 } from '../../math/Vector2.js';
  4. class CubicBezierCurve extends Curve {
  5. constructor( v0 = new Vector2(), v1 = new Vector2(), v2 = new Vector2(), v3 = new Vector2() ) {
  6. super();
  7. this.isCubicBezierCurve = true;
  8. this.type = 'CubicBezierCurve';
  9. this.v0 = v0;
  10. this.v1 = v1;
  11. this.v2 = v2;
  12. this.v3 = v3;
  13. }
  14. getPoint( t, optionalTarget = new Vector2() ) {
  15. const point = optionalTarget;
  16. const v0 = this.v0, v1 = this.v1, v2 = this.v2, v3 = this.v3;
  17. point.set(
  18. CubicBezier( t, v0.x, v1.x, v2.x, v3.x ),
  19. CubicBezier( t, v0.y, v1.y, v2.y, v3.y )
  20. );
  21. return point;
  22. }
  23. copy( source ) {
  24. super.copy( source );
  25. this.v0.copy( source.v0 );
  26. this.v1.copy( source.v1 );
  27. this.v2.copy( source.v2 );
  28. this.v3.copy( source.v3 );
  29. return this;
  30. }
  31. toJSON() {
  32. const data = super.toJSON();
  33. data.v0 = this.v0.toArray();
  34. data.v1 = this.v1.toArray();
  35. data.v2 = this.v2.toArray();
  36. data.v3 = this.v3.toArray();
  37. return data;
  38. }
  39. fromJSON( json ) {
  40. super.fromJSON( json );
  41. this.v0.fromArray( json.v0 );
  42. this.v1.fromArray( json.v1 );
  43. this.v2.fromArray( json.v2 );
  44. this.v3.fromArray( json.v3 );
  45. return this;
  46. }
  47. }
  48. export { CubicBezierCurve };