Shape.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. import { Path } from './Path.js';
  2. import { MathUtils } from '../../math/MathUtils.js';
  3. function Shape( points ) {
  4. Path.call( this, points );
  5. this.uuid = MathUtils.generateUUID();
  6. this.type = 'Shape';
  7. this.holes = [];
  8. }
  9. Shape.prototype = Object.assign( Object.create( Path.prototype ), {
  10. constructor: Shape,
  11. getPointsHoles: function ( divisions ) {
  12. const holesPts = [];
  13. for ( let i = 0, l = this.holes.length; i < l; i ++ ) {
  14. holesPts[ i ] = this.holes[ i ].getPoints( divisions );
  15. }
  16. return holesPts;
  17. },
  18. // get points of shape and holes (keypoints based on segments parameter)
  19. extractPoints: function ( divisions ) {
  20. return {
  21. shape: this.getPoints( divisions ),
  22. holes: this.getPointsHoles( divisions )
  23. };
  24. },
  25. copy: function ( source ) {
  26. Path.prototype.copy.call( this, source );
  27. this.holes = [];
  28. for ( let i = 0, l = source.holes.length; i < l; i ++ ) {
  29. const hole = source.holes[ i ];
  30. this.holes.push( hole.clone() );
  31. }
  32. return this;
  33. },
  34. toJSON: function () {
  35. const data = Path.prototype.toJSON.call( this );
  36. data.uuid = this.uuid;
  37. data.holes = [];
  38. for ( let i = 0, l = this.holes.length; i < l; i ++ ) {
  39. const hole = this.holes[ i ];
  40. data.holes.push( hole.toJSON() );
  41. }
  42. return data;
  43. },
  44. fromJSON: function ( json ) {
  45. Path.prototype.fromJSON.call( this, json );
  46. this.uuid = json.uuid;
  47. this.holes = [];
  48. for ( let i = 0, l = json.holes.length; i < l; i ++ ) {
  49. const hole = json.holes[ i ];
  50. this.holes.push( new Path().fromJSON( hole ) );
  51. }
  52. return this;
  53. }
  54. } );
  55. export { Shape };
粤ICP备19079148号