欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

canvas基础绘制-绚丽倒计时的实例

程序员文章站 2022-04-29 07:51:35
效果图: html:

效果图:

canvas基础绘制-绚丽倒计时的实例

html:

<!doctype html>
<html lang="en">
<head>
 <meta charset="utf-8">
 <title>ball</title>
 <script src="digit_1.js"></script>
 <script src="countdown.js"></script>
</head>
<body >
<canvas id="canvas" ></canvas>
</body>
</html>

digit_1.js在之前的 canvas基础绘制-倒计时 中有贴

countdown.js:

var window_width = 1024;
var window_height = 768;
var radius = 8;
var margin_top = 60;
var margin_left = 30;

var endtime = new date();//const声明变量,不可修改,必须声明时赋值;
endtime.settime( endtime.gettime() + 3600*1000);//当前时间向后一小时;
var curshowtimeseconds = 0;

var balls =[];
const colors = ["#33b5e5","#0099cc","#aa66cc","#9933cc","#99cc00","#669900","#ffbb33","#ff8800","#ff4444","#cc0000"];

window.onload = function () {
 //屏幕自适应
 window_width = document.body.clientwidth;
 window_height = document.body.clientheight;

 radius = math.round(window_width*4/5/108)-1;

 margin_top = math.round(window_height/5);
 margin_left = math.round(window_width/10);

 var canvas = document.getelementbyid("canvas");
 var context = canvas.getcontext("2d");

 canvas.width = window_width;
 canvas.height = window_height;

 curshowtimeseconds = getcurrentshowtimeseconds();
 setinterval(
  function () {
   update();
   render(context);
 },50)

};

function getcurrentshowtimeseconds() {
 var curtime = new date();//获取目前时间;
 var ret = endtime.gettime()-curtime.gettime();
 ret = math.round(ret/1000);//获取秒数差值;
 return ret>=0?ret:0;
}
function update() {

 var nextshowtimeseconds = getcurrentshowtimeseconds();

 var nexthours = parseint(nextshowtimeseconds/3600);
 var nextminutes = parseint((nextshowtimeseconds-nexthours*3600)/60);
 var nextseconds = nextshowtimeseconds%60;

 var curhours = parseint(curshowtimeseconds/3600);
 var curminutes = parseint((curshowtimeseconds-curhours*3600)/60);
 var curseconds = curshowtimeseconds%60;

 if(nextseconds!=curseconds){
  if(parseint(curhours/10)!=parseint(nexthours/10)){
   addballs(margin_left+0,margin_top,parseint(curhours/10));
  }
  if(parseint(curhours%10)!=parseint(nexthours%10)){
   addballs(margin_left+15*(radius+1),margin_top,parseint(curhours%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[i].g;
  //落到画布最底部时反弹起来
  if(balls[i].y>=window_height){
   balls[i].y = window_height-radius;
   balls[i].vy = -balls[i].vy*0.75;
  }
 }

 // 如果小球出了画布,就清除小球,性能优化
 var cnt = 0;
 for(var i=0;i<balls.length;i++){
  if(balls[i].x-radius>0&&balls[i].x+radius<window_width){
   balls[cnt++] = balls[i];
  }
 }

 while (balls.length>math.min(300,cnt)){
  balls.pop();
 }
}
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 * (radius + 1) + (radius + 1),
     y: y + i * 2 * (radius + 1) + (radius + 1),
     g: 1.5 + math.random(),
     vx: math.pow(-1, math.ceil(math.random() * 1000)) * 4,//pow(x,y),x 的 y 次幂;ceil()可对一个数进行上舍入;
     vy: -5,
     color: colors[math.floor(math.random() * colors.length)]//floor()对一个数进行下舍入
    };
    balls.push(aball);
   }
  }
 }
}
function render(cxt) {
 //每一帧都要对动画进行刷新,不然就会新的旧的叠在一起;
 cxt.clearrect(0,0,window_width,window_height);//对一个矩形空间里的动画进行刷新;
 //倒计时的时间绘制
 var hours = parseint(curshowtimeseconds/3600);
 var minutes = parseint((curshowtimeseconds-hours*3600)/60);
 var seconds = curshowtimeseconds%60;

 renderdigit(margin_left,margin_top,parseint(hours/10),cxt);
 renderdigit(margin_left+15*(radius+1),margin_top,parseint(hours%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)";

 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) {
    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();
   }
  }
 }
}

以上这篇canvas基础绘制-绚丽倒计时的实例就是小编分享给大家的全部内容了,希望能给大家一个参考,也希望大家多多支持。