Path.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. import { Vector2 } from '../../math/Vector2.js';
  2. import { CurvePath } from './CurvePath.js';
  3. import { EllipseCurve } from '../curves/EllipseCurve.js';
  4. import { SplineCurve } from '../curves/SplineCurve.js';
  5. import { CubicBezierCurve } from '../curves/CubicBezierCurve.js';
  6. import { QuadraticBezierCurve } from '../curves/QuadraticBezierCurve.js';
  7. import { LineCurve } from '../curves/LineCurve.js';
  8. class Path extends CurvePath {
  9. constructor( points ) {
  10. super();
  11. this.type = 'Path';
  12. this.currentPoint = new Vector2();
  13. if ( points ) {
  14. this.setFromPoints( points );
  15. }
  16. }
  17. setFromPoints( points ) {
  18. this.moveTo( points[ 0 ].x, points[ 0 ].y );
  19. for ( let i = 1, l = points.length; i < l; i ++ ) {
  20. this.lineTo( points[ i ].x, points[ i ].y );
  21. }
  22. return this;
  23. }
  24. moveTo( x, y ) {
  25. this.currentPoint.set( x, y ); // TODO consider referencing vectors instead of copying?
  26. return this;
  27. }
  28. lineTo( x, y ) {
  29. const curve = new LineCurve( this.currentPoint.clone(), new Vector2( x, y ) );
  30. this.curves.push( curve );
  31. this.currentPoint.set( x, y );
  32. return this;
  33. }
  34. quadraticCurveTo( aCPx, aCPy, aX, aY ) {
  35. const curve = new QuadraticBezierCurve(
  36. this.currentPoint.clone(),
  37. new Vector2( aCPx, aCPy ),
  38. new Vector2( aX, aY )
  39. );
  40. this.curves.push( curve );
  41. this.currentPoint.set( aX, aY );
  42. return this;
  43. }
  44. bezierCurveTo( aCP1x, aCP1y, aCP2x, aCP2y, aX, aY ) {
  45. const curve = new CubicBezierCurve(
  46. this.currentPoint.clone(),
  47. new Vector2( aCP1x, aCP1y ),
  48. new Vector2( aCP2x, aCP2y ),
  49. new Vector2( aX, aY )
  50. );
  51. this.curves.push( curve );
  52. this.currentPoint.set( aX, aY );
  53. return this;
  54. }
  55. splineThru( pts /*Array of Vector*/ ) {
  56. const npts = [ this.currentPoint.clone() ].concat( pts );
  57. const curve = new SplineCurve( npts );
  58. this.curves.push( curve );
  59. this.currentPoint.copy( pts[ pts.length - 1 ] );
  60. return this;
  61. }
  62. arc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  63. const x0 = this.currentPoint.x;
  64. const y0 = this.currentPoint.y;
  65. this.absarc( aX + x0, aY + y0, aRadius,
  66. aStartAngle, aEndAngle, aClockwise );
  67. return this;
  68. }
  69. absarc( aX, aY, aRadius, aStartAngle, aEndAngle, aClockwise ) {
  70. this.absellipse( aX, aY, aRadius, aRadius, aStartAngle, aEndAngle, aClockwise );
  71. return this;
  72. }
  73. ellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
  74. const x0 = this.currentPoint.x;
  75. const y0 = this.currentPoint.y;
  76. this.absellipse( aX + x0, aY + y0, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
  77. return this;
  78. }
  79. absellipse( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation ) {
  80. const curve = new EllipseCurve( aX, aY, xRadius, yRadius, aStartAngle, aEndAngle, aClockwise, aRotation );
  81. if ( this.curves.length > 0 ) {
  82. // if a previous curve is present, attempt to join
  83. const firstPoint = curve.getPoint( 0 );
  84. if ( ! firstPoint.equals( this.currentPoint ) ) {
  85. this.lineTo( firstPoint.x, firstPoint.y );
  86. }
  87. }
  88. this.curves.push( curve );
  89. const lastPoint = curve.getPoint( 1 );
  90. this.currentPoint.copy( lastPoint );
  91. return this;
  92. }
  93. copy( source ) {
  94. super.copy( source );
  95. this.currentPoint.copy( source.currentPoint );
  96. return this;
  97. }
  98. toJSON() {
  99. const data = super.toJSON();
  100. data.currentPoint = this.currentPoint.toArray();
  101. return data;
  102. }
  103. fromJSON( json ) {
  104. super.fromJSON( json );
  105. this.currentPoint.fromArray( json.currentPoint );
  106. return this;
  107. }
  108. }
  109. export { Path };