惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

J
Java Code Geeks
IT之家
IT之家
爱范儿
爱范儿
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
博客园_首页
Blog — PlanetScale
Blog — PlanetScale
V
Visual Studio Blog
云风的 BLOG
云风的 BLOG
MyScale Blog
MyScale Blog
阮一峰的网络日志
阮一峰的网络日志
Stack Overflow Blog
Stack Overflow Blog
酷 壳 – CoolShell
酷 壳 – CoolShell
GbyAI
GbyAI
V
V2EX
N
Netflix TechBlog - Medium
Vercel News
Vercel News
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
T
The Blog of Author Tim Ferriss
量子位
博客园 - Franky
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
博客园 - 司徒正美
月光博客
月光博客
F
Fortinet All Blogs

时间的朋友

Windows 命令行密码重置 Anaconda安装 typescript 注解解读1 Konvajs Shape加载自定义图片 sshpass 使用 why-is-node-running webgl笔记 SharedArrayBuffer is not defined blender 常用快捷键 | 时间的朋友 vue -- v3.4commit提交记录2 vue2 升级vue3报错问题整理 着色器 expressjs 源码 hyper-V arch linux 网络配置 element input数字格式化 three 拼接货架 WebAudio笔记 Windows nginx重启bat脚本 vue -- v3.4commit提交记录 URI malformed vue3 -- Class 对象在组件中使用范例 | 时间的朋友 ruby 安装和升级 element-plus 老版本cascader使用卡死问题 vue3 内置Transition组件 | 时间的朋友 前端memo的实现 | 时间的朋友 Vue -- vue-class-component源码 | 时间的朋友 linux 优化脚本 typescript 装饰器 | 时间的朋友 microbundle 源码 | 时间的朋友 WSL2问题解决WslRegisterDistribution failed with error: 0x800701bc
canvas requestAnimationFrame画一个clock | 时间的朋友
2023-06-02 · via 时间的朋友

Published: · LastMod: June 03, 2023 · 228 words

canvas requestAnimationFrame 画一个 clock 🔗

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43

    var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame ||
        window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;

    var h1 = document.getElementsByTagName("h1")[0];
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext('2d');

    function getDateString(d) {
        return d.getFullYear() +
            '-' + ('0' + (d.getMonth() + 1)).slice(-2) +
            '-' + ('0' + d.getDate()).slice(-2) +
            ' ' + ('0' + d.getHours()).slice(-2) +
            ':' + ('0' + d.getMinutes()).slice(-2) +
            ':' + ('0' + d.getSeconds()).slice(-2) +
            '.' + ('00' + d.getMilliseconds()).slice(-3);
    }

    function drawline(width, r, angle) {
        var path = new Path2D();
        path.moveTo(canvas.width / 2, canvas.height / 2);
        path.lineTo(canvas.width / 2 - r * Math.cos(angle * 2 * Math.PI + Math.PI / 2), canvas.width / 2 - r * Math.sin(angle * 2 * Math.PI + Math.PI / 2));
        ctx.lineWidth = width;
        ctx.stroke(path);
    }

    requestAnimationFrame(function step(timestamp) {
        var d = new Date();
        var r_msec = d.getMilliseconds() / 1000;
        var r_seconds = (d.getSeconds() + r_msec) / 60;
        var r_minutes = (d.getMinutes() + r_seconds) / 60;
        var r_hours = (d.getHours() + r_minutes) / 12;

        h1.textContent = getDateString(d);

        ctx.clearRect(0, 0, canvas.width, canvas.height);
        drawline(10, 80, r_hours);
        drawline(5, 120, r_minutes);
        drawline(2, 150, r_seconds);
        // drawline(1, 180, r_msec);

        requestAnimationFrame(step);
    });