【问题标题】:How to set a SVG rect element's background image with jQuery SVG plugin?如何使用 jQuery SVG 插件设置 SVG 矩形元素的背景图像?
【发布时间】:2012-05-31 00:07:12
【问题描述】:

我使用 keith-wood 的 jQuery SVG 插件创建了一个 SVG 矩形。代码如下:

svg.graph._wrapper.rect(group, 0, 100, 40, 20,
                       {fill: 'ivory',
                        stroke: 'black',
                        strokeWidth : 2});

所以我认为我可以轻松更改填充而不是使用“象牙”,我将其更改为“theImageIwantToUse”。但它似乎不起作用。

【问题讨论】:

    标签: jquery svg plugins jquery-plugins


    【解决方案1】:

    您不能直接插入外部图像作为 SVG 对象的背景。

    相反,您首先必须创建这样的模式

    var ptn = svg.pattern(defs, "imgPattern", 0, 0, 100, 100, 0, 0, 10, 10, 
                          {patternUnits: "userSpaceOnUse"});
    svg.image( ptn, 100, 50, 200, 200, "./theImageIwantToUse.png");
    

    然后通过填充属性中的url() 函数使用此模式

    svg.graph._wrapper.rect(group, 0, 100, 40, 20,
                           {fill: 'url(#imgPattern)',
                            stroke: 'black',
                            strokeWidth : 2});
    

    【讨论】:

    【解决方案2】:

    您可以使用 <image> 元素 http://www.w3.org/TR/SVG/struct.html#ImageElement 在 svg 中插入图片

    您提到的插件中提供了适当的功能。

    svg.image(parent, x, y, width, height, ref, settings)  
    

    (http://keith-wood.name/svg.html)

    我怀疑是否可以在图像中使用描边,因此您必须在绘制图像后绘制一个没有填充的矩形以获得您想要的确切结果。

    【讨论】:

    • 这个解决方案的性能比使用 fill 属性的解决方案好很多。
    猜你喜欢
    • 1970-01-01
    • 2021-07-29
    • 2018-02-09
    • 2011-04-17
    • 1970-01-01
    相关资源
    最近更新 更多