javascript - 从javascript中的字符串确定svg元素的边界框
问题描述
如果我在 chrome 中加载 SVG,我可以得到它的元素的边界框,如下所示:
document.getElementById('char1').getBBox()
->
SVGRect {x: 348.5628356933594, y: 78.95916748046875, width: 202.74807739257812, height: 845.1696166992188}
但是,如果我使用 DOMParser 加载相同的 SVG,我会得到一个值为 0 的边界框。
例如:
const svgString = document.documentElement.outerHTML // in practice this would be an svg fetched from elsewhere
let svgElement = new DOMParser().parseFromString(svgString, 'text/xml').documentElement
svgElement.getElementById('char1').getBBox()
->
SVGRect {x: 0, y: 0, width: 0, height: 0}
a) 为什么会这样?b)如何通过使用 svg 字符串而不将其加载到文档中来获得正确的边界框值?
解决方案
推荐阅读
- sql - 在 SQL 中应用动态 between 语句
- r - ggplot - 条件背景
- sql-server - SQL Server Express 2016 用户在 SSMS 中工作,但不能通过内部 IP 从应用程序中工作
- java - 我有一个包含在 ScrollView 中的 ListView 以在标题下显示字符串列表,但无法正确显示
- javascript - 如何通过 javascript 拦截所有 AJAX(xhr 和 fetch) 请求?
- lua - LUA 字符串,删除非字母数字或空格
- r - 我的循环正常运行,但在单个文件上突然失败
- cron - 等待 5 分钟 ---- 运行作业 ---- 作业完成 ---- 等待 5 分钟 .... 等等
- python - 如何从列表列表中创建矩阵彩色图(Python)
- javascript - 在制作动画时对值进行 React Native 访问