html5 canvas绘画时钟
本示例使用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绘画时钟的更多相关文章
- [ZZ+CH] Html5 canvas+js 时钟
总之新Blog入驻以后,又开始老习惯,到处折腾自定义的空间,放些东西. 想起以前大一的时候做过一个Javascript的时间显示器,现在想做一个时钟,当然现在老奸巨猾,会先去看一看有前辈写过没. 前辈 ...
- HTML5 Canvas爱心时钟代码
这是一款数字时钟动画,数字又多个小爱心组成,又何问起整理,随着时间推进,每一秒钟新数字替换旧数字,旧数字离去使用天女散花动画,花是五颜六色的. 查看效果:http://hovertree.com/te ...
- HTML5 Canvas 绘制时钟
网上会看到很多绘制的时钟,看代码也是云里雾里,自学了下Canvas,觉得不难,就自己做了一个. 先看一下截图: 比较简陋,但是该有的都有了,样式只加了个阴影. html代码就不贴了,就一个canvas ...
- html5 Canvas绘制时钟以及绘制运动的圆
1.绘制时钟 <!-- js代码 --> <script type="text/javascript"> window.onload=function(){ ...
- HTML5 canvas 指针时钟
<!doctype html> <html> <head></head> <body> <canvas id="> 您 ...
- html5 canvas js(时钟)
<!doctype html> <html> <head> <title>canvas</title> </head> < ...
- html5 canvas简易时钟
<canvas id='clock' width=500 height=500> 您的浏览器需要升级 </canvas> <script type="text/ ...
- html5 canvas时钟
基础知识点: canvas标签只是图形容器,您必须使用脚本来绘制图形. getContext() 方法可返回一个对象,该对象提供了用于在画布上绘图的方法和属性.——获取上 ...
- HTML5 之Canvas 绘制时钟 Demo
<!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...
随机推荐
- iOS 数据序列化,NSCoding, NSCoder
iOS可以利用NSKeyedArchiver类将对象序列化成NSData存储在磁盘上,但前提是该对象所属的类必须遵从NSCoding协议. NSCoding协议包含两个方法,要序列化的类必须实现它们 ...
- Winform 中DataGridView控件添加行标题
有很多种方法. 1.可以在DataGridView控件中的RowStateChanged事件改变行标题单元格的值(Row.HeaderCell.Value) /// <summary> / ...
- linux 驱动学习笔记04--简单驱动
首先贴代码helloworld.c和Makefile /************************************************************************ ...
- C#与Java的比较
C#与Java的比较 写完后得知维基百科里有更加全面得多的比较: http://en.wikipedia.org/wiki/Comparison_of_C_Sharp_and_Java NET(C#) ...
- javascript照片墙效果
<!doctype html> <html> <head> <meta charset="UTF-8"> <title> ...
- sqlserver存取过程-游标
ALTER proc [dbo].[common_proc_temp2] as begin declare @id varchar(50); declare @cbcontractid varcha ...
- 阿里巴巴Java招聘
大家好: 我是阿里巴巴B2B的应用架构师,现在大量招聘Java工程师,对自己技术有信心的兄弟姐妹,请联系我吧. 版权声明:本文为博主原创文章,未经博主允许不得转载.
- python之路——面向对象(进阶篇)
面向对象进阶:类成员.类成员的修饰符.类的特殊成员 类成员 类成员分为三大类:字段.方法.属性 一.字段 静态字段 (属于类) 普通字段(属于对象) class City: # 静态字段 countr ...
- chrome浏览器开发者工具之同步修改至本地
相信好多小伙伴喜爱webpack的热加载技术,省时而又不繁琐,讨厌F5或者Ctrl+F5. 嘿嘿,现在介绍大家一个在浏览器中修改直接同步到本地代码修改的方法--- (程序员都是从0开始数数的!) 第0 ...
- Window Server 2012 R2 没有照片查看器 打开图片都是画板问题怎么解决
新安装了 Window Server 2012 R2 系统,感觉屌屌的样子,加上开机速度蛮快,心里略爽.结果,打开图片一看,发现竟然是画板,而且还没有照片查看器,顿时泪流满面. 后来我利用了强大的百度 ...