以下是 HTML5 canvas生成图片马赛克效果特效源码 的示例演示效果:
部分效果截图:
HTML代码(index.html):
<!doctype html>
<html lang="zh">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>HTML5 canvas生成图片马赛克效果</title>
<link rel="stylesheet" type="text/css" href="css/default.css">
<style type="text/css">
.thumb{margin-left: 5em;}
.thumb img{max-width: 400px;}
</style>
<!--[if IE]>
<script src="http://libs.useso.com/js/html5shiv/3.7/html5shiv.min.js"></script>
<![endif]-->
</head>
<body>
<div class="container">
<div class="content bgcolor-8">
<h3 class="center">注意:文件下载到本地后在Chrome和IE浏览器中可能看不到效果<br>请使用Firefox浏览器在本地计算机查看该DEMO。</h3>
<p class="center">Change pixel resolution <input type="range" min="4" max="100" value="32" id="range" /> <span id="output">32</span></p>
<div class="thumb">
<img src="img/1.jpg" id="dolly1" />
<img src="img/2.jpg" id="dolly2" />
<img src="img/3.jpg" id="dolly3" />
</div>
</div>
</div>
<script src="js/close-pixelate.js"></script>
<script>
window.onload = function() {
var dolly1 = document.getElementById('dolly1')
var dolly2 = document.getElementById('dolly2')
var dolly3 = document.getElementById('dolly3')
var pixelOpts = [ { resolution: 32 } ]
var pixelDolly1 = dolly1.closePixelate( pixelOpts )
var pixelDolly2 = dolly2.closePixelate( pixelOpts )
var pixelDolly3 = dolly3.closePixelate( pixelOpts )
var range = document.getElementById('range')
var output = document.getElementById('output')
range.addEventListener( 'change', function( event ) {
var res = parseInt( event.target.value, 10 )
res = Math.floor( res / 2 ) * 2
res = Math.max( 4, Math.min( 100, res ) )
output.textContent = res
// console.log( res );
pixelOpts = [ { resolution: res } ]
pixelDolly1.render( pixelOpts )
pixelDolly2.render( pixelOpts )
pixelDolly3.render( pixelOpts )
}, false )
}
</script>
</body>
</html>
CSS代码(default.css):
[class^="icon-"],[class*=" icon-"]{font-family:'icomoon';speak:none;font-style:normal;font-weight:normal;font-variant:normal;text-transform:none;line-height:1;/* Better Font Rendering =========== */
-webkit-font-smoothing:antialiased;-moz-osx-font-smoothing:grayscale;}
body,html{font-size:100%;padding:0;margin:0;}
/* Reset */
*,*:after,*:before{-webkit-box-sizing:border-box;-moz-box-sizing:border-box;box-sizing:border-box;}
/* Clearfix hack by Nicolas Gallagher:http://nicolasgallagher.com/micro-clearfix-hack/ */
.clearfix:before,.clearfix:after{content:" ";display:table;}
.clearfix:after{clear:both;}
body{background:#f9f7f6;color:#404d5b;font-weight:500;font-size:1.05em;font-family:"Segoe UI","Lucida Grande",Helvetica,Arial,"Microsoft YaHei",FreeSans,Arimo,"Droid Sans","wenquanyi micro hei","Hiragino Sans GB","Hiragino Sans GB W3","FontAwesome",sans-serif;}
a{color:#2fa0ec;text-decoration:none;outline:none;}
a:hover,a:focus{color:#74777b;}
;.container{margin:0 auto;text-align:center;overflow:hidden;}
.content{font-size:150%;padding:3em 0;}
.content h2{margin:0 0 2em;opacity:0.1;}
.content p{margin:1em 0;padding:5em 0 0 0;font-size:0.65em;}
.bgcolor-1{background:#f0efee;}
.bgcolor-2{background:#f9f9f9;}
.bgcolor-3{background:#e8e8e8;}
.bgcolor-4{background:#2f3238;color:#fff;}
.bgcolor-5{background:#df6659;color:#521e18;}
.bgcolor-6{background:#2fa8ec;}
.bgcolor-7{background:#d0d6d6;}
.bgcolor-8{background:#3d4444;color:#fff;}
/* Header */
.htmleaf-header{padding:3em 190px 4em;letter-spacing:-1px;text-align:center;}
.htmleaf-header h1{font-weight:600;font-size:2em;line-height:1;margin-bottom:0;}
.htmleaf-header h1 span{font-family:'Raleway';display:block;font-size:60%;font-weight:400;padding:0.8em 0 0.5em 0;color:#c3c8cd;}
/*nav*/
.htmleaf-demo a{color:#1d7db1;text-decoration:none;}
.htmleaf-demo{width:100%;padding-bottom:1.2em;}
.htmleaf-demo a{display:inline-block;margin:0.5em;padding:0.6em 1em;border:3px solid #1d7db1;font-weight:700;}
.htmleaf-demo a:hover{opacity:0.6;}
.htmleaf-demo a.current{background:#1d7db1;color:#fff;}
/* Top Navigation Style */
.htmleaf-links{position:relative;display:inline-block;white-space:nowrap;font-size:1.5em;text-align:center;}
.htmleaf-links::after{position:absolute;top:0;left:50%;margin-left:-1px;width:2px;height:100%;background:#dbdbdb;content:'';-webkit-transform:rotate3d(0,0,1,22.5deg);transform:rotate3d(0,0,1,22.5deg);}
.htmleaf-icon{display:inline-block;margin:0.5em;padding:0em 0;width:1.5em;text-decoration:none;}
.htmleaf-icon span{display:none;}
.htmleaf-icon:before{margin:0 5px;text-transform:none;font-weight:normal;font-style:normal;font-variant:normal;font-family:'icomoon';line-height:1;speak:none;-webkit-font-smoothing:antialiased;}
/* footer */
.htmleaf-footer{width:100%;padding-top:10px;}
.htmleaf-small{font-size:0.8em;}
.center{text-align:center;}
/* icomoon */
.icon-home:before{content:"\e600";}
.icon-pacman:before{content:"\e623";}
.icon-users2:before{content:"\e678";}
.icon-bug:before{content:"\e699";}
.icon-eye:before{content:"\e610";}
.icon-eye-blocked:before{content:"\e611";}
.icon-eye2:before{content:"\e612";}
.icon-arrow-up-left3:before{content:"\e72f";}
.icon-arrow-up3:before{content:"\e730";}
.icon-arrow-up-right3:before{content:"\e731";}
.icon-arrow-right3:before{content:"\e732";}
.icon-arrow-down-right3:before{content:"\e733";}
.icon-arrow-down3:before{content:"\e734";}
.icon-arrow-down-left3:before{content:"\e735";}
.icon-arrow-left3:before{content:"\e736";}
@media screen and (max-width:50em){.htmleaf-header{padding:3em 10% 4em;}
.htmleaf-header h1{font-size:2em;}
}
@media screen and (max-width:40em){.htmleaf-header h1{font-size:1.5em;}
}
@media screen and (max-width:30em){.htmleaf-header h1{font-size:1.2em;}
}