How to implement grayscale and black and white processing of images in Vue?
In front-end development, it is often necessary to perform some special effects processing on images, such as converting images to grayscale or black and white. In Vue, we can use some simple techniques to achieve these effects. This article will introduce how to implement grayscale and black-and-white processing of images in Vue, and attach corresponding code examples.
Grayscale processing
Grayscale processing is to convert color pictures into black and white pictures, so that the pictures only have grayscale information and no color information. The following is a code example to implement grayscale processing in Vue:
<template> <div class="container"> <img class="original lazy" src="/static/imghwm/default1.png" data-src="originalImage" : @load="convertToGrayscale" alt="How to implement grayscale and black and white processing of images in Vue?" > <img class="grayscale lazy" src="/static/imghwm/default1.png" data-src="grayscaleImage" : v-show="grayscale" alt="How to implement grayscale and black and white processing of images in Vue?" > </div> </template> <script> export default { data() { return { originalImage: 'path/to/original/image.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>
In the above code, we bind it through the src
attribute of the <img class="original lazy" src="/static/imghwm/default1.png" data-src="originalImage" alt="How to implement grayscale and black and white processing of images in Vue?" >
tag An original color picture. When the image is loaded, use the convertToGrayscale
method to convert the image to grayscale. In the method, we create a canvas
element, obtain the context of the canvas through getContext('2d')
, and then draw the original image to the canvas. Next, we get the pixel data on the canvas, convert the RGB value of each pixel into a brightness value, then update the RGB value in the pixel data to a brightness value, and finally put the updated pixel data back into the canvas. Finally, the canvas is converted to the base64 encoding of the image by calling the toDataURL
method, and bound to the src
attribute of the grayscale image.
In the above code, we use Vue’s scoped
style to ensure that the style rules only apply to the current component.
Black and white processing
Black and white processing is to convert color pictures into pictures with only black and white. The following is a code example to implement black and white processing in Vue:
<template> <div> <img class="original lazy" src="/static/imghwm/default1.png" data-src="originalImage" : alt="How to implement grayscale and black and white processing of images in Vue?" > <img class="black-and-white lazy" src="/static/imghwm/default1.png" data-src="blackAndWhiteImage" : v-show="blackAndWhite" alt="How to implement grayscale and black and white processing of images in Vue?" > </div> </template> <script> export default { data() { return { originalImage: 'path/to/original/image.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>
In the above code, similar to grayscale processing, we also create a canvas
element and draw the original color picture to the canvas middle. Next, we get the pixel data on the canvas and convert the RGB value of each pixel into a brightness value. The difference is that by comparing the brightness value with the threshold 128, we set the pixels with a brightness greater than 128 as white, and the pixels with a brightness less than 128 as black. Finally, we put the pixel data back into the canvas and convert the canvas to the base64 encoding of the image by calling the toDataURL
method. Finally, we display the black and white image on the page through Vue's data binding again.
The above is the method and sample code to implement image grayscale processing and black and white processing in Vue. These techniques can help us achieve various picture special effects in front-end development. Hope this article is helpful to you!
The above is the detailed content of How to implement grayscale and black and white processing of images in Vue?. For more information, please follow other related articles on the PHP Chinese website!

vue中props可以传递函数;vue中可以将字符串、数组、数字和对象作为props传递,props主要用于组件的传值,目的为了接收外面传过来的数据,语法为“export default {methods: {myFunction() {// ...}}};”。

本篇文章带大家聊聊vue指令中的修饰符,对比一下vue中的指令修饰符和dom事件中的event对象,介绍一下常用的事件修饰符,希望对大家有所帮助!

如何覆盖组件库样式?下面本篇文章给大家介绍一下React和Vue项目中优雅地覆盖组件库样式的方法,希望对大家有所帮助!

react与vue的虚拟dom没有区别;react和vue的虚拟dom都是用js对象来模拟真实DOM,用虚拟DOM的diff来最小化更新真实DOM,可以减小不必要的性能损耗,按颗粒度分为不同的类型比较同层级dom节点,进行增、删、移的操作。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
