canvas - 钟表
Demo : Demo
Demo截图:
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
*{ margin:0;padding:0; }
body,html{width:100%;height:100%; overflow:hidden; min-width: 800px;min-height:800px;}
canvas{ background-color: #fff; padding:0;}
</style>
<script>
window.onload = function(){ var oCanvas = document.getElementById('canvas');
var oCtx = oCanvas.getContext('2d');
var winWidth = document.body.clientWidth;
var winHeight = document.body.clientHeight;
var oBody = document.getElementsByTagName('body')[0];
var propX = propY = 0; var timer = null; var X = 300;
var Y = 300; oCanvas.setAttribute('width',winWidth);
oCanvas.setAttribute('height',winHeight); setTime();
timer = setInterval(setTime,1000); //设置时间方法;
function setTime(){
winWidth = document.body.clientWidth;
winHeight = document.body.clientHeight; var myDate = new Date(),
seconds = myDate.getSeconds(),
minutes = myDate.getMinutes() + seconds/60,
hours = myDate.getHours() + minutes/60; oCtx.clearRect(0,0,winWidth,winHeight); //画底层3分针小刻度;
oCtx.save();
oCtx.beginPath();
for( var i=0,len=60;i<=len;i++ ){ oCtx.arc(X,Y,200,0,Math.PI/180*i*6,false);
oCtx.lineTo(X,Y);
oCtx.stroke();
oCtx.strokeStyle = '#000';
oCtx.lineWidth = 2; }
oCtx.closePath();
oCtx.restore(); //画盖住的底层的白圆;
oCtx.save();
oCtx.beginPath();
oCtx.arc(X,Y,190,0,2*Math.PI);
oCtx.fillStyle="#fff";
oCtx.fill();
oCtx.closePath();
oCtx.restore(); oCtx.save();
oCtx.beginPath();
for( var i=0,len=12;i<=len;i++ ){ oCtx.arc(X,Y,200,0,Math.PI/180*i*30,false);
oCtx.lineTo(X,Y);
oCtx.stroke();
oCtx.strokeStyle = '#000';
oCtx.lineWidth = 4; }
oCtx.closePath();
oCtx.restore(); //画底层时针的大刻度;
oCtx.save();
oCtx.beginPath();
oCtx.arc(X,Y,183,0,2*Math.PI);
oCtx.fillStyle="#fff";
oCtx.fill();
oCtx.closePath();
oCtx.restore(); //画盖住的底层的白圆;
oCtx.save();
oCtx.arc(X,Y,183,0,2*Math.PI);
oCtx.fillStyle="#fff";
oCtx.fill();
oCtx.restore(); //填充数字;
oCtx.save();
var r = 160;
for( var i=1,len=12;i<=12;i++ ){
oCtx.font = "40px Microsoft yahei";
oCtx.textAlign='center';
oCtx.textBaseline='middle';
var x = Math.sin( i*360/12*Math.PI/180 )*r + X;
var y = -Math.cos( i*360/12*Math.PI/180 )*r + Y;
oCtx.fillText(i,x,y,40);
}
oCtx.restore(); //画时针;
oCtx.save();
oCtx.lineCap="round";
oCtx.lineWidth = 8;
oCtx.strokeStyle = "#000" ;
oCtx.translate(X,Y);
oCtx.rotate(hours*30*Math.PI/180);
oCtx.beginPath();
oCtx.moveTo(0,-80);
oCtx.lineTo(0,10);
oCtx.stroke();
oCtx.closePath();
oCtx.restore(); //分时针;
oCtx.save();
oCtx.lineCap="round";
oCtx.lineWidth = 6;
oCtx.strokeStyle = "#000" ;
oCtx.translate(X,Y);
oCtx.rotate( minutes*6*Math.PI/180 );
oCtx.beginPath();
oCtx.moveTo(0,-120);
oCtx.lineTo(0,10);
oCtx.stroke();
oCtx.closePath();
oCtx.restore(); //画分针;
oCtx.save();
oCtx.lineCap="round";
oCtx.lineWidth = 2;
oCtx.strokeStyle = "red" ;
oCtx.translate(X,Y);
oCtx.rotate( seconds*6*Math.PI/180 );
oCtx.beginPath();
oCtx.moveTo(0,-160);
oCtx.lineTo(0,10);
oCtx.stroke();
oCtx.closePath();
oCtx.restore(); //画盖住的底层的白圆;
oCtx.save();
oCtx.arc(X,Y,15,0,2*Math.PI);
oCtx.fillStyle="#000";
oCtx.fill();
oCtx.restore(); oCtx.save();
oCtx.beginPath();
oCtx.arc(X,Y,200,0,2*Math.PI,true);
oCtx.stroke(); oCtx.closePath();
oCtx.restore(); } oCanvas.onmousedown = function( event ){ var ev = window.event || event; var btn = false; if( oCtx.isPointInPath( ev.clientX , ev.clientY ) ){
var yuanX = X;
var yuanY = Y;
var disX = ev.clientX;
var disY = ev.clientY;
btn = true; } document.onmousemove = function( event ){ var ev = window.event || event; if( btn ){ clearInterval(setTime); var lastX = ev.clientX - disX;
var lastY = ev.clientY - disY; X = lastX + yuanX;
Y = lastY + yuanY; if( X <= 200 ){ X = 200; } if( X >= winWidth - 200 ){
X = winWidth - 200;
} if( Y <= 200 ){
Y = 200;
} if( Y >= winHeight - 200 ){
Y = winHeight - 200;
} setTime();
} }
} oCanvas.onmouseup = function(){ btn = false;
document.onmousemove = null;
timer = setInterval(setTime,1000); } window.onresize = function(){ var bodyWidth = document.body.clientWidth;
var bodyHeigth = document.body.clientHeight; propX = bodyWidth / winWidth;
propY = bodyHeigth / winHeight; if( X >= bodyWidth - 200 ){
X = X * propX;
} if( Y >= bodyHeigth - 200 ){
Y = Y * propY;
} winWidth = bodyWidth;
winHeight = bodyHeigth; oCanvas.setAttribute('width',winWidth);
oCanvas.setAttribute('height',winHeight); }
} </script>
</head>
<body>
<canvas id="canvas"></canvas>
</body>
</html>
后记:
其实网上好多这种画表的也没啥可说的就是css3那个差不多。。。
主要是写拖拽让我费点了功夫 isPointInPath() 这个方法是判断点点击是否在画图上但是只是最后一次绘画上面,
所以偷巧在整个表都画完的最后在画了一个空圆做这个isPointInPath()用,剩下就跟Dom拖拽基本一样了。
不过还是有点小bug 就是 在放大缩小浏览器的时候 绘制的圆心点的X、Y坐标可能不对,虽然我用了比例去算这个 但还是有点小问题,没有想出好的解决办法,有知道怎么解决谢谢告知。。。。
canvas - 钟表的更多相关文章
- canvas钟表
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...
- html5 canvas 钟表
<!doctype html> <html> <head> <meta charset="utf-8"> <title> ...
- canvas画画板,canvas画五角星,canvas制作钟表、Konva写钟表
制作一个画画板,有清屏有橡皮擦有画笔可以换颜色 style样式 <head> <meta charset="UTF-8"> <title>画画板 ...
- 基础canvas应用-钟表绘制
首先,canvas语法基础薄弱的小伙伴请点这里,剩下的小伙伴们可以接着往下看了. 一个表,需要画什么出来呢:3条线(时分秒针),1个圆(表盘),以及60条短线/点(刻度). 嗯,没毛病. 那接下来让我 ...
- js实现一个简单钟表动画(javascript+html5 canvas)
第一次在博客园注册发博.有一次去人家单位开标,看到开标网站上有个钟表动画,一时兴起,就写了个简单的钟表动画. 用js和html5 canvas对象实现一个简单钟表程序 主要用到的就是h5的canvas ...
- [Canvas]新版箴言钟表
动态效果点此下载用浏览器打开观看. 本作Github地址:https://github.com/horn19782016/12MaximClock 图例: 代码: <!DOCTYPE html& ...
- Canvas基础——钟表绘制
首先,canvas语法基础薄弱的小伙伴请点这里,剩下的小伙伴们可以接着往下看了. 一个表,需要画什么出来呢:3条线(时分秒针),1个圆(表盘),以及60条短线/点(刻度). 嗯,没毛病. 那接下来让我 ...
- HTML5 Canvas 绘制二十四字真言钟表
代码: <!DOCTYPE html> <html lang="utf-8"> <meta http-equiv="Content-Type ...
- HTML5 Canvas 画钟表
画钟表是2D画图的老生常谈,我也不能免俗弄了一个.代码如下: <!DOCTYPE html> <html lang="utf-8"> <meta ht ...
随机推荐
- HDU 1033(坐标移动 模拟)
题意是说有一点从(300,410)的位置出发,向右移动到(310,410)后开始转向,A 表示向顺时针转,V 表示向逆时针转,每次转向后沿当前方向前进 10 个单位, 输出其坐标,再补充一点格式上的东 ...
- 在ServiceModel客户端配置部分中,找不到引用协定“”的默认终结点元素
鄙人自己写了一个读取文件的ASP.NET服务,在Silverlight工程中添加服务引用(名字为FileIOService)后,出现了如上的错误. 出错原因:服务是在项目B中调用的,但是主项目是A,所 ...
- Spring Boot 2程序不能加载 com.mysql.jdbc.Driver 问题
用Spring Boot Starter 向导生成了一个很简单SpringBoot程序, 用到了 MySQL, 总是下面不能加载 Mysql driver class 错误. Cannot load ...
- loj 10181 绿色通道 二分答案+单调队列DP
空题段长度即为单调队列长度区间 每次二分答案进行check即可 #include<bits/stdc++.h> using namespace std; ; const int inf=0 ...
- transition的属性变化
链接:https://www.cnblogs.com/yehui-mmd/p/5934157.html css3——transition属性和opacity属性 [transition-durat ...
- frame与iframe的区别及基本用法
frame 和 iframe 的区别 1.frame 不能脱离 frameset 单独使用,iframe 可以: 2.frame 不能放到body中,否则将无法显示: 3.iframe 也可以嵌套在f ...
- 博客里的第一篇随笔!QWQ
这里是一个信息蒟蒻,开始自己的博客之旅!!QWQQQQ
- 实现多线程爬取数据并保存到mongodb
多线程爬取二手房网页并将数据保存到mongodb的代码: import pymongo import threading import time from lxml import etree impo ...
- jmeter负载机运行/添加压力机/分布式
• 我们在压测的时候,可能并发比较大, 一台机子已经启动不了那么多并发了,这个时候我们就要使用多台机子一起来发压力,就要添加压力机,添加压力机怎么添加呢,首先要在做压力机的机子上启动jmeter的代理 ...
- mongodb系列~ mongodb慢语句(1)
1 简介:讲讲mongo的慢日志2 慢日志类型 query insert update delete 3 查看慢日志 1 db.system.profile.find() 慢日志总揽 2 d ...