当前位置:网站首页>js实现时钟
js实现时钟
2022-06-28 00:27:00 【明斯克开源】
// console.log(“number=” + number, “index=” + index);
var rad = 2 * pi / 12 * index;
var x = Math.cos(rad) * (r - 30);
var y = Math.sin(rad) * (r - 30);
ctx.textAlign = ‘center’;
// 让数字落在坐标的中心
ctx.textBaseline = ‘middle’;
ctx.fillStyle = “black”;
// 在坐标为(x,y)的地方绘制number
ctx.fillText(number, x, y);
});
//绘制刻度
var i;
for (i = 0; i < 60; i++) {
var rad = (2 * pi / 60) * i;
var x = Math.cos(rad) * (r - 20) ;
// console.log(“x=” + x);
var y = Math.sin(rad) * (r - 20);
// console.log(“y=” + y);
ctx.beginPath();
// 开始绘制圆
ctx.arc(x, y, 2, 0, 2 * pi);
if (i % 5 == 0) {
ctx.fillStyle = “black”;
}
else {
ctx.fillStyle = “gray”;
}
ctx.fill();
}
}
// 绘制秒针
function drawsecond(ms) {
ctx.save();
// 清空子路径列表来启动新路径。
ctx.beginPath();
var rad = (2 * pi / 60)*(ms-15);
// 旋转画布
ctx.rotate(rad);
ctx.moveTo(-5,0);
ctx.lineTo(90,0);
ctx.lineWidth = 1;
ctx.strokeStyle = “red”;
ctx.lineCap = “round”;
ctx.stroke();
ctx.restore();
}
//绘制分针
function drawminute(minute) {
ctx.save();
// 清空子路径列表来启动新路径。
ctx.beginPath();
var rate=(2pi/60)(minute-15);
ctx.rotate(rate);
ctx.moveTo(0,0);
ctx.lineTo(70,0);
ctx.lineWidth = 2;
ctx.lineCap=“round”;
ctx.strokeStyle = “blue”;
ctx.stroke();
ctx.restore();
}
//绘制时针
function drawhour(hour, minute) {
// 清空子路径列表来启动新路径。
ctx.save();
ctx.beginPath();
//每小时弧度
var rad = (2 * pi/12)*(hour-3) ;
//每分钟弧度
var mrad = (2 * pi/12 / 60)*(minute-15);
// 对画布进行旋转
ctx.rotate(rad + mrad);
// 设置画布样式
ctx.lineWidth = 3;
ctx.lineCap = “round”;
ctx.strokeStyle = “black”;
ctx.moveTo(0,0);
ctx.lineTo(45,0);
ctx.stroke();
ctx.restore();
}
function start() {
ctx.clearRect(-250,-200, 500,400);
var time = new Date();
var hour = time.getHours();
console.log(hour);
var minute = time.getMinutes();
var second = time.getSeconds 《一线大厂Java面试题解析+后端开发学习笔记+最新架构讲解视频+实战项目源码讲义》无偿开源 威信搜索公众号【编程进阶路】 ();
console.log(second);
console.log(“second=”+second);
drawBackground();
drawTime();
drawhour(hour,minute);
drawminute(minute);
drawsecond(second);
}
start();
// 每1秒刷新一次
setInterval(start, 1000);
边栏推荐
- ShardingSphere-proxy-5.0.0建立mysql读写分离的连接(六)
- [Yocto RM] 4 - Source Directory Structure
- To understand what is synchronous, asynchronous, serial, parallel, concurrent, process, thread, and coroutine
- SQL injection Bypass (2)
- pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...
- Domain Name System
- Machine learning notes - time series as features
- 【永艺XY椅】试用体验
- Using redis bitmap to realize personnel online monitoring
- Appium自动化测试基础— 补充:App的包名(appPackage)和启动名(appActivity)
猜你喜欢

Shardingsphere-proxy-5.0.0 establish MySQL read / write separation connection (6)

How to handle computer security certificate errors

Jenkins - groovy postbuild plug-in enriches build history information

Cesium 多边形增加文字标签(polygon 加 label)多边形中心点偏移问题解决

Cesium 获取屏幕所在经纬度范围

A set of sai2 brushes is finally finished! Share with everyone!

Jenkins - Pipeline concept and creation method

Numpy----np.tile()函数解析

SQL injection bypass (3)

SQL injection bypass (2)
随机推荐
架构高可靠性应用知识图谱 ----- 微服务架构图谱
The system administrator has set the system policy to prohibit this installation. Solution
What are the risks of opening a compass stock account? Is it safe to open a compass account
Jenkins - built in variable access
Adding text labels to cesium polygons the problem of polygon center point offset is solved
Embedded must learn! Detailed explanation of hardware resource interface - based on arm am335x development board (Part 2)
Jenkins - data sharing and transfer between copy artifact plug-in builds
Learn pickle
yarn下载报错There appears to be trouble with your network connection. Retrying.
Cesium Click to draw polygons (dynamically draw polygons)
Numpy----np.tile()函数解析
Where can I open an account for foreign exchange futures? Which platform is safer for cash in and out?
TD Hero online conference on July 2
Ionic4 realizes half star scoring
Cloud platform KVM migration local virtual machine records
利用redis bitmap实现人员在线情况监控
Numpy----np.meshgrid()
[Yocto RM]9 - QA Error and Warning Messages
Wangxinling, tanweiwei Shanhai (extended version of Chorus) online audition lossless FLAC Download
pytorch_ lightning. utilities. exceptions. MisconfigurationException: You requested GPUs: [1] But...