Path.js 3.7 KB

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