2
0

Shape.js 1.4 KB

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