小程序canvas放大模糊

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.

HTML5 Canvas图像效果应用程序–添加模糊。 今天,我们继续HTML5 canvas示例,希望您还记得我们在2周前制作的应用程序吗? 确保这对您研究图像处理功能非常有趣。 今天,我决定继续并在此处添加新的滤镜-模糊。

Here are our demo and downloadable package:

这是我们的演示和可下载的软件包:

现场演示

[sociallocker]

[社交储物柜]

打包下载

[/sociallocker]

[/ sociallocker]

Ok, download the example files and lets start coding !

好的,下载示例文件并开始编码!

步骤1. HTML (Step 1. HTML)

Here are all html of my demo

这是我的演示的全部html

index.html (index.html)


<!DOCTYPE html>
<html lang="en" >
<head>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/script.js"></script>
<title>HTML5 Canvas Image Effects App - Adding Blur (demo)</title>
</head>
<body><div class="example"><h3><a href="https://www.script-tutorials.com/html5-canvas-image-effects-app/">HTML5 Canvas Image Effects demo (adding blur effect) | Script Tutorials</a></h3><div class="column1"><input type="button" onclick="resetToGrayscale()" value="Grayscale" /><br /><input type="button" onclick="resetToColor()" value="Color" /><br /><input type="button" onclick="resetToBlur()" value="Blur(1)" /><br /><input type="button" onclick="reset()" value="Reset" /><br /><input type="button" onclick="changeGrayValue(0.1)" value="Lighter" /><br /><input type="button" onclick="changeGrayValue(-0.1)" value="Darker" /><br />Red: <input type="button" onclick="changeColorValue('er', 10)" value="More" /><input type="button" onclick="changeColorValue('er', -10)" value="Less" /><br />Green: <input type="button" onclick="changeColorValue('eg', 10)" value="More" /><input type="button" onclick="changeColorValue('eg', -10)" value="Less" /><br />Blue: <input type="button" onclick="changeColorValue('eb', 10)" value="More" /><input type="button" onclick="changeColorValue('eb', -10)" value="Less" />Blur: <input type="button" onclick="changeBlurValue(1)" value="More" /><input type="button" onclick="changeBlurValue(-1)" value="Less" /><br /></div><div class="column2"><canvas id="orig" width="500" height="333"></canvas><canvas id="panel" width="500" height="333"></canvas></div><div style="clear:both;"></div></div>
</body>
</html>

<!DOCTYPE html>
<html lang="en" >
<head>
<link href="css/main.css" rel="stylesheet" type="text/css" />
<script type="text/javascript" src="js/script.js"></script>
<title>HTML5 Canvas Image Effects App - Adding Blur (demo)</title>
</head>
<body><div class="example"><h3><a href="https://www.script-tutorials.com/html5-canvas-image-effects-app/">HTML5 Canvas Image Effects demo (adding blur effect) | Script Tutorials</a></h3><div class="column1"><input type="button" onclick="resetToGrayscale()" value="Grayscale" /><br /><input type="button" onclick="resetToColor()" value="Color" /><br /><input type="button" onclick="resetToBlur()" value="Blur(1)" /><br /><input type="button" onclick="reset()" value="Reset" /><br /><input type="button" onclick="changeGrayValue(0.1)" value="Lighter" /><br /><input type="button" onclick="changeGrayValue(-0.1)" value="Darker" /><br />Red: <input type="button" onclick="changeColorValue('er', 10)" value="More" /><input type="button" onclick="changeColorValue('er', -10)" value="Less" /><br />Green: <input type="button" onclick="changeColorValue('eg', 10)" value="More" /><input type="button" onclick="changeColorValue('eg', -10)" value="Less" /><br />Blue: <input type="button" onclick="changeColorValue('eb', 10)" value="More" /><input type="button" onclick="changeColorValue('eb', -10)" value="Less" />Blur: <input type="button" onclick="changeBlurValue(1)" value="More" /><input type="button" onclick="changeBlurValue(-1)" value="Less" /><br /></div><div class="column2"><canvas id="orig" width="500" height="333"></canvas><canvas id="panel" width="500" height="333"></canvas></div><div style="clear:both;"></div></div>
</body>
</html>

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.

注意,从今天开始,我制作了2个画布对象。 第一个将包含原始图像,第二个将包含我们的效果。 我还添加了3个新按钮。 首先-切换到模糊效果,然后2-增大/减小模糊级别。

步骤2. CSS (Step 2. CSS)

Here are used CSS styles.

这是使用CSS样式。

css / main.css (css/main.css)


body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.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}
h3 {text-align:center;
}
.column1 {float:left;padding-right: 10px;text-align:right;width:185px;
}
.column2 {float:left;width:500px;background-color:#888;padding:10px;
}
input[type=button] {margin:5px;
}

body{background:#eee;font-family:Verdana, Helvetica, Arial, sans-serif;margin:0;padding:0}
.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}
h3 {text-align:center;
}
.column1 {float:left;padding-right: 10px;text-align:right;width:185px;
}
.column2 {float:left;width:500px;background-color:#888;padding:10px;
}
input[type=button] {margin:5px;
}

步骤3. JS (Step 3. JS)

Now – most interesting – its inner functionality (HTML5 canvas script).

现在,最有趣的是它的内部功能(HTML5画布脚本)。

js / script.js (js/script.js)


var canvas;
var context;
var iW = 0; // image width
var iH = 0; // image height
var p1 = 0.99;
var p2 = 0.99;
var p3 = 0.99;
var er = 0; // extra red
var eg = 0; // extra green
var eb = 0; // extra blue
var iBlurRate = 0;
var func = 'color'; // last used function
window.onload = function() {// creating context for original imagecanvasOrig = document.getElementById('orig');contextOrig = canvasOrig.getContext('2d');// draing original imagevar imgObj = new Image();imgObj.onload = function () {iW = this.width;iH = this.height;contextOrig.drawImage(imgObj, 0, 0, iW, iH); // draw the image on the canvas}imgObj.src = 'images/01.jpg';// creating testing contextcanvas = document.getElementById('panel');context = canvas.getContext('2d');
};
function Grayscale() {func = 'grayscale'; // last used functionvar imgd = contextOrig.getImageData(0, 0, iW, iH);var data = imgd.data;for (var i = 0, n = data.length; i < n; i += 4) {var grayscale = data[i] * p1 + data[i+1] * p2 + data[i+2] * p3;data[i]   = grayscale + er; // reddata[i+1] = grayscale + eg; // greendata[i+2] = grayscale + eb; // blue}context.putImageData(imgd, 0, 0);
}
function Color() {func = 'color'; // last used functionvar imgd = contextOrig.getImageData(0, 0, iW, iH);var data = imgd.data;for (var i = 0, n = data.length; i < n; i += 4) {data[i]   = data[i]*p1+er; // reddata[i+1] = data[i+1]*p2+eg; // greendata[i+2] = data[i+2]*p3+eb; // blue}context.putImageData(imgd, 0, 0);
}
function Blur() {func = 'blur'; // last used functionvar imgd = contextOrig.getImageData(0, 0, iW, iH);var data = imgd.data;for (br = 0; br < iBlurRate; br += 1) {for (var i = 0, n = data.length; i < n; i += 4) {iMW = 4 * iW;iSumOpacity = iSumRed = iSumGreen = iSumBlue = 0;iCnt = 0;// data of close pixels (from all 8 surrounding pixels)aCloseData = [i - iMW - 4, i - iMW, i - iMW + 4, // top pixelsi - 4, i + 4, // middle pixelsi + iMW - 4, i + iMW, i + iMW + 4 // bottom pixels];// calculating Sum value of all close pixelsfor (e = 0; e < aCloseData.length; e += 1) {if (aCloseData[e] >= 0 && aCloseData[e] <= data.length - 3) {iSumOpacity += data[aCloseData[e]];iSumRed += data[aCloseData[e] + 1];iSumGreen += data[aCloseData[e] + 2];iSumBlue += data[aCloseData[e] + 3];iCnt += 1;}}// apply average valuesdata[i] = (iSumOpacity / iCnt)*p1+er;data[i+1] = (iSumRed / iCnt)*p2+eg;data[i+2] = (iSumGreen / iCnt)*p3+eb;data[i+3] = (iSumBlue / iCnt);}}context.putImageData(imgd, 0, 0);
}
function reset() {switch(func) {case 'grayscale': resetToGrayscale(); break;case 'color': resetToColor(); break;case 'blur': resetToBlur(); break;}
}
function changeGrayValue(val) {p1 += val;p2 += val;p3 += val;switch(func) {case 'grayscale': Grayscale(); break;case 'color': Color(); break;case 'blur': Blur(); break;}
}
function changeColorValue(sobj, val) {switch (sobj) {case 'er': er += val; break;case 'eg': eg += val; break;case 'eb': eb += val; break;}switch(func) {case 'grayscale': Grayscale(); break;case 'color': Color(); break;case 'blur': Blur(); break;}
}
function changeBlurValue(val) {iBlurRate += val;if (iBlurRate <= 0) Color();if (iBlurRate > 4) iBlurRate = 4;Blur();
}
function resetToColor() {p1 = 1;p2 = 1;p3 = 1;er = eg = eb = 0;iBlurRate = 0;Color();
}
function resetToBlur() {p1 = 1;p2 = 1;p3 = 1;er = eg = eb = 0;iBlurRate = 1;Blur();
}
function resetToGrayscale() {p1 = 0.3;p2 = 0.59;p3 = 0.11;er = eg = eb = 0;iBlurRate = 0;Grayscale();
}

var canvas;
var context;
var iW = 0; // image width
var iH = 0; // image height
var p1 = 0.99;
var p2 = 0.99;
var p3 = 0.99;
var er = 0; // extra red
var eg = 0; // extra green
var eb = 0; // extra blue
var iBlurRate = 0;
var func = 'color'; // last used function
window.onload = function() {// creating context for original imagecanvasOrig = document.getElementById('orig');contextOrig = canvasOrig.getContext('2d');// draing original imagevar imgObj = new Image();imgObj.onload = function () {iW = this.width;iH = this.height;contextOrig.drawImage(imgObj, 0, 0, iW, iH); // draw the image on the canvas}imgObj.src = 'images/01.jpg';// creating testing contextcanvas = document.getElementById('panel');context = canvas.getContext('2d');
};
function Grayscale() {func = 'grayscale'; // last used functionvar imgd = contextOrig.getImageData(0, 0, iW, iH);var data = imgd.data;for (var i = 0, n = data.length; i < n; i += 4) {var grayscale = data[i] * p1 + data[i+1] * p2 + data[i+2] * p3;data[i]   = grayscale + er; // reddata[i+1] = grayscale + eg; // greendata[i+2] = grayscale + eb; // blue}context.putImageData(imgd, 0, 0);
}
function Color() {func = 'color'; // last used functionvar imgd = contextOrig.getImageData(0, 0, iW, iH);var data = imgd.data;for (var i = 0, n = data.length; i < n; i += 4) {data[i]   = data[i]*p1+er; // reddata[i+1] = data[i+1]*p2+eg; // greendata[i+2] = data[i+2]*p3+eb; // blue}context.putImageData(imgd, 0, 0);
}
function Blur() {func = 'blur'; // last used functionvar imgd = contextOrig.getImageData(0, 0, iW, iH);var data = imgd.data;for (br = 0; br < iBlurRate; br += 1) {for (var i = 0, n = data.length; i < n; i += 4) {iMW = 4 * iW;iSumOpacity = iSumRed = iSumGreen = iSumBlue = 0;iCnt = 0;// data of close pixels (from all 8 surrounding pixels)aCloseData = [i - iMW - 4, i - iMW, i - iMW + 4, // top pixelsi - 4, i + 4, // middle pixelsi + iMW - 4, i + iMW, i + iMW + 4 // bottom pixels];// calculating Sum value of all close pixelsfor (e = 0; e < aCloseData.length; e += 1) {if (aCloseData[e] >= 0 && aCloseData[e] <= data.length - 3) {iSumOpacity += data[aCloseData[e]];iSumRed += data[aCloseData[e] + 1];iSumGreen += data[aCloseData[e] + 2];iSumBlue += data[aCloseData[e] + 3];iCnt += 1;}}// apply average valuesdata[i] = (iSumOpacity / iCnt)*p1+er;data[i+1] = (iSumRed / iCnt)*p2+eg;data[i+2] = (iSumGreen / iCnt)*p3+eb;data[i+3] = (iSumBlue / iCnt);}}context.putImageData(imgd, 0, 0);
}
function reset() {switch(func) {case 'grayscale': resetToGrayscale(); break;case 'color': resetToColor(); break;case 'blur': resetToBlur(); break;}
}
function changeGrayValue(val) {p1 += val;p2 += val;p3 += val;switch(func) {case 'grayscale': Grayscale(); break;case 'color': Color(); break;case 'blur': Blur(); break;}
}
function changeColorValue(sobj, val) {switch (sobj) {case 'er': er += val; break;case 'eg': eg += val; break;case 'eb': eb += val; break;}switch(func) {case 'grayscale': Grayscale(); break;case 'color': Color(); break;case 'blur': Blur(); break;}
}
function changeBlurValue(val) {iBlurRate += val;if (iBlurRate <= 0) Color();if (iBlurRate > 4) iBlurRate = 4;Blur();
}
function resetToColor() {p1 = 1;p2 = 1;p3 = 1;er = eg = eb = 0;iBlurRate = 0;Color();
}
function resetToBlur() {p1 = 1;p2 = 1;p3 = 1;er = eg = eb = 0;iBlurRate = 1;Blur();
}
function resetToGrayscale() {p1 = 0.3;p2 = 0.59;p3 = 0.11;er = eg = eb = 0;iBlurRate = 0;Grayscale();
}

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.

如您所见-来源略有变化。 现在我们有2个画布对象,因此我们仅对具有原始图像的第一个画布使用'getImageData'函数。 另外,我还添加了用于模糊效果的新功能。 那么我如何模糊图像呢? –通常,我遍历图像的所有像素,获取每个像素的8个周围像素,计算这8个像素的平均不透明度,红色/绿色/蓝色值,然后将该平均计算值应用于枚举像素。

现场演示

结论 (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!

还不错,不是吗? 今天,我们在应用程序中添加了新的有趣效果-模糊。 我希望在以后的文章中,我们将继续进行图像处理课程。 看到您的感谢和评论,我将非常高兴。 祝好运!

翻译自: https://www.script-tutorials.com/html5-canvas-image-effects-app-adding-blur/

小程序canvas放大模糊

小程序canvas放大模糊_HTML5 Canvas图像效果应用程序–添加模糊相关推荐

  1. canvas 实现图片局部模糊_HTML5 Canvas图片马赛克模糊动画

    本文作者html5tricks,转载请注明出处 经常可以在网上或者电视上看到被马赛克模糊的图片或者视频,今天我们要利用 HTML代码如下 Change pixel resolution 32 定义了一 ...

  2. c语言程序字体放大,C语言写的俄罗斯方块程序减小字体 增大字体 作者.docx

    C语言写的俄罗斯方块程序减小字体 增大字体 作者 C语言写的俄罗斯方块程序减小字体增大字体作者:佚名来源:不详发布时间:2009-9-21 1:11:22收藏到网摘:合作洽谈大概在最近两天之内编码完成 ...

  3. 微信小程序拍照截取取景框内容#canvas

    最近有个需求是拍照识别Vin码,为了识别准确度,要求前端只上传取景框内的内容给后端识别. 通过一天多的碰壁(微信开发者工具,懂的都懂),终于在各种论坛的各种不起眼的地方摸出了一套可以正常使用的流程. ...

  4. 微信小程序-生成保存图文海报-canvas生成图片、二维码、自定义文字样式

    代码仓库:https://gitee.com/DerekAndroid/miniProgramAgen/tree/master/pages/canvasTest 效果:  点击保存图片-     // ...

  5. html5 2d小游戏,GitHub - pepsin/cax: HTML5 Canvas 2D Rendering Engine - 小程序、小游戏以及 Web 通用 Canvas 渲染引擎...

    English | 简体中文 Cax HTML5 Canvas 2D Rendering Engine Features Simple API, Lightweight and High perfor ...

  6. 好程序员分享24个canvas基础知识小结

    好程序员分享24个canvas基础知识小结,非常全面详尽,推荐给大家. 现把canvas的知识点总结如下,以便随时查阅. 1.填充矩形 fillRect(x,y,width,height); 2.绘制 ...

  7. 小猿圈web前端简述canvas如何实现二维码和图片合成

    你经常看到活动海报什么的,上都是有各种宣传文案以及二维码形成的,你知道怎么用canvas实现二维码和图片合成,下面小猿圈Linux讲师为你详细介绍一下canvas如何实现二维码和图片合成的. 使用ur ...

  8. 好程序员分享24个canvas基础知识小结 1

    好程序员分享24个canvas基础知识小结,非常全面详尽,推荐给大家. 现把canvas的知识点总结如下,以便随时查阅. 1.填充矩形 fillRect(x,y,width,height); 2.绘制 ...

  9. java名片合成_HTML5 canvas绘图基础(电子名片生成器源码)

    创建canvas 您的浏览器不支持canvas 基础设置 var canvas = document.getElementById('myCanvas');var ctx = canvas.getCo ...

最新文章

  1. html转义符 xsl转义符
  2. ios系统 ipa文件 打包流程详解 及 常见问题处理
  3. php 半角全角,PHP 全角转半角实现代码
  4. VTK修炼之道77:交互部件_分割/配准类Widget与其他Widget
  5. SAP 电商云 Accelerator 和 Spartacus UI 的工作机制差异
  6. jzoj3347,bzoj3257-[NOI2013模拟]树的难题【树形dp】
  7. Space Time Varying Color Palette
  8. socket可以写成单例嘛_精读设计模式 Singleton 单例模式
  9. RuntimeError: Found 0 files in subfolders of: ./data/image Supported extensions are: .jpg,.jpeg,.png
  10. iOS 图形编程总结
  11. js object转数组_const 和 Object.freeze() 的区别 ?
  12. adalm pluto_将Apache Pluto与Lucene搜索引擎示例教程集成
  13. Win Form中限制TextBox只能输入数字
  14. 响应式Web设计:HTML5和CSS3实战 笔记
  15. Java Web的Excel读取
  16. 使用 Auto-TS 自动化时间序列预测
  17. docker搭建MySQL集群
  18. 成为管理层必会的技能之一!利用Python打造一款员工管理系统~
  19. 计算机word文档无法工作,教您电脑word打不开怎么办
  20. IxChariot测试网络设备性能

热门文章

  1. There are no TAP-Windows adapters on this system. You should be able to create a TAP-Windows adapte
  2. 达梦数据库运维监控之DEM
  3. 做HPC客户的“军需官”,戴尔到底有哪些“黑科技”?
  4. ffmpeg编译gl-transitions(centos7环境为基础)
  5. 每日一支TED——帕特里夏#183;瑞安:不要固执于英语
  6. 支付宝支付接口获取公钥和私钥
  7. 解决R语言PCA主成分分析的cor()函数错误:Error in cor() : ‘x‘必需为数值
  8. 读格林斯潘回忆录-1
  9. java上转型来实现人种和地区的识别
  10. 国际大公司到国内大公司的思考