js实现时钟

// 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);

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值