Home > Web Front-end > JS Tutorial > JS black-and-white settings for pictures

JS black-and-white settings for pictures

php中世界最好的语言
Release: 2018-04-28 15:47:49
Original
2662 people have browsed it

This time I will bring you JS to black and white settings for pictures. What are the precautions for setting black and white pictures with JS? The following is a practical case, let's take a look.

HTML5 introduced the element, which allows us to dynamically draw images in the area through js. This time we made the image black and white by using js to operate the element.

Let’s take a look at the renderings first

The left side is the

img tag, and the right side is the canvas element tag. The structure is as follows The code of

<img src="1.jpg" style="width: 200px; height: 199px"/>
<canvas id="drawing" width="200" height="199" ></canvas>
Copy after login
JS is very simple, with only more than 20 lines. However, in line with the attitude of teaching people how to fish, the source code will be posted at the bottom. Let’s talk about the theory first.

1. About pictures

Everyone should know that the so-called picture is composed of pixels, that is to say, a picture of 300*300 size. There are 300*300 pixels in total, and each pixel is composed of three primary colors (red, green, blue) plus transparency (alpha). So if we want to change the image data of the image, we actually change the data of each pixel of the image.

2. Regarding API

we use

var drawing = document<a href="http://www.php.cn/code/658.html" target="_blank">.getElementById('drawing')</a>; this method You can get the canvas object. The canvas object provides two drawing methods, 2D and 3D. Here we use 2D drawing Use the
context2d = drawing.getContext('2d'); method to obtain the context2d object. After getting the context2d object, we obtain the image data through the method getImageData() provided by context2d. getImageData() has 4 parameters, which respectively represent the x and y coordinates of the screen area and the pixel width and height of the area var imagedata = context2d.getImageData(0, 0, image.width, image.height);

The ImageData object has three properties: width, height and

data. The data attribute is an
array, which stores the data of each pixel in the image. And represented by, red, green, blue, alpha. Then if we want to change the image data of the image, we need to change the data attribute data of imagedata and change the values ​​​​of red, green, blue, and alpha.

Here is the source code

function drawImageData () {
      var context2d = null;
      if(drawing.getContext) {
        context2d = drawing.getContext('2d');
      }
      if (context2d == null) {
        return;
      }
      var image = document.images[0];
      context2d.drawImage(image, 0 , 0);
      var imagedata = context2d.getImageData(0, 0, image.width, image.height);
      var data = imagedata.data;
      console.log('data: ' + data);
      var i,len,red,green,blue,alpha,average;
      for (i = 0 , len = data.length; i < len; i+=4) {
        red = data[i];
        green = data[i + 1];
        blue = data[i + 2];
        alpha = data[i + 3];
        average = Math.floor((red + green + blue) / 3);
        data[i] = average; 
        data[i+1] = average; 
        data[i+2] = average;
      }
      imagedata.data = data;
      context2d.putImageData(imagedata, 0, 0);
    }
Copy after login
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

What steps are needed to determine the type and size of uploaded images

How to remove duplicates when merging multiple arrays Data

#Detailed explanation of the use of React props and state attributes

The above is the detailed content of JS black-and-white settings for pictures. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template