HTML5 Canvas Image Effects App – Adding Blur. Today we continue HTML5 canvas examples, hope that you remember application which we made quite 2 weeks ago? Sure that this was very interesting to you to investigate image processing functions. Today I decided to continue and added new filter here – Blur.
Here are our demo and downloadable package:
[sociallocker]
[/sociallocker]
Ok, download the example files and lets start coding !
Step 1. HTML
Here are all html of my demo
index.html
04 | <link href="css/main.css" rel="stylesheet" type="text/css" /> |
05 | <script type="text/javascript" src="js/script.js"></script> |
06 | <title>HTML5 Canvas Image Effects App - Adding Blur (demo)</title> |
12 | <input type="button" onclick="resetToGrayscale()" value="Grayscale" /><br /> |
13 | <input type="button" onclick="resetToColor()" value="Color" /><br /> |
14 | <input type="button" onclick="resetToBlur()" value="Blur(1)" /><br /> |
15 | <input type="button" onclick="reset()" value="Reset" /><br /> |
16 | <input type="button" onclick="changeGrayValue(0.1)" value="Lighter" /><br /> |
17 | <input type="button" onclick="changeGrayValue(-0.1)" value="Darker" /><br /> |
18 | Red: <input type="button" onclick="changeColorValue('er', 10)" value="More" /> |
19 | <input type="button" onclick="changeColorValue('er', -10)" value="Less" /><br /> |
20 | Green: <input type="button" onclick="changeColorValue('eg', 10)" value="More" /> |
21 | <input type="button" onclick="changeColorValue('eg', -10)" value="Less" /><br /> |
22 | Blue: <input type="button" onclick="changeColorValue('eb', 10)" value="More" /> |
23 | <input type="button" onclick="changeColorValue('eb', -10)" value="Less" /> |
24 | Blur: <input type="button" onclick="changeBlurValue(1)" value="More" /> |
25 | <input type="button" onclick="changeBlurValue(-1)" value="Less" /><br /> |
28 | <canvas id="orig" width="500" height="333"></canvas> |
29 | <canvas id="panel" width="500" height="333"></canvas> |
31 | <div style="clear:both;"></div> |
Make attention, that since today I made 2 canvas objects. First one will contain original image – second one – our effects. Also I added 3 new buttons. First – to switch to Blur effect, next 2 – increase / decrease blur level.
Step 2. CSS
Here are used CSS styles.
css/main.css
01 | body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0} |
02 | .example{background:#FFF;width:730px;font-size:80%;border:1px #000 solid;margin:20px auto;padding:15px;position:relative;-moz-border-radius: 3px;-webkit-border-radius: 3px} |
15 | background-color:#888; |
Step 3. JS
Now – most interesting – its inner functionality (HTML5 canvas script).
js/script.js
013 | window.onload = function() { |
015 | canvasOrig = document.getElementById('orig'); |
016 | contextOrig = canvasOrig.getContext('2d'); |
018 | var imgObj = new Image(); |
019 | imgObj.onload = function () { |
022 | contextOrig.drawImage(imgObj, 0, 0, iW, iH); |
024 | imgObj.src = 'images/01.jpg'; |
026 | canvas = document.getElementById('panel'); |
027 | context = canvas.getContext('2d'); |
029 | function Grayscale() { |
031 | var imgd = contextOrig.getImageData(0, 0, iW, iH); |
032 | var data = imgd.data; |
033 | for (var i = 0, n = data.length; i < n; i += 4) { |
034 | var grayscale = data[i] * p1 + data[i+1] * p2 + data[i+2] * p3; |
035 | data[i] = grayscale + er; |
036 | data[i+1] = grayscale + eg; |
037 | data[i+2] = grayscale + eb; |
039 | context.putImageData(imgd, 0, 0); |
043 | var imgd = contextOrig.getImageData(0, 0, iW, iH); |
044 | var data = imgd.data; |
045 | for (var i = 0, n = data.length; i < n; i += 4) { |
046 | data[i] = data[i]*p1+er; |
047 | data[i+1] = data[i+1]*p2+eg; |
048 | data[i+2] = data[i+2]*p3+eb; |
050 | context.putImageData(imgd, 0, 0); |
054 | var imgd = contextOrig.getImageData(0, 0, iW, iH); |
055 | var data = imgd.data; |
056 | for (br = 0; br < iBlurRate; br += 1) { |
057 | for (var i = 0, n = data.length; i < n; i += 4) { |
059 | iSumOpacity = iSumRed = iSumGreen = iSumBlue = 0; |
063 | i - iMW - 4, i - iMW, i - iMW + 4, |
065 | i + iMW - 4, i + iMW, i + iMW + 4 |
068 | for (e = 0; e < aCloseData.length; e += 1) { |
069 | if (aCloseData[e] >= 0 && aCloseData[e] <= data.length - 3) { |
070 | iSumOpacity += data[aCloseData[e]]; |
071 | iSumRed += data[aCloseData[e] + 1]; |
072 | iSumGreen += data[aCloseData[e] + 2]; |
073 | iSumBlue += data[aCloseData[e] + 3]; |
078 | data[i] = (iSumOpacity / iCnt)*p1+er; |
079 | data[i+1] = (iSumRed / iCnt)*p2+eg; |
080 | data[i+2] = (iSumGreen / iCnt)*p3+eb; |
081 | data[i+3] = (iSumBlue / iCnt); |
084 | context.putImageData(imgd, 0, 0); |
088 | case 'grayscale': resetToGrayscale(); break; |
089 | case 'color': resetToColor(); break; |
090 | case 'blur': resetToBlur(); break; |
093 | function changeGrayValue(val) { |
098 | case 'grayscale': Grayscale(); break; |
099 | case 'color': Color(); break; |
100 | case 'blur': Blur(); break; |
103 | function changeColorValue(sobj, val) { |
105 | case 'er': er += val; break; |
106 | case 'eg': eg += val; break; |
107 | case 'eb': eb += val; break; |
110 | case 'grayscale': Grayscale(); break; |
111 | case 'color': Color(); break; |
112 | case 'blur': Blur(); break; |
115 | function changeBlurValue(val) { |
117 | if (iBlurRate <= 0) Color(); |
118 | if (iBlurRate > 4) iBlurRate = 4; |
121 | function resetToColor() { |
129 | function resetToBlur() { |
137 | function resetToGrayscale() { |
As you can notice – sources slightly changed. Now we have 2 canvas objects, so we will using ‘getImageData’ function only with first canvas with original image. Also I added new functions for Blur effect. So how I Blurring image? – commonly, I passing through all pixels of image, taking 8 surrounding pixels of each pixels, calculating average opacity, red/green/blue values of these 8 pixels, and then, apply that average calculated value to our enum pixel.
Conclusion
Not bad, isn’t it? Today we added new interesting effect to our application – Blur. I hope that in coming articles we will continue lessons for image processing. I will be glad to see your thanks and comments. Good luck!