3

I tried putting an image inside a polygon by using patterns, but it doesn't work. Is there any way to fill this?

<svg tabindex="1" style="width: 175px; height: 216.506px;">

  <polygon points="25,0 75,0 100,43 75,86 25,86 0,43" class="hexfield" tabindex="1" hex-row="0" hex-column="0"></polygon>
  <polygon points="100,44 150,44 175,87 150,130 100,130 75,87" class="hexfield" tabindex="1" hex-row="0" hex-column="1"></polygon>
  <polygon points="25,87 75,87 100,130 75,173 25,173 0,130" class="hexfield" tabindex="1" hex-row="1" hex-column="0"></polygon>
  <polygon points="100,130 150,130 175,173 150,216 100,216 75,173" class="hexfield" tabindex="1" hex-row="1" hex-column="1"></polygon>

  <defs>
    <pattern id="image1" x="0%" y="0%" height="100%" width="100%" viewBox="0 0 64 64">
      <image x="0%" y="0%" width="64" height="64" xlink:href="https://cdn4.iconfinder.com/data/icons/imod/512/Software/labo.png"></image>
    </pattern>
  </defs>
  
  <use xlink:href=".hexfield" fill="yellow"/>
  <use xlink:href=".hexfield" fill="url(#image1)"/>

</svg>
Cloud9c
  • 219
  • 5
  • 16

1 Answers1

1

First of all, be aware that xlink:href is deprecated.

Second, xlink:href value doesn't use CSS syntax (where # means ID and . means class).

So, for referring to a group of SVG's, you should point xlink:href to the id of a tag <g>. But If you want that only one SVG gets the definitions, point xlink:href to the SVG id (not class):

<svg tabindex="1" style="width: 175px; height: 216.506px;">

<g id="hexfield">
  <polygon points="25,0 75,0 100,43 75,86 25,86 0,43"/>
  <polygon points="100,44 150,44 175,87 150,130 100,130 75,87"/>
  <polygon points="25,87 75,87 100,130 75,173 25,173 0,130" id="another"/>
  <polygon points="100,130 150,130 175,173 150,216 100,216 75,173"/>
</g>

  <defs>
    <pattern id="image1" height="100%" width="100%" viewBox="0 0 64 64">
      <image width="64" height="64" xlink:href="https://cdn4.iconfinder.com/data/icons/imod/512/Software/labo.png"/>
    </pattern>
    <pattern id="image2" height="100%" width="100%" viewBox="0 0 64 64">
      <image width="64" height="64" xlink:href="https://cdn4.iconfinder.com/data/icons/imod/512/Software/iPhoto.png"/>
    </pattern>
  </defs>
  
  <use xlink:href="#hexfield" fill="yellow"/>
  <use xlink:href="#hexfield" fill="url(#image1)"/>

  <use xlink:href="#another" fill="red"/>
  <use xlink:href="#another" fill="url(#image2)"/>

</svg>
mugga
  • 66
  • 5