在vue中获取图片的主色调

<template>
    <div>
        <div>
            在vue中获取图片的主色调
            <a href="https://lokeshdhakar.com/projects/color-thief/">官网传送门</a>
            <p>本次仅讲局部引入的方式</p>
            <p>安装:npm i --save colorthief</p>
        </div>

        <img id="background" :src="url" height="100" width="100" crossorigin="anonymous"/><!--后面的属性允许获取网络上的图片-->
        <ul class="show">
            <li v-for="color in colors" :style="{background:`rgb(${color.toString()})`}">
                <!--                rab:{{color.toString()}}-->
            </li>
        </ul>
    </div>
</template>

<script>
    import ColorThief from "colorthief";

    export default {
        name: "Color",
        data() {
            return {
                url: '',
                colors: [],
                //210042 449818741 1442312981
            }
        },
        methods: {
            ImgColor() {
                this.$axios.get(`https://autumnfish.cn/song/detail`,
                    {params: {ids: 210042}}).then((song) => {
                    this.url = song.data.songs[0].al.picUrl;
                    let domImg = document.querySelector('#background');
                    let colorthief = new ColorThief();
                    domImg.addEventListener('load', () => {
                        console.log('加载好了并取色', colorthief.getPalette(domImg, 2));//第二个参数可选(1~10)
                        this.colors = colorthief.getPalette(domImg);
                    })
                })
            }
        },
        mounted() {
            this.ImgColor();
        }
    }
</script>

<style scoped>
    * {
        padding: 0;
        margin: 0;
        list-style: none;
    }

    a {
        color: #835cff;
        text-decoration: none;
    }

    img {
        border-radius: 15px;
    }

    .show > li {
        width: 100px;
        height: 30px;
        border-radius: 15px;
    }
</style>

效果图:

在vue中获取图片主色调效果图

 

 

 

 

  • 8
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值