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

需求

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

效果

做法

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进行设置
相关推荐
BigTopOne4 分钟前
WebRTC Native / Android 开发学习资源整理
前端
excel1 小时前
nuxt 3 升级 nuxt 4 报错之 hasInjectionContext
前端
何以解忧,唯有..1 小时前
LangChain 工具调用(Tool Calling)实战指南
java·前端·langchain
软件聚导航2 小时前
「聚小软 AI 助手」技术升级:从数据库检索到 RAG 知识库问答
前端·数据库·mysql·微信小程序·小程序·ai编程·rag
恋猫de小郭3 小时前
看懂大模型架构术语,帮助你理解目前常见的大模型开源架构
前端·人工智能·ai编程
yma163 小时前
通过提示词实现GitHub Pages 和 Nginx 双部署竟然这么简单?
前端
前端 贾公子3 小时前
第09章:上下文与记忆 (2)
java·服务器·前端
可乐鸡翅yeah_3 小时前
第三方接口对接 M3U8 流媒体排坑实战,解决外部服务商流兼容难题
前端·javascript·python·django·html·m3u8·m3u8在线
GoppViper3 小时前
RDF资源描述框架深度解析:语义Web的数据基石与实战逻辑
前端·数据库
郑州光合科技余经理3 小时前
本地生活服务系统:模块边界与结算字段怎么拆
java·开发语言·前端·后端·系统架构·uni-app·php