html5 canvas js(时钟)

<!doctype html>
<html>
<head>
<title>canvas</title>
</head>
<body>
<canvas id = "clock" width = "500px" height = "500px">
您的浏览器已过时,请更新!
</canvas>
<script type = "text/javascript">
var clock = document.getElementById("clock");
var cxt = clock.getContext("2d"); function drawClock(x) {
var y = x / 2;
var r = (x - 100) / 2;
var x1 = r - 10;
var x2 = r - 20;
var x3 = r - 30;
var x4 = r - 40;
var x5 = r - 50;
//清除画布
cxt.clearRect(0, 0, x, x); var now = new Date;
var sec = now.getSeconds();
var min = now.getMinutes();
var hour = now.getHours(); //解决时针的问题:1、小时之间 2、19:59:30s
hour += min / 60;
hour = hour > 12 ? hour - 12 : hour; //表盘
cxt.lineWidth = 10;
cxt.strokeStyle = "blue";
cxt.beginPath();
cxt.arc(y, y, r, 0, 360, false);
cxt.closePath();
cxt.stroke();
//时刻度
for (var i = 0; i < 12;i++ ) {
cxt.save();
cxt.lineWidth = 7;
cxt.strokeStyle = "#000";
cxt.translate(y, y);
cxt.rotate(i*30 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -x1);
cxt.lineTo(0, -x3);
cxt.closePath();
cxt.stroke();
cxt.restore();
}
//分刻度
for (var i = 0; i < 60; i++) {
cxt.save();
cxt.lineWidth = 5;
cxt.strokeStyle = "#000";
cxt.translate(y, y);
cxt.rotate(i * 6 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -x1);
cxt.lineTo(0, -x2);
cxt.closePath();
cxt.stroke();
cxt.restore();
}
//时针
cxt.save();
cxt.lineWidth = 7;
cxt.strokeStyle = "#000";
cxt.translate(y, y);
cxt.rotate(hour * 30 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -x5);
cxt.lineTo(0, 10);
cxt.closePath();
cxt.stroke();
cxt.restore();
//分针
cxt.save();
cxt.lineWidth = 5;
cxt.strokeStyle = "#000";
cxt.translate(y, y);
cxt.rotate(min * 6 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -x4);
cxt.lineTo(0, 15);
cxt.closePath();
cxt.stroke();
cxt.restore();
//秒针
cxt.save();
cxt.lineWidth = 3;
cxt.strokeStyle = "red";
cxt.translate(y, y);
cxt.rotate(sec * 6 * Math.PI / 180);
cxt.beginPath();
cxt.moveTo(0, -x3);
cxt.lineTo(0, 20);
cxt.closePath();
cxt.stroke();
cxt.lineWidth = 3;
cxt.strokeStyle = "red";
cxt.beginPath();
cxt.arc(0, 0, 5, 0, 360, false);
cxt.fillStyle = "gray";
cxt.fill();
cxt.closePath();
cxt.stroke();
cxt.lineWidth = 3;
cxt.strokeStyle = "red";
cxt.fillStyle = "red";
cxt.beginPath();
cxt.arc(0, -x5, 5, 0, 360, false);
cxt.fill();
cxt.closePath();
cxt.stroke();
cxt.restore();
} //行走
drawClock(500);
setInterval("drawClock(500)", 1000);
</script>
</body>
</html>
html5 canvas js(时钟)的更多相关文章
- [ZZ+CH] Html5 canvas+js 时钟
总之新Blog入驻以后,又开始老习惯,到处折腾自定义的空间,放些东西. 想起以前大一的时候做过一个Javascript的时间显示器,现在想做一个时钟,当然现在老奸巨猾,会先去看一看有前辈写过没. 前辈 ...
- HTML5 Canvas爱心时钟代码
这是一款数字时钟动画,数字又多个小爱心组成,又何问起整理,随着时间推进,每一秒钟新数字替换旧数字,旧数字离去使用天女散花动画,花是五颜六色的. 查看效果:http://hovertree.com/te ...
- html5 canvas+js实现ps钢笔抠图
html5 canvas+js实现ps钢笔抠图 1. 项目要求需要用js实现photoshop中钢笔抠图功能,就用了近三四天的时间去解决它,最终还是基本上把他实现了. 做的过程中走了不少弯路,最终一同 ...
- html5 canvas+js实现ps钢笔抠图(速抠图 www.sukoutu.com)
html5 canvas+js实现ps钢笔抠图(速抠图 www.sukoutu.com) 根据html5 canvas+js实现ps钢笔抠图的实现,aiaito 开发者开发了一套在线抠图工具,速抠 ...
- html5 canvas绘画时钟
本示例使用HTML5 canvas,模拟显示了一个时钟, 请使用支持HTML5的浏览器预览效果: HTML部分: <!DOCTYPE html> <html lang="e ...
- html5 canvas js(数字时钟)
<!doctype html> <html> <head> <title>canvas dClock</title> </head ...
- 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="> 您 ...
随机推荐
- boost-tokenizer分词库学习
boost-tokenizer学习 tokenizer库是一个专门用于分词(token)的字符串处理库;可以使用简单易用的方法把一个字符串分解成若干个单词;tokenizerl类是该库的核心,它以容器 ...
- hdu 4322(最大费用最大流)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4322 思路:建图真的是太巧妙了!直接copy大牛的了: 由于只要得到糖就肯定有1个快乐度,在这一点上糖 ...
- React资料
基于ReactNative开发的APPhttp://reactnative.cn/cases.htmlhttp://www.cnblogs.com/qiangxia/p/5584622.html F8 ...
- iOS 开发之--打测试包的时候报错的解决方法
在打测试包的时候,一直出现一个报错,解决方法如下: 方法一: 描述文件所在目录是:~/Library/MobileDevice/Provisioning\ Profiles/,可以直接前往文件夹,把所 ...
- UVALive 6560 The Urge to Merge
题目链接:传送门 题目大意:有一个 3*n 的矩阵,每个格子里有一个数,你可以选择相邻的两个格子合并成一个,并且权值变为两数乘积,若一个数未合并,权值变为0,求最后权值总和最大值. 题目思路:以 2^ ...
- JZOJ.3777【NOI2015模拟8.17】最短路(shortest)
Description 小Y最近学得了最短路算法,一直想找个机会好好练习一下.话虽这么说,OJ上最短路的题目都被他刷光了.正巧他的好朋友小A正在研究一类奇怪的图,他也想凑上去求下它的最短 ...
- spring定时器的cronexpression表达式
转自:https://www.cnblogs.com/yaowen/p/3779284.html 相关配置: import com.alibaba.dubbo.config.annotation.Se ...
- window下搭建python开发环境
搭建一个python开发环境比较简单,所以就稍微记录一下. 1.下载python然后安装 2.配置环境变量 3.在eclipse添加PyDev插件 1.下载python 官网:https://www. ...
- Runtime Error! R6025-pure virtual function call 问题怎么解决
一.故障现象:1.360软件的木马查杀.漏洞修复等组件不能使用,提示runtime error2.暴风影音等很多软件不能正常使用3.设备管理器不能打开,提示“MMC 不能打开文件”4.部分https安 ...
- mysql 客户端命令行下 直接查询并导出数据
mysql原来还能这么导出数据,涨知识了. 方式1: select ....(sql语句) INTO OUTFILE '/var/lib/mysql/msg_data.csv ' (导出的文件位置 ...