Sin descripción

index.ts 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. /** 通用的 Svg 助手 */
  2. export class SvgHelper {
  3. public static createRect = (
  4. width: number | string,
  5. height: number | string,
  6. attributes?: Array<[string, string]>
  7. ): SVGRectElement => {
  8. const rect = document.createElementNS('http://www.w3.org/2000/svg', 'rect');
  9. rect.setAttribute('width', width.toString());
  10. rect.setAttribute('height', height.toString());
  11. if (attributes) {
  12. SvgHelper.setAttributes(rect, attributes);
  13. }
  14. return rect;
  15. };
  16. public static createLine = (
  17. x1: number | string,
  18. y1: number | string,
  19. x2: number | string,
  20. y2: number | string,
  21. attributes?: Array<[string, string]>
  22. ): SVGLineElement => {
  23. const line = document.createElementNS('http://www.w3.org/2000/svg', 'line');
  24. line.setAttribute('x1', x1.toString());
  25. line.setAttribute('y1', y1.toString());
  26. line.setAttribute('x2', x2.toString());
  27. line.setAttribute('y2', y2.toString());
  28. if (attributes) {
  29. SvgHelper.setAttributes(line, attributes);
  30. }
  31. return line;
  32. };
  33. public static createPolygon = (
  34. points: string,
  35. attributes?: Array<[string, string]>
  36. ): SVGPolygonElement => {
  37. const polygon = document.createElementNS('http://www.w3.org/2000/svg', 'polygon');
  38. polygon.setAttribute('points', points);
  39. if (attributes) {
  40. SvgHelper.setAttributes(polygon, attributes);
  41. }
  42. return polygon;
  43. };
  44. public static createCircle = (
  45. radius: number,
  46. attributes?: Array<[string, string]>
  47. ): SVGCircleElement => {
  48. const circle = document.createElementNS('http://www.w3.org/2000/svg', 'circle');
  49. circle.setAttribute('cx', (radius / 2).toString());
  50. circle.setAttribute('cy', (radius / 2).toString());
  51. circle.setAttribute('r', radius.toString());
  52. if (attributes) {
  53. SvgHelper.setAttributes(circle, attributes);
  54. }
  55. return circle;
  56. };
  57. public static createGroup = (attributes?: Array<[string, string]>): SVGGElement => {
  58. const g = document.createElementNS('http://www.w3.org/2000/svg', 'g');
  59. if (attributes) {
  60. SvgHelper.setAttributes(g, attributes);
  61. }
  62. return g;
  63. };
  64. public static setAttributes = (el: SVGElement, attributes: Array<[string, string]>) => {
  65. for (const [attr, value] of attributes) {
  66. el.setAttribute(attr, value);
  67. }
  68. };
  69. public static createTransform = (): SVGTransform => {
  70. const svg = document.createElementNS('http://www.w3.org/2000/svg', 'svg');
  71. return svg.createSVGTransform();
  72. };
  73. public static createDefs = (): SVGDefsElement => {
  74. const defs = document.createElementNS('http://www.w3.org/2000/svg', 'defs');
  75. return defs;
  76. };
  77. public static createMarker = (
  78. id: string,
  79. orient: string,
  80. markerWidth: number | string,
  81. markerHeight: number | string,
  82. refX: number | string,
  83. refY: number | string,
  84. markerElement: SVGGraphicsElement
  85. ): SVGMarkerElement => {
  86. const marker = document.createElementNS('http://www.w3.org/2000/svg', 'marker');
  87. SvgHelper.setAttributes(marker, [
  88. ['id', id],
  89. ['orient', orient],
  90. ['markerWidth', markerWidth.toString()],
  91. ['markerHeight', markerHeight.toString()],
  92. ['refX', refX.toString()],
  93. ['refY', refY.toString()]
  94. ]);
  95. marker.appendChild(markerElement);
  96. return marker;
  97. };
  98. public static createText = (attributes?: Array<[string, string]>): SVGTextElement => {
  99. const text = document.createElementNS('http://www.w3.org/2000/svg', 'text');
  100. text.setAttribute('x', '0');
  101. text.setAttribute('y', '0');
  102. if (attributes) {
  103. SvgHelper.setAttributes(text, attributes);
  104. }
  105. return text;
  106. };
  107. public static createTSpan = (
  108. text: string,
  109. attributes?: Array<[string, string]>
  110. ): SVGTSpanElement => {
  111. const tspan = document.createElementNS('http://www.w3.org/2000/svg', 'tspan');
  112. tspan.textContent = text;
  113. if (attributes) {
  114. SvgHelper.setAttributes(tspan, attributes);
  115. }
  116. return tspan;
  117. };
  118. }