用canvas简单画一个时钟

<style>
    body {
        text-align: center;
    }
    #canvas {
        background-color: powderblue;
    }

</style>
<body>
    <!--设置画布高和宽 -->
    <canvas id="canvas" height="400px" width="400px"></canvas>
</body>
<script src="./test.js"></script>
let ctx = document.getElementById('canvas').getContext('2d');

function time() {
    // 先清空画布
    ctx.clearRect(0, 0, 400, 400) 

    // 画外圆
    ctx.beginPath();
    ctx.arc(200, 200, 200, 0, 2 * Math.PI);
    ctx.stroke();

    // 画时针刻度
    for(let i = 0; i < 12; i++) {
        ctx.beginPath();
        ctx.lineWidth = 3;
        ctx.save();
        ctx.translate(200, 200);
        ctx.rotate(i / 6 * Math.PI);
        ctx.moveTo(0, -175);
        ctx.lineTo(0, -200);
        ctx.stroke();
        
        ctx.font = 'normal 12px Arial';
        ctx.textAlign = 'center';
        ctx.lineWidth = 1;
        ctx.strokeText(`${i === 0 ? 12 : i === 6 ? 9 : i}`, 0, -160)

        ctx.restore()
    }
    
    // 画分针刻度
    for(let i = 0; i < 60; i++) {
        ctx.beginPath();
        ctx.lineWidth = 2;
        ctx.save();
        ctx.translate(200, 200);
        ctx.rotate(i / 30 * Math.PI);
        ctx.moveTo(0, -190);
        ctx.lineTo(0, -200);
        ctx.stroke();
        ctx.restore()
    }

    // 获取当前时间
    let nowTime = new Date();
    let hours = nowTime.getHours();
    let minutes = nowTime.getMinutes();
    let seconds = nowTime.getSeconds();

    // 将24小时转化为12小时制
    hours = hours > 12 ? hours - 12 : hours;
    hours = hours + minutes / 60;
    minutes = minutes + seconds / 60;

    // 画时针
    ctx.beginPath();
    ctx.lineWidth = 3;
    ctx.save();
    ctx.translate(200, 200);
    ctx.rotate(hours / 6 * Math.PI);
    ctx.moveTo(0, -80);
    ctx.lineTo(0, 0);
    ctx.stroke();
    ctx.restore();

    // 画分针
    ctx.beginPath();
    ctx.lineWidth = 2;
    ctx.save();
    ctx.translate(200, 200);
    ctx.rotate(minutes / 30 * Math.PI);
    ctx.moveTo(0, -130);
    ctx.lineTo(0, 0);
    ctx.stroke();
    ctx.restore();

    // 画秒针
    ctx.beginPath();
    ctx.lineWidth = 1;
    ctx.save();
    ctx.translate(200, 200);
    ctx.rotate(seconds / 30 * Math.PI);
    ctx.moveTo(0, -170);
    ctx.lineTo(0, 0);
    ctx.stroke();
    ctx.restore();

    ctx.beginPath();
    ctx.save();
    ctx.font = 'normal 14px Arial';
    ctx.textAlign = 'center';
    ctx.lineWidth = 1;
    ctx.translate(200, 200);
    ctx.strokeText(`${formatTime(nowTime.getHours())}${formatTime(nowTime.getMinutes())}${formatTime(nowTime.getSeconds())}`, 0, 80);
    ctx.restore();

    setInterval(time, 1000);
}

function formatTime(time) {
    return time < 10 ? '0' + time : time;
}

time()

// 将canvas生成图片,添加到body里面
let canvasDom = document.getElementById('canvas')
let imageUrl = canvasDom.toDataURL('image/png')
let image = document.createElement('img')
image.src = imageUrl
document.body.appendChild(image)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值