All files / jianpu-renderer/utils SVGHelper.ts

0% Statements 0/41
0% Branches 0/1
0% Functions 0/1
0% Lines 0/41

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42                                                                                   
/**
 * SVG辅助函数
 */

export class SVGHelper {
  private static readonly SVG_NS = 'http://www.w3.org/2000/svg';
  
  /**
   * 创建SVG元素
   */
  static createElement(tag: string): SVGElement {
    return document.createElementNS(SVGHelper.SVG_NS, tag);
  }
  
  /**
   * 创建SVG组
   */
  static createGroup(id?: string, classes?: string[]): SVGGElement {
    const g = SVGHelper.createElement('g') as SVGGElement;
    if (id) g.id = id;
    if (classes) classes.forEach(cls => g.classList.add(cls));
    return g;
  }
  
  /**
   * 创建SVG文本
   */
  static createText(
    x: number,
    y: number,
    content: string,
    fontSize: number = 14
  ): SVGTextElement {
    const text = SVGHelper.createElement('text') as SVGTextElement;
    text.setAttribute('x', x.toString());
    text.setAttribute('y', y.toString());
    text.setAttribute('font-size', fontSize.toString());
    text.textContent = content;
    return text;
  }
}