文章插图
前端小试牛刀,实现了六款简单常见HTML5 Canvas特效滤镜,并且封装成一个纯JavaScript可调用的API文件gloomyfishfilter.js 。支持的特效滤镜分别为:
1.反色
2.灰色调
3.模糊
4.浮雕
5.雕刻
6.合理
滤镜原理解释:2.灰色调:获取一个预期点RGB值r,g,b则新的RGB值
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.模糊:基于一个5 * 5的卷积核
4.浮雕与雕刻:
根据当前预期的前一个预期RGB值与它的后一个重新的RGB值之差再加上128
5.总体:模拟了物体在镜子中与之对应的效果 。
杂项准备1、如何获取Canvas 2d context对象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 imagetempContext = canvas.getContext("2d");2、如何添加一个DOM img对象到Canvas对象中var source = document.getElementById("source");tempContext.drawImage(source, 0, 0, canvas.width,canvas.height);3、如何从Canvas对象中获取预定数据var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);var binaryData = canvasData.data;4、如何对DOM对象实现鼠标ClickEvent绑定function bindButtonEvent(element, type, handler) {if(element.addEventListener){element.addEventListener(type, handler,**false**);}else{element.attachEvent('on'+type, handler);// for IE6,7,8} }5、如何调用实现的gfilter API完成滤镜功能<scriptsrc=https://www.520longzhigu.com/diannao/*"gloomyfishfilter.js"*></script> //导入API文件gfilter.colorInvertProcess(binaryData, len); //调用 API6、浏览器支持:IE,FF,Chrome上测试通过,其中IE上支持通过以下标签实现:<meta http-equiv="X-UA-Compatible"*content=*"chrome=IE8"> 效果演示:
应用程序源代码:CSS部分:
#svgContainer {width:800px;height:600px;background-color:#EEEEEE;}#sourceDiv { float: left; border: 2px solid blue} #targetDiv { float: right;border: 2px solid red}filter1.html中HTML源代码:
<!DOCTYPE html><html><head><meta http-equiv="X-UA-Compatible" content="chrome=IE8"><meta http-equiv="Content-type" content="text/html;charset=UTF-8"><title>Canvas Filter Demo</title><link href="https://www.520longzhigu.com/diannao/default.css" rel="stylesheet" /><script src="https://www.520longzhigu.com/diannao/gloomyfishfilter.js"></scrip></head><body><h1>HTML Canvas Image Process - By Gloomy Fish</h1><div id="svgContainer"><div id="sourceDiv"><img id="source" src="https://www.520longzhigu.com/test.png" /></div><div id="targetDiv"><canvas id="target"></canvas></div></div><div id="btn-group"><button type="button" id="invert-button">反色</button><button type="button" id="adjust-button">灰色调</button><button type="button" id="blur-button">模糊</button><button type="button" id="relief-button">浮雕</button><button type="button" id="diaoke-button">雕刻</button><button type="button" id="mirror-button">镜像</button></div></body></html>filter1.html中JavaScript源代码:
var tempContext = null; // global variable 2d contextwindow.onload = function() {var source = document.getElementById("source");var canvas = document.getElementById("target");canvas.width = source.clientWidth;canvas.height = source.clientHeight;if (!canvas.getContext) {console.log("Canvas not supported. Please install a HTML5 compatible browser.");return;}// get 2D context of canvas and draw imagetempContext = canvas.getContext("2d");tempContext.drawImage(source, 0, 0, canvas.width, canvas.height);// initialization actionsvar inButton = document.getElementById("invert-button");var adButton = document.getElementById("adjust-button");var blurButton = document.getElementById("blur-button");var reButton = document.getElementById("relief-button");var dkButton = document.getElementById("diaoke-button");var mirrorButton = document.getElementById("mirror-button");// bind mouse click eventbindButtonEvent(inButton, "click", invertColor);bindButtonEvent(adButton, "click", adjustColor);bindButtonEvent(blurButton, "click", blurImage);bindButtonEvent(reButton, "click", fudiaoImage);bindButtonEvent(dkButton, "click", kediaoImage);bindButtonEvent(mirrorButton, "click", mirrorImage);}function bindButtonEvent(element, type, handler){if(element.addEventListener) {element.addEventListener(type, handler, false);} else {element.attachEvent('on'+type, handler); // for IE6,7,8}}function invertColor() {var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);var binaryData = canvasData.data;// Processing all the pixelsgfilter.colorInvertProcess(binaryData, len);// Copying back canvas data to canvastempContext.putImageData(canvasData, 0, 0);}function adjustColor() {var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);var binaryData = canvasData.data;// Processing all the pixelsgfilter.colorAdjustProcess(binaryData, len);// Copying back canvas data to canvastempContext.putImageData(canvasData, 0, 0);}function blurImage() {var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);// Processing all the pixelsgfilter.blurProcess(tempContext, canvasData);// Copying back canvas data to canvastempContext.putImageData(canvasData, 0, 0);}function fudiaoImage() {var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);// Processing all the pixelsgfilter.reliefProcess(tempContext, canvasData);// Copying back canvas data to canvastempContext.putImageData(canvasData, 0, 0);}function kediaoImage() {var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);// Processing all the pixelsgfilter.diaokeProcess(tempContext, canvasData);// Copying back canvas data to canvastempContext.putImageData(canvasData, 0, 0);}function mirrorImage() {var canvas = document.getElementById("target");var len = canvas.width * canvas.height * 4;var canvasData = https://www.520longzhigu.com/diannao/tempContext.getImageData(0, 0, canvas.width, canvas.height);// Processing all the pixelsgfilter.mirrorProcess(tempContext, canvasData);// Copying back canvas data to canvastempContext.putImageData(canvasData, 0, 0);}滤镜源代码(gloomyfishfilter.js):
以上关于本文的内容,仅作参考!温馨提示:如遇健康、疾病相关的问题,请您及时就医或请专业人士给予相关指导!
「四川龙网」www.sichuanlong.com小编还为您精选了以下内容,希望对您有所帮助:- 北海港股票行情及代码详解 北海港股票有哪些
- 宽带连接连接不上怎么办? 宽带连接连接不上怎么办
- 0x000000d1电脑蓝屏代码是什么意思,各种蓝屏代码的原因在这里!
- 附:永久免费刷钻的方法和代码 怎么刷钻教程
- php上传文件代码接口 php上传文件代码
- phpstorm 详细教程 php之phpstorm自动代码补全的使用
- 股票程序源码 真正有用的股票软件源代码
- 抖音照片涂抹变漫画 抖音照片涂成漫画特效怎么弄照片变漫画教程
- 0x000000f4,小编教你如何解决电脑蓝屏代码0x000000F4
- 抖音怎么把照片弄成漫画效果 抖音照片涂成漫画特效怎么弄照片变漫画教程