杨小杰博客提供免费教程下载和网站搭建技术教程,主要分享和发布网站源码,致力创造一个高质量网络资源教程的分享平台

canvas荧光表源码分享

Young小杰2018-5-29 11:11网站搭建(3)862查询中……小标签: 网站源码 canvas 源码分享 原创

无聊看了一下网页制作从入门到放弃的书,看到一个canvas表盘案例觉得不错,就搞出来美化了一下,就是这个鸟样子

canvas表盘.jpg

<!DOCTYPE html>
<html>
<head>
	<meta charset="utf-8">
	<title>canvas钟表</title>
	<meta name="Keywords" content="canvas钟表">
	<meta name="author" content="Youngxj">
	<style type="text/css">
	body{margin:0;}
</style>
</head>
<body onload="run()">
	<canvas id="canvas" width=320 height=320 >如果你看到这段文字,说明你的浏览器弱爆了!</canvas>
	<script>
		window.onload=draw;
		function draw() {
			var canvas=document.getElementById('canvas');
			var context=canvas.getContext('2d');
			context.save();
			//改变表盘中心位置
			context.translate(160,160);
			var deg=2*Math.PI/12; 
			context.save(); 
			context.beginPath(); 
			for(var i=0;i<13;i++){
				var x=Math.sin(i*deg);
				var y=-Math.cos(i*deg);
				context.lineTo(x*160,y*160); 
			}
			var c=context.createRadialGradient(0,0,0,0,0,130);
			//改变表盘背景颜色(可渐变)
			c.addColorStop(0,"#000000");
			c.addColorStop(1,"#000000")
			context.fillStyle=c;
			context.fill();
			context.closePath(); 
			context.restore(); 

			context.save();
			context.beginPath();
			for(var i=1;i<13;i++){
				var x1=Math.sin(i*deg);
				var y1=-Math.cos(i*deg);
				context.fillStyle="green";
				context.font="bold 20px Calibri";
				context.textAlign='center';
				context.textBaseline='middle';
				context.fillText(i,x1*120,y1*120); 
			}
			context.closePath(); 
			context.restore(); 

			context.save();
			context.beginPath(); 
			for(var i=0;i<12;i++){
				var x2=Math.sin(i*deg);
				var y2=-Math.cos(i*deg);
				context.moveTo(x2*148,y2*148);
				context.lineTo(x2*135,y2*135); 
			} 
			context.strokeStyle='green';
			context.lineWidth=4;
			context.stroke();
			context.closePath();
			context.restore(); 

			context.save();
			var deg1=2*Math.PI/60;
			context.beginPath(); 
			for(var i=0;i<60;i++){
				var x2=Math.sin(i*deg1);
				var y2=-Math.cos(i*deg1);
				context.moveTo(x2*146,y2*146);
				context.lineTo(x2*140,y2*140); 
			} 
			context.strokeStyle='green';
			context.lineWidth=2;
			context.stroke();
			context.closePath(); 
			context.restore(); 

			context.save();
			// 表盘文字颜色、字体大小等
			context.strokeStyle="green";
			context.font=' 13px sans-serif';
			context.textAlign='center';
			context.textBaseline='middle';
			// 改变表盘中间文字内容
			context.strokeText(getwork(),0,45); 
			context.strokeText(startTime(),0,65);
			context.strokeText(nyr(),0,85); 
			context.restore(); 

			var time=new Date();
			var h=(time.getHours()%12)*2*Math.PI/12;
			var m=time.getMinutes()*2*Math.PI/60;
			var s=time.getSeconds()*2*Math.PI/60;
			context.save();
			context.rotate( h + m/12 + s/720) ;
			context.beginPath();
			context.moveTo(0,6);
			context.lineTo(0,-85);
			context.strokeStyle="green";
			context.lineWidth=6;
			context.stroke();
			context.closePath();
			context.restore();

			context.save();
			context.rotate( m+s/60 ) ;
			context.beginPath();
			context.moveTo(0,8);
			context.lineTo(0,-105);
			context.strokeStyle="green";
			context.lineWidth=4;
			context.stroke();
			context.closePath();
			context.restore();

			context.save();
			context.rotate( s ) ;
			context.beginPath();
			context.moveTo(0,10);
			context.lineTo(0,-120);
			context.strokeStyle="green";
			context.lineWidth=2;
			context.stroke();
			context.closePath();
			context.restore(); 
			context.restore();
			setTimeout(draw, 1000);
		}
		// 当前时间
		function startTime(){
			var today=new Date();
			var h=today.getHours();
			var m=today.getMinutes();
			var s=today.getSeconds();
			m=checkTime(m);
			s=checkTime(s);
			return h+":"+m+":"+s;
			t=setTimeout(function(){startTime()},500);
		}
		function checkTime(i){
			if (i<10){
				i="0" + i;
			}
			return i;
		}
		// 当前星期
		function getwork(){
			var l = ["日","一","二","三","四","五","六"];
			var d = new Date().getDay();
			var str = '星期'+l[d];
			return str;
		}
		// 当前年月日
		function nyr(){
			var myDate = new Date();
			return myDate.toLocaleDateString();

		}
	</script>
</body>
</html>


评论:

_零度时光 Google Chrome 55.0.2883.87 Windows 7 2018-07-13 22:45
世界离了你不会不转,只会越转越快,毕竟轻了很多。你的文章写的太好啦,赞一个回复了[真棒]
MIRACLE E Google Chrome 57.0.2987.132 Linux 2018-05-29 12:47
直接ifame插入行吗 还有大小能不能调节
Young小杰 2018-05-29 12:48
回复了MIRACLE E:canvas基本都不支持自适应大小,这个默认320了

发表评论: