<template>
<div class="container">
<img :src="originalImage" class="original" @load="convertToGrayscale">
<img :src="grayscaleImage" class="grayscale" v-show="grayscale">
</div>
</template>
<script>
export default {
data() {
return {
originalImage: '/images/pic.jpg',
grayscaleImage: '',
grayscale: false
};
},
methods: {
convertToGrayscale() {
let canvas = document.createElement('canvas');
let ctx = canvas.getContext('2d');
let img = document.querySelector('.original');
canvas.width = img.width;
canvas.height = img.height;
ctx.drawImage(img, 0, 0);
let imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
let data = imageData.data;
for (let i = 0; i < data.length; i += 4) {
let brightness = (data[i] + data[i + 1] + data[i + 2]) / 3;
data[i] = brightness;
data[i + 1] = brightness;
data[i + 2] = brightness;
}
ctx.putImageData(imageData, 0, 0);
this.grayscaleImage = canvas.toDataURL();
this.grayscale = true;
}
}
};
</script>
<style scoped>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.original, .grayscale {
max-width: 300px;
}
.grayscale {
display: none;
}
</style>
以上是编程学习网小编为您介绍的“如何在Vue中实现图片的灰度处理?”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:如何在Vue中实现图片的灰度处理?
基础教程推荐
猜你喜欢
- Vue项目问题——vue-router重写push方法,解决相同路径跳转报错 2023-10-08
- js实现从右向左缓缓浮出网页浮动层广告的方法 2024-03-11
- 纯CSS实现鼠标放上滑动出半透明效果的文字说明(新手小白 必看) 2024-01-23
- CSS将div内容垂直居中案例总结 2022-11-20
- 纯CSS实现一个简单步骤条的示例代码 2024-03-31
- 用ul、li标签创建css横向导航菜单示例 2024-03-10
- Vue3.0的设计目标是什么?做了哪些优化? 2023-10-08
- 关于js中window.location.href,location.href,parent.location.href,top.location.href的用法与区别 2024-01-03
- 如何处理JSON中的特殊字符 2024-02-07
- Three.Js实现看房自由小项目 2024-01-07