SPath2D.js 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.SPath2D = void 0;
  4. var index_1 = require("./index");
  5. var lib_1 = require("@persagy-web/base/lib");
  6. var SPath2D = (function () {
  7. function SPath2D() {
  8. this._path = new Path2D();
  9. this.svgBuilder = new lib_1.SStringBuilder();
  10. }
  11. SPath2D.prototype.svgPath = function () {
  12. return this.svgBuilder.toString(" ");
  13. };
  14. SPath2D.prototype.addPath = function (path) {
  15. this._path.addPath(path._path);
  16. this.svgBuilder.append(path.svgBuilder.toString(" "));
  17. };
  18. SPath2D.prototype.moveTo = function (x, y) {
  19. this._path.moveTo(x, y);
  20. this.svgBuilder.append("M " + x + " " + y);
  21. };
  22. SPath2D.prototype.lineTo = function (x, y) {
  23. this._path.lineTo(x, y);
  24. this.svgBuilder.append("L " + x + " " + y);
  25. };
  26. SPath2D.prototype.rect = function (x, y, width, height) {
  27. var r = x + width;
  28. var b = y + height;
  29. this._path.moveTo(x, y);
  30. this._path.lineTo(r, y);
  31. this._path.lineTo(r, b);
  32. this._path.lineTo(x, b);
  33. this._path.closePath();
  34. this.svgBuilder.append("M " + x + " " + y + " L" + r + " " + y + " L" + r + " " + b + " L" + x + " " + b + " Z");
  35. };
  36. SPath2D.prototype.polyline = function (points) {
  37. var _this = this;
  38. if (points.length < 2) {
  39. return;
  40. }
  41. points.map(function (it, index) {
  42. if (index == 0) {
  43. _this._path.moveTo(it.x, it.y);
  44. _this.svgBuilder.append("M " + it.x + " " + it.y);
  45. }
  46. else {
  47. _this._path.lineTo(it.x, it.y);
  48. _this.svgBuilder.append("L " + it.x + " " + it.y);
  49. }
  50. });
  51. };
  52. SPath2D.prototype.polygon = function (points) {
  53. var _this = this;
  54. if (points.length < 3) {
  55. return;
  56. }
  57. points.map(function (it, index) {
  58. if (index == 0) {
  59. _this._path.moveTo(it.x, it.y);
  60. _this.svgBuilder.append("M " + it.x + " " + it.y);
  61. }
  62. else {
  63. _this._path.lineTo(it.x, it.y);
  64. _this.svgBuilder.append("L " + it.x + " " + it.y);
  65. }
  66. });
  67. this._path.closePath();
  68. this.svgBuilder.append("Z");
  69. };
  70. SPath2D.arc = function (x, y, width, height, startAngle, endAngle) {
  71. var a = width / 2;
  72. var b = height / 2;
  73. var p1 = SPath2D.arcPoint(a, b, startAngle);
  74. var p2 = SPath2D.arcPoint(a, b, endAngle);
  75. var large = SPath2D.largeArcFlag(startAngle, endAngle);
  76. p1.x = x + a + p1.x;
  77. p1.y = y + b - p1.y;
  78. p2.x = x + a + p2.x;
  79. p2.y = y + b - p2.y;
  80. return "M " + p1.x + " " + p1.y + " A " + a + " " + b + " 0 " + large + " 0 " + p2.x + " " + p2.y;
  81. };
  82. SPath2D.chord = function (x, y, width, height, startAngle, endAngle) {
  83. var a = width / 2;
  84. var b = height / 2;
  85. var p1 = SPath2D.arcPoint(a, b, startAngle);
  86. var p2 = SPath2D.arcPoint(a, b, endAngle);
  87. var large = SPath2D.largeArcFlag(startAngle, endAngle);
  88. p1.x = x + a + p1.x;
  89. p1.y = y + b - p1.y;
  90. p2.x = x + a + p2.x;
  91. p2.y = y + b - p2.y;
  92. return "M " + p1.x + " " + p1.y + " A " + a + " " + b + " 0 " + large + " 0 " + p2.x + " " + p2.y + " Z";
  93. };
  94. SPath2D.pie = function (x, y, width, height, startAngle, endAngle) {
  95. var a = width / 2;
  96. var b = height / 2;
  97. var p1 = SPath2D.arcPoint(a, b, startAngle);
  98. var p2 = SPath2D.arcPoint(a, b, endAngle);
  99. var large = SPath2D.largeArcFlag(startAngle, endAngle);
  100. p1.x = x + a + p1.x;
  101. p1.y = y + b - p1.y;
  102. p2.x = x + a + p2.x;
  103. p2.y = y + b - p2.y;
  104. return "M " + (x + a) + " " + (y + b) + " L " + p1.x + " " + p1.y + " A " + a + " " + b + " 0 " + large + " 0 " + p2.x + " " + p2.y + " Z";
  105. };
  106. SPath2D.largeArcFlag = function (startAngle, endAngle) {
  107. return ((Math.abs(endAngle - startAngle) * 180) / Math.PI) % 360 >
  108. 179.99999
  109. ? 1
  110. : 0;
  111. };
  112. SPath2D.arcPoint = function (a, b, angle) {
  113. var p = new index_1.SPoint();
  114. if (angle >= 0) {
  115. angle = angle - Math.floor(angle / Math.PI / 2) * 2 * Math.PI;
  116. }
  117. else {
  118. angle = angle + Math.ceil(-angle / Math.PI / 2) * 2 * Math.PI;
  119. }
  120. if (Math.abs(Math.PI / 2 - angle) <= 0.000001) {
  121. p.x = 0;
  122. p.y = b;
  123. }
  124. else if (Math.abs((Math.PI * 3) / 2 - angle) <= 0.000001) {
  125. p.x = 0;
  126. p.y = -b;
  127. }
  128. else if (Math.PI / 2 < angle && angle < (Math.PI * 3) / 2) {
  129. p.x =
  130. -(a * b) /
  131. Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle));
  132. p.y =
  133. -(a * b * Math.tan(angle)) /
  134. Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle));
  135. }
  136. else {
  137. p.x =
  138. (a * b) /
  139. Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle));
  140. p.y =
  141. (a * b * Math.tan(angle)) /
  142. Math.sqrt(b * b + a * a * Math.tan(angle) * Math.tan(angle));
  143. }
  144. return p;
  145. };
  146. return SPath2D;
  147. }());
  148. exports.SPath2D = SPath2D;