为 svg:image 设置圆角 [英] Setting rounded corners for svg:image
问题描述
我试图用 d3.js 为 s svg:image(嵌入在 SVG 中的图像)制作圆角.我不知道如何正确设置图像样式,因为根据 W3C 规范,我应该能够使用 CSS,但是更窄的边框或圆角边缘对我有用.
I was trying to make rounded corners for s svg:image (image embedded in SVG) with d3.js. I can't find out how to properly style the image, because according to W3C spec I should be able to use CSS, but neighter border nor rounded edges work for me.
提前致谢.
vis.append("svg:image")
.attr("width", width/3)
.attr("height", height-10)
.attr("y", 5)
.attr("x", 5)
.attr("style", "border:1px solid black;border-radius: 15px;")
.attr("xlink:href",
"http://www.acuteaday.com/blog/wp-content/uploads/2011/03/koala-australia-big.jpg");
已测试浏览器:Firefox、Chrome
Browsers tested: Firefox, Chrome
推荐答案
'border-radius' 不适用于 svg:image 元素(无论如何).一种解决方法是创建一个带圆角的矩形,并使用剪辑路径.
'border-radius' doesn't apply to svg:image elements (yet anyway). A workaround would be to create a rect with rounded corners, and use a clip-path.
一个例子.
相关部分源码:
<defs>
<rect id="rect" x="25%" y="25%" width="50%" height="50%" rx="15"/>
<clipPath id="clip">
<use xlink:href="#rect"/>
</clipPath>
</defs>
<use xlink:href="#rect" stroke-width="2" stroke="black"/>
<image xlink:href="boston.jpg" width="100%" height="100%" clip-path="url(#clip)"/>
也可以创建多个 rect 元素而不是使用 .
It's also possible to create several rect elements instead of using <use>
.
这篇关于为 svg:image 设置圆角的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!