function svgTransform(graph) {
const k = graph.store.transform[0];
const dx = graph.store.transform[6];
const dy = graph.store.transform[7];
const s = graph.config.spaceSize;
const screenSize = graph.store.screenSize;
const aspectRatio = graph.store.screenSize[1] / graph.store.screenSize[0];
const canvasWidthInSpace = s / k;
const canvasHeightInSpace = canvasWidthInSpace * aspectRatio;
const t = {
translate: {
x: (dx * screenSize[0] / 2) + (screenSize[0] / 2),
y: - (dy * screenSize[1] / 2) + (screenSize[1] / 2)
},
scale: {
x: k,
y: -k
},
aspectRatio,
transform: graph.store.transform
}
t.string = `translate(${t.translate.x} ${t.translate.y}) scale(${t.scale.x} ${t.scale.y})`
t.coordinatesToClip = (point) => {
return [
(point[0] - s/2) * aspectRatio / canvasWidthInSpace + dx,
(point[1] - s/2) / canvasWidthInSpace + dy,
]
}
t.coordinatesToPixels = (point, width, height) =>{
const p1 = t.coordinatesToClip(point);
return [
(p1[0] / 2 + 0.5) * width,
(-p1[1] / 2 + 0.5) * height,
]
}
return t
}