小球不會(huì)下落,一直疊加
var?Width=1024; var?Height=768; var?Ru=8;?//小圓的半徑,后面圓心X/Y坐標(biāo)加1,是為了美觀 var?Top?=60;??//?數(shù)字距離畫(huà)布上邊距 var?Left?=30;?//?數(shù)字距離畫(huà)布左邊距 var?endDate?=new?Date(2014,10,3,23,59,59); var?showTimeSeconds?=0;?//?用來(lái)存放開(kāi)始時(shí)與結(jié)束時(shí)之間的秒數(shù) var?balls=[];?//存放小球 var?ballColors=["#33b5e5","#0099cc","#a6c","#93c","#9c0","#690","#fb3","#f80","#f44","#c00"]; window.onload?=function(){ ?????var?canvas?=document.getElementById("canvas"); ?????canvas.width=Width;?? ?????canvas.height=Height; ?????var?context?=canvas.getContext("2d"); ????? ?????showTimeSeconds?=getTimeSeconds(); ?????setInterval(function(){ ??????????????render(context); ?????????????upDate(); ?????},100); } function?upDate(){ ????var?nextShowTimeSeconds?=getTimeSeconds(); ????var?nextHours?=parseInt(?nextShowTimeSeconds?/?3600); ????var?nextMinutes?=parseInt(?(nextShowTimeSeconds-nextHours*3600)/60?); ????var?nextSeconds?=nextShowTimeSeconds%60; ????var?curHours?=parseInt(?showTimeSeconds?/?3600); ????var?curMinutes?=parseInt(?(showTimeSeconds-curHours*3600)/60?); ????var?curSeconds?=showTimeSeconds%60; ????if(nextSeconds?!=?curSeconds){ ??????????? ???????????if(parseInt(curHours/10)?!=?parseInt(nextHours/10)){ ????????????????addBalls(Left?+?0*(Ru+1)?,Top,parseInt(curHours/10)); ???????????} ???????????if(parseInt(curHours%10)?!=?parseInt(nextHours%10)){ ????????????????addBalls(Left?+?15*(Ru+1)?,Top,parseInt(curHours%10)); ???????????} ???????????if(parseInt(curMinutes/10)?!=?parseInt(nextMinutes/10)){ ????????????????addBalls(Left?+?39*(Ru+1)?,Top,parseInt(curMinutes/10)); ???????????} ???????????if(parseInt(curMinutes%10)?!=?parseInt(nextMinutes%10)){ ????????????????addBalls(Left?+?54*(Ru+1)?,Top,parseInt(curMinutes%10)); ???????????} ???????????if(parseInt(curSeconds/10)?!=?parseInt(nextSeconds/10)){ ????????????????addBalls(Left?+?78*(Ru+1)?,Top,parseInt(curSeconds/10)); ???????????} ???????????if(parseInt(curSeconds%10)?!=?parseInt(nextSeconds%10)){ ????????????????addBalls(Left?+?93*(Ru+1)?,Top,parseInt(curSeconds%10)); ???????????} ???????????showTimeSeconds?=nextShowTimeSeconds; ????} ????upDateBalls(); } function?upDateBalls(){ ????for(var?i=0;i<balls.length;i++){ ???????????balls[i].x??+=bals[i].vx; ???????????balls[i].y??+=balls[i].vy; ???????????balls[i].vy?+=balls[i].g; ???????????if(balls[i].y?>=?Height-Ru){ ????????????????balls[i].y?=Height-Ru; ????????????????balls[i].vy?=?-?balls[i].vy*0.75; ???????????} ????} } function?addBalls(x,y,num){ ?????for(var?i=0;i<digit[num].length;i++){ ??????????for(var?j=0;j<digit[num][i].length;j++){ ????????????????if(digit[num][i][j]?==?1){ ??????????????????????var?aBall={ ?????????????????????????????x:?x+j*2*(Ru+1)+(Ru+1), ?????????????????????????????y:?y+i*2*(Ru+1)+(Ru+1), ?????????????????????????????g:?1.5+Math.random(),?//重力? ????????????????????????????vx:?Math.pow(-1,Math.ceil(Math.random()*1000))*4,?//x軸方向隨機(jī)運(yùn)動(dòng) ????????????????????????????vy:?-5,?//y軸有一種上拋的效果 ????????????????????????color:?ballColors[Math.floor(Math.random()*ballColors.length)] ??????????????????????}; ??????????????????????balls.push(aBall); ????????????????} ??????????} ?????} } function?getTimeSeconds(){ ?????var?curTime?=new?Date(); ?????var?ret?=endDate.getTime()-curTime.getTime(); ?????ret?=Math.round(ret/1000); ?????return?ret>=0???ret?:0?; } ?function?render(cxt){ ??????cxt.clearRect(0,0,Width,Height);??//?刷新,防止疊加 ??????var?hours?=parseInt(?showTimeSeconds?/?3600); ??????var?minutes?=parseInt(?(showTimeSeconds-hours*3600)/60?); ??????var?seconds?=showTimeSeconds%60; ?????? ??????/*?把數(shù)字拆分成兩部分繪制:12?→?1+2?等?*/ ??????renderDigit(Left,Top,parseInt(hours/10)?,cxt);?//?數(shù)字之間占15個(gè)大小 ??????renderDigit(Left?+?15*(Ru+1)?,Top,parseInt(hours%10)?,cxt); ??????renderDigit(Left?+?30*(Ru+1)?,Top?,10?,cxt);??//?digit[10]是冒號(hào)的點(diǎn)陣圖,占9個(gè)大小 ??????renderDigit(Left?+?39*(Ru+1)?,Top?,parseInt(minutes/10)?,cxt); ??????renderDigit(Left?+?54*(Ru+1)?,Top?,parseInt(minutes%10)?,cxt); ??????renderDigit(Left?+?69*(Ru+1)?,Top,10,cxt); ??????renderDigit(Left?+?78*(Ru+1)?,Top,parseInt(seconds/10)?,cxt); ??????renderDigit(Left?+?93*(Ru+1)?,Top,parseInt(seconds%10)?,cxt); ? ??????for(var?i=0;i<balls.length;i++){ ??????????cxt.fillStyle?=balls[i].color; ??????????cxt.beginPath(); ??????????cxt.arc(balls[i].x,balls[i].y,Ru,0,2*Math.PI); ??????????cxt.closePath(); ??????????cxt.fill(); ??????} ?} ?function?renderDigit(x?,y?,num?,cxt){???//?繪制圓 ??????cxt.fillStyle?="rgb(0,102,153)"; ??????for(var?i=0;i<digit[num].length;i++){??//數(shù)字對(duì)應(yīng)的點(diǎn)陣圖0、1、2。。?;蛘呤?冒號(hào)‘:’ ???????????for(var?j=0;j<digit[num][i].length;j++){??//??判斷點(diǎn)陣圖中的每一行 ?????????????????if(digit[num][i][j]?==?1){????//?1:繪制?0:不繪制 ????????????????????????cxt.beginPath(); ????????????????????????cxt.arc(x+j*2*(Ru+1)+(Ru+1)?,y+i*2*(Ru+1)+(Ru+1)?,Ru?,?0,2*Math.PI); ????????????????????????cxt.closePath(); ????????????????????????cxt.fill(); ?????????????????} ???????????} ??????} ?}
2014-11-06
大贊!也希望關(guān)注這個(gè)課程的后續(xù):《Canvas繪圖詳解》:)
2014-11-02
已解決,是69行balls 少了一個(gè) l