canvas炫酷时钟
2024-09-03 14:13:19
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;
}
参考自:腾讯课堂渡一教育
最新文章
- Windows Server 2012 虚拟化实战:SCVMM的安装和部署
- AFNetworking讲解
- spring @resource @ Autowired
- Java中处理Linux信号量
- Android使用adb工具及root权限完成手机抓包
- win7充分利用cpu来提供计算机性能
- Android代码混淆和项目宣布步骤记录器
- WAP页面点击与hover延迟解决之道
- BNUOJ 34981 A Matrix
- 搭建Gitlab
- 解决Ubuntu 16.04 软件中心闪退
- C++编程练习(1)----“实现简单的线性表的顺序存储结构“
- tkinter模块常用参数(python3)
- 文件传输协议(FTP)
- Vue-router的API详解
- Android全面屏适配
- BP神经网络与Python实现
- Linux-(diff)
- 读书笔记-《Linux/Unix设计思想》
- day9学python 类+异常处理+初识socket