var can1;
var can2;
var ctx1;
var ctx2;
var lastTime;
var deltaTime;
var bgPic=new Image();
var canWidth;
var canHeight;
document.body.onload=game;
function game(){
lastTime=Date.now();
deltaTime=0;
init();
gameloop();
}
function init(){
can1=document.getElementById("canvas1");
can2=document.getElementById("canvas2");
ctx1=can1.getContext("2d");
ctx2=can2.getContext("2d");
bgPic.src="src/background.jpg";
canWidth=canvas1.width;
canHeight=canvas1.height;
}
function gameloop(){
window.requestAnimFrame(gameloop);
var now=Date.now();
deltaTime=now-lastTime;
lastTime=now;
drawBackground();
}
求解答!!!!!!!!!!!!!!!!!!
和你一样的问题,我的代码是
var can1,can2;
var ctx1,ctx2;
var lastTime,deltaTime;
var bgPic = new Image();
var canWidth;
var canHeight;
document.body.onload = game;
function game () {
init();
lastTime = Date.now();
deltaTime = 0;
gameloop();
}
function init () {
can1= document.getElementById('canvas1');
ctx1 = can1.getContext('2d');
can2= document.getElementById('canvas2');
ctx2 = can2.getContext('2d');
bgPic.src = "./src/background.jpg";
canWidth = can1.width;
canHeight = can1.height;
}
function gameloop () {
window.requestAnimFrame(gameloop);
var now = Date.now();
deltaTime = now-lastTime;
lastTime = now;
drawBackground();
}
background.js的代码是:
function drawBackground () {
ctx2.drawImage(bgPic,0,0,canWidth,canHeight);
}
你的这里:
canWidth=canvas1.width;
canHeight=canvas1.height;
改成:
canWidth=can1.width;
canHeight=can1.height;
试下