微信小程序根据图片生成背景颜色有效果图

效果图


取得是图片中间10个像素算出背景颜色

.wxml

复制代码
<canvas type="2d" id="imageCanvas" style="--w: {{w}}px;--h: {{h}}px;" />
<view style="background: {{backgroundColor}};">
  <image bind:tap="updateIndex" data-index="{{index}}" wx:for="{{image}}" 
  wx:key="index" src="{{item}}" mode="aspectFill" style="{{select === index?'border:2px solid red;':''}}" />
</view>

.wxss

复制代码
canvas{
  visibility: hidden;
  position: absolute;
  z-index: -1;
  width: var(--w);
  height: var(--h);
  left: var(--w);
  top: calc(var(--w) * -1);
}
view{
  height: 100vh;
  text-align: center;
  transition: all 0.5s;
}
image{
  width:300rpx;
  height:300rpx;
  margin: 30rpx 30rpx 0;
  box-sizing: border-box;
}

.js

复制代码
Page({
  data:{
    w:0,
    h:0,
    image:[
      'https://c-ssl.dtstatic.com/uploads/blog/202402/27/1mS1Nve5iQqnQpN.thumb.400_0.jpg',
      'https://c-ssl.dtstatic.com/uploads/blog/202402/23/aLS3821yt0BE1yW.thumb.400_0.jpeg',
      'https://c-ssl.dtstatic.com/uploads/blog/202402/25/lGSgwXVEhxOzbqx.thumb.400_0.jpeg',
      'https://c-ssl.dtstatic.com/uploads/blog/202402/27/lGS1anBpixO7xG3.thumb.400_0.jpg',
    ],
    select:0
  },
  onLoad(options) {
    this.getColor()
  },
  updateIndex(e){
    this.setData({
      select:e.currentTarget.dataset.index
    })
    this.getColor()
  },
  getColor(){
    var that = this
    const src = that.data.image[that.data.select]
    that.getImageData(src, pixels => {
      const mainColor = that.MainColor(pixels);
      that.setData({
        backgroundColor: `rgb(${mainColor[0]}, ${mainColor[1]}, ${mainColor[2]})`,
      });
    });
  },
  getImageData(src, fn) {
    var that = this
    wx.getImageInfo({
      src,
      success(res) {
        const { width, height } = res;
        that.setData({
          w:width,
          h:height
        })
        wx.createSelectorQuery().select('#imageCanvas').fields({ node: true, size: true }).exec((rect) => {
          const canvas = rect[0].node
          const ctx = canvas.getContext('2d')
          canvas.width = width
          canvas.height = height
          const image = canvas.createImage()
          image.src = src
          image.onload = () => {
            ctx.drawImage(image,0,0,width,height)
            const x = (width - 10) / 2 | 0,y = (height - 10) / 2 | 0
            const o = ctx.getImageData(x, y, 10, 10)
            fn(o.data);
          };
        })
      },
    });
  },
  MainColor(pixels) {
    const colorFrequency = {};
    for (let i = 0; i < pixels.length; i += 4) {
      const red = pixels[i];
      const green = pixels[i + 1];
      const blue = pixels[i + 2];
      const key = `${red},${green},${blue}`;
      if (!colorFrequency[key]) {
        colorFrequency[key] = 0;
      }
      colorFrequency[key]++;
    }
    let mainColor = '';
    let maxCount = 0;
    Object.keys(colorFrequency).forEach(key => {
      if (colorFrequency[key] > maxCount) {
        maxCount = colorFrequency[key];
        mainColor = key.split(',').map(Number);
      }
    });
    return mainColor;
  },
})

遇到问题可以看我主页加我Q,很少看博客,对你有帮助别忘记点赞收藏。

相关推荐
汝生淮南吾在北5 小时前
SpringBoot3+Vue3小区物业报修系统+微信小程序
微信小程序·小程序·vue·毕业设计·springboot·课程设计·毕设
静待雨落5 小时前
如何在Taro项目中使用axios
微信小程序·taro
前端小黑屋6 小时前
小程序直播挂件Pendant问题
前端·微信小程序·直播
汤姆yu6 小时前
基于微信小程序的自习室座位预约系统
微信小程序·小程序
喝牛奶的小蜜蜂8 小时前
微信小程序|云环境共享-使用指南
前端·微信小程序·ai编程
千寻技术帮10 小时前
10379_基于SSM的校园跑腿服务平台
mysql·微信小程序·校园跑腿·ssm
fanruitian11 小时前
微信小程序 springboot获取手机号
spring boot·微信小程序·notepad++
鲁Q同志14 小时前
微信小程序调用上一页的方法(主包,分包)
微信小程序·小程序
风月歌14 小时前
小程序项目之基于微信小程序的高校课堂教学管理系统源代码(源码+文档)
java·微信小程序·小程序·毕业设计·源码
计算机毕设指导62 天前
基于微信小程序的校园食堂点餐系统【源码文末联系】
java·spring boot·微信小程序·小程序·tomcat·maven·intellij-idea