close.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. import { Line } from '../line';
  2. import { LineTo } from './lineto';
  3. import { Segment } from './segment';
  4. export class Close extends Segment {
  5. get end() {
  6. if (!this.subpathStartSegment) {
  7. throw new Error('Missing subpath start segment. (This segment needs a subpath ' +
  8. 'start segment (e.g. MoveTo), or segment has not yet been added' +
  9. ' to a path.)');
  10. }
  11. return this.subpathStartSegment.end;
  12. }
  13. get type() {
  14. return 'Z';
  15. }
  16. get line() {
  17. return new Line(this.start, this.end);
  18. }
  19. bbox() {
  20. return this.line.bbox();
  21. }
  22. closestPoint(p) {
  23. return this.line.closestPoint(p);
  24. }
  25. closestPointLength(p) {
  26. return this.line.closestPointLength(p);
  27. }
  28. closestPointNormalizedLength(p) {
  29. return this.line.closestPointNormalizedLength(p);
  30. }
  31. closestPointTangent(p) {
  32. return this.line.closestPointTangent(p);
  33. }
  34. length() {
  35. return this.line.length();
  36. }
  37. divideAt(ratio) {
  38. const divided = this.line.divideAt(ratio);
  39. return [
  40. // do not actually cut into the segment, first divided part can stay as Z
  41. divided[1].isDifferentiable() ? new LineTo(divided[0]) : this.clone(),
  42. new LineTo(divided[1]),
  43. ];
  44. }
  45. divideAtLength(length) {
  46. const divided = this.line.divideAtLength(length);
  47. return [
  48. divided[1].isDifferentiable() ? new LineTo(divided[0]) : this.clone(),
  49. new LineTo(divided[1]),
  50. ];
  51. }
  52. getSubdivisions() {
  53. return [];
  54. }
  55. pointAt(ratio) {
  56. return this.line.pointAt(ratio);
  57. }
  58. pointAtLength(length) {
  59. return this.line.pointAtLength(length);
  60. }
  61. tangentAt(ratio) {
  62. return this.line.tangentAt(ratio);
  63. }
  64. tangentAtLength(length) {
  65. return this.line.tangentAtLength(length);
  66. }
  67. isDifferentiable() {
  68. if (!this.previousSegment || !this.subpathStartSegment) {
  69. return false;
  70. }
  71. return !this.start.equals(this.end);
  72. }
  73. scale() {
  74. return this;
  75. }
  76. rotate() {
  77. return this;
  78. }
  79. translate() {
  80. return this;
  81. }
  82. equals(s) {
  83. return (this.type === s.type &&
  84. this.start.equals(s.start) &&
  85. this.end.equals(s.end));
  86. }
  87. clone() {
  88. return new Close();
  89. }
  90. toJSON() {
  91. return {
  92. type: this.type,
  93. start: this.start.toJSON(),
  94. end: this.end.toJSON(),
  95. };
  96. }
  97. serialize() {
  98. return this.type;
  99. }
  100. }
  101. (function (Close) {
  102. function create() {
  103. return new Close();
  104. }
  105. Close.create = create;
  106. })(Close || (Close = {}));
  107. //# sourceMappingURL=close.js.map