function game() { // body... init(); lasttime = Date.now(); delttime = 0; game_loop(); } function init () { //获得canvas contex //canvas1 用来处理鱼,土壤,UI,圆圈 can1 = document.getElementById("canvas1"); //canvas2 用来处理背景 海葵 can2 = document.getElementById("canvas2"); ctx1 = can1.getContext('2d'); ctx2 = can2.getContext('2d'); bgPic.src="../SRC/bg.jpg";//文件路径 canWidth = can2.width; canHight = can2.height; dram_image(); } function game_loop () { // body...比如鱼的游动 //该函数要对浏览器适配 requestAnimFrame(game_loop);//比setTimeout setInterval 科学 这个是根机器性能有关 //console.log("game"); var now = Date.now(); delttime = now - lasttime; lasttime = now; } /*画canvas背景*/ function dram_image (){ console.log(canHight,canWidth,bgPic); ctx2.drawImage(bgPic,0,0,canWidth,canHight); }
写成这样在init里面调用就行了
function drawBackground() {
bgPic.onload=function(){
ctx2.drawImage(bgPic,0,0,canWidth,canHeight);
}
}
同问,,,