效果图在博客首页上。

html:

<canvas id="canvas" >Your browser does not support canvas</canvas>

css:

canvas {
display:block;
margin:60px auto;

border:1px solid black;

}

js:

<script>
window.onload=function(){
var canvas=document.getElementById('canvas');
var ctx=canvas.getContext("2d");
canvas.width=800;
canvas.height=600;

/*获取时间*/
function colock(){
var myDate=new Date();
var myHour=myDate.getHours();
var myMin=myDate.getMinutes();
var mySec=myDate.getSeconds();

/*背景*/
ctx.save();
ctx.rect(0,0,800,600);
ctx.fillStyle='#ddd';
ctx.fill();
ctx.stroke();
ctx.restore();

/*表盘*/
ctx.save();
ctx.beginPath();
ctx.arc(400,300,200,0,2*Math.PI,true);
ctx.fillStyle='#fff';
ctx.strokeStyle="#ffee00";
ctx.lineWidth=20;
ctx.fill();
ctx.closePath();
ctx.stroke();
ctx.restore();

/*数字*/
ctx.save();
ctx.beginPath();
ctx.font="15px Arial";
ctx.fillStyle='black';
var num=['3','4','5','6','7','8','9','10','11','12','1','2'];
var r=150;
//ctx.arc(100,100,150,0,2*Math.PI,true);
for(var i=0;i<12;i++){
ctx.fillText(num[i],400+r*Math.cos(i*30*Math.PI/180),300+r*Math.sin(i*30*Math.PI/180));
}
ctx.closePath();
ctx.restore();

/*时针*/
ctx.save();
ctx.beginPath();
ctx.translate(400,300);
ctx.rotate((myHour-3)*30*Math.PI/180+(30*myMin/60)*Math.PI/180);
ctx.lineTo(-20,0);
ctx.lineTo(60,0);
ctx.lineWidth=8;
ctx.closePath();
ctx.strokeStyle='black';
ctx.stroke();
ctx.restore();

/*分针*/
ctx.save();
ctx.beginPath();
ctx.translate(400,300);
ctx.rotate((myMin-15)*6*Math.PI/180+Math.PI/2+(6*mySec/60)*Math.PI/180);
ctx.lineTo(0,20);
ctx.lineTo(0,-90);
ctx.lineWidth=6;
ctx.closePath();
ctx.strokeStyle='#333';
ctx.stroke();
ctx.restore();

/*秒针*/
ctx.save();
ctx.beginPath();
ctx.translate(400,300);
ctx.rotate((mySec-15)*6*Math.PI/180+Math.PI/2);
ctx.lineTo(0,20);
ctx.lineTo(0,-120);
ctx.lineWidth=3;
ctx.closePath();
ctx.strokeStyle='red';
ctx.stroke();
ctx.restore();
}

colock();
setInterval(colock,1000);

}

用canvas画时钟的更多相关文章

  1. canvas画时钟

    <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...

  2. canvas画时钟,重拾乐趣!

    canvas时钟--效果图 一.先来简单介绍画时钟需要的canvas知识 1.在HTML页面中添加canvas元素,必须定义canvas元素的id属性值以便接下来的调用. HTML代码: <ca ...

  3. canvas 画时钟 会动呦

    //半径 var r = 130; //重置原点 ctx.save(); ctx.translate(400, 500); //使用translate重置原点 function drawClock() ...

  4. html5学习(一)--canvas画时钟

    利用空余时间学习一下html5. <!doctype html> <html> <head></head> <body> <canva ...

  5. 深夜,用canvas画一个时钟

    深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...

  6. [JS,Canvas]日历时钟

    [JS,Canvas]日历时钟 Html: <!doctype html> <html> <head> <meta charset="UTF-8&q ...

  7. 使用canvas绘制时钟

    使用canvas绘制时钟  什么使canvas呢?HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图 ...

  8. HTML5 之Canvas 绘制时钟 Demo

    <!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...

  9. 简单酷炫的Canvas数字时钟

    声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 我记得很早之前就看过这个DEMO,是岑安大大博客里看到的: 就是这个数字时钟,当时觉得这个创意不错,但是也没去折腾.直到昨天同事又在网上看 ...

随机推荐

  1. linux:习惯

    1>.系统里面必须最少有两个账号,一个root,一个普通账户; 2>.平时登录系统均使用普通账户登录,除非需要配置或者执行特殊命令才切换至root; 3>.linux系统下严格区分大 ...

  2. 【转】轻量级分布式 RPC 框架

    第一步:编写服务接口 第二步:编写服务接口的实现类 第三步:配置服务端 第四步:启动服务器并发布服务 第五步:实现服务注册 第六步:实现 RPC 服务器 第七步:配置客户端 第八步:实现服务发现 第九 ...

  3. 刷新本地的DNS缓存

    用“WIN +R”快捷键打开运行窗口,输入“cmd”命令,进行命令行窗口.

  4. Leetcode: Frog Jump

    A frog is crossing a river. The river is divided into x units and at each unit there may or may not ...

  5. Summary: Process & Tread

    refer to http://www.programmerinterview.com/index.php/operating-systems/thread-vs-process/ A process ...

  6. Python学习总结7:随机字符串和随机数

    Python生成随机数与随机字符串,需要import random模块.random模块最常用的几个函数如下: 1. random.random(a, b) 用于生成一个0到1的随机符点数: 0 &l ...

  7. ArcGIS Engine中加载数据

    ArcGIS Engine中加载数据 http://blog.csdn.net/gisstar/article/details/4206822   分类: AE开发积累2009-05-21 16:49 ...

  8. paper 37 : WINCE的BIB文件解析

    WINCE的BIB文件解析 BIB的全称为Binary Image Builder,在Wince编译过程中的最后MakeImage阶段会用到BIB文件,BIB文件的作用是指示构建系统如何构建二进制映像 ...

  9. 07---Net基础加强

    第六节复习 泛型和非泛型集合的区别 通常情况下,建议您使用泛型集合,因为这样可以获得类型安全的直接优点而不需要从基集合类型派生并实现类型特定的成员.此外,如果集合元素为值类型,泛型集合类型的性能通常优 ...

  10. java父类转子类的一个方法

    一般子类可以转父类.但父类转子类就会报cast error. 使用jsonobject 思想:先把父类转jsonstring 再把jsonstring转子类.剩余的子类值可以设定进去. import ...