<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图片处理
上面是“如何在Vue中实现图片的灰度处理?”的全面内容,想了解更多关于 vuejs 内容,请继续关注web建站教程。
当前网址:https://ipkd.cn/webs_12894.html
workflows工作流
- 一艘来自工业时代的飞船ComfyUI工作流
- 一个孤独的身影在未来主义城市
- 一幅以霓虹灯照亮的城市天际线和未来主义画
- 羚羊安上小白兔的门牙ComfyUI工作流
- 丛林里坐着一尊佛像
- 一尊白玉猴王ComfyUI工作流
- 彩色雄鹰玉雕ComfyUI工作流
- 一架令人难忘的美丽钢琴ComfyUI工作流
猜你喜欢
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!