zoukankan      html  css  js  c++  java
  • canvas 时钟

    jsp代码

    <!DOCTYPE html>
    <html lang="en">
    <head>
    <meta charset="UTF-8">
    <title>Document</title>
    </head>
    <body>
    <canvas id="drawing"></canvas>
    <script>
    var drawing = document.getElementById('drawing');
    if(drawing.getContext){
    var cxt = drawing.getContext('2d');
    var W = drawing.width = 200;
    var H = drawing.height = 200;
    var R = W / 2;
    var cw = cxt.lineWidth = 0.1*R;
    function drawStatics(){
    cxt.save();
    cxt.translate(R,R);
    cxt.beginPath();
    cxt.lineWidth = 0.1*R;
    cxt.arc(0,0,R-cw/2,0,2*Math.PI,false);
    cxt.stroke();

    cxt.beginPath();
    cxt.font = 0.2 * R + 'px 宋体';
    cxt.textAlign = 'center';
    cxt.textBaseline = 'middle';
    var r1 = 0.8*R - cw/2;
    for(var i = 12; i > 0; i--){
    var radius = 2*Math.PI/12 * i + 1.5*Math.PI;
    var x = Math.cos(radius) * r1;
    var y = Math.sin(radius) * r1;
    cxt.fillText(i,x,y);
    }

    cxt.beginPath();
    var r2 = 0.9*R - cw/2;
    for(var i = 0; i < 60; i++){
    var radius = 2*Math.PI/60*i + 1.5*Math.PI;
    var x = Math.cos(radius) * r2;
    var y = Math.sin(radius) * r2;
    cxt.beginPath();
    if(i%5 === 0){
    cxt.arc(x,y,cw/5,0,2*Math.PI,false);
    }else{
    cxt.arc(x,y,cw/8,0,2*Math.PI,false);
    }
    cxt.fill();
    }
    cxt.restore();
    }
    function drawDot(){
    cxt.save();
    cxt.translate(R,R);
    cxt.beginPath();
    cxt.arc(0,0,cw/3,0,2*Math.PI,false);
    cxt.fillStyle = '#fff';
    cxt.fill();
    cxt.restore();
    }
    function drawSecond(second){
    cxt.save();
    cxt.translate(R,R);
    cxt.beginPath();
    var radius = 2*Math.PI/60 * second;
    cxt.rotate(radius);
    cxt.lineWidth = 2;
    cxt.moveTo(0,cw*2);
    cxt.lineTo(0,-0.8*R);
    cxt.strokeStyle = 'red';
    cxt.stroke();
    cxt.restore();
    }
    function drawMinute(minute,second){
    cxt.save();
    cxt.translate(R,R);
    cxt.beginPath();
    var radius = 2*Math.PI/60 * minute;
    var sRaiuds = 2*Math.PI/60/60 * second;
    cxt.rotate(radius + sRaiuds);
    cxt.lineWidth = 4;
    cxt.lineCap = 'round';
    cxt.moveTo(0,cw);
    cxt.lineTo(0,-(0.8*R - cw/2));
    cxt.stroke();
    cxt.restore();
    }
    function drawHour(hour,minute){
    cxt.save();
    cxt.translate(R,R);
    cxt.beginPath();
    var radius = 2*Math.PI/12 * hour;
    var mRaiuds = 2*Math.PI/12/60 * minute;
    cxt.rotate(radius + mRaiuds);
    cxt.lineWidth = 6;
    cxt.lineCap = 'round';
    cxt.moveTo(0,cw/2);
    cxt.lineTo(0,-(0.8*R - cw*2));
    cxt.stroke();
    cxt.restore();
    }
    function draw(){
    cxt.clearRect(0,0,W,H);
    drawStatics();
    var now = new Date();
    drawHour(now.getHours(),now.getMinutes());
    drawMinute(now.getMinutes(),now.getSeconds());
    drawSecond(now.getSeconds());
    drawDot();
    }
    draw();

    设置函数循环运行时间单位 (ms)
    setInterval(draw,500);
    }
    </script>
    </body>
    </html>

  • 相关阅读:
    [luoguP2770] 航空路线问题(最小费用最大流)
    javascript技巧大全套
    jLim
    自己封装的Ajax
    JavaScript函数调用规则
    JavaScript模板引擎使用
    JavaScript MD5
    JavaScript SHA-1
    JavaScript Map对象的实现
    javascript中的_return_false和return_true
  • 原文地址:https://www.cnblogs.com/dingjiuping/p/7463925.html
Copyright © 2011-2022 走看看