Fill out the SVG shape with another SVG shape

Can one SVG shape be used as filling in another shape?

+3


source to share


1 answer


You want to use SVG template . See this example :

<svg viewBox="0 0 800 400" xmlns="http://www.w3.org/2000/svg">
  <defs>
    <pattern id="TrianglePattern" patternUnits="userSpaceOnUse"
             x="0" y="0" width="100" height="100"
             viewBox="0 0 10 10" >
      <path d="M0,0 L7,0 L3.5,7 z" fill="red" stroke="blue" />
    </pattern> 
  </defs>

  <!-- The ellipse is filled using a triangle pattern paint server
       and stroked with black -->
  <ellipse fill="url(#TrianglePattern)" stroke="black" stroke-width="5"  
           cx="400" cy="200" rx="350" ry="150" />
</svg>

      



Note that:

  • viewBox

    from <pattern>

    will copy what is drawn. (As you can see in the example, the top stroke and top left corner of each triangle are slightly hidden.)

  • When the size viewBox

    is different from width

    and height

    , you effectively scale the size of the graphics in your template. (The example scales a 7-unit-wide triangle in the pattern 10 times, so only 7 of them plus the spacer are visible across the 700-unit width ellipse.)

+6


source







All Articles