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 ...
随机推荐
- IntelliJ IDEA(2017)安装和破解
IDEA 全称 IntelliJ IDEA,是Java语言开发的集成环境,IntelliJ在业界被公认为最好的java开发工具之一,尤其在智能代码助手.代码自动提示.重构.J2EE支持.各类版本工具( ...
- php中inset 和 和 empty 的区别
inset函数 用途:检测变量是否设置判断:检测变量是否设置,并且不是 NULL.如果已经使用 unset() 释放了一个变量之后,它将不再是 isset().若使用 isset() 测试一个被设置成 ...
- 深入理解Python异步编程(上)
本文代码整理自:深入理解Python异步编程(上) 参考:A Web Crawler With asyncio Coroutines 一.同步阻塞方式 import socket def blocki ...
- nginx接入let's encrypt
按以下步骤: 一.放开443端口 我的是云服务器,默认没开放443端口,需要先在控制台放开 二.使用let’s encrypt 生成证书 执行以下命令: git clone https://githu ...
- IntelliJ IDEA 创建Web项目(全教程)
说明:IntelliJ IDEA 版本为14.JDK 版本为1.7tomcat 版本为apache-tomcat-7.0.70 注:在创建过程中注意相关软件版本位数的问题.32位,64位的软件混搭会导 ...
- 使用PHP+MySql操作——实现微信投票功能
1. 投票主页面: <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://w ...
- Python基础5-常用模块
本节大纲 模块介绍 time &datetime模块 random os sys shutil shelve xml处理 yaml处理 configparser hashlib subproc ...
- python作业高级FTP
转载自:https://www.cnblogs.com/sean-yao/p/7882638.html 作业需求: 1. 用户加密认证 2. 多用户同时登陆 3. 每个用户有自己的家目录且只能访问自己 ...
- .Net Core 配置文件appsettings
1.配置文件为appsettings 在appsettings添加ConnectionStrings: { "Logging": { "IncludeScopes&quo ...
- react-踩坑记录——iconfont
选取图标,添加至购物车后,下载代码. 后将下载了的文件夹改名,放入css文件夹中.在组件中使用到的时候按路径引入“iconfont.css”文件即可. 使用