您可以使用画布在褪色的各个阶段创建平面。
使用 context.globalAlpha 设置绘制图像的不透明度。
context.globalAlpha=0.50; // opacity at 50%
context.drawImage(yourPlane,0,0);
性能说明:由于绘制预先存在的图像比应用效果然后绘制要快,因此您可以将平面保存为处于不同渐变阶段的图像。
var plane50percent=new Image();
plane50percent.src=canvas.toDataURL(); // image.onload omitted for brevity
然后只需在褪色的各个阶段绘制预先绘制的平面即可获得效果。
这是代码和小提琴:http: //jsfiddle.net/m1erickson/v5TwY/
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" />
<script src="http://code.jquery.com/jquery.min.js"></script>
<style>
body{ background-color: ivory; }
canvas{border:1px solid red;}
</style>
<script>
$(function(){
var canvas=document.getElementById("canvas");
var ctx=canvas.getContext("2d");
window.requestAnimFrame = (function(callback) {
return window.requestAnimationFrame || window.webkitRequestAnimationFrame || window.mozRequestAnimationFrame || window.oRequestAnimationFrame || window.msRequestAnimationFrame ||
function(callback) {
window.setTimeout(callback, 1000 / 60);
};
})();
var fps = 60;
// image loader
var img=new Image();
img.onload=function(){
animate();
}
img.src="https://dl.dropboxusercontent.com/u/139992952/stackoverflow/b2.png";
function animate() {
setTimeout(function() {
requestAnimFrame(animate);
// set the current opacity
ctx.globalAlpha-=.02;
// draw the image
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.drawImage(img,5,5);
if(ctx.globalAlpha<=0){ return; }
}, 1000 / fps);
}
}); // end $(function(){});
</script>
</head>
<body>
<canvas id="canvas" width=405 height=200></canvas>
</body>
</html>