The implementation of getBoundingClientRect()
varies from browser to browser when it comes to a SVG path with a given stroke width, with browsers like Firefox including the stroke width (unlike Chrome, for instance).
By "including the stroke width" I'd expect just that, the bounding box containing the path with its stroke but nothing else. However, as you can see in this basic example, it seems that in Firefox the stroke-width is creating an additional border around the whole element, with twice the value of the stroke-width itself:
console.log(document.querySelector("path").getBoundingClientRect())
path {
stroke: black;
stroke-width: 30px;
}
<svg>
<path d="M0,20 L300,20"></path>
</svg>
This is a screenshot of the very snippet above, inspecting the element:
As you can see, not only getBoundingClientRect()
but even the dev tools inspector is getting the bounding rect wrong (420x120
instead of 300x30
. It's worth mentioning that Chrome and Safari will return 300x0
, as described in the first paragraph).
Is this the expected behaviour?