var?can1;
var?cam2;
var?ctx1;
var?ctx2;
var?lastTime;
var?deltaTime;
var?bgPic?=?new?Image();
var?canWidth;
var?canHeight;
var?ane;
var?fruit;
document.body.onload?=?game;
function?game()?{
//?body...
init();
gameloop();
}
function?init(){
//獲得canvas?context
can1?=?document.getElementById("canvas1");
can2?=?document.getElementById("canvas2");
ctx1?=?can1.getContext('2d');
ctx2?=?can2.getContext('2d');
bgPic.src?=?'./src/background.jpg';
canWidth?=?can1.width;
canHeight?=?can1.height;
drawBackground();
ane?=?new?aneObj();
ane.init();
fruit?=?new?fruitObj();
fruit.init();
}
function?gameloop(){
//requestAnimFrame(gameloop);
window.requestAnimFrame(gameloop);
var?now?=?Date.now();
deltaTime?=?now?-?lastTime;
lastTime?=?now;
ane.draw();
fruit.draw();
}
var?aneObj?=?function(){
this.x?=?[];
this.len?=?[];
}
aneObj.prototype.num?=?50;
aneObj.prototype.init?=?function(){
for?(var?i?=?0;?i?<=?this.num;?i++)?{
this.x[i]?=?i?*?16?+?Math.random()?*?20;
this.len[i]?=?200?+?Math.random()?*?20;
}
}
aneObj.prototype.draw?=?function(){
ctx2.save();
ctx2.globalAlpha=0.2;
ctx2.lineWidth??=?20;
ctx2.lineCap?=?"round";
ctx2.strokeStyle?=?"#3b154e";
for?(var?i?=?0;?i?<?this.num;?i++)?{
ctx2.beginPath();
ctx2.moveTo(this.x[i],?canHeight);
ctx2.lineTo(this.x[i],?canHeight?-?this.len[i]);
ctx2.stroke();
}
ctx2.restore();
}
2016-08-25
要在gameloop最前面加一個(gè)clearRect()重繪,不然會(huì)一直疊加就不透明了
2016-08-22
對(duì)上面定義屬性名字寫(xiě)錯(cuò)了
var?can1;
var?cam2;
var?ctx1;
var?ctx2;