html5简单特效代码(html5零基础入门教程)
在显示马赛克图像时,淡入动画是逐渐从很模糊到很清晰马赛克的过程,淡出动画的处理是从图像变得清晰后又慢慢变得模糊的过程,下面我们就来看看具体的内容。
我们先来看看马赛克图像的淡入效果
代码如下
<!doctype html>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″/>
<title></title>
<meta charset=”utf-8″ />
<script type=”text/javascript”>
var imagedata;
var fademosaicsize = 0;
var thandle;
var mem_canvas;
var mem_context;
var context;
var img;
function loadimage() {
img = new image();
mem_canvas = document.createelement(‘canvas’);
img.onload = function onimageload() {
mem_canvas.width = img.width;
mem_canvas.height = img.height;
mem_context = mem_canvas.getcontext(‘2d’);
mem_context.drawimage(img, 0, 0);
imagedata = mem_context.getimagedata(0, 0, mem_canvas.width, mem_canvas.height);
startfadein();
}
img.src = ‘img/luffy.jpg’;
var canvas = document.getelementbyid(‘simplecanvas’);
if (!canvas || !canvas.getcontext) {
return false;
}else{
context = canvas.getcontext(‘2d’);
}
}
function startfadein() {
fademosaicsize = 64;
thandle = setinterval(onfadein, 50);
}
function onfadein() {
if (fademosaicsize <= 1) {
clearinterval(thandle);
context.drawimage(img, 32, 32);
} else {
createmosaic(mem_context, mem_canvas.width, mem_canvas.height, fademosaicsize);
context.drawimage(mem_canvas, 32, 32);
fademosaicsize = math.floor(fademosaicsize / 1.5);
}
}
function createmosaic(context, width,height,mosaicsize) {
var x=0;
var y=0;
for (y = 0; y < height; y = y + mosaicsize) {
for (x = 0; x < width; x = x + mosaicsize) {
var cr = imagedata.data[(y * width + x) * 4];
var cg = imagedata.data[(y * width + x) * 4 + 1];
var cb = imagedata.data[(y * width + x) * 4 + 2];
context.fillstyle = “rgb(“+cr+”,”+cg+”,”+cb+”)”;
context.fillrect(x, y, x + mosaicsize, y + mosaicsize);
}
}
}
</script>
</head>
<body onload=”loadimage();” style=”background-color:#d0d0d0;”>
<canvas id=”simplecanvas” width=”640″ height=”360″ style=”background-color:#ffffff;”></canvas>
<div>canvas demo</div>
<div id=”output”></div>
</body>
</html>
说明:
由于body标签的onload事件,在页面显示时会通过调用loadimagen()函数开始处理。
页面显示后创建内部绘制的canves对象,并读取图像。获得绘制后的像素数据。之后,启动计时器处理,在计时器的事件中创建马赛克图像并将其绘制到画面上。在执行计时器处理时,会减少马赛克的大小(fademosaicsize),从很粗的马赛克开始变得很清晰的马赛克动画,以表示淡入效果。
要调整渐变速度,可以更改计时器的间隔,
fademosaicsize = math.floor(fademosaicsize / 1.5);
运行结果:
显示上面的html文件。显示很深的马赛克图像。
这是一个动态的过程,图像会慢慢的变清晰,最后就会出现如下效果
看完了淡入的效果,接下来我们来看看马赛克图像淡入淡出的效果实现
代码如下
<!doctype html>
<html>
<head>
<meta http-equiv=”content-type” content=”text/html; charset=utf-8″/>
<title></title>
<meta charset=”utf-8″ />
<script type=”text/javascript”>
var imagedata;
var fademosaicsize = 0;
var thandle;
var mem_canvas;
var mem_context;
var context;
var img;
function loadimage() {
img = new image();
mem_canvas = document.createelement(‘canvas’);
img.onload = function onimageload() {
mem_canvas.width = img.width;
mem_canvas.height = img.height;
mem_context = mem_canvas.getcontext(‘2d’);
mem_context.drawimage(img, 0, 0);
imagedata = mem_context.getimagedata(0, 0, mem_canvas.width, mem_canvas.height);
startfadein();
}
img.src = ‘img/luffy.jpg’;
var canvas = document.getelementbyid(‘simplecanvas’);
if (!canvas || !canvas.getcontext) {
return false;
}else{
context = canvas.getcontext(‘2d’);
}
}
function startfadein() {
fademosaicsize = 64;
thandle = setinterval(onfadein, 50);
}
function startfadeout() {
fademosaicsize = 1;
thandle = setinterval(onfadeout, 50);
}
function onfadein() {
if (fademosaicsize <= 1) {
clearinterval(thandle);
context.drawimage(img, 32, 32);
thandle = setinterval(onshow, 2000);
} else {
createmosaic(mem_context, mem_canvas.width, mem_canvas.height, fademosaicsize);
context.drawimage(mem_canvas, 32, 32);
fademosaicsize = math.floor(fademosaicsize / 1.5);
}
}
function onshow() {
clearinterval(thandle);
startfadeout();
}
function onfadeout() {
if (64 < fademosaicsize) {
clearinterval(thandle);
context.fillstyle = “#ffffff”;
context.fillrect(32, 32, mem_canvas.width, mem_canvas.height);
} else {
createmosaic(mem_context, mem_canvas.width, mem_canvas.height, fademosaicsize);
context.drawimage(mem_canvas, 32, 32);
fademosaicsize = math.ceil(fademosaicsize * 1.5);
}
}
function createmosaic(context, width,height,mosaicsize) {
var x=0;
var y=0;
for (y = 0; y < height; y = y + mosaicsize) {
for (x = 0; x < width; x = x + mosaicsize) {
var cr = imagedata.data[(y * width + x) * 4];
var cg = imagedata.data[(y * width + x) * 4 + 1];
var cb = imagedata.data[(y * width + x) * 4 + 2];
context.fillstyle = “rgb(“+cr+”,”+cg+”,”+cb+”)”;
context.fillrect(x, y, x + mosaicsize, y + mosaicsize);
}
}
}
</script>
</head>
<body onload=”loadimage();” style=”background-color:#d0d0d0;”>
<canvas id=”simplecanvas” width=”640″ height=”360″ style=”background-color:#ffffff;”></canvas>
<div>canvas demo</div>
<div id=”output”></div>
</body>
</html>
说明:
在处理完前一个代码之后,它执行2秒的间隔,然后在该间隔中执行onfadeout,是淡出之前执行的代码。
运行结果
执行上面的html文件,将显示如下马赛克效果
然后逐渐变得清晰,如下
之后,图像显示约两秒钟,然后图像开始出现马赛克效果,如下图所示
然后,马赛克逐渐变粗,最后隐藏,如下所示,页面什么也没有了
以上就是html5 canvas如何实现马赛克的淡入淡出效果(代码)的详细内容,更多请关注其它相关文章!