应用程序源代码:
CSS部分:
Home > Web Front-end > JS Tutorial > body text

Examples of implementing six special effects filters in HTML5 Canvas using pure JavaScript_javascript skills

WBOY
Release: 2016-05-16 17:30:56
Original
1074 people have browsed it

I tried my hand at it, implemented six simple and common HTML5 Canvas special effects filters, and encapsulated them into a pure JavaScript callable API file gloomyfishfilter.js. The supported special effect filters are:
1. Inverse color
2. Gray tone
3. Blur
4. Embossing
5. Engraving
6 .Mirror

Explanation of filter principle:
1. Inverse color: Get a pixel RGB value r, g, b and the new RGB value is (255-r, 255 -g, 255-b)
2. Gray tone: Get a pixel RGB value r, g, b and the new RGB value is

Copy code The code is as follows:

newr = (r * 0.272) (g * 0.534) (b * 0.131);
newg = (r * 0.349) (g * 0.686) (b * 0.168);
newb = (r * 0.393) (g * 0.769) (b * 0.189);

3. Blur: based on a 5*5 volume Core
4. Relief and engraving:
Based on the difference between the RGB value of the previous pixel of the current pixel and the RGB value of the next pixel plus 128
5. Mirror: simulates the object in the mirror the corresponding effect.
Miscellaneous preparation
1. How to obtain the Canvas 2d context object
Copy code The code is as follows:

var canvas = document.getElementById("target");
canvas.width = source.clientWidth;
canvas.height = source.clientHeight;
if(!canvas.getContext) {
console.log("Canvas not supported. Please install a HTML5compatible browser.");
return;
}
// get 2D context of canvas and draw image
tempContext = canvas.getContext ("2d");

2. How to draw a DOM img object into a Canvas object
Copy code The code is as follows:

var source = document.getElementById("source");
tempContext.drawImage(source, 0, 0, canvas.width,canvas.height);

3. How to get pixel data from Canvas object
Copy code The code is as follows:

var canvas = document.getElementById("target");
varlen = canvas.width * canvas.height * 4;
var canvasData = tempContext.getImageData(0, 0, canvas.width , canvas.height);
var binaryData = canvasData.data;

4. How to implement mouse Click event binding on DOM objects
Copy code The code is as follows:

function bindButtonEvent(element, type, handler)
{
if(element.addEventListener){
element.addEventListener(type, handler,false);
}else {
element.attachEvent('on' type, handler);// for IE6,7,8
}
}

5. How to call the implemented gfilter API to complete the filter function
Copy the code The code is as follows:

//Import API file
gfilter.colorInvertProcess(binaryData, len); //Call API

6. Browser support: passed the test on IE, FF, and Chrome. Support on IE is achieved through the following tags:
Copy code The code is as follows:



Effect demonstration :
Examples of implementing six special effects filters in HTML5 Canvas using pure JavaScript_javascript skills
Application source code:
CSS part:
Copy code The code is as follows:

#svgContainer {
width:800px;
height:600px;
background-color:#EEEEEE;
}
# sourceDiv { float: left; border: 2px solid blue}
#targetDiv { float: right; border: 2px solid red}

HTML source code in filter1.html:
Copy code The code is as follows:






Canvas Filter Demo

Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template