I have a div block:
<div id="container"></div>
There is an SVG path inside this block with SVG circles. When I do click, I get relative coordinates to id="container". The top-left corner is 0,0.
The SVG element is always full width, height of container. But the SVG has its own view port.
How can I recalculate all points of the circle (x,y) of SVG to relative coordinates of #container?
<div id="container">
<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 2000 1000" width="2000px" height="1000px">
<circle cx="100" cy="100" r="10" fill="#4285f4"></circle>
</svg>
</div>
I need to know the relative coordinates of each of circle, relative to the parent div.
JavaScript click is:
document.getElementById('container').onclick = function clickEvent(e) {
var rect = e.target.getBoundingClientRect();
var x = e.clientX - rect.left; //x position within the element.
var y = e.clientY - rect.top; //y position within the element.
console.log("Left? : " + x + " ; Top? : " + y + ".");
}
My attempt: https://jsfiddle.net/3g8un5w1/5/
I want to be able to transform in between position of the circle on canvas and mouse pointer position even when it's scaled via css.