Fill SVG path element with a background-image

后端 未结 1 642
遥遥无期
遥遥无期 2020-11-22 01:22

Is it possible to set a background-image for an SVG element?

For instance, if I set the element class=\"wall\", t

相关标签:
1条回答
  • 2020-11-22 01:34

    You can do it by making the background into a pattern:

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

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

    Working example

    0 讨论(0)
提交回复
热议问题