ChatGPT解决这个技术问题 Extra ChatGPT

Get width/height of SVG element

What is the proper way to get the dimensions of an svg element?

http://jsfiddle.net/langdonx/Xkv3X/

Chrome 28:

style x
client 300x100
offset 300x100

IE 10:

stylex 
client300x100 
offsetundefinedxundefined 

FireFox 23:

"style" "x"
"client" "0x0"
"offset" "undefinedxundefined"

There are width and height properties on svg1, but .width.baseVal.value is only set if I set the width and height attributes on the element.

The fiddle looks like this:

HTML

<svg id="svg1" xmlns="http://www.w3.org/2000/svg" version="1.1">
    <circle cx="50" cy="50" r="40" stroke="black" stroke-width="1" fill="red" />
    <circle cx="150" cy="50" r="40" stroke="black" stroke-width="1" fill="green" />
    <circle cx="250" cy="50" r="40" stroke="black" stroke-width="1" fill="blue" />
</svg>

JS

var svg1 = document.getElementById('svg1');

console.log(svg1);
console.log('style', svg1.style.width + 'x' + svg1.style.height);
console.log('client', svg1.clientWidth + 'x' + svg1.clientHeight);
console.log('offset', svg1.offsetWidth + 'x' + svg1.offsetHeight);

CSS

#svg1 {
    width: 300px;
    height: 100px;
}

B
Behrang

Use the getBBox function:

The SVGGraphicsElement.getBBox() method allows us to determine the coordinates of the smallest rectangle in which the object fits. [...]

http://jsfiddle.net/Xkv3X/1/

var bBox = svg1.getBBox();
console.log('XxY', bBox.x + 'x' + bBox.y);
console.log('size', bBox.width + 'x' + bBox.height);

Is there no way to treat it like an HTML element and get the actual size the svg element is occupying despite what has been drawn? jsfiddle.net/Xkv3X/4
Btw, I'm trying to determine the size of the canvas that I can draw on before rendering shapes.
I think it might be better to check available size of container (the div which contains your svg) then you can set appropriate size to you svg
@Langdon that's just been fixed in Firefox. Firefox 33 will report something similar to Chrome.
Today I reported a problem with getBoundingClientRect for FireFox 38 when symbol/use are used. In this case getBoundingClientRect extends the element to the border of viewBox. Also there is a known issue about stroke-width. So getBoundingClientRect is not cross browser solution for now.
o
obysky

FireFox have problemes for getBBox(), i need to do this in vanillaJS.

I've a better Way and is the same result as real svg.getBBox() function !

With this good post : Get the real size of a SVG/G element

var el   = document.getElementById("yourElement"); // or other selector like querySelector()
var rect = el.getBoundingClientRect(); // get the bounding rectangle

console.log( rect.width );
console.log( rect.height);

this one ! as said firefox has problems yet this works in most browsers :)
returns zero when the svg is hidden(for example in bootstrap tab)
caniuse.com/#feat=getboundingclientrect - all looks okay now, FF12+, Chrome 4+
C
Cichelero

I'm using Firefox, and my working solution is very close to obysky. The only difference is that the method you call in an svg element will return multiple rects and you need to select the first one.

var chart = document.getElementsByClassName("chart")[0];
var width = chart.getClientRects()[0].width;
var height = chart.getClientRects()[0].height;

This is the way to get the size before CSS transform is applied.
a
afkatja

SVG has properties width and height. They return an object SVGAnimatedLength with two properties: animVal and baseVal. This interface is used for animation, where baseVal is the value before animation. From what I can see, this method returns consistent values in both Chrome and Firefox, so I think it can also be used to get calculated size of SVG.


where svg is your element... let svgWidth = svg.width.baseVal.value ; let svgHeight = svg.height.baseVal.value ;
Unfortunately this doesn't work on Firefox at the moment - it'll give you an initial value but if you change the svg dimensions and try it again it gives you the original value.
S
Sergio

This is the consistent cross-browser way I found:

var heightComponents = ['height', 'paddingTop', 'paddingBottom', 'borderTopWidth', 'borderBottomWidth'],
    widthComponents = ['width', 'paddingLeft', 'paddingRight', 'borderLeftWidth', 'borderRightWidth'];

var svgCalculateSize = function (el) {

    var gCS = window.getComputedStyle(el), // using gCS because IE8- has no support for svg anyway
        bounds = {
            width: 0,
            height: 0
        };

    heightComponents.forEach(function (css) { 
        bounds.height += parseFloat(gCS[css]);
    });
    widthComponents.forEach(function (css) {
        bounds.width += parseFloat(gCS[css]);
    });
    return bounds;
};

R
Robert Longson

From Firefox 33 onwards you can call getBoundingClientRect() and it will work normally, i.e. in the question above it will return 300 x 100.

Firefox 33 will be released on 14th October 2014 but the fix is already in Firefox nightlies if you want to try it out.


P
Piuksu

Use .getAttribute()!

var height = document.getElementById('rect').getAttribute("height"); var width = document.getElementById('rect').getAttribute("width"); var x = document.getElementById('rect').getAttribute("x"); alert("height: " + height + ", width: " + width + ", x: " + x);


M
Martin Wantke

A save method to determine the width and height unit of any element (no padding, no margin) is the following:

let div   = document.querySelector("div");
let style = getComputedStyle(div);

let width  = parseFloat(style.width.replace("px", ""));
let height = parseFloat(style.height.replace("px", ""));