用canvas画时钟
效果图在博客首页上。
html:
<canvas id="canvas" >Your browser does not support canvas</canvas>
css:
canvas {
display:block;
margin:60px auto;
border:1px solid black;
}
js:
<script>
window.onload=function(){
var canvas=document.getElementById('canvas');
var ctx=canvas.getContext("2d");
canvas.width=800;
canvas.height=600;
/*获取时间*/
function colock(){
var myDate=new Date();
var myHour=myDate.getHours();
var myMin=myDate.getMinutes();
var mySec=myDate.getSeconds();
/*背景*/
ctx.save();
ctx.rect(0,0,800,600);
ctx.fillStyle='#ddd';
ctx.fill();
ctx.stroke();
ctx.restore();
/*表盘*/
ctx.save();
ctx.beginPath();
ctx.arc(400,300,200,0,2*Math.PI,true);
ctx.fillStyle='#fff';
ctx.strokeStyle="#ffee00";
ctx.lineWidth=20;
ctx.fill();
ctx.closePath();
ctx.stroke();
ctx.restore();
/*数字*/
ctx.save();
ctx.beginPath();
ctx.font="15px Arial";
ctx.fillStyle='black';
var num=['3','4','5','6','7','8','9','10','11','12','1','2'];
var r=150;
//ctx.arc(100,100,150,0,2*Math.PI,true);
for(var i=0;i<12;i++){
ctx.fillText(num[i],400+r*Math.cos(i*30*Math.PI/180),300+r*Math.sin(i*30*Math.PI/180));
}
ctx.closePath();
ctx.restore();
/*时针*/
ctx.save();
ctx.beginPath();
ctx.translate(400,300);
ctx.rotate((myHour-3)*30*Math.PI/180+(30*myMin/60)*Math.PI/180);
ctx.lineTo(-20,0);
ctx.lineTo(60,0);
ctx.lineWidth=8;
ctx.closePath();
ctx.strokeStyle='black';
ctx.stroke();
ctx.restore();
/*分针*/
ctx.save();
ctx.beginPath();
ctx.translate(400,300);
ctx.rotate((myMin-15)*6*Math.PI/180+Math.PI/2+(6*mySec/60)*Math.PI/180);
ctx.lineTo(0,20);
ctx.lineTo(0,-90);
ctx.lineWidth=6;
ctx.closePath();
ctx.strokeStyle='#333';
ctx.stroke();
ctx.restore();
/*秒针*/
ctx.save();
ctx.beginPath();
ctx.translate(400,300);
ctx.rotate((mySec-15)*6*Math.PI/180+Math.PI/2);
ctx.lineTo(0,20);
ctx.lineTo(0,-120);
ctx.lineWidth=3;
ctx.closePath();
ctx.strokeStyle='red';
ctx.stroke();
ctx.restore();
}
colock();
setInterval(colock,1000);
}
用canvas画时钟的更多相关文章
- canvas画时钟
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...
- canvas画时钟,重拾乐趣!
canvas时钟--效果图 一.先来简单介绍画时钟需要的canvas知识 1.在HTML页面中添加canvas元素,必须定义canvas元素的id属性值以便接下来的调用. HTML代码: <ca ...
- canvas 画时钟 会动呦
//半径 var r = 130; //重置原点 ctx.save(); ctx.translate(400, 500); //使用translate重置原点 function drawClock() ...
- html5学习(一)--canvas画时钟
利用空余时间学习一下html5. <!doctype html> <html> <head></head> <body> <canva ...
- 深夜,用canvas画一个时钟
深夜,用canvas画一个时钟 查看demo 这几天准备阿里巴巴的笔试,可以说已经是心力交瘁,自从阿里和蘑菇街的内推被刷掉之后,开始越来越怀疑起自己的能力来,虽然这点打击应该是微不足道的.毕竟校招在刚 ...
- [JS,Canvas]日历时钟
[JS,Canvas]日历时钟 Html: <!doctype html> <html> <head> <meta charset="UTF-8&q ...
- 使用canvas绘制时钟
使用canvas绘制时钟 什么使canvas呢?HTML5 <canvas> 元素用于图形的绘制,通过脚本 (通常是JavaScript)来完成.<canvas> 标签只是图 ...
- HTML5 之Canvas 绘制时钟 Demo
<!DOCTYPE html> <html> <head> <title>Canvas 之 时钟 Demo</title> <!--简 ...
- 简单酷炫的Canvas数字时钟
声明:本文为原创文章,如需转载,请注明来源WAxes,谢谢! 我记得很早之前就看过这个DEMO,是岑安大大博客里看到的: 就是这个数字时钟,当时觉得这个创意不错,但是也没去折腾.直到昨天同事又在网上看 ...
随机推荐
- Java基础之创建窗口——使用SpringLayout管理器(TrySpringLayout)
控制台程序. 可以把JFrame对象aWindow的内容面板的布局管理器设置为javax.swing.SpringLayout管理器. SpringLayout类定义的布局管理器根据javax.swi ...
- python 安装扩展库
直接使用pip安装时非常慢,而且还非常容易失败.通过在网上查找,发现可以使用镜像安装,如下: pip install xxx -i http://pypi.douban.com/simple/ 但是在 ...
- 单链表的回文判断(O(n)时间复杂度和O(1)的空间复杂度)
对于单链表来说,判断回文最简单的方法就是遍历链表,将链表中的元素复制到数组中,然后对数组进行判断是否是回文数组,但是这不符合O(1)的空间复杂度. 由于空间复杂度的要求,需要就地操作链表,不能开辟多余 ...
- 为benchmarksql的PostgreSQL java驱动进行升级
为benchmarksql的PostgreSQL java驱动进行升级[root@minion1 benchmarksql-4.1.0]# wget https://jdbc.postgresql.o ...
- js字符串转化为方法调用
function alertFunc(str1,str2,str3){ alert(str1); alert(str2); alert(str3); } function callAlert(func ...
- 20145207 《Java程序设计》第一周学习总结
不好意思,来晚了 别的先不说,先道个歉,放假前跟娄老师多少发生点矛盾,望原谅. 假期忙实习还有一些其他事情,为了认真对待这门课,把剩下的时间留下来,争取一天一章来弥补. 由于没选课加上另一门课没开 ...
- 救火必备linux命令
系统参数: cat /proc/cpuinfo cpu相关参数 cat /proc/meminfo 内存相关参数 cat /proc/loadavg 负载情况 性能参数: )top M:按内存使用排序 ...
- smarty简单介绍
smarty简单介绍 示意图如下 简单介绍smarty.class.php类的大体内容,如下: <?php class Smarty //此类就是libs中的Smarty.class.php类 ...
- IE已经被抛弃,但是不能遗忘
虽然IE的兼容问题,在我写这篇文章的时候基本已经被抛弃了,但是我觉得还是应该了解一下最基本的解决办法. 就像中国的历史已经过去,但是我们不能忘记一样的. 逐个版本解决法 .bb{ background ...
- linux下奇怪的“重名”文件
是这样的,文件创建是通过远程命令来进行的. 就是在表单中输入命令,然后使用php的system来执行. 表单使用的是多行文本输入框. 可能某次使用 类似touch这种命令创建文件的时候多按了一次回车, ...