moveto.js 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. import { Line } from '../line';
  2. import { Curve } from '../curve';
  3. import { Point } from '../point';
  4. import { LineTo } from './lineto';
  5. import { Segment } from './segment';
  6. export class MoveTo extends Segment {
  7. constructor(x, y) {
  8. super();
  9. this.isVisible = false;
  10. this.isSubpathStart = true;
  11. if (Line.isLine(x) || Curve.isCurve(x)) {
  12. this.endPoint = x.end.clone().round(2);
  13. }
  14. else {
  15. this.endPoint = Point.create(x, y).round(2);
  16. }
  17. }
  18. get start() {
  19. throw new Error('Illegal access. Moveto segments should not need a start property.');
  20. }
  21. get type() {
  22. return 'M';
  23. }
  24. bbox() {
  25. return null;
  26. }
  27. closestPoint() {
  28. return this.end.clone();
  29. }
  30. closestPointLength() {
  31. return 0;
  32. }
  33. closestPointNormalizedLength() {
  34. return 0;
  35. }
  36. closestPointT() {
  37. return 1;
  38. }
  39. closestPointTangent() {
  40. return null;
  41. }
  42. length() {
  43. return 0;
  44. }
  45. lengthAtT() {
  46. return 0;
  47. }
  48. divideAt() {
  49. return [this.clone(), this.clone()];
  50. }
  51. divideAtLength() {
  52. return [this.clone(), this.clone()];
  53. }
  54. getSubdivisions() {
  55. return [];
  56. }
  57. pointAt() {
  58. return this.end.clone();
  59. }
  60. pointAtLength() {
  61. return this.end.clone();
  62. }
  63. pointAtT() {
  64. return this.end.clone();
  65. }
  66. tangentAt() {
  67. return null;
  68. }
  69. tangentAtLength() {
  70. return null;
  71. }
  72. tangentAtT() {
  73. return null;
  74. }
  75. isDifferentiable() {
  76. return false;
  77. }
  78. scale(sx, sy, origin) {
  79. this.end.scale(sx, sy, origin);
  80. return this;
  81. }
  82. rotate(angle, origin) {
  83. this.end.rotate(angle, origin);
  84. return this;
  85. }
  86. translate(tx, ty) {
  87. if (typeof tx === 'number') {
  88. this.end.translate(tx, ty);
  89. }
  90. else {
  91. this.end.translate(tx);
  92. }
  93. return this;
  94. }
  95. clone() {
  96. return new MoveTo(this.end);
  97. }
  98. equals(s) {
  99. return this.type === s.type && this.end.equals(s.end);
  100. }
  101. toJSON() {
  102. return {
  103. type: this.type,
  104. end: this.end.toJSON(),
  105. };
  106. }
  107. serialize() {
  108. const end = this.end;
  109. return `${this.type} ${end.x} ${end.y}`;
  110. }
  111. }
  112. (function (MoveTo) {
  113. function create(...args) {
  114. const len = args.length;
  115. const arg0 = args[0];
  116. // line provided
  117. if (Line.isLine(arg0)) {
  118. return new MoveTo(arg0);
  119. }
  120. // curve provided
  121. if (Curve.isCurve(arg0)) {
  122. return new MoveTo(arg0);
  123. }
  124. // points provided
  125. if (Point.isPointLike(arg0)) {
  126. if (len === 1) {
  127. return new MoveTo(arg0);
  128. }
  129. // this is a moveto-with-subsequent-poly-line segment
  130. const segments = [];
  131. // points come one by one
  132. for (let i = 0; i < len; i += 1) {
  133. if (i === 0) {
  134. segments.push(new MoveTo(args[i]));
  135. }
  136. else {
  137. segments.push(new LineTo(args[i]));
  138. }
  139. }
  140. return segments;
  141. }
  142. // coordinates provided
  143. if (len === 2) {
  144. return new MoveTo(+args[0], +args[1]);
  145. }
  146. // this is a moveto-with-subsequent-poly-line segment
  147. const segments = [];
  148. for (let i = 0; i < len; i += 2) {
  149. const x = +args[i];
  150. const y = +args[i + 1];
  151. if (i === 0) {
  152. segments.push(new MoveTo(x, y));
  153. }
  154. else {
  155. segments.push(new LineTo(x, y));
  156. }
  157. }
  158. return segments;
  159. }
  160. MoveTo.create = create;
  161. })(MoveTo || (MoveTo = {}));
  162. //# sourceMappingURL=moveto.js.map