在 SVG 中添加背景图像

Add background image in SVG

我需要在 svg 中添加背景图片。但我只得到黑色矩形框,图像不显示。如何添加背景图片?

这是我的代码:

<style>
    path {
        fill: none;
        stroke: #000;
        stroke-width: 3px;
        stroke-linejoin: round;
        stroke-linecap: round;
    }
</style>

<svg width="1000" height="700">
    <!-- <rect fill="#fff" width="100%" height="100%"></rect> -->

    <defs>
        <pattern id="img1" patternUnits="userSpaceOnUse" x="0" y="0" width="1000" height="700">
            <image xlink:href="home/nayana/Documents/Text Editor Files/abstract-hd.jpg" width="600" height="450" />             
        </pattern>
    </defs>
    <path d="M5,5 l0,680 l980,0 l0,-680 l-980,0 fill="url(#img1)" />
</svg>

提前致谢

更正路径中的语法错误,您在末尾缺少 " 并从 CSS 中删除 fill:none 以覆盖与路径一起使用的填充属性:

完整代码:

path {
  stroke: #000;
  stroke-width: 3px;
  stroke-linejoin: round;
  stroke-linecap: round;
}
<svg width="1000" height="700">
    <!-- <rect fill="#fff" width="100%" height="100%"></rect> -->

    <defs>
        <pattern id="img1" patternUnits="userSpaceOnUse" x="0" y="0" width="1000" height="700">
                    <image xlink:href="https://lorempixel.com/600/450/" width="600" height="450" />             
            </pattern>
    </defs>
    <path d="M5,5
        l0,680 l980,0 l0,-680 l-980,0"          
        fill="url(#img1)" />
</svg>

如今最简单的用法是简单 <image href="bgnd.png"/>。这也可以采用 x、y、宽度、高度。 https://developer.mozilla.org/en-US/docs/Web/SVG/Element/image