SVG渐变
前面的话
给SVG元素应用填充和描边,除了使用纯色外,还可以使用渐变。本文将详细介绍SVG渐变
线性渐变
有两种类型的渐变:线性渐变和径向渐变。必须给渐变内容指定一个id属性,否则文档内的其他元素不能引用它。为了让渐变能被重复使用,渐变内容需要定义在<defs>标签内部,而不是定义在形状上面
线性渐变沿着直线改变颜色,要插入一个线性渐变,需要在SVG文件的defs元素内部,创建一个<linearGradient> 节点
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1">
<stop offset="0%" stop-color="#05a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
结果如下所示,默认情况下实现的是水平方向的渐变
<stop>元素一共有3个属性,包括offset,stop-color,stop-opacity
offset用来设置色标位置
stop-color用来设置色标颜色
stop-opacity用来设置色标的透明度
下面是一个例子
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1">
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
【x1、x2、y1、y2】
线性渐变包括x1、x2、y1、y2这四个属性,用来控制渐变的大小和方向。取值为0-100%,或者0-1的小数。默认地,x1=y1=y2=0、x2=1
如果变成垂直方向的渐变,则需要设置为x1=x2=y1=0、y2=1
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" x1=0 x2=0 y1=0 y2=1>
<stop offset="0%" stop-color="#05a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
如果将y2或x2设置为50%,则50%-100%这一部分区域填充为最后一个色标的纯色
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" x1=0 x2=50% y1=0 y2=50%>
<stop offset="0%" stop-color="#05a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
当然了,可以有多个色标
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" x1=0 x2=1 y1=0 y2=1>
<stop offset="0%" stop-color="#05a"/>
<stop offset="20%" stop-color="#50a"/>
<stop offset="40%" stop-color="#5a0"/>
<stop offset="60%" stop-color="#a05"/>
<stop offset="80%" stop-color="#a50"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
渐变除了可以作为填充,也可以作为描边
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" x1=0 x2=1 y1=0 y2=1>
<stop offset="0%" stop-color="#05a"/>
<stop offset="20%" stop-color="#50a"/>
<stop offset="40%" stop-color="#5a0"/>
<stop offset="60%" stop-color="#a05"/>
<stop offset="80%" stop-color="#a50"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="transparent" stroke-width="10" stroke="url(#Gradient1)"/>
</svg>
【xlink:href】
xlink:href属性用于在一个渐变中引用另一个渐变,被引用的渐变的属性是可继承的,也可以被重写
下面的例子中,Gradient2引用了Gradient1的渐变,并重写了渐变的方向
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" x1=0 x2=1 y1=0 y2=1>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
<linearGradient id="Gradient2" xlink:href="#Gradient1" x1=0 x2=0 y1=0 y2=1>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient2)"/>
</svg>
【gradientUnits】
gradientUnits有两个的值,userSpaceOnUse和objectBoundingBox,这用于决定渐变是否随着引用它的元素进行缩放。也就是说它决定了x1、y1、x2、y2的缩放
userSpaceOnUse: x1、y1、x2、y2表示当前用户坐标系统的坐标。也就是说渐变中的值都是绝对值
objectBoundingBox: x1, y1, x2, y2表示应用渐变的元素创建的边界坐标系统。也就是说渐变随着应用的元素进行了缩放
如果不设置,默认取值是objectBoundingBox
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" gradientUnits="objectBoundingBox">
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
如果设置为userSpaceOnUse,则x1、x2、y1、y2需要设置为用户坐标系的坐标绝对值
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<linearGradient id="Gradient1" gradientUnits="userSpaceOnUse" x1="10" x2="60" y1="0" y2="0">
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
【spreadMethod】
spreadMethod可以接受三个值,pad,reflect,repeat,它定义了渐变如何开始和结束,当cx和cy的值是在0%到100%里面的时候
pad:(默认值)使用开始和结束位置的颜色结点来填充剩余的部分
reflect: 反射渐变图案,从开始->结束,再从结束->开始,然后开始->结束,往复直到空间都填满
repeat: 从start-to-end重复渐变图案,直到空间填满
pad为默认值
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<linearGradient id="Gradient1" spreadMethod=pad x1=0.4 x2=0.6>
<stop offset="0%" stop-color="#05a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
下面是reflect的效果
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<linearGradient id="Gradient1" spreadMethod=reflect x1=0.4 x2=0.6>
<stop offset="0%" stop-color="#05a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
下面是repeat的效果
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg" >
<defs>
<linearGradient id="Gradient1" spreadMethod=repeat x1=0.4 x2=0.6>
<stop offset="0%" stop-color="#05a"/>
<stop offset="100%" stop-color="#0a5"/>
</linearGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
径向渐变
径向渐变与线性渐变相似,只是它是从一个点开始发散绘制渐变。创建径向渐变需要在文档的defs中添加一个<radialGradient>元素
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1">
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
【cx、cy、r、fx、fy】
与线性渐变的x1、y1、x2、y2属性不同,径向渐变使用cx、cy、r、fx、fy这五个属性来设置渐变
r 设置圆的半径
cx、cy 定义渐变的中心点坐标
fx、fy 定义渐变的焦点坐标

如果不设置,r默认0.5,即元素宽度或高度的一半;cx、cy默认为0.5;fx、fy默认为0.5
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" r=0.5 cx=0.5 cy=0.5 fx=0.5 fy=0.5>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
巧妙地设置焦点坐标,可以实现聚光灯的效果
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" r=0.5 cx=0.5 cy=0.5 fx=0.8 fy=0.8>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
【xlink:href】
xlink:href属性用于在一个渐变中引用另一个渐变,被引用的渐变的属性是可继承的,也可以被重写
下面的例子中,Gradient2引用了Gradient1的渐变,并重写了渐变的方向
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1">
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
<radialGradient id="Gradient2" xlink:href="#Gradient1" fx=0.6>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient2)"/>
</svg>
【gradientUnits】
gradientUnits有两个的值,userSpaceOnUse和objectBoundingBox,这用于决定渐变是否随着引用它的元素进行缩放。也就是说它决定了cx、cy、fx、fy、r的缩放
userSpaceOnUse: cx、cy、fx、fy、r表示当前用户坐标系统的坐标。也就是说渐变中的值都是绝对值
objectBoundingBox: cx、cy、fx、fy、r表示应用渐变的元素创建的边界坐标系统。也就是说渐变随着应用的元素进行了缩放
如果不设置,默认取值是objectBoundingBox
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" gradientUnits="objectBoundingBox">
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
如果设置为userSpaceOnUse,则cx、cy、fx、fy、r需要设置为用户坐标系的坐标绝对值
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" gradientUnits="userSpaceOnUse" cx=35 cy=35 fx=35 fy=35 r=25>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
【spreadMethod】
和线性渐变一样。它可以接受三个值,pad,reflect,repeat,它定义了渐变如何开始和结束,当cx和cy的值是在0%到100%里面的时候
pad:(默认值)使用开始和结束位置的颜色结点来填充剩余的部分
reflect: 反射渐变图案,从开始->结束,再从结束->开始,然后开始->结束,往复直到空间都填满
repeat: 从start-to-end重复渐变图案,直到空间填满
pad为默认值
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" r=0.2 spreadMethod=pad>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
下面是reflect的效果
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" r=0.2 spreadMethod=reflect>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
下面是repeat的效果
<svg height="70" version="1.1" xmlns="http://www.w3.org/2000/svg">
<defs>
<radialGradient id="Gradient1" r=0.2 spreadMethod=repeat>
<stop offset="0%" stop-color="#05a"/>
<stop offset="50%" stop-color="#50a" stop-opacity="0.5"/>
<stop offset="100%" stop-color="#0a5"/>
</radialGradient>
</defs>
<rect id="rect1" x="10" y="10" rx="15" ry="15" width="50" height="50" fill="url(#Gradient1)"/>
</svg>
SVG渐变的更多相关文章
- 学习SVG系列(5):SVG渐变
SVG渐变 渐变是一种从一种颜色到另一种颜色的平滑过渡,可以把多个颜色的过渡应用到同一个元素. 渐变有两种: Linear Redial 线性渐变-<linearGradient> lin ...
- 走进SVG
什么是SVG?也许现在很多人都听说过SVG的人比较多,但不一定了解什么是SVG:SVG(Scalable Vector Graphics 一大串看不懂的英文)可伸缩矢量图形,它是用XML格式来定义用于 ...
- svg矢量图
svg简介 Scalable Vector Graphics 可缩放矢量图形 SVG 图像在放大或改变尺寸的情况下其图形质量不会有所损失 svg知识点 svg如何绘图 svg和cnavas区别 svg ...
- SVG介绍
SVG介绍 SVG是指可缩放矢量图(Scalable Vector Graphics).SVG使用XML格式来定义图形.SVG可以直接嵌入到HTML页面中. 位图和矢量图 位图(Bitmap)是由很多 ...
- svg基础知识体系建立
一.简介:SVG 是使用 XML 来描述二维图形和绘图程序的语言. SVG 指可伸缩矢量图形 (Scalable Vector Graphics) SVG 用来定义用于网络的基于矢量的图形 SVG 使 ...
- SVG和canvas画图,js求数组最大最小值
windows命令行的内容怎么复制,右键选择标记,选中内容后再点击鼠标右键就复制了. 安装Node.js后再用npm install命令会出现如下warn:saveError ENOENT: no s ...
- HTML5 可缩放矢量图形(1)—SVG基础
参考文档1 SVG基础 SVG介绍 概念:SVG 是使用 XML 来描述二维图形和绘图程序的语言.(理解就是一个在网页上使用笔画图的过程) 什么是SVG SVG 指可伸缩矢量图形 (Scalable ...
- 关于 CSS 反射倒影的研究思考
原文地址:https://css-tricks.com/state-css-reflections 译者:nzbin 友情提示:由于演示 demo 的兼容性,推荐火狐浏览.该文章篇幅较长,内容庞杂,有 ...
- CSS遮罩——如何在CSS中使用遮罩
Css遮罩是2008年4月由苹果公司添加到webkit引擎中的.遮罩提供一种基于像素级别的,可以控制元素透明度的能力,类似于png24位或png32位中的alpha透明通道的效果. 图像是由rgb三个 ...
随机推荐
- Android中的广播
Android中的广播 广播接受器,可以比喻成收音机.而广播则可以看成电台. Android系统内部相当于已经有一个电台 定义了好多的广播事件,比如外拨电话 短信到来 sd卡状态 电池电量变化... ...
- [js高手之路] es6系列教程 - 不定参数与展开运算符(...)
三个点(...)在es6中,有两个含义: 用在形参中, 表示传递给他的参数集合, 类似于arguments, 叫不定参数. 语法格式: 在形参面前加三个点( ... ) 用在数组前面,可以把数组的值 ...
- NYOJ--325--深度优先搜索--zb的生日
/* Name: NYOJ--325--zb的生日 Author: shen_渊 Date: 15/04/17 08:18 Description: 输入时计算总质量,DFS搜索和总质量差值一般最接近 ...
- mysql 触发器(trigger)
触发器(trigger):监视某种情况,并触发某种操作. 触发器创建语法四要素:1.监视地点(table) 2.监视事件(insert/update/delete) 3.触发时间(after/befo ...
- 再起航,我的学习笔记之JavaScript设计模式06(抽象工厂模式)
我的学习笔记是根据我的学习情况来定期更新的,预计2-3天更新一章,主要是给大家分享一下,我所学到的知识,如果有什么错误请在评论中指点出来,我一定虚心接受,那么废话不多说开始我们今天的学习分享吧! 前两 ...
- absolute 的containing block( 容器块)计算方式跟正常流有什么不同?
无论属于哪种,都要先找到其祖先元素中最近的 position 值不为 static 的元素,然后再判断:1.若此元素为 inline 元素,则 containing block 为能够包含这个元素生成 ...
- 强密码和弱密码并没有什么区别?NIST密码安全标准更新:不再建议密码要求混合大写字母、字符和数字
作为一名认真负责的小编,每次注册账号设置密码的时候都是最痛苦的,太简单的怕被破解,太难的又记不住. 等你好不容易记住密码,三个月后IT同学过来拍拍你的肩膀,"你的密码到期了,记得改啊--&q ...
- sshpass做秘钥分发,ansible做自动化运维工具
最近公司机器的增多,顺便还要上报表系统,考虑到服务器越来越多,手工的管理显得越来的越吃力,所以打算推进公司自动化运维工具的使用. 推进的过程中,一步一个坑踩过来的.由于公司之前未运用过自动化运维工具, ...
- [算法题] Remove Element
题目内容 本题来源:LeetCode Given an array and a value, remove all instances of that value in place and retur ...
- 01迷宫 洛谷 p1141
题目描述 有一个仅由数字0与1组成的n×n格迷宫.若你位于一格0上,那么你可以移动到相邻4格中的某一格1上,同样若你位于一格1上,那么你可以移动到相邻4格中的某一格0上. 你的任务是:对于给定的迷宫, ...