获得SVG / G元素的实际大小 [英] Get the real size of a SVG/G element

查看:428
本文介绍了获得SVG / G元素的实际大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有任何准确的方法来获取svg元素的实际大小,其中包括笔画,滤镜或其他元素,这些元素在Javascript中有助于元素的实际大小?

Is there any accurate way to get the real size of a svg element that includes stroke, filters or other elements contributing to the element's real size from within Javascript?

我已经尝试了很多东西,现在我觉得我已经走到了尽头: - (

I have tried pretty much everything coming to my mind and now I feel I'm coming to a dead end :-(

更新问题以添加更多上下文(Javascript)

推荐答案

你不能直接获取值。但是,您可以获得边界矩形的尺寸:

You can't get the values directly. However, you can get the dimensions of the bounding rectangle:

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);

至少在实际版本中支持它所有主要浏览器。

It is supported at least in the actual versions of all major browser.

这篇关于获得SVG / G元素的实际大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆