能否为SVG <path>
元素设置 background-image
背景图片呢?
例如,如果我给元素设置了 class="wall"
,CSS样式 .wall {fill: red;}
可以生效,但是 .wall{background-image: url(wall.jpg)}
和 .wall {background-color: red;}
都不能生效。
能否为SVG <path>
元素设置 background-image
背景图片呢?
例如,如果我给元素设置了 class="wall"
,CSS样式 .wall {fill: red;}
可以生效,但是 .wall{background-image: url(wall.jpg)}
和 .wall {background-color: red;}
都不能生效。
你可以通过将背景制作成图案来实现:
<defs>
<pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
<image href="wall.jpg" x="0" y="0" width="100" height="100" />
</pattern>
</defs>
根据您的图像调整宽度和高度,然后像这样从路径引用它:
<path d="M5,50
l0,100 l100,0 l0,-100 l-100,0
M215,100
a50,50 0 1 1 -100,0 50,50 0 1 1 100,0
M265,50
l50,100 l-100,0 l50,-100
z"
fill="url(#img1)" />
data:image/png;base64,iVBORw0KGgoAA
代替 wall.jpg 吗? - Christophxlink:href
自 SVG 2 起已被弃用,现在应该直接使用 href
。https://developer.mozilla.org/zh-CN/docs/Web/SVG/Attribute/xlink:href - Blake Regalia