我对SVG
不太熟悉,如果这是一个初学者问题,那就不好意思了。 我正在尝试弄清楚如何使图像显示为引用图像的完整宽度和高度。 我正在使用D3.js
构建一个图形,我想让一个标志出现在角落里。 问题是,图像href
将使用JavaScript设置,因此被引用的图像永远不是固定的宽度或高度。 我想要的是图像以其完整的宽度和高度显示,而不是缩放。 我希望能够自动设置图像的宽度和高度,基于其内容,例如将width
和height
属性设置为auto
。但是这只会导致图像不显示。
d3.select("body")
.append("svg")
.attr('id','mySVG')
.attr({
"width": "100%",
"height": "100%"
})
.attr("viewBox", "0 0 " + width + " " + height )
.attr("preserveAspectRatio", "none");
d3.select('#mySVG')
.append('image')
.attr('image-rendering','optimizeQuality')
.attr('height','auto') <--- This does not work
.attr('width','auto') <--- This does not work
.attr('xlink:href', logoUrl)
.attr('x','0')
.attr('y','0');
我该如何指定SVG图像的宽度
和高度
,以便根据引用图像的大小动态确定?
html
的img
标签吗?如果可能的话,我更喜欢在SVG
中保留解决方案。希望将图像嵌入到正在构建的图表的SVG
中。 - BruceHill