curveto.js 5.7 KB

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