17782: removes linter warnings for cwl-svg code.
[arvados-workbench2.git] / src / lib / cwl-svg / utils / geometry.ts
1 export class Geometry {
2
3     static distance(x1: number, y1: number, x2: number, y2: number) {
4         return Math.sqrt(Math.pow(x2 - x1, 2) + Math.pow(y2 - y1, 2));
5     }
6
7     static getTransformToElement(from: SVGElement, to: SVGElement) {
8         const getPosition = (node: SVGGElement, addE = 0, addF = 0): SVGMatrix => {
9
10             if (!node.ownerSVGElement) {
11                 // node is the root svg element
12                 const matrix = (node as SVGSVGElement).createSVGMatrix();
13                 matrix.e = addE;
14                 matrix.f = addF;
15                 return matrix;
16             } else {
17                 // node still has parent elements
18                 const {e, f} = node.transform.baseVal.getItem(0).matrix;
19                 return getPosition(node.parentNode as SVGGElement, e + addE, f + addF);
20             }
21         };
22
23         const toPosition = getPosition(to as SVGAElement);
24         const fromPosition = getPosition(from as SVGAElement);
25
26         const result = from.ownerSVGElement!.createSVGMatrix();
27         result.e = toPosition.e - fromPosition.e;
28         result.f = toPosition.f - fromPosition.f;
29
30         return result.inverse();
31     }
32 }