SVGPathRebuilder.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  1. import { isAroundZero } from './helper.js';
  2. var mathSin = Math.sin;
  3. var mathCos = Math.cos;
  4. var PI = Math.PI;
  5. var PI2 = Math.PI * 2;
  6. var degree = 180 / PI;
  7. var SVGPathRebuilder = (function () {
  8. function SVGPathRebuilder() {
  9. }
  10. SVGPathRebuilder.prototype.reset = function (precision) {
  11. this._start = true;
  12. this._d = [];
  13. this._str = '';
  14. this._p = Math.pow(10, precision || 4);
  15. };
  16. SVGPathRebuilder.prototype.moveTo = function (x, y) {
  17. this._add('M', x, y);
  18. };
  19. SVGPathRebuilder.prototype.lineTo = function (x, y) {
  20. this._add('L', x, y);
  21. };
  22. SVGPathRebuilder.prototype.bezierCurveTo = function (x, y, x2, y2, x3, y3) {
  23. this._add('C', x, y, x2, y2, x3, y3);
  24. };
  25. SVGPathRebuilder.prototype.quadraticCurveTo = function (x, y, x2, y2) {
  26. this._add('Q', x, y, x2, y2);
  27. };
  28. SVGPathRebuilder.prototype.arc = function (cx, cy, r, startAngle, endAngle, anticlockwise) {
  29. this.ellipse(cx, cy, r, r, 0, startAngle, endAngle, anticlockwise);
  30. };
  31. SVGPathRebuilder.prototype.ellipse = function (cx, cy, rx, ry, psi, startAngle, endAngle, anticlockwise) {
  32. var dTheta = endAngle - startAngle;
  33. var clockwise = !anticlockwise;
  34. var dThetaPositive = Math.abs(dTheta);
  35. var isCircle = isAroundZero(dThetaPositive - PI2)
  36. || (clockwise ? dTheta >= PI2 : -dTheta >= PI2);
  37. var unifiedTheta = dTheta > 0 ? dTheta % PI2 : (dTheta % PI2 + PI2);
  38. var large = false;
  39. if (isCircle) {
  40. large = true;
  41. }
  42. else if (isAroundZero(dThetaPositive)) {
  43. large = false;
  44. }
  45. else {
  46. large = (unifiedTheta >= PI) === !!clockwise;
  47. }
  48. var x0 = cx + rx * mathCos(startAngle);
  49. var y0 = cy + ry * mathSin(startAngle);
  50. if (this._start) {
  51. this._add('M', x0, y0);
  52. }
  53. var xRot = Math.round(psi * degree);
  54. if (isCircle) {
  55. var p = 1 / this._p;
  56. var dTheta_1 = (clockwise ? 1 : -1) * (PI2 - p);
  57. this._add('A', rx, ry, xRot, 1, +clockwise, cx + rx * mathCos(startAngle + dTheta_1), cy + ry * mathSin(startAngle + dTheta_1));
  58. if (p > 1e-2) {
  59. this._add('A', rx, ry, xRot, 0, +clockwise, x0, y0);
  60. }
  61. }
  62. else {
  63. var x = cx + rx * mathCos(endAngle);
  64. var y = cy + ry * mathSin(endAngle);
  65. this._add('A', rx, ry, xRot, +large, +clockwise, x, y);
  66. }
  67. };
  68. SVGPathRebuilder.prototype.rect = function (x, y, w, h) {
  69. this._add('M', x, y);
  70. this._add('l', w, 0);
  71. this._add('l', 0, h);
  72. this._add('l', -w, 0);
  73. this._add('Z');
  74. };
  75. SVGPathRebuilder.prototype.closePath = function () {
  76. if (this._d.length > 0) {
  77. this._add('Z');
  78. }
  79. };
  80. SVGPathRebuilder.prototype._add = function (cmd, a, b, c, d, e, f, g, h) {
  81. var vals = [];
  82. var p = this._p;
  83. for (var i = 1; i < arguments.length; i++) {
  84. var val = arguments[i];
  85. if (isNaN(val)) {
  86. this._invalid = true;
  87. return;
  88. }
  89. vals.push(Math.round(val * p) / p);
  90. }
  91. this._d.push(cmd + vals.join(' '));
  92. this._start = cmd === 'Z';
  93. };
  94. SVGPathRebuilder.prototype.generateStr = function () {
  95. this._str = this._invalid ? '' : this._d.join('');
  96. this._d = [];
  97. };
  98. SVGPathRebuilder.prototype.getStr = function () {
  99. return this._str;
  100. };
  101. return SVGPathRebuilder;
  102. }());
  103. export default SVGPathRebuilder;