使用canvas绘制一个时钟

项目分为两个文件,clock.html和clock.js,下面贴出两个源代码

clock.html

<!DOCTYPE html>
<html lang="en">
<head>
    <meta name="viewport" charset="UTF-8" content="width=device-width,initial-scale=1">
    <title>Clock</title>
    <style type="text/css">
        div{
            text-align: center;
            margin-top: 250px;
        }
        #clock{
            border: 1px solid #ccc
        }
    </style>
</head>
<body>
    <div>
        <canvas id="clock" height="200px" width="200px"></canvas>
        <script type="text/javascript" src="clock.js"></script>
    </div>
</body>
</html>


clock.js

//设置全局变量
var dom = document.getElementById('clock');
var ctx = dom.getContext('2d');
var width = ctx.canvas.width;//
var height = ctx.canvas.height;
var r = width / 2;

//绘制时钟的背景
function drawBackground(){
    ctx.save();
    ctx.translate(r,r);
    ctx.beginPath();
    ctx.arc(0,0,r-5,0,2*Math.PI,false);//绘制一个圆形
    ctx.lineWidth = 10;
    ctx.stroke();

    var hourNumbers = [3,4,5,6,6,8,9,10,11,12,1,2];//设置整点数字
    ctx.font = '18px Arial';//设置整点数字的字体
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    hourNumbers.forEach(function(numbers,i){
        //计算圆上时钟的坐标
        var rad = 2*Math.PI / 12 *i;
        var x = Math.cos(rad)*(r-30);
        var y = Math.sin(rad)*(r-30);
        ctx.fillText(numbers,x,y);
    });
//设置小时的圆点
    for(var i=0;i<60;i++){
        var rad = 2*Math.PI / 60 *i;
        var x = Math.cos(rad)*(r-18);
        var y = Math.sin(rad)*(r-18);
        ctx.beginPath();
        //设置整点为深颜色,非整点为浅颜色,每5个循环一次
        if(i % 5 == 0){
            ctx.fillStyle = '#000';
            ctx.arc(x,y,2,0,2*Math.PI,false);
        }
        else{
            ctx.fillStyle = '#ccc';//填充的颜色
            ctx.arc(x,y,2,0,2*Math.PI,false);
        }
        ctx.fill();
    }
}

//利用canvas绘制出时针,其中save(),beginPath(),moveTo(),lineTo()都是canvas绘图工具有的函数,直接调用就可以了
//下面分针和秒针一样的,模仿即可
function drawHour(hour,minute){
        ctx.save();//在当前时间内保存绘制出的时针
        ctx.beginPath();
        var rad = 2 * Math.PI / 12 *hour;//时针的弧度
        var mrad = 2 * Math.PI / 12 / 60 * minute;//表示分针在走的时候时针多出来的弧度
        ctx.rotate(rad+mrad);
        ctx.lineWidth = '6';//时针的长度
        ctx.lineCap='round';//绘制时针的头顶为方形
        ctx.moveTo(0,10);
        ctx.lineTo(0,-r / 2);
        ctx.stroke();
        ctx.restore();
}
//利用canvas绘制出分针
function drawMinute(minute){
        ctx.save();
        ctx.beginPath();
        var rad = 2 * Math.PI / 60 * minute;
        ctx.rotate(rad);
        ctx.lineWidth = '3';
        ctx.lineCap='round';
        ctx.moveTo(0,10);
        ctx.lineTo(0,-r + 30);
        ctx.stroke();
        ctx.restore();
}
//利用canvas绘制出秒针
function drawSecond(second){
        ctx.save();
        ctx.beginPath();
        var rad = 2 * Math.PI / 60 * second;
        ctx.fillStyle = '#ccc';
        ctx.rotate(rad);
        ctx.moveTo(-2,20);
        ctx.lineTo(2,20);
        ctx.lineTo(1,-r + 18);
        ctx.lineTo(-1,-r + 18);
        ctx.fill();
        ctx.restore();
}
//画出中间的圆点
function drawDot(){
    ctx.beginPath();
    ctx.fillStyle = '#fff';
    ctx.arc(0,0,3,0,2 * Math.PI,false);
    ctx.fill();
}

function draw(){
    ctx.clearRect(0,0,width,height);//清除前一个画出的时针,分针,秒针
    var now = new Date();//新建一个对象获取当前时间
    var hour = now.getHours();
    var minute = now.getMinutes();
    var second = now.getSeconds();
    drawBackground();
    drawHour(hour,minute);
    drawMinute(minute);
    drawSecond(second);
    drawDot();
    ctx.restore();
}
setInterval(draw,1000);//每隔1s绘制一次图形

希望有更好方法的同学联系我哈!


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然可以!以下是使用 Canvas 绘制一个简单的时钟的示例代码: ```html <!DOCTYPE html> <html> <head> <title>Canvas 时钟</title> <style> #clockCanvas { border: 1px solid #000; } </style> </head> <body> <canvas id="clockCanvas" width="200" height="200"></canvas> <script> function drawClock() { var canvas = document.getElementById('clockCanvas'); var context = canvas.getContext('2d'); var radius = canvas.height / 2; context.translate(radius, radius); radius = radius * 0.9; setInterval(function() { drawFace(context, radius); drawNumbers(context, radius); drawTime(context, radius); }, 1000); } function drawFace(context, radius) { var grad; context.beginPath(); context.arc(0, 0, radius, 0, 2 * Math.PI); context.fillStyle = 'white'; context.fill(); grad = context.createRadialGradient(0, 0, radius * 0.95, 0, 0, radius * 1.05); grad.addColorStop(0, '#333'); grad.addColorStop(0.5, 'white'); grad.addColorStop(1, '#333'); context.strokeStyle = grad; context.lineWidth = radius * 0.1; context.stroke(); context.beginPath(); context.arc(0, 0, radius * 0.1, 0, 2 * Math.PI); context.fillStyle = '#333'; context.fill(); } function drawNumbers(context, radius) { var angle; var num; context.font = radius * 0.15 + 'px arial'; context.textBaseline = 'middle'; context.textAlign = 'center'; for (num = 1; num < 13; num++) { angle = num * Math.PI / 6; context.rotate(angle); context.translate(0, -radius * 0.85); context.rotate(-angle); context.fillText(num.toString(), 0, 0); context.rotate(angle); context.translate(0, radius * 0.85); context.rotate(-angle); } } function drawTime(context, radius) { var now = new Date(); var hour = now.getHours(); var minute = now.getMinutes(); var second = now.getSeconds(); // 对时、分、秒进行角度转换 hour = hour % 12; hour = (hour * Math.PI / 6) + (minute * Math.PI / (6 * 60)) + (second * Math.PI / (360 * 60)); drawHand(context, hour, radius * 0.5, radius * 0.07); minute = (minute * Math.PI / 30) + (second * Math.PI / (30 * 60)); drawHand(context, minute, radius * 0.8, radius * 0.07); second = (second * Math.PI / 30); drawHand(context, second, radius * 0.9, radius * 0.02); } function drawHand(context, pos, length, width) { context.beginPath(); context.lineWidth = width; context.lineCap = 'round'; context.moveTo(0, 0); context.rotate(pos); context.lineTo(0, -length); context.stroke(); context.rotate(-pos); } drawClock(); </script> </body> </html> ``` 你可以将这段代码保存为 HTML 文件并在浏览器中打开,就可以看到一个实时的时钟效果。希望这能帮到你!如果还有其他问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值