用HTML5 Canvas写了个时钟

这周在学canvas,就写了个时钟练习练习

window.onload = function(){
				
				var Timer = function(canvas_id){
					
					this.canvas = document.getElementById(canvas_id);
					this.ctx = this.canvas.getContext('2d');
					this.width = this.canvas.width;
					this.height = this.canvas.height;	
									
				};
				
				
				Timer.prototype = {
					
				
					
					draw:function(){
						
						this.ctx.save();
						this.ctx.translate(0,0);
						this.ctx.clearRect(0,0,this.width,this.height);	
						this.drawBackground();
						this.drawClock();
						this.drawClockPoint();
						this.ctx.restore();
										 
						
						
					},
					
					drawBackground:function(){
						
						this.ctx.globalAlpha  = 0.5;
						this.ctx.fillStyle = 'blue';
						this.ctx.fillRect(0,0,this.width,this.height);
						
					},
					
					drawClock:function(){
						
						
						//move to center
						this.ctx.translate(this.width/2,this.height/2);
						
						
						//inner arc
// 	
						this.ctx.fillStyle = '#333333';
						this.ctx.beginPath();
						this.ctx.arc(0,0,(this.width)/20,0,Math.PI*2,true);
						this.ctx.fill();
// 						
						//outer arc
						this.ctx.fillStyle = 'yellow';
						this.ctx.beginPath();
						this.ctx.arc(0,0,(this.width/2),0,Math.PI*2,true);
						this.ctx.fill();
						
						// Outer arc line
						for(var i=0;i<60;i++){
							this.ctx.save();
							this.ctx.rotate(Math.PI*2/60*i)
							this.ctx.beginPath();
							if(i % 5 == 0){
								this.ctx.moveTo((this.width/2.5),0);
							}else{
								this.ctx.moveTo((this.width/2.2),0);
							}
							
							this.ctx.lineTo(this.width/2,0)
							this.ctx.stroke();
							this.ctx.restore();
						}
						
						
					},
					
					drawClockPoint:function(){
						var currentDate = new Date();
						var hour = currentDate.getHours();
						var minute = currentDate.getMinutes();
						var second = currentDate.getSeconds();
						
						
						// alert(second);
						
						//画秒钟
						this.ctx.save();
						// this.ctx.fillStyle = 'black';
						this.ctx.rotate(Math.PI*2/60*second - Math.PI/2)
						this.ctx.beginPath();
						this.ctx.moveTo(0,0);
						this.ctx.lineTo(this.width/2,0);
						this.ctx.stroke();
					    this.ctx.restore();
					    
					    
					    //画分钟
						this.ctx.save();
						// this.ctx.lineWidth=5;
						this.ctx.fillStyle = 'black';
						this.ctx.rotate(Math.PI*2/60*(minute+second/60)  - Math.PI/2)
						this.ctx.beginPath();
						this.ctx.moveTo(0,0);
						this.ctx.lineTo(this.width/3,0);
						this.ctx.stroke();
					    this.ctx.restore();
					    
					    
					    //画时钟
						this.ctx.save();
						// this.ctx.lineWidth=10;
						this.ctx.fillStyle = 'black';
						this.ctx.rotate(Math.PI*2/12*(hour+minute/60+second/3600) - Math.PI/2)
						this.ctx.beginPath();
						this.ctx.moveTo(0,0);
						this.ctx.lineTo(this.width/3.5,0);
						this.ctx.stroke();
					    this.ctx.restore();
					    
					    
					    
						
					}
					
					
					
					
				};
					
					
				
			 var timer = new Timer('canvas');
			 timer.draw();
			 
			 function run (){
			 	timer.draw();
			 }
			 
			 window.setInterval(run,1000);
			 
				

	
				
			}

 页面代码

很短到一句

<canvas id='canvas' width='200' height='200'></canvas>

 

 

 

 

 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是使用 Canvas 绘制一个简单的时钟的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas 时钟</title> <style> #clockCanvas { border: 1px solid #000; } </style> </head> <body> <canvas id="clockCanvas" width="200" height="200"></canvas> <script> function drawClock() { var canvas = document.getElementById('clockCanvas'); var context = canvas.getContext('2d'); var radius = canvas.height / 2; context.translate(radius, radius); radius = radius * 0.9; setInterval(function() { drawFace(context, radius); drawNumbers(context, radius); drawTime(context, radius); }, 1000); } function drawFace(context, radius) { var grad; context.beginPath(); context.arc(0, 0, radius, 0, 2 * Math.PI); context.fillStyle = 'white'; context.fill(); grad = context.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05); grad.addColorStop(0, '#333'); grad.addColorStop(0.5, 'white'); grad.addColorStop(1, '#333'); context.strokeStyle = grad; context.lineWidth = radius * 0.1; context.stroke(); context.beginPath(); context.arc(0, 0, radius * 0.1, 0, 2 * Math.PI); context.fillStyle = '#333'; context.fill(); } function drawNumbers(context, radius) { var angle; var num; context.font = radius * 0.15 + 'px arial'; context.textBaseline = 'middle'; context.textAlign = 'center'; for (num = 1; num < 13; num++) { angle = num * Math.PI / 6; context.rotate(angle); context.translate(0, -radius * 0.85); context.rotate(-angle); context.fillText(num.toString(), 0, 0); context.rotate(angle); context.translate(0, radius * 0.85); context.rotate(-angle); } } function drawTime(context, radius) { var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); // 对时、分、秒进行角度转换 hour = hour % 12; hour = (hour * Math.PI / 6) + (minute * Math.PI / (6 * 60)) + (second * Math.PI / (360 * 60)); drawHand(context, hour, radius * 0.5, radius * 0.07); minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60)); drawHand(context, minute, radius * 0.8, radius * 0.07); second = (second * Math.PI / 30); drawHand(context, second, radius * 0.9, radius * 0.02); } function drawHand(context, pos, length, width) { context.beginPath(); context.lineWidth = width; context.lineCap = 'round'; context.moveTo(0, 0); context.rotate(pos); context.lineTo(0, -length); context.stroke(); context.rotate(-pos); } drawClock(); </script> </body> </html> ``` 你可以将这段代码保存为 HTML 文件并在浏览器打开,就可以看到一个实时的时钟效果。希望这能帮到你!如果还有其他问题,请随时提问。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值