Canvas實現毛玻璃效果解決方式1:使用stackblur.jsjavascript
在Android系統中實現圖片的毛玻璃效果比較好用的類庫是:Android StackBlurcss
官方Git地址:https://github.com/kikoso/android-stackblur前端
在前端Canvas中可使用StackBlur.js:
Git地址:https://github.com/flozz/StackBlurjava
簡單API說明:jquery
下面是針對不一樣的源(圖片或者 Canvas 等)進行 StackBlur 的調用。android
圖像做爲源:git
StackBlur.image(sourceImage, targetCanvas, radius, blurAlphaChannel);
RGBA Canvas 做爲源:github
StackBlur.canvasRGBA(targetCanvas, top_x, top_y, width, height, radius);
RGB Canvas 做爲源:bootstrap
StackBlur.canvasRGB(targetCanvas, top_x, top_y, width, height, radius);
RGBA ImageData 做爲源:canvas
StackBlur.imageDataRGBA(imageData, top_x, top_y, width, height, radius);
RGB ImageData 做爲源:
StackBlur.imageDataRGB(imageData, top_x, top_y, width, height, radius);
實例1:
<!--使用Canvas加載圖片--> <canvas id="canvas1" width="300" height="200"></canvas> <br /><br /> <img id="img1" src="../Images/mm1.jpg" /> <script> var img1 = document.getElementById('img1'); img1.onload = function () { //調用模糊處理 StackBlur.image('img1', 'canvas1', 100, true); } </script>
實例2:相對完整點
1.添加引用
<link href="//cdn.bootcss.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"> <link href="//cdn.bootcss.com/bootstrap-slider/9.4.1/css/bootstrap-slider.css" rel="stylesheet"> <script src="//cdn.bootcss.com/jquery/1.12.4/jquery.min.js"></script> <script src="//cdn.bootcss.com/bootstrap/3.3.7/js/bootstrap.min.js"></script> <script src="//cdn.bootcss.com/bootstrap-slider/9.4.1/bootstrap-slider.min.js"></script> <script src="../Js/stackblur.js"></script>
2.HTM和js
<!--使用Canvas加載圖片--> <canvas id="canvas1" width="300" height="200"></canvas> <br /><br /> <img id="img1" src="../Images/mm1.jpg" /> <br /> <style> #ex1Slider .slider-selection { background: #BABABA; } </style> <input id="ex1" data-slider-id="ex1Slider" type="text" data-slider-min="0" data-slider-max="100" data-slider-step="1" data-slider-value="100" /> <script> var img1 = document.getElementById('img1'); img1.onload = function () { //調用模糊處理 StackBlur.image('img1', 'canvas1', 100, true); } //滑動控件 $('#ex1').slider().on('change', function (e) { var newValue = e.value.newValue; //調用模糊處理 StackBlur.image('img1', 'canvas1', newValue, true); }); </script>