SVG 复用(defs、symbol、use)

<defs>与<symbol>的相同点

  • <defs>元素用于预定义一个元素使其能够在SVG图像中重复使用。
  • <symbol>元素用于定义可重复使用的符号。
  • 嵌入在<defs>或<symbol>元素中的图形是不会被直接显示的,除非你使用<use>元素来引用它。

<defs>与<symbol>的不同点

  • xlink定义了一套标准的在 XML 文档中创建超级链接的方法,可以用它来引用<symbol>元素或<defs>内定义的元素和组。
  • 一个<symbol>元素可以有preserveAspectRatio和viewBox属性。而<g>元素不能拥有这些属性。

因此相比于在<defs>元素中使用<g>的方式来复用图形,使用<symbol>元素也许是一个更好的选择。

<symbol>的preserveAspectRatio、viewBox属性

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
    <symbol id="shape1" preserveAspectRatio="xMinYMin meet" viewBox="0 0 50 100">
        <rect x="0" y="0" width="50" height="50" style="fill:green;"/>
    </symbol>
    <symbol id="shape2" preserveAspectRatio="xMinYMin slice" viewBox="0 0 50 100" >
        <rect x="0" y="0" width="50" height="50" style="fill:blue"/>
    </symbol>

    <rect x="0" y="0" width="50" height="50" fill="red" />
    <use xlink:href="#shape1" x="25" y="25" width="200" height="100"/>
    <use xlink:href="#shape2" x="75" y="50" width="200" height="100"/>
</svg>      

这里写图片描述

<use>的transform属性

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="400">
    <defs>
        <g id="ShapeGroup">
            <rect x="50" y="50" width="100" height="100" fill="#69C" stroke="red" stroke-width="2"/>
            <circle cx="100" cy="100" r="40" stroke="#00f" fill="none" stroke-width="5"/>
        </g>
    </defs>
    <use xlink:href="#ShapeGroup" transform="translate(-10,0) scale(0.5)"/>
    <use xlink:href="#ShapeGroup" transform="translate(10,10) scale(1)"/>
    <use xlink:href="#ShapeGroup" transform="translate(50,60) scale(1.5)"/>
</svg>

这里写图片描述

<use>的x、y属性

<svg xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="300" height="300">
    <circle cx = "100" cy = "100" r = "50" fill = "green" stroke = "black" stroke-width = "3"/>
    <ellipse cx = "100" cy = "100" rx = "50" ry = "30" fill = "blue" stroke = "black" stroke-width = "3"/>

    <defs>
        <circle id = "s1" cx = "100" cy = "100" r = "50" fill = "yellow" stroke = "black" stroke-width = "3"/>
        <ellipse id = "s2" cx = "100" cy = "100" rx = "50" ry = "30" fill = "salmon" stroke = "black" stroke-width = "3"/>
    </defs>
    <use x = "100" y = "60" xlink:href = "#s1"/>
    <use x = "50" y = "100" xlink:href = "#s2"/>

    <line x1="100" y1="100" x2="200" y2="160" stroke="red" stroke-width = "3"/>
    <line x1="100" y1="100" x2="150" y2="200" stroke="pink" stroke-width = "3"/>
</svg>

如下图可以看出,<use> 中的 x 和 y 相当于 <text> 标签中的 dx 和 dy。但是注意:<use>标签并不支持 dx 和 dy。

这里写图片描述

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
SVG(Scalable Vector Graphics)是一种基于 XML 的矢量图形格式,而 defs 元素是 SVG 中的一个重要元素,用于定义可重复使用的图形元素或属性。下面是对 svg defs 的详细解释: 1. 定义:defs 元素是 SVG 的一个容器元素,用于存放可重复使用的图形元素或属性定义。它不会直接呈现任何图形,而是被其他 SVG 元素引用。 2. 用途:defs 元素主要用于定义共享的图形元素,以便在整个 SVG 文档中重复使用。通过将这些图形元素放在 defs 中,可以减少代码的冗余,并提高可维护性。 3. 嵌套规则:defs 元素可以包含各种 SVG 元素,如路径、文本、图像等。它可以被放置在 SVG 根元素或其他容器元素中,以便在需要的时候引用。 4. 引用方式:要引用 defs 中的图形元素或属性,可以使用使用 <use> 元素。通过在 <use> 元素中指定 xlink:href 属性,并指向 defs 中定义的元素,就可以在文档中使用这些定义的图形元素。 5. 示例:以下是一个使用 defs 元素定义和引用共享图形元素的示例: ```html <svg width="200" height="200"> <defs> <circle id="myCircle" cx="50" cy="50" r="40" fill="red" /> </defs> <use xlink:href="#myCircle" /> </svg> ``` 在上述示例中,定义了一个圆形元素,并将其放在 defs 中,然后使用 <use> 元素引用了这个圆形元素。这样就可以在 SVG 中重复使用这个圆形元素,而不需要重复定义。 总结:svg defs 元素是 SVG 中的一个容器元素,用于定义可重复使用的图形元素或属性。通过将图形元素放在 defs 中,并使用 <use> 元素引用,可以在整个 SVG 文档中重复使用这些定义的元素。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值