Bin
2025-12-16 9e0b2ba2c317b1a86212f24cbae3195ad1f3dbfa
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
interface TransformCoordinate {
  x: number;
  y: number;
}
 
/**
 * Utility class for handling SVG transform operations
 * Supports parsing transform strings, combining matrices, and applying transformations to coordinates
 */
export class SVGTransformUtils {
  /**
   * Extract and combine transform matrix for element relative to parent
   */
  static getTransformMatrix(element: SVGElement, parent: Element): DOMMatrix | null {
    try {
      let combinedMatrix = new DOMMatrix();
      let currentElement: SVGElement | null = element;
 
      // Walk up the DOM tree to collect all transforms
      while (currentElement && currentElement !== parent) {
        const transform = currentElement.getAttribute("transform");
        const transformOrigin = currentElement.getAttribute("transform-origin");
 
        if (transform) {
          const elementMatrix = SVGTransformUtils.parseTransformString(transform, transformOrigin);
          if (elementMatrix) {
            // Multiply matrices (order matters: parent transforms are applied first)
            combinedMatrix = elementMatrix.multiply(combinedMatrix);
          }
        }
 
        currentElement = currentElement.parentElement as unknown as SVGElement;
      }
 
      return combinedMatrix.isIdentity ? null : combinedMatrix;
    } catch (error) {
      console.error(`Error getting transform matrix: ${error}`);
      return null;
    }
  }
 
  /**
   * Parse transform string to DOMMatrix
   */
  static parseTransformString(transformStr: string, transformOrigin?: string | null): DOMMatrix | null {
    try {
      const matrix = new DOMMatrix();
 
      // Handle transform-origin by translating to origin, applying transform, then translating back
      let originX = 0;
      let originY = 0;
 
      if (transformOrigin) {
        const origins = transformOrigin.split(/\s+/);
        originX = Number.parseFloat(origins[0]) || 0;
        originY = Number.parseFloat(origins[1]) || 0;
      }
 
      // Apply transform-origin offset
      if (originX !== 0 || originY !== 0) {
        matrix.translateSelf(originX, originY);
      }
 
      // Parse and apply transforms
      const transforms = transformStr.match(/(\w+)\s*\([^)]*\)/g) || [];
 
      for (const transform of transforms) {
        const [, type, params] = transform.match(/(\w+)\s*\(([^)]*)\)/) || [];
        if (!type || !params) continue;
 
        const values = params
          .split(/[\s,]+/)
          .map((v) => Number.parseFloat(v.trim()))
          .filter((v) => !isNaN(v));
 
        switch (type.toLowerCase()) {
          case "translate":
            matrix.translateSelf(values[0] || 0, values[1] || 0);
            break;
          case "translatex":
            matrix.translateSelf(values[0] || 0, 0);
            break;
          case "translatey":
            matrix.translateSelf(0, values[0] || 0);
            break;
          case "scale":
            matrix.scaleSelf(values[0] || 1, values[1] || values[0] || 1);
            break;
          case "scalex":
            matrix.scaleSelf(values[0] || 1, 1);
            break;
          case "scaley":
            matrix.scaleSelf(1, values[0] || 1);
            break;
          case "rotate":
            matrix.rotateSelf(values[0] || 0, values[1] || 0, values[2] || 0);
            break;
          case "skewx":
            matrix.skewXSelf(values[0] || 0);
            break;
          case "skewy":
            matrix.skewYSelf(values[0] || 0);
            break;
          case "matrix":
            if (values.length >= 6) {
              matrix.multiplySelf(new DOMMatrix([values[0], values[1], values[2], values[3], values[4], values[5]]));
            }
            break;
        }
      }
 
      // Apply inverse transform-origin offset
      if (originX !== 0 || originY !== 0) {
        matrix.translateSelf(-originX, -originY);
      }
 
      return matrix;
    } catch (error) {
      console.error(`Error parsing transform string "${transformStr}": ${error}`);
      return null;
    }
  }
 
  /**
   * Apply transform matrix to a coordinate point
   */
  static applyTransform(coord: TransformCoordinate, matrix: DOMMatrix): TransformCoordinate {
    const point = new DOMPoint(coord.x, coord.y);
    const transformedPoint = point.matrixTransform(matrix);
 
    return {
      x: transformedPoint.x,
      y: transformedPoint.y,
    };
  }
 
  /**
   * Apply transform matrix to multiple coordinate points
   */
  static applyTransformToCoordinates(coords: TransformCoordinate[], matrix: DOMMatrix | null): TransformCoordinate[] {
    return matrix ? coords.map((coord) => SVGTransformUtils.applyTransform(coord, matrix)) : coords;
  }
}
 
export type { TransformCoordinate };