<template>
<div class="container">
<img :src="originalImage" class="original" @load="convertToBlackAndWhite">
<img :src="blackAndWhiteImage" class="black-and-white" v-show="blackAndWhite">
</div>
</template>
<script>
export default {
data() {
return {
originalImage: 'images/pic.jpg',
blackAndWhiteImage: '',
blackAndWhite: false
};
},
methods: {
convertToBlackAndWhite() {
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;
if (brightness > 128) {
data[i] = 255;
data[i + 1] = 255;
data[i + 2] = 255;
} else {
data[i] = 0;
data[i + 1] = 0;
data[i + 2] = 0;
}
}
ctx.putImageData(imageData, 0, 0);
this.blackAndWhiteImage = canvas.toDataURL();
this.blackAndWhite = true;
}
}
};
</script>
<style scoped>
.container {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
}
.original, .black-and-white {
max-width: 300px;
}
.black-and-white {
display: none;
}
</style>
以上是编程学习网小编为您介绍的“如何在Vue中实现图片的黑白处理?”的全面内容,想了解更多关于 vuejs 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:如何在Vue中实现图片的黑白处理?
基础教程推荐
猜你喜欢
- jQuery层叠选择器用法实例分析 2024-04-08
- 用js+cookie记录滚动条位置 2024-04-15
- 浅谈CSS不规则边框的生成方案 2022-11-20
- JS原生手写轮播图效果 2024-04-09
- 基于JavaScript实现 获取鼠标点击位置坐标的方法 2023-12-01
- layui formSelects实现下拉框select多选,并且编辑时候回显 2023-11-30
- 微信小程序 跳转方式总结 2024-01-03
- layui tree组件勾选后做判断,如果不符合条件则取消勾选 2024-11-22
- Ajax实现登录案例 2023-02-23
- ajax实现用户名校验的传统和jquery的$.post方式(实例讲解) 2023-02-14