我试图用d3.js为svg:image (嵌入到SVG中的图像)创建圆角。我找不到如何正确地设置图片样式,因为根据W3C规范,我应该能够使用CSS,但相邻边框和圆角边缘对我来说是有效的。
提前谢谢。
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
发布于 2011-09-15 22:21:05
‘’border radius‘不适用于svg:image元素(目前还没有)。一种解决方法是创建一个圆角矩形,并使用剪辑路径。
An example。
源代码的相关部分:
<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元素,而不是使用<use>。
发布于 2018-02-24 22:29:38
对于那些只对制作圆形头像感兴趣的人,这里有一个使用d3 v4的示例。请注意,您需要在图像位于(0,0)位置时应用剪裁,因此需要将图像转换()到您想要的位置。
import { select } from 'd3-selection';
const AVATAR_WIDTH = 80;
const avatarRadius = AVATAR_WIDTH / 2;
const svg = select('.my-container');
const defs = this.svg.append("defs");
defs.append("clipPath")
.attr("id", "avatar-clip")
.append("circle")
.attr("cx", avatarRadius)
.attr("cy", avatarRadius)
.attr("r", avatarRadius)
svg.append("image")
.attr("x", 0)
.attr("y", 0)
.attr("width", AVATAR_WIDTH)
.attr("height", AVATAR_WIDTH)
.attr("xlink:href", myAvatarUrl)
.attr("clip-path", "url(#avatar-clip)")
.attr("transform", "translate(posx, posy)")
.append('My username')发布于 2020-05-15 08:19:51
另一个简单的选择:
将html <img>标记包装在<foreignObject>标记中。这允许您使用普通的html样式:
<foreignObject x='0' y='0' width='100px' height='100px'>
<img
width='100px'
height='100px'
src={'path/to/image.png'}
style={{ borderRadius: '50%' }}
/>
</foreignObject>https://stackoverflow.com/questions/7430580
复制相似问题