1 function imgLoad(imgURL,fn) { 2 var imgArr = {};//存储图片 3 var tempImg ; //临时存储图片; 4 var loaded = 0; //当前已经加载完成几张 5 var imageLength = 0;//要加载几张 6 for (var key in imgURL) { 7 imageLength++; 8 tempImg = new Image(); 9 tempImg.onload = function () { 10 loaded++; 11 if(loaded >= imageLength){ 12 fn(imgArr); 13 } 14 } 15 tempImg.src = imgURL[key]; 16 imgArr[key] = tempImg; 17 } 18 }
调用的时候一个字母写错了 ,调试了 大半天
1 imgLoad({ 2 red:'./images/NPCrabbitbaby.png', 3 blue:'./images/NPCrabbitbaby-2.png' 4 },function(imgObj){ 5 cvs.drawImage(imgObj.red,10,10,100,100); 6 cvs.drawImage(imgObj.blue,150,10,100,100); 7 })