curveto.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. import { Curve } from '../curve';
  2. import { Point } from '../point';
  3. import { Segment } from './segment';
  4. export class CurveTo extends Segment {
  5. constructor(arg0, arg1, arg2, arg3, arg4, arg5) {
  6. super();
  7. if (Curve.isCurve(arg0)) {
  8. this.controlPoint1 = arg0.controlPoint1.clone().round(2);
  9. this.controlPoint2 = arg0.controlPoint2.clone().round(2);
  10. this.endPoint = arg0.end.clone().round(2);
  11. }
  12. else if (typeof arg0 === 'number') {
  13. this.controlPoint1 = new Point(arg0, arg1).round(2);
  14. this.controlPoint2 = new Point(arg2, arg3).round(2);
  15. this.endPoint = new Point(arg4, arg5).round(2);
  16. }
  17. else {
  18. this.controlPoint1 = Point.create(arg0).round(2);
  19. this.controlPoint2 = Point.create(arg1).round(2);
  20. this.endPoint = Point.create(arg2).round(2);
  21. }
  22. }
  23. get type() {
  24. return 'C';
  25. }
  26. get curve() {
  27. return new Curve(this.start, this.controlPoint1, this.controlPoint2, this.end);
  28. }
  29. bbox() {
  30. return this.curve.bbox();
  31. }
  32. closestPoint(p) {
  33. return this.curve.closestPoint(p);
  34. }
  35. closestPointLength(p) {
  36. return this.curve.closestPointLength(p);
  37. }
  38. closestPointNormalizedLength(p) {
  39. return this.curve.closestPointNormalizedLength(p);
  40. }
  41. closestPointTangent(p) {
  42. return this.curve.closestPointTangent(p);
  43. }
  44. length() {
  45. return this.curve.length();
  46. }
  47. divideAt(ratio, options = {}) {
  48. // TODO: fix options
  49. const divided = this.curve.divideAt(ratio, options);
  50. return [new CurveTo(divided[0]), new CurveTo(divided[1])];
  51. }
  52. divideAtLength(length, options = {}) {
  53. // TODO: fix options
  54. const divided = this.curve.divideAtLength(length, options);
  55. return [new CurveTo(divided[0]), new CurveTo(divided[1])];
  56. }
  57. divideAtT(t) {
  58. const divided = this.curve.divideAtT(t);
  59. return [new CurveTo(divided[0]), new CurveTo(divided[1])];
  60. }
  61. getSubdivisions() {
  62. return [];
  63. }
  64. pointAt(ratio) {
  65. return this.curve.pointAt(ratio);
  66. }
  67. pointAtLength(length) {
  68. return this.curve.pointAtLength(length);
  69. }
  70. tangentAt(ratio) {
  71. return this.curve.tangentAt(ratio);
  72. }
  73. tangentAtLength(length) {
  74. return this.curve.tangentAtLength(length);
  75. }
  76. isDifferentiable() {
  77. if (!this.previousSegment) {
  78. return false;
  79. }
  80. const start = this.start;
  81. const control1 = this.controlPoint1;
  82. const control2 = this.controlPoint2;
  83. const end = this.end;
  84. return !(start.equals(control1) &&
  85. control1.equals(control2) &&
  86. control2.equals(end));
  87. }
  88. scale(sx, sy, origin) {
  89. this.controlPoint1.scale(sx, sy, origin);
  90. this.controlPoint2.scale(sx, sy, origin);
  91. this.end.scale(sx, sy, origin);
  92. return this;
  93. }
  94. rotate(angle, origin) {
  95. this.controlPoint1.rotate(angle, origin);
  96. this.controlPoint2.rotate(angle, origin);
  97. this.end.rotate(angle, origin);
  98. return this;
  99. }
  100. translate(tx, ty) {
  101. if (typeof tx === 'number') {
  102. this.controlPoint1.translate(tx, ty);
  103. this.controlPoint2.translate(tx, ty);
  104. this.end.translate(tx, ty);
  105. }
  106. else {
  107. this.controlPoint1.translate(tx);
  108. this.controlPoint2.translate(tx);
  109. this.end.translate(tx);
  110. }
  111. return this;
  112. }
  113. equals(s) {
  114. return (this.start.equals(s.start) &&
  115. this.end.equals(s.end) &&
  116. this.controlPoint1.equals(s.controlPoint1) &&
  117. this.controlPoint2.equals(s.controlPoint2));
  118. }
  119. clone() {
  120. return new CurveTo(this.controlPoint1, this.controlPoint2, this.end);
  121. }
  122. toJSON() {
  123. return {
  124. type: this.type,
  125. start: this.start.toJSON(),
  126. controlPoint1: this.controlPoint1.toJSON(),
  127. controlPoint2: this.controlPoint2.toJSON(),
  128. end: this.end.toJSON(),
  129. };
  130. }
  131. serialize() {
  132. const c1 = this.controlPoint1;
  133. const c2 = this.controlPoint2;
  134. const end = this.end;
  135. return [this.type, c1.x, c1.y, c2.x, c2.y, end.x, end.y].join(' ');
  136. }
  137. }
  138. (function (CurveTo) {
  139. function create(...args) {
  140. const len = args.length;
  141. const arg0 = args[0];
  142. // curve provided
  143. if (Curve.isCurve(arg0)) {
  144. return new CurveTo(arg0);
  145. }
  146. // points provided
  147. if (Point.isPointLike(arg0)) {
  148. if (len === 3) {
  149. return new CurveTo(args[0], args[1], args[2]);
  150. }
  151. // this is a poly-bezier segment
  152. const segments = [];
  153. for (let i = 0; i < len; i += 3) {
  154. segments.push(new CurveTo(args[i], args[i + 1], args[i + 2]));
  155. }
  156. return segments;
  157. }
  158. // coordinates provided
  159. if (len === 6) {
  160. return new CurveTo(args[0], args[1], args[2], args[3], args[4], args[5]);
  161. }
  162. // this is a poly-bezier segment
  163. const segments = [];
  164. for (let i = 0; i < len; i += 6) {
  165. segments.push(new CurveTo(args[i], args[i + 1], args[i + 2], args[i + 3], args[i + 4], args[i + 5]));
  166. }
  167. return segments;
  168. }
  169. CurveTo.create = create;
  170. })(CurveTo || (CurveTo = {}));
  171. //# sourceMappingURL=curveto.js.map