Home > Article > Web Front-end > Learn while playing with HTML5 (3) - Pixels and Colors
1. Understanding Color
We can see colorful images on the computer screen. In fact, these images are composed of pixels. . So what are pixels? What is the color? (If you ask these two questions, you must be a person who loves thinking) A pixel actually corresponds to a set of consecutive binary bits in memory. Since it is a binary bit, the value of each bit can of course only be 0 or 1 already! In this way, this set of consecutive binary bits can be combined into many situations by 0 and 1, and each combination determines a color of the pixel. First look at the picture below
We can see that this picture describes six pixels, a total of 24 small Made of square boxes.
Note: A small box in the picture represents a byte , which is 8 binary bits.
Therefore, Each pixel is composed of four bytes. The meanings of these four bytes are also marked in the figure:
The first byte determines the red value of the pixel
The second byte determines the green value of the pixel
The third byte determines the blue value of the pixel
The fourth byte determines the transparency value of the pixel
The size of each sub-color value is from 0 to 255 (Question: Why can it only go to 255?), the value of transparency: 0 represents completely transparent, 255 represents completely opaque
In this way, we can use(255,0,0,255) to represent a pure redpixel
In memory, it is a 32-bit string like this: 11111111 00000000 00000000 11111111
2. Manipulate pixels
Understanding the essence of colors and pixels, we can perform more complex processing of graphics.
However, HTML5 Currently there is no method to directly manipulate pixels like setPixel or getPixel, but we also have a way
that is to use ImageData Object:
ImageData object is used to save the image pixel value. It has three attributes of width, height and data, among which the data attribute is A continuous array, all pixel values of the image are actually stored in data.
The method of saving pixel values in the data attribute is exactly the same as what we saw in the previous picture:
imageData.data[index*4 +0] imageData.data[index*4 +1] imageData.data[index*4 +2] imageData.data[index*4 +3]
The above takes out the continuous elements in the data array The four adjacent values represent the red, green, blue and transparency values of the th index+1 pixel in the image respectively. the size of.
Note:index starts from 0, there are a total of width * height pixels in the image, and a total of width * height * 4 values are stored in the array
Context object Context has three methods for creating, reading and setting ImageData objects. They are
createImageData(width, height): in memory Create an ImageData object (i.e. pixel array) of a specified size in the object. The pixels in the object are all black and transparent, i.e. rgba(0,0,0,0)
getImageData( x, y, width, height): Returns an ImageData object, which contains the pixel array of the specified area
putImageData(data, x, y): Draws the ImageData object Go to the designated area of the screen
3. A simpleimage processingexample
So much has been said above, We use the knowledge we have to play with imageprogramming, and maybe one day we will play PhotoShop in Chrome.
The program probably looks like this:
1. Draw a picture onto a canvas element. In order not to cause a security error (Security_ERR:DOM EXCEPTION 18), I use Banner background image at the top of the blog. If you want to run this example, you may need to change it to your own picture
2. There are four slide bars, representing the four components of GRBA respectively
3. Drag the slide bar to see the corresponding The color component will increase or decrease
4. If the image becomes transparent, the background of the canvas element will be displayed. I set this background as my avatar, haha.
思路:其实就是用 getImageData 方法,将你想改变的那一块区域的像素数组取出来,然后根据你拖动的滑动条和滑动条的数值,来更改那一块区域里所有像素对应颜色分量的值。处理完毕后再用 putImageData 方法绘制到画布上,就是这么简单。
下面是代码:
简单的图像处理 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><canvas id="test1" width="507" height="348" style="background-image:url(http://images.cnblogs.com/cnblogs_com/myqiao/262115/r_2204793492575248335.jpg)">你的浏览器不支持 <canvas>标签, 请使用 Chrome 浏览器 或者 FireFox 浏览器</canvas> 红色:<input type="range" min="1" max="100" onchange="colorChange(event,0)"/> 绿色:<input type="range" min="1" max="100" onchange="colorChange(event,1)"/> 蓝色:<input type="range" min="1" max="100" onchange="colorChange(event,2)"/> 透明:<input type="range" min="1" max="100" onchange="colorChange(event,3)"/> <script type="text/javascript"> //获取上下文对象 var canvas = document.getElementById("test1"); var ctx = canvas.getContext("2d"); //画布的宽度和长度 var width = parseInt(canvas.getAttribute("width")); var height = parseInt(canvas.getAttribute("height")); //装入图像 var image = new Image(); image.onload =imageLoaded; //顶部背景图片 image.src = "/skins/Valentine/images/banner2.gif"; //用来保存像素数组的变量 var imageData=null; function imageLoaded() { // 将图片画到画布上 ctx.drawImage(image, 0, 0); //取图像的像素数组 imageData = ctx.getImageData(0, 0, width, height); } function colorChange(event,offset){ imageLoaded(); for (var y = 0; y < imageData.height; y++) { for (x = 0;x < imageData.width; x++) { //index 为当前要处理的像素编号 var index = y * imageData.width + x; //一个像素占四个字节,即 p 为当前指针的位置 var p = index * 4; //改变当前像素 offset 颜色分量的数值,offset 取值为0-3 var color = imageData.data[p + offset] * event.target.value / 50; // 颜色值限定在[0..255] color = Math.min(255, color); //将改变后的颜色值存回数组 imageData.data[p + offset]=color } } //输出到屏幕 ctx.putImageData(imageData, 0, 0); } </script>
四、绘制随机颜色的点
这个例子是在画布上随机选择一个点,然后再给他一个随机的颜色值,其实用到的方法和上面的例子大同小异,就是需求不同罢了。
下面是代码和程序实例:
随机颜色的点 Code highlighting produced by Actipro CodeHighlighter (freeware) http://www.CodeHighlighter.com/ --><canvas id="test2" width="300" height="300" style=" background-color: black">你的浏览器不支持 <canvas>标签,请使用 Chrome 浏览器 或者 FireFox 浏览器</canvas> <input type="button" value="画随机点" onclick="interval=setInterval(randomPixel,1);" /> <input type="button" value="停止" onclick="clearInterval(interval);"/> <input type="button" value="清除" onclick="clearCanvas();"/> <script type="text/javascript"> //获取上下文对象 var canvas = document.getElementById("test2"); var ctx = canvas.getContext("2d"); //画布的宽度和长度 var width = parseInt(canvas.getAttribute("width")); var height = parseInt(canvas.getAttribute("height")); var imageData = ctx.createImageData(width, height); function randomPixel(){ var x= parseInt(Math.random()*width); var y= parseInt(Math.random()*height); var index = y * width + x; var p = index * 4; imageData.data[p + 0] = parseInt(Math.random() * 256); imageData.data[p + 1] = parseInt(Math.random() * 256); imageData.data[p + 2] = parseInt(Math.random() * 256); imageData.data[p + 3] =parseInt(Math.random() * 256); ctx.putImageData(imageData,0,0); } function clearCanvas(){ ctx.clearRect(0,0,width,height); imageData = ctx.createImageData(width, height); } </script>
The above is the detailed content of Learn while playing with HTML5 (3) - Pixels and Colors. For more information, please follow other related articles on the PHP Chinese website!