d3-path.js 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // https://d3js.org/d3-path/ v1.0.9 Copyright 2019 Mike Bostock
  2. (function (global, factory) {
  3. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  4. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  5. (global = global || self, factory(global.d3 = global.d3 || {}));
  6. }(this, function (exports) { 'use strict';
  7. var pi = Math.PI,
  8. tau = 2 * pi,
  9. epsilon = 1e-6,
  10. tauEpsilon = tau - epsilon;
  11. function Path() {
  12. this._x0 = this._y0 = // start of current subpath
  13. this._x1 = this._y1 = null; // end of current subpath
  14. this._ = "";
  15. }
  16. function path() {
  17. return new Path;
  18. }
  19. Path.prototype = path.prototype = {
  20. constructor: Path,
  21. moveTo: function(x, y) {
  22. this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y);
  23. },
  24. closePath: function() {
  25. if (this._x1 !== null) {
  26. this._x1 = this._x0, this._y1 = this._y0;
  27. this._ += "Z";
  28. }
  29. },
  30. lineTo: function(x, y) {
  31. this._ += "L" + (this._x1 = +x) + "," + (this._y1 = +y);
  32. },
  33. quadraticCurveTo: function(x1, y1, x, y) {
  34. this._ += "Q" + (+x1) + "," + (+y1) + "," + (this._x1 = +x) + "," + (this._y1 = +y);
  35. },
  36. bezierCurveTo: function(x1, y1, x2, y2, x, y) {
  37. this._ += "C" + (+x1) + "," + (+y1) + "," + (+x2) + "," + (+y2) + "," + (this._x1 = +x) + "," + (this._y1 = +y);
  38. },
  39. arcTo: function(x1, y1, x2, y2, r) {
  40. x1 = +x1, y1 = +y1, x2 = +x2, y2 = +y2, r = +r;
  41. var x0 = this._x1,
  42. y0 = this._y1,
  43. x21 = x2 - x1,
  44. y21 = y2 - y1,
  45. x01 = x0 - x1,
  46. y01 = y0 - y1,
  47. l01_2 = x01 * x01 + y01 * y01;
  48. // Is the radius negative? Error.
  49. if (r < 0) throw new Error("negative radius: " + r);
  50. // Is this path empty? Move to (x1,y1).
  51. if (this._x1 === null) {
  52. this._ += "M" + (this._x1 = x1) + "," + (this._y1 = y1);
  53. }
  54. // Or, is (x1,y1) coincident with (x0,y0)? Do nothing.
  55. else if (!(l01_2 > epsilon));
  56. // Or, are (x0,y0), (x1,y1) and (x2,y2) collinear?
  57. // Equivalently, is (x1,y1) coincident with (x2,y2)?
  58. // Or, is the radius zero? Line to (x1,y1).
  59. else if (!(Math.abs(y01 * x21 - y21 * x01) > epsilon) || !r) {
  60. this._ += "L" + (this._x1 = x1) + "," + (this._y1 = y1);
  61. }
  62. // Otherwise, draw an arc!
  63. else {
  64. var x20 = x2 - x0,
  65. y20 = y2 - y0,
  66. l21_2 = x21 * x21 + y21 * y21,
  67. l20_2 = x20 * x20 + y20 * y20,
  68. l21 = Math.sqrt(l21_2),
  69. l01 = Math.sqrt(l01_2),
  70. l = r * Math.tan((pi - Math.acos((l21_2 + l01_2 - l20_2) / (2 * l21 * l01))) / 2),
  71. t01 = l / l01,
  72. t21 = l / l21;
  73. // If the start tangent is not coincident with (x0,y0), line to.
  74. if (Math.abs(t01 - 1) > epsilon) {
  75. this._ += "L" + (x1 + t01 * x01) + "," + (y1 + t01 * y01);
  76. }
  77. this._ += "A" + r + "," + r + ",0,0," + (+(y01 * x20 > x01 * y20)) + "," + (this._x1 = x1 + t21 * x21) + "," + (this._y1 = y1 + t21 * y21);
  78. }
  79. },
  80. arc: function(x, y, r, a0, a1, ccw) {
  81. x = +x, y = +y, r = +r, ccw = !!ccw;
  82. var dx = r * Math.cos(a0),
  83. dy = r * Math.sin(a0),
  84. x0 = x + dx,
  85. y0 = y + dy,
  86. cw = 1 ^ ccw,
  87. da = ccw ? a0 - a1 : a1 - a0;
  88. // Is the radius negative? Error.
  89. if (r < 0) throw new Error("negative radius: " + r);
  90. // Is this path empty? Move to (x0,y0).
  91. if (this._x1 === null) {
  92. this._ += "M" + x0 + "," + y0;
  93. }
  94. // Or, is (x0,y0) not coincident with the previous point? Line to (x0,y0).
  95. else if (Math.abs(this._x1 - x0) > epsilon || Math.abs(this._y1 - y0) > epsilon) {
  96. this._ += "L" + x0 + "," + y0;
  97. }
  98. // Is this arc empty? We’re done.
  99. if (!r) return;
  100. // Does the angle go the wrong way? Flip the direction.
  101. if (da < 0) da = da % tau + tau;
  102. // Is this a complete circle? Draw two arcs to complete the circle.
  103. if (da > tauEpsilon) {
  104. this._ += "A" + r + "," + r + ",0,1," + cw + "," + (x - dx) + "," + (y - dy) + "A" + r + "," + r + ",0,1," + cw + "," + (this._x1 = x0) + "," + (this._y1 = y0);
  105. }
  106. // Is this arc non-empty? Draw an arc!
  107. else if (da > epsilon) {
  108. this._ += "A" + r + "," + r + ",0," + (+(da >= pi)) + "," + cw + "," + (this._x1 = x + r * Math.cos(a1)) + "," + (this._y1 = y + r * Math.sin(a1));
  109. }
  110. },
  111. rect: function(x, y, w, h) {
  112. this._ += "M" + (this._x0 = this._x1 = +x) + "," + (this._y0 = this._y1 = +y) + "h" + (+w) + "v" + (+h) + "h" + (-w) + "Z";
  113. },
  114. toString: function() {
  115. return this._;
  116. }
  117. };
  118. exports.path = path;
  119. Object.defineProperty(exports, '__esModule', { value: true });
  120. }));