使用背景图像填充SVG路径元素 [英] Fill SVG path element with a background-image

查看:124
本文介绍了使用背景图像填充SVG路径元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否可以为svg 路径设置 backgound-image

Is it possible to set a backgound-image for a svg path element?

例如,如果我将元素的类设置为 wall ,则css样式 .wall {fill:red; } ,但 .wall {background-image:url(wall.jpg)} {background-color:red;}

For instance, if I set the element's class to wall, the css style .wall {fill: red;} works, but .wall{background-image: url(wall.jpg)} does not, neither .wall {background-color: red;}.

推荐答案

模式

<defs>
  <pattern id="img1" patternUnits="userSpaceOnUse" width="100" height="100">
    <image xlink:href="wall.jpg" x="0" y="0" width="100" height="100" />
  </pattern>
</defs>

根据您的图像调整宽度和高度,然后从路径中引用如下: p>

Adjust the width and height according to your image, then reference it from the path like this:

<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)" />

工作示例

这篇关于使用背景图像填充SVG路径元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆