前面的话

  给SVG元素应用填充和描边,除了使用纯色和渐变外,还可以使用图案。本文将详细介绍SVG图案

概述

  <pattern>可以实现重复的效果,在canvas中被翻译为模式,而在SVG中被翻译为图案或笔刷

  SVG图案一般用于SVG图形对象的填充fill或描边stroke。这个图形可以是一个SVG元素,也可以是位图图像,通过<pattern>元素在x轴或y轴方向以固定的间隔平铺。

  在pattern元素内部可以包含任何之前包含过的其它基本形状,并且每个形状都可以使用任何样式样式化,包括渐变和半透明

  可以在<pattern>元素内定义图案,然后通过id引用

  下面是一个简单的示例

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1"width=0.2 height=0.2>
<circle cx="2" cy="2" r="2" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

尺寸设置

  从上面的例子中,可以看出width和height是必须的属性,用来表示每一个图案在图形中占据的比例。如上所示,width、height值为0.25,则占据25%的比例,则每行可以有5个图案,每列也可以有5个图案

  如果设置width、height值为0.5,则每行每列都有2个图案

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.5 height=0.5>
<circle cx="2" cy="2" r="2" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

【图案内的图形尺寸】

  如果对图案内的图形尺寸进行设置,将会显示出不同的效果

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2>
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

  下面例子中,则图案width、height值为0.2,即图形每行每列都包含5个图案。因此,每个图案的尺寸是10*10,而图案中的图形circle的半径为5,圆点为(5,5),则正好可以放下一个圆

坐标系统

  patternUnits,定义pattern的坐标系统。它的可能值有两个,userSpaceOnUseobjectBoundingBox

  userSpaceOnUse:xywidthheight表示的值都是当前用户坐标系统的值。也就是说,这些值没有缩放,都是绝对值

  objectBoundingBox(默认值):xywidthheight表示的值都是外框的坐标系统(包裹pattern的元素)。也就是说,图案的单位进行了一个缩放,比如:pattern中为1的值,会变成和包裹元素的外框的widthheight一样的大小

  与渐变不同,pattern有第二个属性patternContentUnits,它描述了pattern元素基于基本形状使用的单元系统。它的可能值也有两个,userSpaceOnUse(默认值)objectBoundingBox

  [注意]patternContentUnits的默认值是userSpaceOnUse

  下面是默认值的情况

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2 patternUnits="objectBoundingBox" patternContentUnits="userSpaceOnUse">
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

  如果使用patternUnits和patternContentUnits都使用objectBoundingBox,要保持原图案,则需要进行如下设置

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2 patternUnits="objectBoundingBox" patternContentUnits="objectBoundingBox">
<circle cx="0.1" cy="0.1" r="0.1" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

  如果使用patternUnits和patternContentUnits都使用userSpaceOnUse,要保持原图案,则需要进行如下设置

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=10 height=10 patternUnits="userSpaceOnUse" patternContentUnits="userSpaceOnUse">
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

  如果使用patternUnits使用userSpaceOnUse,patternContentUnits使用objectBoundingBox,要保持原图案,则需要进行如下设置

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=10 height=10 patternUnits="userSpaceOnUse" patternContentUnits="objectBoundingBox">
<circle cx="0.1" cy="0.1" r="0.1" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

位置设置

  SVG图案使用x、y属性来进行位置设置。默认地,x、y属性为0,即图案从左上角开始绘制

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2 x=0 y=0 >
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

  改变x、y的值,如设置为0.1,则从圆心处开始绘制

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2 x=0.1 y=0.1 >
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

  如果使用userSpaceOnUse坐标系统,要保持原图案,则需要进行如下设置

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=10 height=10 x=5 y=5 patternUnits="userSpaceOnUse" >
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="#630" fill="url(#pattern1)"/>
</svg>

描边

  当然了,图案pattern除了用于填充,还可以用于描边

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=20 height=20 patternUnits="userSpaceOnUse" >
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="url(#pattern1)" stroke-width="10"/>
</svg>

  如果图案pattern的宽高设置的恰当,则会出现在边界线的两侧

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=10 height=10 patternUnits="userSpaceOnUse" >
<circle cx="5" cy="5" r="5" stroke="none" fill="#393" />
</pattern>
</defs>
<rect id="rect1" x="10" y="10" width="50" height="50" stroke="url(#pattern1)" stroke-width="10"/>
</svg>

使用图片

  上次所有的实例中,都是使用SVG图形来创建图案。下面将使用一个图像作为SVG图案,该图像宽为50px,高为50px,是两个绿色的正方形组成的棋盘图案

  这里使用了一个<image>元素,然后通过xlink:href属性引用图像

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2 >
<image xlink:href="http://7xpdkf.com1.z0.glb.clouddn.com/runjs/img/wpid-pattern-50.png" width=10 height=10></image>
</pattern>
</defs>
<rect id="rect1" x="0" y="0" width="50" height="50" fill="url(#pattern1)" />
</svg>

图案引用

  使用xlink:href属性,可以在SVG文档中引用另一个图案

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2>
<image xlink:href="http://7xpdkf.com1.z0.glb.clouddn.com/runjs/img/wpid-pattern-50.png" width=10 height=10></image>
</pattern>
<pattern id="pattern2" xlink:href="#pattern1" x=0.1>
</pattern>
</defs>
<rect id="rect1" x="0" y="0" width="50" height="50" fill="url(#pattern2)" />
</svg>

  可以通过在一个图案中的图形中引用另一个图案来实现图案嵌套的效果

<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<pattern id="pattern1" width=0.2 height=0.2>
<image xlink:href="http://7xpdkf.com1.z0.glb.clouddn.com/runjs/img/wpid-pattern-50.png" width=2.5 height=2.5></image>
</pattern>
<pattern id="pattern2" width=0.2 height=0.2>
<circle cx=5 cy=5 r=5 fill="url(#pattern1)"></circle>
</pattern>
</defs>
<rect id="rect1" x="0" y="0" width="50" height="50" fill="url(#pattern2)" />
</svg>

SVG图案的更多相关文章

  1. SVG图案填充-Pattern

    SVG图案一般用于SVG图形对象的填充fill或描边stroke.这个图形可以是一个SVG元素,也可以是位图图像,通过<pattern>元素在x轴或y轴方向以固定的间隔平铺. <pa ...

  2. 如何给SVG填充和描边应用线性渐变

    给SVG元素应用填充和描边有三种方法(戳这里学习SVG填充和描边的相关内容).你可以使用纯色.图案或渐变.前面两种方法我们之前已经讲过了,现在我们来讨论第三种方法——渐变. SVG提供了两种渐变——线 ...

  3. HTML5程序设计--SVG

    SVG(Scalable Vector Graphics):可缩放矢量图形,一种二维图形表示语言. 借助SVG,我们可以实现很多同Canvas API类型的绘制操作,但在Canvas元素上绘制文本的时 ...

  4. 在 React、Vue项目中使用 SVG

    在一些现代的扁平化设计网站,特别是移动端网站,经常会包含许多简单而清晰的小图标,例如网站图标.用户的默认头像.移动端网页首页底部固定的切换栏等,这些小图标一般都是由美工做好,可能会放到精灵图上,前端再 ...

  5. 8款超好用的SVG编辑工具用起来

    随着响应式网页的发展,对于内容呈现的要求也越来越高,尤其是图像.为了在各种设备上能实现自然伸缩或扩展而不影响图片质量,所以矢量图形(SVG)正变得越来越受欢迎. 大家都知道,在计算机图形学中,有两种主 ...

  6. HTML躬行记(1)——SVG

    <svg>是矢量图的根元素,通过xmlns属性声明命名空间,从而告诉用户代理标记名称属于哪个XML方言.在下面的示例中,为<svg>元素声明了宽度和高度(默认以像素为单位),其 ...

  7. Trianglify - 生成五彩缤纷的 SVG 背景图案

    Trianglify 是一个能够生成五颜六色的三角形图案的 JavaScript 库,可以用来作为 SVG 图像和 CSS 背景.它的灵感来自于 Btmills 的 Geopattern,并使用 d3 ...

  8. Trianglify – 五彩缤纷的 SVG 背景图案

    Trianglify 是一个能够生成五颜六色的三角形图案的 JavaScript 库,可以用来作为 SVG 图像和 CSS 背景.它的灵感来自于 Btmills 的 Geopattern,并使用 d3 ...

  9. 7. svg学习笔记-图案和渐变

    之前,我们仅仅使用纯色来为图形填充颜色和绘制轮廓,除此之外,我们还可以使用图案和渐变来填充图形或者是绘制轮廓. 图案 图案的效果类似于,在网页中给一个元素指定背景图像,当背景图像的尺寸小于元素的尺寸的 ...

随机推荐

  1. 一张图告诉你最流行的 7 个 JavaScript框架特点

    欢迎大家持续关注葡萄城控件技术团队博客,更多更好的原创文章尽在这里~~​ AngularJ.js                                                  由g ...

  2. jquery.imagezoom.js制作鼠标悬停图片放大镜特效、参数和最简教程

    一.插件介绍 今天在用到放大镜效果的时候,突然发现网站里没有放大镜的插件.于是总结了一下,放到这里.为自己,也为他人提供方便.jquery.imagezoom.js这款插件用途很简单,就是鼠标移过去, ...

  3. n以内质数占的比例

    2 ->0.5 10 ->0.4 100-> 0.25 1000->0.168 10000->0.1229 100000->0.09592 1000000-> ...

  4. Python3常用网络编程模块介绍

    一.socket模块 网络服务都是建立在socket基础之上的,socket是网络连接端点,是网络的基础:每个socket都被绑定到指定的IP和端口上: 1.首先使用socket(family=AF_ ...

  5. python_装饰器_语法糖

    什么是高阶函数? -- 把函数名当做参数传给另外一个函数,在另外一个函数中通过参数调用执行 #!/usr/bin/python3 __author__ = 'beimenchuixue' __blog ...

  6. 对接第三方平台JAVA接口问题推送和解决

    前言 本节所讲为实际项目中与第三方对接出现的问题最后还是靠老大解决了问题以此作为备忘录,本篇分为三小节,一小节解析Java加密接口数据,二小节解析XML文件需注意问题,最后一节则是请求Java Soa ...

  7. python数据分析入门学习笔记

    学习利用python进行数据分析的笔记&下星期二内部交流会要讲的内容,一并分享给大家.博主粗心大意,有什么不对的地方欢迎指正~还有许多尚待完善的地方,待我一边学习一边完善~ 前言:各种和数据分 ...

  8. DataGuard实战1

    DataGuard实战1 -------------------------------------------2013/10/27   一.Primary数据库的配置及操作 1. 确定主库处于归档日 ...

  9. 移动端为何不使用click而模拟tap事件及解决方案

    移动端click会遇到2个问题,click会有200-300ms的延迟,同时click事件的延迟响应,会出现穿透,即点击会触发非当前层的点击事件. 为什么会存在延迟? Google开发者文档中有提到: ...

  10. Win7 IE11无法打开的可能解决办法

    IE11点击后无反应,应该如何解决呢?我的机器出现上述情况后,采用下面的方法解决了问题:第一步,进入Windows 7的运行,打开Regedit,即注册表编辑器.依次找到 >>HKEY_C ...