javascript - 带有 HTML5 的 LED 时钟动画
问题描述
我正在使用 HTML5 CANVAS 创建一个点状 LED 时钟。我对第二个点的动画有疑问。当下面的代码运行时,脚本会读取当前时间,计算秒数并绘制相应数量的点。当秒数过去时,它应该绘制新的点,当下一分钟开始时,它应该清除之前绘制的所有第二个点,而是绘制相同的点图案并且它们的数量已经在开始时读取。有人知道如何修复动画吗?
HTML:
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<script async="" src="tex2.js"></script>
<canvas id="clock" width="1200" height="675" style="background-color: #000000">
Your browser doesn't support CANVAS tag.
</canvas>
</body>
</html>
JS:
/*eslint-env browser*/
/*jslint browser:true */
var clock = document.getElementById("clock"),
face = clock.getContext("2d"),
height = 1200,
width = 675,
marg = 80,
dotRadius = 8,
faceRadius = (width - marg) / 2,
angle,
i,
x,
y,
time = new Date(),
secs = time.getSeconds();
function drawFace() {
'use strict';
face.fillStyle = "#FF0000";
for (i = 0; i <= secs; i = i + 1) {
angle = i * (Math.PI / 30) + Math.PI * -0.5;
x = Math.cos(angle) * faceRadius + height / 2;
y = Math.sin(angle) * faceRadius + width / 2;
face.beginPath();
face.arc(x, y, dotRadius, 0, 2 * Math.PI);
face.fill();
face.closePath();
}
face.fill();
window.requestAnimationFrame(drawFace);
}
function drawAll() {
'use strict';
window.requestAnimationFrame(drawFace);
drawFace();
}
drawAll();
解决方案
推荐阅读
- computer-science - 带有正负十进制值的九的补码问题
- junit - ExceptionInInitializerError 由于空静态模拟
- vba - 如何从返回对象列表的 C# 方法在 Access VBA 中获取数据
- javascript - 为什么 setDate 在传递字符串值时给出奇怪的答案?
- regex - 在表格中使用匹配正则表达式和数组公式
- django - django更新用户表单
- sql-server - 发布 dacpac(SSDT 数据库项目)时为数据库配置初始大小和自动增长大小的默认值
- latex - Atom 编辑器无法打开 .tex 文件
- python - 使用 BeautifulSoup 从自行车比赛中提取数据
- authentication - 为 Web 和移动应用程序设计通用登录