温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

html5 canvas 画时钟

发布时间:2020-02-26 12:50:17 来源:网络 阅读:456 作者:antlove 栏目:移动开发
<!DOCTYPE HTML>
<html>
<body>

<canvas id="myCanvas" width="400" height="400" ></canvas>

<script type="text/javascript">
    // get the canvas
    var myCanvas = document.getElementById("myCanvas");
    var ctx = myCanvas.getContext("2d");

    function showClock(){
        ctx.save();
        ctx.save();
        ctx.save();
        ctx.save();
        ctx.save();
        ctx.save();
        ctx.save();
        ctx.clearRect(0,0,400,400);

        // create the clock border
        ctx.restore();
        ctx.beginPath();
        ctx.strokeStyle="pink";
        ctx.lineWidth=10;
        ctx.arc(200,200,150,0,Math.PI*2,true);
        ctx.stroke();


        // create the hour icon
        ctx.restore();
        ctx.lineWidth=8;
        ctx.strokeStyle="blue";
        ctx.translate(200,200);
        for(var i=0;i<12;i++){
          ctx.beginPath();      
          ctx.moveTo(135,0);
          ctx.lineTo(150,0);
          ctx.rotate(Math.PI/6);
          ctx.stroke();
        }

        // create the minute icon
        ctx.restore();
        ctx.lineWidth=4;
        ctx.strokeStyle="blue";
        ctx.translate(200,200);
        for(var i=0;i<60;i++){
          ctx.beginPath();      
          ctx.moveTo(145,0);
          ctx.lineTo(150,0);
          ctx.rotate(Math.PI/30);
          ctx.stroke();
        }

        var date = new Date();
        var hour = date.getHours();
        var minute = date.getMinutes();
        var second = date.getSeconds();
        hour = hour>=12?hour-12:hour;
        
        // create the hour hand
        ctx.restore();
        ctx.lineWidth = 14;
        ctx.strokeStyle="pink";
        ctx.translate(200,200);
        ctx.rotate(hour*(Math.PI/6) + (Math.PI/360)*minute + (Math.PI/21600)*second-Math.PI/2);
        ctx.beginPath();      
        ctx.moveTo(-20,0);
        ctx.lineTo(90,0);     
        ctx.stroke();
       

        // create the minute hand
        ctx.restore();
        ctx.lineWidth = 10;
        ctx.strokeStyle="pink";
        ctx.translate(200,200);
        ctx.rotate((Math.PI/30)*minute + (Math.PI/1800)*second-Math.PI/2);
        ctx.beginPath();      
        ctx.moveTo(-30,0);
        ctx.lineTo(125,0);     
        ctx.stroke();

        // create the second hand
        ctx.restore();
        ctx.lineWidth = 4;
        ctx.strokeStyle="pink";
        ctx.translate(200,200);
        ctx.rotate( (Math.PI/30)*second-Math.PI/2);
        ctx.beginPath();      
        ctx.moveTo(-35,0);
        ctx.lineTo(135,0);     
        ctx.stroke();

        ctx.restore();
        setTimeout(showClock,1000);
    }

showClock();
</script>
</body>
</html>


向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI