Is it possible to use an image in place of the stroke of an SVG path?

后端 未结 2 399
心在旅途
心在旅途 2021-01-20 02:05

First off, I know this question is very similar to this question, but I tried implementing that solution with an SVG PATH and it did not w

相关标签:
2条回答
  • 2021-01-20 02:30

    It works fine on firefox. I am not sure what the problem is that you are having.

    #container svg {
      fill: none;
      stroke-width: 10px;
      stroke: url(#pattern);
      stroke-dasharray:1628;
      stroke-dashoffset:1628.1;
      animation:polyline 3.15s linear 0.5s forwards;
    }
    
    @keyframes polyline {
      to {
        stroke-dashoffset:0;
      }
    }
    <div id="container">
      <svg>
        <defs>
          <pattern id="pattern" width="1600" height="800" patternUnits="userSpaceOnUse">
            <image xlink:href="http://lorempixel.com/1600/800/nature" width="1600" height="800" />
          </pattern>
        </defs>
    
        <path d="M0,5
                 L184,5
                 C202,5 202,5 202,36
                 L202,86
                 L327,85
                 L421,166
                 L460,166
                 L499,132
                 L588,211
                 L617,211
                 L712,134
                 L748,165
                 L780,165
                 L830,111
                 L913,212
                 L938,212
                 L1028,140
                 L1078,184
                 L1107,184
                 L1152,140
                 L1263,249
                 L1263,248"
              />
      </svg>
    </div>

    0 讨论(0)
  • 2021-01-20 02:33

    That's a Chrome/Safari bug you're relying on.

    stroke:url(#pattern);
    

    is actually shorthand for

    stroke:url(<this file>#pattern);
    

    but there's no pattern in the css file. Chrome gets this wrong, Firefox gets it right. If you fix the reference Firefox will work but unfortunately Chrome won't any longer. The most compatible solution would therefore be to move your CSS (at least the bit that references the pattern) into the SVG file itself within <style> tags.

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