美文网首页
SVG pattern

SVG pattern

作者: CODERLIHAO | 来源:发表于2020-04-03 23:06 被阅读0次
<svg width="240" height="160">
    <defs>
        <pattern id="p" patternUnits="userSpaceOnUse" width="60" height="60">
            <rect width="30" height="30" fill="#f99" x="0" y="0"></rect>
        </pattern>
    </defs>
    <rect width="240" height="160" stroke="#aaa" fill="url(#p)" />
    <rect width="60" height="60" x="1" y="1" stroke="#00f" stroke-width="2" fill="url(#p)" />
</svg>
image.png
<svg width="240" height="160">
    <defs>
        <pattern id="p" patternUnits="userSpaceOnUse" width="40" height="40">
            <rect width="30" height="30" fill="#f99" x="0" y="0"></rect>
        </pattern>
    </defs>
    <rect width="240" height="160" stroke="#aaa" fill="url(#p)" />
    <rect width="60" height="60" x="1" y="1" stroke="#00f" stroke-width="2" fill="url(#p)" />
</svg>

image.png
patternUnits
  • userSpaceOnUse
    这也是默认值
    上面的demo中width与height就是实际的宽高,pattern内的rect内的x与就是在width=60与height=60坐标内填充,

  • objectBoundingBox
    使用这个时,width与height就是比例

<svg width="240" height="160">
    <defs>
        <pattern id="p" patternUnits="objectBoundingBox" width="1" height="1">
            <rect width="30" height="30" fill="#f99" x="0" y="0"></rect>
        </pattern>
    </defs>
    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="2" fill="url(#p)" />
</svg>

这里的pattern实际的宽度是100(rect的width)乘以1后的结果,就是100px,只能填充rect一个

image.png
<svg width="240" height="160">
    <defs>
        <pattern id="p" patternUnits="objectBoundingBox" width="0.4" height="0.4">
            <rect width="30" height="30" fill="#f99" x="0" y="0"></rect>
        </pattern>
    </defs>
    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="2" fill="url(#p)" />
</svg>

width=0.4后的实际宽度是100*0.4 = 40px


image.png
<svg width="240" height="160">
    <defs>
    <pattern id="p" patternUnits="objectBoundingBox" width=".4" height=".4">
        <rect width="30" height="30" fill="#f99" x="0" y="0"></rect>
    </pattern>
    </defs>
    <rect width="80" height="80" x="120" y="30" stroke="#000" stroke-width="2" fill="url(#p)" />
    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="2" fill="url(#p)" />
</svg>

两个rect宽度不同,乘以0.4后的结果也是不一样

image.png
patternContentUnits

与patternUnits作用一致,只不过patternContentUnits对应的

<svg width="240" height="160">
    <defs>
        <pattern id="p1" patternUnits="objectBoundingBox" width=".4" height=".4" patternContentUnits="userSpaceOnUse">
            <rect width="20" height="20" fill="#f99" x="0" y="0"></rect>
        </pattern>
        <pattern id="p2" patternUnits="objectBoundingBox" width=".4" height=".4" patternContentUnits="objectBoundingBox">
            <rect width=".2" height=".2" fill="#f99" x="0" y="0"></rect>
        </pattern>
    </defs>
    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="2" fill="url(#p1)" />
    <rect width="100" height="100" x="120" y="30" stroke="#000" stroke-width="2" fill="url(#p2)" />
</svg>

效果看起来一样,第一个矩形是1000.4 = 40px,只不过pattern内的矩形长宽都是20px
第二个是pattern的长宽是100
0.4 = 40px,但是第二个pattern内的rect的长宽却是100*0.2 = 20px

image.png
<svg width="240" height="160">
    <defs>
        <pattern id="p1" patternUnits="objectBoundingBox" width=".4" height=".4" patternContentUnits="userSpaceOnUse">
            <rect width="20" height="20" fill="#f99" x="0" y="0"></rect>
        </pattern>
        <pattern id="p2" patternUnits="objectBoundingBox" width=".5" height=".5" patternContentUnits="objectBoundingBox">
            <rect width=".2" height=".2" fill="#f99" x="0" y="0"></rect>
        </pattern>
    </defs>

    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="2" fill="url(#p1)" />
    <rect width="100" height="100" x="120" y="30" stroke="#000" stroke-width="2" fill="url(#p2)" />
</svg>
image.png
patternTransform
<svg width="240" height="160">
    <defs>
        <pattern id="p1" patternUnits="objectBoundingBox" width=".1" height=".4" >
            <rect width="5" height="50" fill="#000" x="0" y="0"></rect>
            <rect width="5" height="50" fill="#f99" x="5" y="0"></rect>
        </pattern>
    </defs>
    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="1" fill="url(#p1)" />
</svg>
image.png
<svg width="240" height="160">
    <defs>
        <pattern id="p1" patternUnits="objectBoundingBox" width=".1" height=".4" patternTransform="rotate(45)">
            <rect width="5" height="50" fill="#000" x="0" y="0"></rect>
            <rect width="5" height="50" fill="#f99" x="5" y="0"></rect>
        </pattern>
    </defs>
    <rect width="100" height="100" x="10" y="30" stroke="#000" stroke-width="1" fill="url(#p1)" />
</svg>
image.png

参考

相关文章

  • SVG pattern

    patternUnits userSpaceOnUse这也是默认值上面的demo中width与height就是实际...

  • Android图片之svg

    1.SVG是什么2.SVG优点3.SVG使用4.获取SVG5.封装使用6.SVG动画 1.SVG是什么? SVG(...

  • SVG的使用

    SVG图片 一. SVG介绍 1.1. SVG概念解析 SVG全称: Scalable Vector Graphi...

  • 资源

    SVG svg icon 对应git(node.js写的) svg animation study svg cs...

  • (第六天)HTML5之SVG的了解与使用&Web数据存储

    SVG 什么是SVG? SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用...

  • svg standalone

    简单的 SVG 实例 一个简单的SVG图形例子: 这里是SVG文件(SVG文件的保存与SVG扩展): "http:...

  • SVG学习笔记

    SVG学习笔记 简介 SVG使用XML来描述二维图形和绘图程序的语言。 SVG形状 SVG在HTML页面 SVG ...

  • SVG

    Menu SVG 实例 SVG 形状 SVG 实例 SVG 的 用来创建一个圆。cx 和 cy ...

  • SVG简介及其用法

    一、SVG - 基础 1.什么是SVG 2.SVG的优势 3.SVG与canvas的区别 4.用途 5.svg再将...

  • svg实例

    一个简单的SVG图形例子: 这里是SVG文件(SVG文件的保存与SVG扩展): SVG 代码解析: 第一行包含了 ...

网友评论

      本文标题:SVG pattern

      本文链接:https://www.haomeiwen.com/subject/xmylphtx.html