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


    需求

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

    效果

    做法

    npm库

    grade.js

    • 所提供图像中前 2 个主色生成的互补渐变
    • https://github.com/benhowdle89/grade

    COLOR THIEF

    • 只需使用Javascript即可从图像中获取调色板。

    • https://github.com/lokesh/color-thief

    • https://lokeshdhakar.com/projects/color-thief

    rgbaster.js

    • https://github.com/briangonzalez/rgbaster.js
    • 一个非常简单、零依赖、基于 promise 的 javascript 库,用于从图像(在浏览器中)中提取主要颜色。

    算法

    • 基本思路都是获取每一种rgb的出现次数,进行计算分析,如果出现跨域问题还需要后端配合解决

    • https://kuangyx.cn/docs/%E6%96%87%E7%AB%A0/%E5%89%8D%E7%AB%AF/%E6%8F%90%E5%8F%96%E5%9B%BE%E7%89%87%E4%B8%BB%E9%A2%98%E8%89%B2.html

    • 也可以直接使用这位大佬写好的

      • https://github.com/hubingliang/colorfulImg
      • 代码里面的colorfulImg

    简单做法

    做法1

    • 效果

    • 代码
    <html lang="en">
      <head>
        <meta charset="UTF-8" />
        <meta name="viewport" content="width=device-width, initial-scale=1.0" />
        <title>Documenttitle>
      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>
    
    
    • 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

    参考文章

    • https://www.cnblogs.com/coco1s/p/14754188.html
    • https://www.zhangxinxu.com/wordpress/2014/08/image-dominant-color-get-rgbaster-js/
    • https://blog.csdn.net/weixin_64388392/article/details/134426851

    后话

    • 其实简单做法里面的思路,也可以根据背景来设置字体颜色,大概思路就是字体颜色设置为透明,并利用background-clip进行设置
  • 相关阅读:
    个人微信号管理工具哪个好?
    readline
    Ubuntu22.04如何开机重新自动运行脚本
    视觉系统设计实例halcon-winform-13.图像存储
    Vue2学习重点总结(Vue配置和使用+Vue脚手架配置代理+Vuex+Router)
    国际啤酒节真好玩,有中兴通讯在还不用怕没网!
    go 中 select 源码阅读
    安泰:精密电流源电路原理及应用
    go初识iris框架(六) - session使用和控制
    Java学数据结构(4)——PriorityQueue(优先队列)& 二叉堆(binary heap)
  • 原文地址:https://blog.csdn.net/u014582342/article/details/136309865