Path.js 3.8 KB

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