用js做时钟

在画指针的时候我们需要这样的属性,指针的初始点,末尾点,以及颜色,宽度。4画数字,刻度的数字跟刻度的道理是差不多的,但是,画数字是我们需要定一个点,正常情况下,这个点是这个四方字的左下角,所以,我们要将这个左下角的点定在这个方块字中间,这样,字才会处于我们想要的地方,因为每个字的宽高是不同的,就比如1和12他们的长宽是不同的,所以我们需要获取他们长宽的具体数值,再将他们的数值调用,运用到我们的这个点上,算出来这个点的位置,我们就可以将我们的1~12放置到我们需要的位置。有开始画线,就有结束画线。
摘要由CSDN通过智能技术生成

1、做表盘。正方形,也就是将表的框架固定好.   

<div class="clock">
    <canvas id="view" height="400px"width="400px"></canvas>
</div>


2、画圆。在表盘上画一个圆,这样子会更好看一些,在画线的过程中注意开始画线与结束画线,可以说,在一定程度上,电脑就是个智障。有开始画线,就有结束画线。没有结束画线的话,如果继续画线,那么画线将在上一条线结束点开始。所以切记,一定要有结束画线。

//画圆
ctx.beginPath();
ctx.arc(200,200,200,0,Math.PI*2,false);
ctx.lineWidth=3;
ctx.strokeStyle='blue';
ctx.stroke();
ctx.closePath()


3、做刻度。刻度的话,分为两部分,一部分是分钟刻度,还有一部分是小时刻度。画刻度是有先后的,首先画分钟刻度,之后再画小时刻度,因为小时刻度较为长,后画小时刻度会掩盖在小时刻度线上的分钟刻度,当然如果小时刻度与分钟刻度颜色一样就没必要分前后了。画刻度是有规律的,所以我们可以使用for循环,我们可以使用这样一个方法,找到两个圆,同一个圆心,半径不同的同心圆,将内圆作为画刻度的起始点,外圆作为画刻度的结束点,但是我们这个点我们是得计算的我们将表盘的左上角作为原

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
以下是JavaScript实现动态时钟的代码: HTML: ```html <!DOCTYPE html> <html> <head> <title>动态时钟</title> <style> body { background-color: #f1f1f1; } .clock { margin: 50px auto; width: 200px; height: 200px; border-radius: 50%; background-color: white; box-shadow: 0px 0px 20px rgba(0, 0, 0, 0.1); position: relative; } .hour-hand { position: absolute; top: 50%; left: 50%; width: 6px; height: 50px; background-color: black; transform-origin: bottom center; transform: translateX(-50%) rotate(0deg); animation: hourAnim 43200s linear infinite; } .minute-hand { position: absolute; top: 50%; left: 50%; width: 4px; height: 80px; background-color: black; transform-origin: bottom center; transform: translateX(-50%) rotate(0deg); animation: minuteAnim 3600s linear infinite; } .second-hand { position: absolute; top: 50%; left: 50%; width: 2px; height: 100px; background-color: red; transform-origin: bottom center; transform: translateX(-50%) rotate(0deg); animation: secondAnim 60s linear infinite; } @keyframes hourAnim { from { transform: translateX(-50%) rotate(0deg); } to { transform: translateX(-50%) rotate(360deg); } } @keyframes minuteAnim { from { transform: translateX(-50%) rotate(0deg); } to { transform: translateX(-50%) rotate(360deg); } } @keyframes secondAnim { from { transform: translateX(-50%) rotate(0deg); } to { transform: translateX(-50%) rotate(360deg); } } .clock-center { position: absolute; top: 50%; left: 50%; width: 10px; height: 10px; background-color: black; border-radius: 50%; transform: translate(-50%, -50%); } </style> </head> <body> <div class="clock"> <div class="hour-hand"></div> <div class="minute-hand"></div> <div class="second-hand"></div> <div class="clock-center"></div> </div> <script src="clock.js"></script> </body> </html> ``` JavaScript: ```javascript function updateClock() { const now = new Date(); const hour = now.getHours(); const minute = now.getMinutes(); const second = now.getSeconds(); const hourDegree = (hour % 12) * 30 + (minute / 60) * 30; const minuteDegree = minute * 6; const secondDegree = second * 6; document.querySelector(".hour-hand").style.transform = `translateX(-50%) rotate(${hourDegree}deg)`; document.querySelector(".minute-hand").style.transform = `translateX(-50%) rotate(${minuteDegree}deg)`; document.querySelector(".second-hand").style.transform = `translateX(-50%) rotate(${secondDegree}deg)`; } setInterval(updateClock, 1000); ``` 这个代码通过 `setInterval` 方法每秒更新一次时钟, `updateClock` 函数获取当前时间并计算出每个指针应该旋转的角度,然后通过设置 `transform` 属性来旋转指针。其中,时针每小时旋转30度,分针每分钟旋转6度,秒针每秒钟旋转6度。 在HTML文件中,我们定义了一个时钟的容器 `.clock`,并在其中添加了时针、分针、秒针和中心点。每个指针都有自己的类名和CSS样式,也有自己的动效果。其中,时针的动总时长为43200秒(12小时),分针的动总时长为3600秒(1小时),秒针的动总时长为60秒(1分钟)。这样可以让指针在一定的时间内完成一圈的旋转。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值