SVG getScreenCTM
const el = document.body.appendChild(
document.createElement`div`
);
el.innerHTML = `
<svg width="200" height="200" viewBox="0 0 200 200">
<rect
class="rect"
transform="translate(50, 50) scale(1.2) rotate(25)"
fill="purple"
x="0" y="0" width="50" height="50" />
</svg>
`;
const box = document.body.appendChild(
document.createElement`div`
);
Object.assign(box.style, {
position: 'absolute',
left: 0, top: 0,
width: '50px',
height: '50px',
transformOrigin: '0 0',
outline: '5px solid red'
});
const rect = document.querySelector('.rect');
const {a, b, c, d, e, f} = rect.getScreenCTM()
box.style.transform = `
matrix(${[a, b, c, d, e, f]})
`;
The transformation matrix of an SVG element can be obtained using getScreenCTM
or getCTM
. The latter of which will be relative to the SVG coordinate space, vs the coordinate space of the page.
Here we take the matrix data from getScreenCTM
and use it on a div to place a border over an SVG rect
node. This is great for layering HTML on top of SVG.