前端取图片相同颜色作为遮罩或者背景

需求

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

效果

做法

npm库

grade.js

COLOR THIEF

rgbaster.js

算法

简单做法

做法1

  • 效果
  • 代码
html 复制代码
<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进行设置
相关推荐
鹿心肺语15 分钟前
前端HTML转PDF的两种主流方案深度解析
前端·javascript
海石34 分钟前
去到比北方更北的地方—2025年终总结
前端·ai编程·年终总结
一个懒人懒人41 分钟前
Promise async/await与fetch的概念
前端·javascript·html
Mintopia1 小时前
Web 安全与反编译源码下的权限设计:构筑前后端一致的防护体系
前端·安全
输出输入1 小时前
前端核心技术
开发语言·前端
Mintopia1 小时前
Web 安全与反编译源码下的权限设计:构建前后端一体的信任防线
前端·安全·编译原理
林深现海1 小时前
Jetson Orin nano/nx刷机后无法打开chrome/firefox浏览器
前端·chrome·firefox
黄诂多1 小时前
APP原生与H5互调Bridge技术原理及基础使用
前端
前端市界2 小时前
用 React 手搓一个 3D 翻页书籍组件,呼吸海浪式翻页,交互体验带感!
前端·架构·github
文艺理科生2 小时前
Nginx 路径映射深度解析:从本地开发到生产交付的底层哲学
前端·后端·架构