本示例使用HTML5 canvas,模拟显示了一个时钟, 请使用支持HTML5的浏览器预览效果:

HTML部分:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0,minimum-scale=1.0,maximum-scale=1.0">
<title>canvas绘图</title>
</head>
<body onload="init()">
<canvas id="canvas" width="200px" height="200px"></canvas>
</body>
</html>

JavaScript部分:

function init(){
var canvas = document.getElementById("canvas"),
context = canvas.getContext("2d"); setInterval(function(){draw(canvas, context)},1000);
}
function draw(canvas, context){
var x = canvas.width,
y = canvas.height,
r = Math.min(x/2, y/2); context.clearRect(0, 0, x, y); //清除绘画历史
//绘画钟框
context.fillStyle = "#f1f1f1";
drawCircle(context, x, y, r);
//绘画文字
var tx = x/2,ty = y/2,tr = 0.8*r;
context.font = "bold 12px 微软雅黑";
context.fillStyle = "#000";
drawText(context, "1", tx + 0.5*tr,ty - 0.866*tr);
drawText(context, "2", tx + 0.866*tr, ty - 0.5*tr);
drawText(context, "3", tx + tr, ty);
drawText(context, "4", tx + 0.866*tr, ty + 0.5*tr);
drawText(context, "5", tx + 0.5*tr, ty + 0.866*tr);
drawText(context, "6", tx, ty + tr);
drawText(context, "7", tx - 0.5*tr, ty + 0.866*tr);
drawText(context, "8", tx - 0.866*tr, ty + 0.5*tr);
drawText(context, "9", tx - tr, ty);
drawText(context, "10", tx - 0.866*tr, ty - 0.5*tr);
drawText(context, "11", tx - 0.5*tr, ty - 0.866*tr);
drawText(context, "12", tx, ty - tr);
//获取当前时间
var date = new Date(),
h = date.getHours(),
m = date.getMinutes(),
s = date.getSeconds(),
angleH = (360/12)*Math.PI/180,
angleM = (360/60)*Math.PI/180
context.strokeSyle = "#000";
//绘制时刻度
drawScale(context, x, y, r, angleH, -0.88*r, -0.96*r, 3, 12);
//绘制分刻度
drawScale(context, x, y, r, angleM, -0.93*r, -0.96*r, 1, 60);
//绘画时分秒针
drawCircle(context, x, y, 3);
drawNeedle(context, x, y, r, h*angleH + m*angleM/12, -0.5*r);
drawNeedle(context, x, y, r, m*angleM + s*angleM/60, -0.6*r);
drawNeedle(context, x, y, r, s*angleM, -0.75*r); }
//绘画圆
function drawCircle(context, x, y, r){
context.save();
context.beginPath();
context.arc(x/2, y/2, r, 0, Math.PI*2, 0);
context.fill();
context.closePath();
context.restore();
}
//绘画文字方法
function drawText(context, text, x, y){
context.save();
x -= (context.measureText(text).width/2);
y += 4;
context.translate(x, y);
context.fillText(text, 0, 0);
context.restore();
}
//绘制刻度方法
function drawScale(context, x, y, r, rotate, start, end, lineWidth, loop){
context.save();
context.beginPath();
context.translate(x/2,y/2);
context.lineWidth = lineWidth;
for (var i = 0; i < loop; i++) {
context.rotate(rotate);
context.moveTo(0, start);
context.lineTo(0, end);
}
context.closePath();
context.stroke();
context.restore();
}
//绘画时分秒针方法
function drawNeedle(context, x, y, r, rotate, line){
context.save();
context.translate(x/2,y/2);
context.beginPath();
context.rotate(rotate);
context.moveTo(0, 0.1*r);
context.lineTo(0, line);
context.closePath();
context.stroke();
context.restore();
}

html5 canvas绘画时钟的更多相关文章

  1. [ZZ+CH] Html5 canvas+js 时钟

    总之新Blog入驻以后,又开始老习惯,到处折腾自定义的空间,放些东西. 想起以前大一的时候做过一个Javascript的时间显示器,现在想做一个时钟,当然现在老奸巨猾,会先去看一看有前辈写过没. 前辈 ...

  2. HTML5 Canvas爱心时钟代码

    这是一款数字时钟动画,数字又多个小爱心组成,又何问起整理,随着时间推进,每一秒钟新数字替换旧数字,旧数字离去使用天女散花动画,花是五颜六色的. 查看效果:http://hovertree.com/te ...

  3. HTML5 Canvas 绘制时钟

    网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas ...

  4. html5 Canvas绘制时钟以及绘制运动的圆

    1.绘制时钟 <!-- js代码 --> <script type="text/javascript"> window.onload=function(){ ...

  5. HTML5 canvas 指针时钟

    <!doctype html> <html> <head></head> <body> <canvas id="> 您 ...

  6. html5 canvas js(时钟)

    <!doctype html> <html> <head> <title>canvas</title> </head> < ...

  7. html5 canvas简易时钟

    <canvas id='clock' width=500 height=500> 您的浏览器需要升级 </canvas> <script type="text/ ...

  8. html5 canvas时钟

    基础知识点:                canvas标签只是图形容器,您必须使用脚本来绘制图形. getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性.——获取上 ...

  9. HTML5 之Canvas 绘制时钟 Demo

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

随机推荐

  1. Haproxy ssl 配置方式

    通过haproxy redirect请求重定向的方法实现HTTP跳转HTTPS 配置实现http跳转到https,采用redirect重定向的做法,只需在frontend端添加: frontend h ...

  2. mobx源码解读1

    mobx是redux的代替品,其本身就是一个很好的MVVM框架.因此花点力气研究一下它. 网上下最新的2.75 function Todo() { this.id = Math.random() mo ...

  3. 2016 Multi-University Training Contest 2 - 1005 Eureka

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5738 题目大意:给定平面上的n个点,一个集合合法当且仅当集合中存在一对点u,v,对于集合中任意点w,均 ...

  4. HTML Meta中添加X-UA-Compatible和IE=Edge,chrome=1有什么作用

    你好,这个属性主要是设置浏览器优先使用什么模式来渲染页面的.常见写法如下:<meta http-equiv="X-UA-Compatible" content="I ...

  5. eclipse查看class文件的源码

    eclipse查看class文件的源码: 1.网上下载jadClipse的jar包和执行文件jad.exe和 net.sf.jadclipse_3.3.0.jar. 2.把上面下载的jar包放在ecp ...

  6. Python中的range函数用法

    函数原型:range(start, end, scan): 参数含义:start:计数从start开始.默认是从0开始.例如range(5)等价于range(0, 5); end:技术到end结束,但 ...

  7. Spring整合jdbc

    首先web.xml文件跟往常一样,加载spring容器和加载org.springframework.web.context.ContextLoaderListener读取applicationCont ...

  8. C# winform key value型数据如何绑定ComBox (hashtable,keyvaluepair,dictionary )

    cbUserAgent是一个combox ArrayList list = new ArrayList(); Dictionary<string, string> useragents = ...

  9. sql模糊匹配中%、_的处理

    防sql注入之模糊匹配中%._处理: StringBuilder sbSql = new StringBuilder(); sbSql.Append(@"SELECT * from tabl ...

  10. 无法在Web服务器上启动调试,已附加了一个调试器

    运行环境:开发环境:Windows7旗舰版64bit.VisualStudio2008 With SP1.ArcEngine10.0.NetFrameWork4.0.IIS7和C#开发语言. 问题描述 ...