canvas画的时钟
结合几天来学习的canvas的API,终于完成了一个时钟呵呵
html
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>canvas</title>
<style type="text/css">
canvas { border: 1px solid black; }
#change{width:200px; line-height:30px; color:#fff; text-align:center; background:blue;}
</style>
<script src="canvas.js" type="text/javascript" charset="utf-8"></script>
</head> <body>
<canvas id="canvas" width="600" height="400">current stock price: $3.15 +0.15</canvas> </html>
js
window.onload = function() {
new clock();
}
function clock() {
var canvas = document.getElementById('canvas');
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 300, 300);
var date = new Date(); //获取当前时间
this.hour = date.getHours();
this.min = date.getMinutes();
this.sec = date.getSeconds();
this.hour = this.hour + this.min / 60;
this.hour = this.hour > 12 ? this.hour - 12 : this.hour;
this.radius = 115;
this.dotX = 150;
this.dotY = 150;
this.maxBorderWidth = 8;
this.minBorderWidth = 2;
this.clockBg();
this.drawHour();
this.drawMin();
this.drawSec();
this.onreset();
}
clock.prototype.onreset = function() {
var ctx = canvas.getContext('2d');
ctx.clearRect(0, 0, 300, 300);
var date = new Date(); //获取当前时间
this.hour = date.getHours();
this.min = date.getMinutes();
this.sec = date.getSeconds();
this.hour = this.hour + this.min / 60;
this.hour = this.hour > 12 ? this.hour - 12 : this.hour;
this.clockBg();
this.drawHour();
this.drawMin();
this.drawSec();
var self = this;
setInterval(function() {
self.onreset();
}, 1000);
}
clock.prototype.clockBg = function() {
var ctx = canvas.getContext('2d');
ctx.beginPath();
ctx.fillStyle = "#e3e3e3";
ctx.arc(this.dotX, this.dotY, this.radius, 0, 2 * Math.PI, false);
ctx.fill();
ctx.closePath();
for (var i = 0; i < 60; i++) {
ctx.save();
var pointLong;
if (i % 5 == 0) {
ctx.lineWidth = this.maxBorderWidth;
pointLong = 25;
} else {
ctx.lineWidth = this.minBorderWidth;
pointLong = 12;
}
ctx.strokeStyle = "#000";
ctx.translate(this.dotX, this.dotY);
ctx.rotate(i * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -this.radius + this.maxBorderWidth);
ctx.lineTo(0, -this.radius + this.maxBorderWidth + pointLong);
ctx.closePath();
ctx.stroke();
ctx.restore();
};
}
clock.prototype.drawHour = function() {
var ctx = canvas.getContext('2d');
ctx.save();
ctx.strokeStyle = "#000";
ctx.lineWidth = this.maxBorderWidth;
ctx.translate(this.dotX, this.dotY);
ctx.rotate(this.hour * 30 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, 20);
ctx.lineTo(0, -70);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
clock.prototype.drawMin = function () {
var ctx = canvas.getContext('2d');
ctx.save();
ctx.strokeStyle = "#00";
ctx.lineWidth = this.maxBorderWidth;
ctx.translate(this.dotX, this.dotY);
ctx.rotate(this.min*6*Math.PI/180);
ctx.beginPath();
ctx.moveTo(0, 20);
ctx.lineTo(0, -80);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
clock.prototype.drawSec = function() {
var ctx = canvas.getContext('2d');
ctx.save();
ctx.strokeStyle = "#f00";
ctx.lineWidth = this.minBorderWidth;
ctx.translate(this.dotX, this.dotY);
ctx.rotate(this.sec * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, 20);
ctx.lineTo(0, -100);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = "#f00";
ctx.arc(0, 0, 5, 0, 2 * Math.PI, true);
ctx.closePath();
ctx.fill();
ctx.restore();
}
function drawTime() {
var clock = document.getElementById('canvas');
var ctx = clock.getContext('2d');
ctx.clearRect(0, 0, 244, 300);
var date = new Date(); //获取当前时间
var hour = date.getHours();
var min = date.getMinutes();
var sec = date.getSeconds();
hour = hour + min / 60;
hour = hour > 12 ? hour - 12 : hour;
var width = 244;
var height = 280;
var dot = { //时钟中心
x: width / 2,
y: width / 2,
radius: 4
}
var radius = 115;
var maxBorderWidth = 8;
var minBorderWidth = 2;
//绘制时钟中心点
ctx.lineWidth = maxBorderWidth;
ctx.beginPath();
ctx.fillStyle = "#e2e2e2";
ctx.arc(dot.x, dot.y, radius, 0, 2 * Math.PI, true);
ctx.fill();
ctx.closePath();
//绘制时刻度、分刻度
for (var i = 0; i < 60; i++) {
ctx.save();
var pointLong;
if (i % 5 == 0) {
ctx.lineWidth = maxBorderWidth;
pointLong = 25;
} else {
ctx.lineWidth = minBorderWidth;
pointLong = 12;
}
ctx.strokeStyle = "#000";
ctx.translate(dot.x, dot.y);
ctx.rotate(i * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -radius + maxBorderWidth);
ctx.lineTo(0, -radius + maxBorderWidth + pointLong);
ctx.closePath();
ctx.stroke();
ctx.restore();
}
//绘制时针
ctx.save();
ctx.lineWidth = maxBorderWidth;
ctx.translate(dot.x, dot.y);
ctx.rotate(hour * 30 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -55);
ctx.lineTo(0, 25);
ctx.closePath();
ctx.stroke();
ctx.restore();
//绘制分针
ctx.save();
ctx.lineWidth = maxBorderWidth;
ctx.translate(dot.x, dot.y);
ctx.rotate(min * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -97);
ctx.lineTo(0, 25);
ctx.closePath();
ctx.stroke();
ctx.restore();
//绘制秒针
ctx.save();
ctx.strokeStyle = "red";
ctx.lineWidth = minBorderWidth;
ctx.translate(dot.x, dot.y);
ctx.rotate(sec * 6 * Math.PI / 180);
ctx.beginPath();
ctx.moveTo(0, -75);
ctx.lineTo(0, 25);
ctx.closePath();
ctx.stroke();
ctx.beginPath();
ctx.fillStyle = '#D6231C';
ctx.arc(0, -75, 6, 0, 2 * Math.PI, true); //绘制秒针针尖的小圆点
ctx.fill();
ctx.closePath();
ctx.restore();
//装饰时钟中心 两个小圆叠加
ctx.beginPath();
ctx.fillStyle = '#982127';
ctx.arc(dot.x, dot.y, dot.radius, 0, 2 * Math.PI, true);
ctx.fill();
ctx.closePath();
ctx.beginPath();
ctx.fillStyle = '#D6231C';
ctx.arc(dot.x, dot.y, 2, 0, 2 * Math.PI, true);
ctx.fill();
ctx.closePath();
//再时钟上添加签名
ctx.fillStyle = "#000";
ctx.font = "15px Comic Sans MS";
ctx.fillText("Chal Mine", dot.x - 30, dot.y + 50);
}
canvas画的时钟的更多相关文章
- 深夜,用canvas画一个时钟
深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...
- 玩转html5(四)----使用canvas画一个时钟(可以动的哦!)
先给个效果图,我画的比较丑,大家可以自己美化一下, 直接上代码: <!DOCTYPE html> <meta charset="utf-8"> <ht ...
- HTML5 Canvas画数字时钟
先不说废话,没代码算个蛋. 一些地方注释都写得比较清楚,不过这只是部分,因为只有秒针,但是时针,分针的逻辑都是一致的. 代码中有些坐标不知道为什么较不准,看看就好
- canvas画一个时钟
效果图如下 <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF ...
- 用canvas画一个时钟
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- canvas画时钟
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- 利用canvas画一个实时时钟
先放一张效果图: 下面是源代码: <!DOCTYPE html> <html> <head> <meta charset="UTF-8"& ...
- H5 canvas圆形的时钟
今天用H5中的canvas标签做一个时钟,H5中有很多好用的新增标签,真的很不错. 1.canvas标签介绍 <canvas> 标签定义图形,比如图表和其他图像,你必须使用脚本来绘制图形. ...
- canvas实例_时钟
效果图:是一个会动的时钟 一.时钟的组成 1.表盘(蓝色) 2.刻度(黑色) 3.时针(黑色) 4.分针(黑色) 5.秒针(红色)需美化 二.主要应用的技术 Canvas画线 Canv ...
随机推荐
- how2j网站前端项目——天猫前端(第一次)学习笔记1
首先是公共页面的学习,有页头.页脚和搜索框. 一.页头就是天猫网站的置顶导航栏: 看似简单,实际做起来也不容易. 写html还是比较简单的,撸起袖子就可以写完.可要想做到上图的样式就难了,难就难在CS ...
- php RSA加密传输代码示例(轉)
原文地址:http://www.cnblogs.com/firstForEver/p/5803940.html 涉及敏感数据的传输,双方最好约定使用加密解密.那RSA非对称加密就大有作为了. 服务端可 ...
- ALV界面显示
PERFORM ADD. IS_LAYOUT_LVC-CWIDTH_OPT = 'X'. IS_LAYOUT_LVC-SEL_MODE = 'A'. CALL FUNCTION 'REUSE_ ...
- xml转化为Dictionary
代码 public SortedDictionary<string, object> FromXml(string xml) { SortedDictionary<string, o ...
- Android开发日常-listVIiew嵌套webView回显阅读位置
详情页布局结构 需求是回显webview展示网页的阅读位置 方案1: 使用webview.getScrollY()获取滑动到的位置,用setScrollY()回显设置, 但是两个方法都出现了问题,g ...
- Vue 快速原型开发
快速原型开发 注意: 是:serve 而不是 server 通过使用 vue serve 和 vue build 命令对单个 *.vue 文件进行快速原型开发,不过这需要先额外安装一个全局的扩展 go ...
- 织梦替换ueditor百度编辑器,支持图片水印 教程
1下载ueditor百度编辑器 2 把下载的zip解压得到ueditor文件夹,把解压到的ueditor文件夹扔进你网站的include文件夹去 3 打开 /include/inc/inc_fun_f ...
- UVALive - 3266 (贪心) 田忌赛马
耳熟能详的故事,田忌赛马,第一行给出田忌的马的速度,第二行是齐王的马的速度,田忌赢一场得200,输一场失去200,平局不得也不失,问最后田忌最多能得多少钱? 都知道在故事里,田忌用下等马对上等马,中等 ...
- django DEBUG=False
在django的settings中. 将DEBUG 设置为False. 会出现 #python manage.py runserver 8888 CommandError: You must set ...
- mysql索引相关
索引有主键索引.唯一索引.普通索引 单列索引,复合索引. 复合索引(a,b,c),可以理解是有三个索引,分别是a.b.c三个索引 前缀不是a的话,复合索引都不起作用,前缀用函数或者是范围,比如< ...