求大神指教,为什么我的这个只是闪一下就没了,有颜色的小球并不滚动
var WINDOW_WIDTH=400;//画布长度
var WINDOW_HEIGHT=300;//画布高度
var RADIUS=2;//每个数字中小圆点的半径
var MARGIN_LEFT=20;//第一个数字离画布左边的距离
var MARGIN_TOP=30;//第一个数字离画布上边的距离
//截止的日期
const endTime=new Date(2015,11,5,18,47,52);
var curShowTimeSeconds=0;
var balls=[];
const colors=["#33b5e5","#0099cc","#aa66cc","#9933cc","#99cc00","#669900","#ffbb33","#ff8800","#ff4444","#cc0000"];
window.onload=function(){
var canvas=document.getElementById('canvas');
//获取画布环境
var context=canvas.getContext('2d');
canvas.width=WINDOW_WIDTH;
canvas.height=WINDOW_HEIGHT;
//获取此时的时间
curShowTimeSeconds=getCurrentShowTimeSeconds();
//定时器每隔50毫秒执行一次程序
setInterval(function(){
render(context);
update();
},50);
}
//获取截止日期到现在日期的差值
function getCurrentShowTimeSeconds(){
var curTime=new Date();
var ret=endTime.getTime()-curTime.getTime();//毫秒
ret=Math.round(ret/1000);//转换成秒
return ret>=0?ret:0;//若差值大于0则返回,若小于0,则返回0
}
function update(){
//执行update时再次获取此时时间
var nextShowTimeSeconds=getCurrentShowTimeSeconds();
var nextHours=parseInt(nextShowTimeSeconds/3600);
var nextMinutes = parseInt((nextShowTimeSeconds-nextHours*3600)/60);
var nextSeconds = nextShowTimeSeconds%60;
//主程序时获取的时间
var curHour=parseInt(curShowTimeSeconds/3600);
var curMinutes=parseInt((curShowTimeSeconds-curHour*3600)/60);
var curSeconds=curShowTimeSeconds%60;
if(nextSeconds!=curSeconds){//判断update时的时间和主程序时获取的现在时间是否一样,使倒计时转动,并且小球跳动
if(parseInt(curHour/10)!=parseInt(nextHours/10)){
addBalls(MARGIN_LEFT ,MARGIN_TOP,parseInt(curHour/10));
}
if(parseInt(curHour%10)!=parseInt(nextHours%10)) {
addBalls(MARGIN_LEFT + 15 * (RADIUS + 1), MARGIN_TOP, parseInt(curHour % 10));
}
if(parseInt(curMinutes/10)!=parseInt(nextMinutes/10)) {
addBalls(MARGIN_LEFT + 39 * (RADIUS + 1), MARGIN_TOP, parseInt(curMinutes / 10));
}
if(parseInt(curMinutes%10)!=parseInt(nextMinutes%10)) {
addBalls(MARGIN_LEFT + 54 * (RADIUS + 1), MARGIN_TOP, parseInt(curMinutes % 10));
}
if(parseInt(curSeconds/10)!=parseInt(nextSeconds/10)) {
addBalls(MARGIN_LEFT + 78 * (RADIUS + 1), MARGIN_TOP, parseInt(curSeconds / 10));
}
if(parseInt(curSeconds%10)!=parseInt(nextSeconds%10)) {
addBalls(MARGIN_LEFT + 93 * (RADIUS + 1), MARGIN_TOP, parseInt(curSeconds % 10));
}
curShowTimeSeconds=nextShowTimeSeconds;
}
updateBalls();
}
function updateBalls(){
for(var i=0;i<balls.length;i++){
balls[i].x+=balls[i].vx;
balls[i].y+=balls[i].vy;
balls[i].vy+=balls.g;
if(balls[i].y>=WINDOW_HEIGHT-RADIUS){
balls[i].y=WINDOW_HEIGHT-RADIUS;
balls[i].vy=-balls[i].vy*0.75;
}
}
}
function addBalls(x,y,num){
for(var i=0;i<lend1[num].length;i++){
for(var j=0;j<lend1[num][i].length;j++){
if(lend1[num][i][j]==1){
var aBall={
x:x+j*2*(RADIUS+1)+(RADIUS+1),
y:y+i*2*(RADIUS+1)+(RADIUS+1),
g:0.5+Math.random(),
vx:Math.pow(-1,Math.ceil(Math.random()*1000))*2,
vy:-5,
color:colors[Math.floor(Math.random()*colors.length)]
};
balls.push(aBall);
}
}
}
}
//画出此刻离截止日期的时间
function render(cxt){
//清除前一次的图形
cxt.clearRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT);
//截止的小时
var hour=parseInt(curShowTimeSeconds/3600);
//截止的分钟
var minutes=parseInt((curShowTimeSeconds-hour*3600)/60);
//截止的分钟
var seconds=curShowTimeSeconds%60;
renderDigit(MARGIN_LEFT,MARGIN_TOP,parseInt(hour/10),cxt);
renderDigit(MARGIN_LEFT+15*(RADIUS+1),MARGIN_TOP,parseInt(hour%10),cxt);
renderDigit(MARGIN_LEFT+30*(RADIUS+1),MARGIN_TOP,10,cxt);
renderDigit(MARGIN_LEFT+39*(RADIUS+1),MARGIN_TOP,parseInt(minutes/10),cxt);
renderDigit(MARGIN_LEFT+54*(RADIUS+1),MARGIN_TOP,parseInt(minutes%10),cxt);
renderDigit(MARGIN_LEFT+69*(RADIUS+1),MARGIN_TOP,10,cxt);
renderDigit(MARGIN_LEFT+78*(RADIUS+1),MARGIN_TOP,parseInt(seconds/10),cxt);
renderDigit(MARGIN_LEFT+93*(RADIUS+1),MARGIN_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,RADIUS,0,2*Math.PI,true);
cxt.closePath();
cxt.fill();
}
}
//画出每个数字
function renderDigit(x,y,num,cxt){
//圆点的颜色
cxt.fillStyle="rgb(0,102,153)";
//对lend1进行遍历,i是每个数字的行数,j是没个数字的列数
for(var i=0;i<lend1[num].length;i++){
for(var j=0;j<lend1[num][i].length;j++){
if(lend1[num][i][j]==1){
cxt.beginPath();
cxt.arc(x+j*2*(RADIUS+1)+(RADIUS+1),y+i*2*(RADIUS+1)+(RADIUS+1),RADIUS,0,2*Math.PI);
cxt.closePath();
cxt.fill();
}
}
}
}