HTML5-炫丽的时钟效果Canvas绘图与动画基础练习

源自慕课网

效果如下:

全部代码:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
 <head>
  <title> new document </title>
  <meta name="generator" content="editplus" />
  <meta name="author" content="" />
  <meta name="keywords" content="" />
  <meta name="description" content="" />
 </head>

 <body>
  <canvas id="canvas"></canvas>
 </body>
</html>
<script type="text/javascript" src="digit.js"></script>
<script type="text/javascript">
var WINDOW_WIDTH=1024;
var WINDOW_HEIGHT=600;
var MARGIN_TOP=60;
var MARGIN_LEFT=30;
//存放彩色小球
var balls=[];

const colors=["#33B5E5","#0099CC","#AA66CC","#669900","#FFBB33","#FF8800","#FF4444","CC0000"];
var r=8;//圆半径
	window.onload=function(){
	var canvas=document.getElementById("canvas");
	var context=canvas.getContext("2d");
	canvas.width=WINDOW_WIDTH;
	canvas.height=WINDOW_HEIGHT;

    window.setInterval(function(){
	   var curr=new Date();
	   var curHours=curr.getHours();
	   var curMinutes=curr.getMinutes();
	   var curSeconds=curr.getSeconds();

       //当时间的秒数改变时添加彩色小球
	   if(preSeconds!=curSeconds)
	   {

        if(parseInt(curHours/10)!=parseInt(preHours/10)){
		addBall(MARGIN_LEFT,MARGIN_TOP,parseInt(curHours/10));
		}
		  if(parseInt(curHours%10)!=parseInt(preHours%10)){
		addBall(MARGIN_LEFT+15*(r+1),MARGIN_TOP,parseInt(curHours%10));
		}
		 if(parseInt(curMinutes/10)!=parseInt(preMinutes/10)){
		addBall(MARGIN_LEFT+39*(r+1),MARGIN_TOP,parseInt(curMinutes/10));
		}
		  if(parseInt(curMinutes%10)!=parseInt(preMinutes%10)){
		addBall(MARGIN_LEFT+54*(r+1),MARGIN_TOP,parseInt(curMinutes%10));
		}

		 if(parseInt(curSeconds/10)!=parseInt(preSeconds/10)){
		addBall(MARGIN_LEFT+78*(r+1),MARGIN_TOP,parseInt(curSeconds/10));
		}
		  if(parseInt(curSeconds%10)!=parseInt(preSeconds%10)){
		addBall(MARGIN_LEFT+93*(r+1),MARGIN_TOP,parseInt(curSeconds%10));
		}
	   }
	   render(context);	

		},50);

	}

	//添加小球(数字num上的彩色小球,x,y:数字方块容器的左上角顶点坐标)
    function addBall(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 ball={
					 x:x+j*2*(r+1)+r+1,
					 y:y+i*2*(r+1)+r+1,
					 g:1.5+Math.random(),
					 vx:Math.pow(-1,Math.ceil(Math.random()*1000))*4,//结果为-4/4
					 vy:-5,
                     color:colors[Math.floor(Math.random()*colors.length)]
					 };
					 balls.push(ball);

				 }
			 }
	     }
	}
  //----画彩色小球
  function renderBalls(context){
	  		//context.clearRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT)
     for (var i=0;i<balls.length ;i++ )
     {
        context.beginPath();
		context.arc(balls[i].x,balls[i].y,r,0,2*Math.PI,false);
		context.fillStyle=balls[i].color;
		context.fill();
		context.closePath();
     }
  }

 //----更新彩色小球位置
  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+r>=WINDOW_HEIGHT){
	 balls[i].y=WINDOW_HEIGHT-r;
	 balls[i].vy=-balls[i].vy*0.6;
	 }
	 }
  }

	var preHours;
	var preMinutes;
	var preSeconds;

	//渲染整个画布
	function render(context)
	{
		context.clearRect(0,0,WINDOW_WIDTH,WINDOW_HEIGHT)
        var now=new Date();
		var hours=now.getHours();
		var minutes=now.getMinutes();
		var seconds=now.getSeconds();
		preHours=hours;
		preMinutes=minutes;
        preSeconds=seconds;
		//---时
	    renderDigit(MARGIN_LEFT,MARGIN_TOP,parseInt(hours/10),context);
	    renderDigit(MARGIN_LEFT+15*(r+1),MARGIN_TOP,hours%10,context);
        renderDigit(MARGIN_LEFT+30*(r+1),MARGIN_TOP,10,context);
        //---分
    	renderDigit(MARGIN_LEFT+39*(r+1),MARGIN_TOP,parseInt(minutes/10),context);
	    renderDigit(MARGIN_LEFT+54*(r+1),MARGIN_TOP,minutes%10,context);
	    renderDigit(MARGIN_LEFT+69*(r+1),MARGIN_TOP,10,context);
        //---秒
    	renderDigit(MARGIN_LEFT+78*(r+1),MARGIN_TOP,parseInt(seconds/10),context);
	    renderDigit(MARGIN_LEFT+93*(r+1),MARGIN_TOP,seconds%10,context);
        //---画小球
	    renderBalls(context);
		//---改变小球路径
	    UpdateBalls();

	}
	//渲染每个数字
	function renderDigit(x,y,num,context)
	{
	context.fillStyle="green";
	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){
			context.beginPath();
			context.arc(x+j*2*(r+1)+r+1,y+i*2*(r+1)+r+1,r,0,2*Math.PI,false);
			context.closePath();
			context.fill();
			}
		}
	}
	}

</script>

  

数字的结构定义:digit.js
digit =
    [
        [
            [0,0,1,1,1,0,0],
            [0,1,1,0,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,0,1,1,0],
            [0,0,1,1,1,0,0]
        ],//0
        [
            [0,0,0,1,1,0,0],
            [0,1,1,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [1,1,1,1,1,1,1]
        ],//1
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,0,0,0],
            [0,1,1,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,0,0,0,1,1],
            [1,1,1,1,1,1,1]
        ],//2
        [
            [1,1,1,1,1,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,1,0,0],
            [0,0,0,0,1,1,0],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//3
        [
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,1,0],
            [0,0,1,1,1,1,0],
            [0,1,1,0,1,1,0],
            [1,1,0,0,1,1,0],
            [1,1,1,1,1,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,1,1]
        ],//4
        [
            [1,1,1,1,1,1,1],
            [1,1,0,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,1,1,1,1,0],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//5
        [
            [0,0,0,0,1,1,0],
            [0,0,1,1,0,0,0],
            [0,1,1,0,0,0,0],
            [1,1,0,0,0,0,0],
            [1,1,0,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//6
        [
            [1,1,1,1,1,1,1],
            [1,1,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,0,0,1,1,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0],
            [0,0,1,1,0,0,0]
        ],//7
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,1,1,0]
        ],//8
        [
            [0,1,1,1,1,1,0],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [1,1,0,0,0,1,1],
            [0,1,1,1,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,0,1,1],
            [0,0,0,0,1,1,0],
            [0,0,0,1,1,0,0],
            [0,1,1,0,0,0,0]
        ],//9
        [
            [0,0,0,0],
            [0,0,0,0],
            [0,1,1,0],
            [0,1,1,0],
            [0,0,0,0],
            [0,0,0,0],
            [0,1,1,0],
            [0,1,1,0],
            [0,0,0,0],
            [0,0,0,0]
        ]//:
    ];

  

时间: 2024-10-09 01:19:43

HTML5-炫丽的时钟效果Canvas绘图与动画基础练习的相关文章

炫丽的倒计时效果Canvas绘图与动画基础

digit.js 1 digit = [ 2 [ 3 //0 4 [0, 0, 1, 1, 1, 0, 0], 5 [0, 1, 1, 0, 1, 1, 0], 6 [1, 1, 0, 0, 0, 1, 1], 7 [1, 1, 0, 0, 0, 1, 1], 8 [1, 1, 0, 0, 0, 1, 1], 9 [1, 1, 0, 0, 0, 1, 1], 10 [1, 1, 0, 0, 0, 1, 1], 11 [1, 1, 0, 0, 0, 1, 1], 12 [0, 1, 1, 0, 1

canvas-炫丽的倒计时效果Canvas绘图与动画基础

canvas 是基于转台来绘制的 来了解一下canvas的浏览器兼容性问题,如下图所示.(截图自can i use) tips:刚刚拿去ie8下测了一下,什么反应都没有,提前设定好的,如果该浏览器不支持的话要提示的也不提示,开发者工具中的Element中的代码也什么都没有. HTML: <canvas id="canvas" style="border:1px solid red;"></canvas> JS : var canvas = d

学习Canvas绘图与动画基础 制作弧和圆(五)

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>制作弧和圆</title> 6 </head> 7 <body> 8 <canvas id="canvas" style="border:1px solid #aaa; disp

学习Canvas绘图与动画基础 为多边形着色(三)

1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title>多边形着色</title> 6 </head> 7 <body> 8 <canvas id="canvas" style="border:1px solid #aaa; disp

学习Canvas绘图与动画基础(一)

一.创建canvas 1 <!DOCTYPE html> 2 <html> 3 <head lang="en"> 4 <meta charset="UTF-8"> 5 <title></title> 6 </head> 7 8 <body> 9 <canvas id="canvas" width="1024" height=&

HTML5中video标签与canvas绘图的使用

video标签的使用 video标签定义视频, 它是html5中的新标签, 它的属性如下(参考自文档): domo01 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>demo01</title> </head> <body> <video src="madashu

[ html canvas createImageData 创建万花筒效果 ] canvas绘图属性 createImageData 属性讲解 及创建万花筒效果

1 <!DOCTYPE html> 2 <html lang='zh-cn'> 3 <head> 4 <title>Insert you title</title> 5 <meta name='description' content='this is my page'> 6 <meta name='keywords' content='keyword1,keyword2,keyword3'> 7 <meta htt

Canvas绘图与动画详解

1.canvas 绘制       当canvas不设置大小时,默认宽300,高150:       注意:不建议使用css 设置大小,可以利用行内样式 width="",height=""去设置(无单位):                 canvas 是基于状态进行绘制的:      举例说明:            上述代码显示结果为全部均为黑色:       如何更改? 在绘制前context.beginPath(),结束后 context.closePat

HTML5 学习总结(四)——canvas绘图、WebGL、SVG

HTML5 学习总结(四)--canvas绘图.WebGL.SVG 目录 一.Canvas 1.1.创建canvas元素 1.2.画线 1.3.绘制矩形 1.4.绘制圆弧 1.5.绘制图像 1.6.绘制文字 1.7.随机颜色与简单动画 二.WebGL 2.1.HTML5游戏开发 2.2.1.Cocos2D-HTML5 2.2.2.Egret(白鹭引擎) 三.SVG 3.1.SVG Hello Wrold 3.2.多种引入SVG的方法 3.3.画直线 3.4.画椭圆 3.5.文本与矩形 3.6.向