当前位置:网站首页>一篇文章带你了解SVG 渐变知识
一篇文章带你了解SVG 渐变知识
2020-11-06 20:42:00 【Python进阶者】
渐变是一种从一种颜色到另一种颜色的平滑过渡。另外,可以把多个颜色的过渡应用到同一个元素上。
SVG渐变主要有两种类型:(Linear,Radial)。
一、SVG 线性渐变
<linearGradient>元素用于定义线性渐变。
<linearGradient>标签必须嵌套在<defs>的内部。<defs>标签是definitions的缩写,可对诸如渐变之类的特殊元素进行定义。
线性渐变可以定义为水平,垂直或角渐变。
/*y1和y2相等,而x1和x2不同时,可创建水平渐变。
当x1和x2相等,而y1和y2不同时,可创建垂直渐变。
当x1和x2不同,且y1和y2不同时,可创建角形渐变。*/
实例 1
定义水平线性渐变从黄色到红色的椭圆形。
SVG代码
<!DOCTYPE html>
<html>
<body style="background-color: aqua;">
<title>项目</title>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
</body>
</html>
运行效果:

代码解析:
-
<linearGradient>标签的id属性可为渐变定义一个唯一的名称。
-
<linearGradient>标签的X1,X2,Y1,Y2属性定义渐变开始和结束位置。
-
渐变的颜色范围可由两种或多种颜色组成,每种颜色通过一个<stop>标签来规定。offset属性用来定义渐变的开始和结束位置。
-
填充属性把 ellipse 元素链接到此渐变。
实例 2
定义一个垂直线性渐变从黄色到红色的椭圆形。
SVG代码:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="0%" y2="100%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
运行效果:

实例 3
定义一个椭圆形,水平线性渐变从黄色到红色并添加一个椭圆内文本。
SVG代码:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<linearGradient id="grad1" x1="0%" y1="0%" x2="100%" y2="0%">
<stop offset="0%" style="stop-color:rgb(255,255,0);stop-opacity:1" />
<stop offset="100%" style="stop-color:rgb(255,0,0);stop-opacity:1" />
</linearGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" /> <text fill="#000" font-size="45" font-family="Verdana"
x="150" y="86"> SVG</text>
</svg>
运行效果:

代码解析:
<text> 元素是用来添加一个文本。
二、SVG 放射性渐变
<radialGradient>元素用于定义放射性渐变。
<radialGradient>标签必须嵌套在<defs>的内部。<defs>标签是definitions的缩写,它可对诸如渐变之类的特殊元素进行定义。
实例 1
定义一个放射性渐变从白色到蓝色椭圆。
SVG代码:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1">
<defs>
<radialGradient id="grad1" cx="50%" cy="50%" r="50%" fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255);
stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
运行效果:

代码解析:
-
<radialGradient>标签的 id 属性可为渐变定义一个唯一的名称。
-
CX,CY和r属性定义的最外层圆和Fx和Fy定义的最内层圆。
-
渐变颜色范围可以由两个或两个以上的颜色组成。每种颜色用一个<stop>标签指定。offset属性用来定义渐变色开始和结束。
-
填充属性把ellipse元素链接到此渐变。
实例 2
定义放射性渐变从白色到蓝色的另一个椭圆。
SVG代码:
<svg xmlns="http://www.w3.org/2000/svg" version="1.1"> <defs> <radialGradient id="grad1" cx="20%" cy="30%" r="30%"
fx="50%" fy="50%">
<stop offset="0%" style="stop-color:rgb(255,255,255); stop-opacity:0" />
<stop offset="100%" style="stop-color:rgb(0,0,255);stop-opacity:1" />
</radialGradient>
</defs>
<ellipse cx="200" cy="70" rx="85" ry="55" fill="url(#grad1)" />
</svg>
运行效果:

三、总结
本文基于HTML基础,介绍了图像SVG元素中的渐变效果,通过案例的分析,再实际项目中需要注意的点,对代码进行解析。开发项目中遇到的难题,都提供了一些有效的解决办法。
欢迎大家积极尝试,有时候看到别人实现起来很简单,但是到自己动手实现的时候,总会有各种各样的问题,切勿眼高手低,勤动手,才可以理解的更加深刻。
代码很简单,希望能够帮助读者更好的去学习SVG。
想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/ 想学习更多Python网络爬虫与数据挖掘知识,可前往专业网站:http://pdcfighting.com/
版权声明
本文为[Python进阶者]所创,转载请带上原文链接,感谢
https://my.oschina.net/u/4521128/blog/4705765
边栏推荐
- IPFS/Filecoin合法性:保护个人隐私不被泄露
- 從小公司進入大廠,我都做對了哪些事?
- Menu permission control configuration of hub plug-in for azure Devops extension
- Python crawler actual combat details: crawling home of pictures
- (1) ASP.NET Introduction to core3.1 Ocelot
- OPTIMIZER_ Trace details
- 深度揭祕垃圾回收底層,這次讓你徹底弄懂她
- Existence judgment in structured data
- 网络安全工程师演示:原来***是这样获取你的计算机管理员权限的!【维持】
- Python自动化测试学习哪些知识?
猜你喜欢

2018中国云厂商TOP5:阿里云、腾讯云、AWS、电信、联通 ...

DevOps是什么

采购供应商系统是什么?采购供应商管理平台解决方案

This article will introduce you to jest unit test

多机器人行情共享解决方案

如何玩转sortablejs-vuedraggable实现表单嵌套拖拽功能
![[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor](/img/cc/17b647d403c7a1c8deb581dcbbfc2f.jpg)
[JMeter] two ways to realize interface Association: regular representation extractor and JSON extractor

Working principle of gradient descent algorithm in machine learning

选择站群服务器的有哪些标准呢?

EOS创始人BM: UE,UBI,URI有什么区别?
随机推荐
Save the file directly to Google drive and download it back ten times faster
How long does it take you to work out an object-oriented programming interview question from Ali school?
《Google軟體測試之道》 第一章google軟體測試介紹
条码生成软件如何隐藏部分条码文字
High availability cluster deployment of jumpserver: (6) deployment of SSH agent module Koko and implementation of system service management
数据产品不就是报表吗?大错特错!这分类里有大学问
Grouping operation aligned with specified datum
Programmer introspection checklist
CCR炒币机器人:“比特币”数字货币的大佬,你不得不了解的知识
Working principle of gradient descent algorithm in machine learning
WeihanLi.Npoi 1.11.0/1.12.0 Release Notes
Polkadot series (2) -- detailed explanation of mixed consensus
选择站群服务器的有哪些标准呢?
Microservices: how to solve the problem of link tracing
Vuejs development specification
如何将数据变成资产?吸引数据科学家
Aprelu: cross border application, adaptive relu | IEEE tie 2020 for machine fault detection
03_ Detailed explanation and test of installation and configuration of Ubuntu Samba
速看!互联网、电商离线大数据分析最佳实践!(附网盘链接)
Top 10 best big data analysis tools in 2020