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 ...
随机推荐
- Ubuntu防火墙配置
转载自:http://blog.csdn.net/sumer0922/article/details/7485584Ubuntu11.04默认的是UFW(ufw 即uncomplicated fire ...
- 字符串加u的特殊需求
#coding:utf-8 L = ['a','b','c'] S = [] for i in L: tmp = str(i).decode('utf-8') S.append(tmp) print ...
- pyinstller的安装
下载:http://www.pyinstaller.org/ 解压到目录 切换到python目录 执行命令: python.exe D:\Download\PyInstaller-2.1\setup. ...
- 不使用SwitchHosts修改C:\Windows\System32\drivers\etc\hosts文件
1.nginx中的nginx.conf完成修改,配置好了端口和域名www.xuecheng.com 2.找到Hosts文件,将Hosts文件复制到桌面.(Windows 10系统Hosts文件路径为: ...
- 让 div中的div垂直居中的方法!!同样是抄袭来的(*^__^*)
同样 ,水平居中很简单,给子div设置margin:0px auto; 垂直居中也不难::给父div设置display:table-cell;vertical-align:middle; 重点是dis ...
- if 循环的深入理解 哈希表的一种应用
哈希表的值作为一个颜色容器,值默认为标识1, 表示未曾用过,若用过标识为0: 1: 程序第一步 遍历哈希表,查找标识为1 未曾用过的颜色 我用了这个: string colorno_us ...
- hdu 1983(BFS+DFS) 怪盗Kid
http://acm.hdu.edu.cn/showproblem.php?pid=1983 首先,题目要求出口和入口不能封闭,那么,只要把出口或入口的周围全给封闭了那盗贼肯定无法成功偷盗,出口或入口 ...
- windows上安装RabbitMQ
windows下 安装 rabbitMQ 及操作常用命令 rabbitMQ是一个在AMQP协议标准基础上完整的,可服用的企业消息系统.它遵循Mozilla Public License开源协议,采用 ...
- kvm介绍 转载
KVM 介绍(1):简介及安装 学习 KVM 的系列文章: (1)介绍和安装 (2)CPU 和 内存虚拟化 (3)I/O QEMU 全虚拟化和准虚拟化(Para-virtulizaiton) (4)I ...
- 多维数组sorted函数的用法
对某一个位置排列 l=[[1,5,7,9],[5,10,6,11],[4,2,1,4]] newlist=sorted(l,key=lambda iterm : iterm[0],reverse=Tr ...