<!DOCTYPE html> <html> <head> <meta http-equiv="Content-type" content="text/html; charset = utf-8"> </head> <script type="text/javascript" charset="utf-8"> var imageLoader = { loaded:true, loadedImages:0, totalImages:0, load:function(url){ this.totalImages++; this.loaded = false; var image = new Image(); image.src = url; image.onload = function(){//图片加载后调用的图片 imageLoader.loadedImages++; if(imageLoader.loadedImages === imageLoader.totalImages){ imageLoader.loaded = true; } } return image; } }; function pageLoaded(){ var canvas = document.getElementById(‘testcanvas‘);//得到画布 var context = canvas.getContext(‘2d‘);//得到绘图环境 参数为绘图环境的类型 imageLoader.loaded = false; var image_gx1 = imageLoader.load(‘/testimg/gx/gx1.png‘); var image_gx2 = imageLoader.load(‘/testimg/gx/gx2.jpg‘); var image_gx3 = imageLoader.load(‘/testimg/gx/gx3.jpg‘); var image_gx4 = imageLoader.load(‘/testimg/gx/gx4.jpg‘); var interHandler = setInterval(function () { if(imageLoader.loaded){ context.drawImage(image_gx1,0,0,100,100); context.drawImage(image_gx2,101,0,100,100); context.drawImage(image_gx3,201,0,100,100); context.drawImage(image_gx4,301,0,100,100); clearInterval(interHandler) } },50); } </script> <body onload="pageLoaded();"> <canvas width="1200" height="480" id="testcanvas" style="border: black 1px solid"> </canvas> </body> </html>
原文地址:http://quietnight.blog.51cto.com/7163892/1912913