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 ...
随机推荐
- logstash日志采集工具的安装部署
1.从官网下载安装包,并通过Xftp5上传到机器集群上 下载logstash-6.2.3.tar.gz版本,并通过Xftp5上传到hadoop机器集群的第一个节点node1上的/opt/uploads ...
- 自学python 6.
内容:id() is == 编码 解码1.好声音选秀比赛评委在打分的时候可以进行输入. 假设有10个评委.让10个评委进行打分, 要求, 分数必须大于5分, 小于10分.count = 1while ...
- sweetalert插件的使用
sweetalert是一个漂亮的弹窗插件,使用它可以完成各种炫酷的弹窗效果 链接:sweetalert 实例 删除演示 urls.py from django.contrib import admin ...
- luogu 1966 火柴排队 离散化+逆序对
题意:找到最小改变对数使a数组的第i大和b数组的第i大相等 则先将a,b,数组编号再排序,则数组显示的就是排名第i的数的编号 再关键一步:c[a[i].id]=b[i].id 实质上就是新建一个数组, ...
- 修改教材P74 一行代码 NineNineTable.java, 让执行结果是个三角形
修改教材P74 一行代码 NineNineTable.java, 让执行结果是个三角形: 提交在IDEA或命令行中运行结查截图,加上学号水印,没学号的不给成绩 2*9=18 3*9=27 4*9=36 ...
- faster rcnn 做识别
faster rcnn 主要分为四个部分: 1. convolutional part: 特征提取 可以使用vgg,resnet 等等 2.region proposal network: 生成 re ...
- Linux修改MAC地址方法
Linux修改MAC地址方法 - Linux modifies MAC address method ifconfig wlan0 down ifconfig wlan0 hw ether MAC地址 ...
- Windows代替touch命令
Windows 代替Linux中的touch命令: echo >
- 第21月第4天 leetcode codinginterview c++
1.leetcode Implement strStr(). Returns the index of the first occurrence of needle in haystack, or - ...
- 第19月第20天 UITableView:改变 TableHeaderView 的高度 获取目录大小
1.UITableView:改变 TableHeaderView 的高度 CGRect newFrame = headerView.frame; newFrame.size.height = newF ...