需求

  • 遮罩层取图片相同/相似的颜色作为遮罩

效果

做法

npm库

grade.js

COLOR THIEF

rgbaster.js

算法

简单做法

做法1

  • 效果

  • 代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Document</title>
</head>
<body>
<!-- 原始图 -->
<div class="origin"></div>
<!-- 模糊 -->
<div class="mask"></div>
<style>
div.origin {
position: relative;
width: 320px;
height: 200px;
overflow: hidden;
background-repeat: no-repeat;
background-image: url("https://oss.dreamlove.top/i/2024/02/20/xqsvvg.png");
}
div.mask{
position: relative;
width: 320px;
height: 200px;
overflow: hidden;
/* 关键代码 */
background-repeat: no-repeat;
background-image: url("https://oss.dreamlove.top/i/2024/02/20/xqsvvg.png");
background-size: 20000%;
background-position: center;
}
</style>
</body>
</html>

参考文章

后话

  • 其实简单做法里面的思路,也可以根据背景来设置字体颜色,大概思路就是字体颜色设置为透明,并利用background-clip进行设置