canvas炫酷时钟

实现的功能

  • 主要用到canvas的一些基础api
  • 直接看效果

html:

   <canvas id="myCanvas" width="500" height="500"></canvas>

css:

   #myCanvas{
position: absolute;
top: 50%;
left: 50%;
margin-top: -250px;
margin-left: -250px;
}

js:

    var myCanvas = document.getElementById('myCanvas');
var ctx = myCanvas.getContext('2d'); ctx.strokeStyle = '#00ffff';
ctx.lineWidth = '15';
ctx.shadowBlur = '20';
ctx.shadowColor = 'black'; function draw(){
//获取时间
var date = new Date();
var today = date.toDateString();
var h = date.getHours();
var m = date.getMinutes();
var s = date.getSeconds();
var ms = date.getMilliseconds();
var s_ms = s + ms/1000;
//背景
var grd =ctx.createRadialGradient(250,250,50,250,250,300);
grd.addColorStop(0,'red');
grd.addColorStop(1,'black');
ctx.fillStyle = grd;
ctx.fillRect(0,0,500,500);
//时分秒圆弧
// 360/12 * h 时
ctx.beginPath();
ctx.arc(250, 250, 200, 1.5 * Math.PI, formatdeg(360 / 12 * h - 90));
ctx.stroke(); // 360/60 * m 分
ctx.beginPath();
ctx.arc(250, 250, 170, 1.5 * Math.PI, formatdeg(360 / 60 * m - 90));
ctx.stroke(); // 360/60 * s 秒
ctx.beginPath();
ctx.arc(250, 250, 140, 1.5 * Math.PI, formatdeg(360 / 60 * s_ms - 90));
ctx.stroke(); ctx.font = '20px Arial';
ctx.textAlign = 'center';
ctx.fillStyle = '#00ffff';
ctx.fillText(today,250,250); ctx.fillText(formatTime(h) + ' : ' + formatTime(m) + ' : ' + formatTime(s) + ' ' + formatMs(ms), 250, 280);
};
setInterval(draw,40); //时间处理
function formatTime(time){
return ('0' + time).slice(-2);
}
function formatMs(ms){
if(ms <10){
return "00" + ms;
}else if(ms <100){
return "0" + ms;
}else{
return ms;
}
}
//角度转弧度
function formatdeg(deg){
var fd = Math.PI / 180;
return deg * fd;
}

参考自:腾讯课堂渡一教育

最新文章

  1. Windows Server 2012 虚拟化实战:SCVMM的安装和部署
  2. AFNetworking讲解
  3. spring @resource @ Autowired
  4. Java中处理Linux信号量
  5. Android使用adb工具及root权限完成手机抓包
  6. win7充分利用cpu来提供计算机性能
  7. Android代码混淆和项目宣布步骤记录器
  8. WAP页面点击与hover延迟解决之道
  9. BNUOJ 34981 A Matrix
  10. 搭建Gitlab
  11. 解决Ubuntu 16.04 软件中心闪退
  12. C++编程练习(1)----“实现简单的线性表的顺序存储结构“
  13. tkinter模块常用参数(python3)
  14. 文件传输协议(FTP)
  15. Vue-router的API详解
  16. Android全面屏适配
  17. BP神经网络与Python实现
  18. Linux-(diff)
  19. 读书笔记-《Linux/Unix设计思想》
  20. day9学python 类+异常处理+初识socket

热门文章

  1. Understanding FiddlerScript
  2. php常用命令
  3. mysql-connector-java(6.0以上)的时差问题
  4. 图像基础知识之YUV
  5. too many positional arguments错误
  6. shell语法学习
  7. Dotmemory 内存分析工具
  8. 一、Spring之组件注册-@Configuration&amp;@Bean给容器中注册组件
  9. [转]NGINX-检测客户端是通过电脑还是移动设备访问的,将请求重定向到适配的WEB站点
  10. [转帖]Latch