吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 6475|回复: 67
收起左侧

[其他] HTML5时钟用来教小朋友认识时钟

  [复制链接]
lzh68575536 发表于 2022-11-22 12:54
本帖最后由 lzh68575536 于 2022-11-22 13:01 编辑

专门用来教小朋友认识时钟
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>HTML5time</title>
        <!--本地jQuery-->
    <script src="./jquery.min.js"></script>
    <style>
        .clocks {
            height: 900px;
            margin: 25px auto;
            position: relative;
            width: 900px;
        }
    </style>
</head>
<body>
<header>
</header>
<div class="clocks">
    <canvas id="canvas" width="800" height="800"></canvas>
</div>
</body>
<script>
    //初始化参数
    var canvas, ctx;
    var clockRadius = 250;
    var clockImage;

        //清空画布
    function clear() {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    }
        
        //画图
    function drawScene(hours = 0,minutes = 0,seconds = 0) {
                clockImage = new Image();
        clockImage.src = "./time.png";//本地图片
                clockImage.onload = function(){ctx.drawImage(clockImage, 50, 50, 700, 700)};
        clear();
        //获取时间
        var date = new Date();
        var hours = hours>0?hours:date.getHours();
        var minutes = minutes>0?minutes:date.getMinutes();
        var seconds = seconds>0?seconds:date.getSeconds();
        hours = hours > 12 ? hours - 12 : hours;
        var hour = hours + minutes / 60;
        var minute = minutes ;
        ctx.save();
        ctx.translate(canvas.width / 2, canvas.height / 2);
        ctx.beginPath();

        ctx.font = '36px Arial';
        ctx.fillStyle = '#000';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
                //标注小时
        for (var n = 1; n <= 12; n++) {
            var theta = (n - 3) * (Math.PI * 2) / 12;
            var x = clockRadius * 1 * Math.cos(theta);
            var y = clockRadius * 1 * Math.sin(theta);
            ctx.fillText(n, x, y);
        }
                
                //标注分钟
        for (var n = 1; n <= 60; n++) {
            var theta = (n-15) * (Math.PI * 2) / 60;
            var x = clockRadius * 1.4 * Math.cos(theta);
            var y = clockRadius * 1.4 * Math.sin(theta);
            ctx.fillStyle = 'red';
            ctx.font = "20px sans-serif"
            ctx.fillText(n, x, y);
        }

        //画时钟
        ctx.save();
        var theta = (hour - 3) * 2 * Math.PI / 12;
        ctx.rotate(theta);
        ctx.beginPath();
        ctx.moveTo(-15, -5);
        ctx.lineTo(-15, 5);
        ctx.lineTo(clockRadius * 0.7, 1);
        ctx.lineTo(clockRadius * 0.7, -1);
        ctx.fillStyle = 'blue';
        ctx.fill();
        ctx.restore();

        //画分钟
        ctx.save();
        var theta = (minute - 15) * 2 * Math.PI / 60;
        ctx.rotate(theta);
        ctx.beginPath();
        ctx.moveTo(-15, -4);
        ctx.lineTo(-15, 4);
        ctx.lineTo(clockRadius * 1, 1);
        ctx.lineTo(clockRadius * 1, -1);
        ctx.fillStyle = 'red';
        ctx.fill();
        ctx.restore();

        //画秒钟
        ctx.save();
        var theta = (seconds - 15) * 2 * Math.PI / 60;
        ctx.rotate(theta);
        ctx.beginPath();
        ctx.moveTo(-15, -3);
        ctx.lineTo(-15, 3);
        ctx.lineTo(clockRadius * 1.2, 1);
        ctx.lineTo(clockRadius * 1.2, -1);
        ctx.fillStyle = '#D5D52B';
        ctx.fill();
        ctx.restore();
        ctx.restore();
    }

    $(function(){
        canvas = document.getElementById('canvas');
        ctx = canvas.getContext('2d');
        //setInterval(drawScene, 1000);//当前时间
        drawScene(12,1,30);//手动调整时间
    });
</script>
</html>

钟表背景图片

钟表背景图片

效果图

效果图

免费评分

参与人数 12吾爱币 +10 热心值 +12 收起 理由
sht281 + 1 + 1 谢谢@Thanks!
浓茶 + 1 + 1 要是能指定时分秒就更好了
青云山 + 1 14楼有成品
chains2021 + 1 + 1 我很赞同!
jg518 + 1 + 1 谢谢@Thanks!
ialove + 2 + 1 热心回复!
djsonc + 1 + 1 谢谢@Thanks!
chaoslaw + 1 谢谢@Thanks!
吾爱爱破解 + 1 + 1 我很赞同!
halou + 1 + 1 谢谢@Thanks!
wkdxz + 1 鼓励转贴优秀软件安全工具和文档!
W17633606556 + 1 + 1 我很赞同!

查看全部评分

本帖被以下淘专辑推荐:

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

xingling 发表于 2022-11-23 08:54

https://img12.360buyimg.com/ddimg/jfs/t1/6650/26/26983/43130/637d6e84E2b619ce6/5fb355870546c27e.png
xingling 发表于 2022-11-23 08:55
[HTML] 纯文本查看 复制代码
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8" />
    <title>HTML5time</title>
        <!--本地jQuery-->
<script src="https://code.jquery.com/jquery-3.6.1.min.js"></script>
    <style>
        .clocks {
            height: 900px;
            margin: 25px auto;
            position: relative;
            width: 900px;
        }
        #canvas{
                background:url([img]https://img12.360buyimg.com/ddimg/jfs/t1/6650/26/26983/43130/637d6e84E2b619ce6/5fb355870546c27e.png[/img]);
    background-size:800px 800px;
    background-repeat:no-repeat;
        }
    </style>
</head>
<body>
<header>
</header>
<div class="clocks">
    <canvas id="canvas" width="800" height="800"></canvas>
</div>
</body>
<script>
    //初始化参数
    var canvas, ctx;
    var clockRadius = 250;
    var clockImage;
 
        //清空画布
    function clear() {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
    }
         
        //画图
    function drawScene(hours = 0,minutes = 0,seconds = 0) {
                clockImage = new Image();
        clockImage.src = "";//本地图片
                clockImage.onload = function(){ctx.drawImage(clockImage, 50, 50, 700, 700)};
        clear();
        //获取时间
        var date = new Date();
        var hours = hours>0?hours:date.getHours();
        var minutes = minutes>0?minutes:date.getMinutes();
        var seconds = seconds>0?seconds:date.getSeconds();
        hours = hours > 12 ? hours - 12 : hours;
        var hour = hours + minutes / 60;
        var minute = minutes ;
        ctx.save();
        ctx.translate(canvas.width / 2, canvas.height / 2);
        ctx.beginPath();
 
        ctx.font = '36px Arial';
        ctx.fillStyle = '#000';
        ctx.textAlign = 'center';
        ctx.textBaseline = 'middle';
                //标注小时
        for (var n = 1; n <= 12; n++) {
            var theta = (n - 3) * (Math.PI * 2) / 12;
            var x = clockRadius * 1 * Math.cos(theta);
            var y = clockRadius * 1 * Math.sin(theta);
            ctx.fillText(n, x, y);
        }
                 
                //标注分钟
        for (var n = 1; n <= 60; n++) {
            var theta = (n-15) * (Math.PI * 2) / 60;
            var x = clockRadius * 1.4 * Math.cos(theta);
            var y = clockRadius * 1.4 * Math.sin(theta);
            ctx.fillStyle = 'red';
            ctx.font = "20px sans-serif"
            ctx.fillText(n, x, y);
        }
 
        //画时钟
        ctx.save();
        var theta = (hour - 3) * 2 * Math.PI / 12;
        ctx.rotate(theta);
        ctx.beginPath();
        ctx.moveTo(-15, -5);
        ctx.lineTo(-15, 5);
        ctx.lineTo(clockRadius * 0.7, 1);
        ctx.lineTo(clockRadius * 0.7, -1);
        ctx.fillStyle = 'blue';
        ctx.fill();
        ctx.restore();
 
        //画分钟
        ctx.save();
        var theta = (minute - 15) * 2 * Math.PI / 60;
        ctx.rotate(theta);
        ctx.beginPath();
        ctx.moveTo(-15, -4);
        ctx.lineTo(-15, 4);
        ctx.lineTo(clockRadius * 1, 1);
        ctx.lineTo(clockRadius * 1, -1);
        ctx.fillStyle = 'red';
        ctx.fill();
        ctx.restore();
 
        //画秒钟
        ctx.save();
        var theta = (seconds - 15) * 2 * Math.PI / 60;
        ctx.rotate(theta);
        ctx.beginPath();
        ctx.moveTo(-15, -3);
        ctx.lineTo(-15, 3);
        ctx.lineTo(clockRadius * 1.2, 1);
        ctx.lineTo(clockRadius * 1.2, -1);
        ctx.fillStyle = '#D5D52B';
        ctx.fill();
        ctx.restore();
        ctx.restore();
    }
 
    $(function(){
        canvas = document.getElementById('canvas');
        ctx = canvas.getContext('2d');
       setInterval(drawScene, 1000);//当前时间
       // drawScene(12,1,30);//手动调整时间
    });
</script>
</html>
wkdxz 发表于 2022-11-22 14:04
lzh68575536 发表于 2022-11-22 13:03
那个无法上传,网上随便搞一个就行了

我把他搞下来了,给你补上链接,谢谢分享


https://wkdxz.lanzout.com/iLaaM0gl170b
密码:7i21

免费评分

参与人数 1热心值 +1 收起 理由
aq666 + 1 热心回复!

查看全部评分

Abby0501 发表于 2022-11-22 12:59
dd,支持一下
hopeknow 发表于 2022-11-22 13:02
没有jquery.min.js脚本?
 楼主| lzh68575536 发表于 2022-11-22 13:03
hopeknow 发表于 2022-11-22 13:02
没有jquery.min.js脚本?

那个无法上传,网上随便搞一个就行了
W17633606556 发表于 2022-11-22 13:04
感谢分享
52new 发表于 2022-11-22 13:04
写得不错。
zang135579 发表于 2022-11-22 13:27
hopeknow 发表于 2022-11-22 13:02
没有jquery.min.js脚本?

$(function)可以用window.onload
superjason 发表于 2022-11-22 13:27
为啥我的不会动,js要放入同目录了
hangjiasoft 发表于 2022-11-22 13:29
感谢分享
 楼主| lzh68575536 发表于 2022-11-22 13:47
superjason 发表于 2022-11-22 13:27
为啥我的不会动,js要放入同目录了

你确定你一下,jQuery的路径问题,直接在网页上面F12是否能打开jQuery文件
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 提醒:禁止复制他人回复等『恶意灌水』行为,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-16 11:19

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表