一篇文章带你了解SVG 蒙版(Mask)

简介: 一篇文章带你了解SVG 蒙版(Mask)

image.png

SVG蒙版功能可将蒙版应用于SVG形状。蒙版可确定SVG形状的哪些部分可见,以及具有什么透明度。运行效果可以将SVG蒙版视为剪切路径的更高级版本。


一、简单的蒙版


代码解析:

本示例使用ID=mask1定义一个蒙版。


元素内部是一个元素。元素定义了蒙版的形状。


定义了一个使用mask的元素,元素使用CSS style属性mask内部引用mask ID属性。

例:

<svg width="500" height="120"><defs><mask id="mask1" x="0" y="0" width="100" height="100"><rect x="0" y="0" width="100" height="50" style="stroke:none; fill: #ffffff" /></mask></defs><rect x="1" y="1" width="100" height="100" style="stroke: none; fill: #0000ff; mask: url(#mask1)" /></svg>

注:

要显示的矩形的高度为100像素,但垂直的前50个像素是可见的。那是因为蒙版矩形只有50个像素高。矩形仅在蒙版矩形所覆盖的部分中可见。


黑色轮廓矩形是没有蒙版的矩形的大小。


二、其他形状的蒙版


可以使用任何SVG形状作为蒙版。


使用圆圈作为蒙版。

<svg> <defs>   <mask id="mask2" x="0" y="0" width="100" height="100" >     <circle cx="25" cy="25" r="25" style="stroke:none; fill: #ffffff"/>   </mask> </defs>
 <rect x="1" y="1" width="100" height="100"   style="stroke: none; fill: #0000ff; mask: url(#mask2)"/>
</svg>

运行效果:

image.png

注:仅在可见蒙版圆的地方可见引用蒙版的矩形。


三、蒙版形状颜色定义蒙版不透明度


1. 如何去定义不透明度 ?

蒙版形状(圆形或矩形)的填充颜色设置为#ffffff。


蒙版形状的颜色定义使用蒙版的形状的不透明度。蒙版形状的颜色越接近#ffffff(白色),使用蒙版的形状将越不透明。蒙版形状的颜色越接近#000000(黑色),使用蒙版的形状将越透明。


2. 案例

其中蒙版由两个具有不同颜色(#ffffff和#66666)的矩形组成。蒙版用于单个矩形,因此运行效果可以使用蒙版查看蒙版中的两个不同形状如何影响相同形状。

<svg width="500" height="120"><defs> <mask id="mask3" x="0" y="0" width="100" height="100" >
   <rect x="0" y="0"  width="100" height="50"         style="stroke:none; fill: #ffffff"/>
   <rect x="0" y="50" width="100" height="50"         style="stroke:none; fill: #666666"/> </mask></defs>
<text x="10" y="55" style="stroke: none; fill: #000000;">  此文本在矩形下方</text>
<rect x="1" y="1" width="100" height="100"   style="stroke: none; fill: #0000ff; mask: url(#mask3)"/> </svg>

运行效果:

image.png


四、在蒙版中使用渐变


如果对用作蒙版的形状应用渐变,则可以实现蒙版所应用的形状的渐变透明度。


使用渐变的蒙版,使用蒙版的矩形以及该矩形下的文本,因此可以看到其透明度如何随着蒙版的渐变而变化。

例:

<svg width="500" height="120"><defs><linearGradient id="gradient1" x1="0%" y1="0%" x2="100%" y2="0%" spreadMethod="pad"><stop offset="0%" stop-color="#ffffff" stop-opacity="1" /><stop offset="100%" stop-color="#000000" stop-opacity="1" /></linearGradient>
<mask id="mask4" x="0" y="0" width="200" height="100"><rect x="0" y="0" width="200" height="100" style="stroke:none; fill: url(#gradient1)" /></mask></defs>
<text x="10" y="55" style="stroke: none; fill: #000000;">此文本在矩形下方</text><rect x="1" y="1" width="200" height="100" style="stroke: none; fill: #FF0000; mask: url(#mask4)" /></svg>

运行效果:

image.png

注:渐变蒙版可以与其他效果(例如填充图案)结合使用。

SVG代码:

<svg width="500" height="120"><defs>
 <linearGradient id="gradient2"               x1="0%"   y1="0%"               x2="100%" y2="0%"               spreadMethod="pad">   <stop offset="0%"   stop-color="#ffffff" stop-opacity="1"/>   <stop offset="100%" stop-color="#000000" stop-opacity="1"/> </linearGradient>
 <pattern id="pattern2"        x="10" y="10" width="20" height="20"        patternUnits="userSpaceOnUse" >
   <circle cx="10" cy="10" r="10" style="stroke: none; fill: #FF0000; " />
 </pattern>
 <mask id="mask6" x="0" y="0" width="200" height="100" >     <rect x="0" y="0"  width="200" height="100"         style="stroke:none; fill: url(#gradient2)"/> </mask></defs>
<text x="10" y="55" style="stroke: none; fill: #000000;">  此文本在矩形下方</text><rect x="1" y="1" width="200" height="100"     style="stroke: none; fill: url(#pattern2); mask: url(#mask6)"/> </svg>

运行效果:

image.png

注:其中可见矩形使用填充图案作为填充,并在其蒙版中使用渐变。


要显示的矩形如何引用其CSS属性中的fill填充图案,以及如何引用其CSS属性中的mask蒙版。


五、在蒙版中使用填充图案


也可以在蒙版中使用填充图案,从而使蒙版成为填充图案的形状。

例:

<svg width="500" height="120"><defs> <pattern id="pattern1"        x="10" y="10" width="20" height="20"        patternUnits="userSpaceOnUse" >
     <circle cx="10" cy="10" r="10" style="stroke: none; fill: #999999" /> </pattern>
 <mask id="mask5" x="0" y="0" width="200" height="100" >   <rect x="0" y="0"  width="200" height="100"       style="stroke:none; fill: url(#pattern1)"/> </mask></defs>
<text x="10" y="55" style="stroke: none; fill: #000000;">  此文本在矩形下方</text><rect x="1" y="1" width="200" height="100"   style="stroke: none; fill: #FF0000; mask: url(#mask5)"/> </svg>

运行效果

image.png

注:矩形现在是半透明的,其中填充图案绘制了圆圈,而在其他位置完全透明。


六、总结


本文基于HTML基础,介绍了SVG中蒙版的应用。定义不同形状的蒙版,设置蒙版的不透明度,蒙版中使用渐变,以及蒙版应用填充图案。都通过项目,进行详细的讲解。


希望能够帮助你更好的学习。

------------------- End -------------------

相关文章
npm如何切换淘宝源镜像
npm如何切换淘宝源镜像
4196 0
|
Windows
FL Studio20.9.2永久免费中文版水果软件
FL Studio随着近年来摇滚、电音的发展,越来越多的人开始对电子音乐编曲感兴趣,而电音编曲的首要条件,就是需要一个好的DAW(数字音频工作站),常用的DAW有很多,例如Cubase、Nuendo、Pro Tools、 SONAR等等,但以小编的个人体验感而言,最顺手的还是FL Studio。
3394 0
|
5月前
|
数据采集 移动开发 算法
【硬件测试】基于FPGA的QPSK调制+软解调系统开发与硬件片内测试,包含信道模块,误码统计模块,可设置SNR
本文基于FPGA实现QPSK调制与软解调系统,包含Testbench、高斯信道、误码率统计模块,并支持不同SNR设置。硬件版本新增ILA在线数据采集和VIO在线SNR设置功能,提供无水印完整代码及测试结果。通过VIO分别设置SNR为6dB和12dB,验证系统性能。配套操作视频便于用户快速上手。 理论部分详细解析QPSK调制原理及其软解调实现过程,涵盖信号采样、相位估计、判决与解调等关键步骤。软解调通过概率估计(如最大似然法)提高抗噪能力,核心公式为*d = d_hat / P(d_hat|r[n])*,需考虑噪声对信号点分布的影响。 附Verilog核心程序代码及注释,助力理解与开发。
145 5
|
XML 分布式计算 Hadoop
hadoop
hadoop
1417 0
|
JSON Java Go
Go 语言性能优化技巧
在Go语言中优化性能涉及数字字符串转换(如用`strconv.Itoa()`代替`fmt.Sprintf()`)、避免不必要的字符串到字节切片转换、预分配切片容量、使用`strings.Builder`拼接、有效利用并发(`goroutine`和`sync.WaitGroup`)、减少内存分配、对象重用(`sync.Pool`)、无锁编程、I/O缓冲、正则预编译和选择高效的序列化方法。这些策略能显著提升代码执行效率和系统资源利用率。
222 13
ECharts 饼图切换数据源bug 开始没数据显示 切换或刷新后显示
ECharts 饼图切换数据源bug 开始没数据显示 切换或刷新后显示
300 0
|
11月前
|
存储 人工智能 网络安全
科技云报到:云服务的中场战事,从AI应用开始
从去年的大模型之战,到今年的AI应用之争,云服务正在迈入全新的发展阶段。AI这个杠杆将各家厂商的竞争策略更向前推进了一步。
225 0
|
Web App开发 JSON 小程序
苹果app开发apple-app-site-association文件配置
apple-app-site-association 是苹果的配置文件,用于建立app和网站关联,支持Universal Links,使点击网站链接能直接打开相应app内部页面。配置文件为JSON格式,需上传至服务器`.well-known`目录或根目录。通过检查三个链接来测试配置,确保Content-Type为`application/json`。成功配置后,点击链接能在iPhone备忘录或Safari中直接唤起app,但可能有24-48小时延迟。
SVG pattern 使用(patternUnits、patternContentUnits)
SVG pattern 使用(patternUnits、patternContentUnits)
412 0