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

    var canvas = document.getElementById("canvas");

    var ctx = canvas.getContext('2d');
    var rem = 500 / 200;

    function clock() {
    ctx.clearRect(0, 0, 500, 500)
    var now = new Date();
    var second = now.getSeconds();
    var minute = now.getMinutes();
    var hour1 = now.getHours();
    var hour = hour1 + minute / 60;
    hour = hour > 12 ? hour - 12 : hour;

    var year = now.toLocaleDateString()
    var time = now.toLocaleTimeString()
    ctx.font = "18px Arial"
    ctx.fillText(year + time, 160, 200)


    //表盘
    ctx.beginPath()
    ctx.lineWidth = '8'
    var gradient = ctx.createLinearGradient(0, 0, 500, 0);
    gradient.addColorStop(0, "red");
    gradient.addColorStop(0.5, "yellow");
    gradient.addColorStop(1, "blue");
    ctx.arc(250, 250, 200, 0, Math.PI * 2)
    ctx.strokeStyle = gradient
    ctx.stroke()
    ctx.closePath()

    // fun()
    //时针

    for (var i = 0; i < 12; i++) {
    ctx.save()
    ctx.strokeStyle = "black"
    ctx.translate(250, 250)
    ctx.rotate(30 * Math.PI / 180 * i)
    ctx.beginPath()
    ctx.lineWidth = 4
    ctx.moveTo(0, -196)
    ctx.lineTo(0, -180)

    // ctx.font = '25px Arial bold';
    // if (i == 0) {
    // ctx.fillText(12, -12, -150)
    // } else {
    // ctx.fillText(i, -12, -150)
    // }


    ctx.stroke()
    ctx.closePath()
    ctx.restore()
    // ctx.translate(250, 250)

    }

    //60 刻度
    for (var i = 0; i < 60; i++) {
    ctx.save()

    ctx.strokeStyle = "black"
    ctx.translate(250, 250)
    ctx.rotate(6 * Math.PI / 180 * i)
    ctx.beginPath()
    ctx.lineWidth = 2

    ctx.moveTo(0, -196)
    ctx.lineTo(0, -188)
    ctx.stroke()
    ctx.closePath()
    ctx.restore()
    }


    ctx.save();
    ctx.lineWidth = 3;
    ctx.strokeStyle = "black";
    ctx.translate(250, 250);
    ctx.rotate(Math.PI / 180 * 30 * hour)
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(0, -80);
    ctx.closePath();
    ctx.stroke();
    ctx.restore()

    ctx.save();
    ctx.lineWidth = 2;
    ctx.strokeStyle = "red"
    ctx.translate(250, 250);
    ctx.rotate(Math.PI / 180 * 6 * minute)
    ctx.beginPath();
    ctx.moveTo(-2, 0);
    ctx.lineTo(0, -130);
    ctx.closePath();
    ctx.stroke();
    ctx.restore()
    ctx.save();

    ctx.lineWidth = 1;
    ctx.strokeStyle = "pink";
    ctx.translate(250, 250);
    ctx.rotate(Math.PI / 180 * 6 * second)
    ctx.beginPath();
    ctx.moveTo(0, 0);
    ctx.lineTo(0, -150);
    ctx.closePath();
    ctx.stroke();
    ctx.restore()


    ctx.beginPath()
    ctx.strokeStyle = 'skyblue';
    ctx.arc(251, 250, 1, 0, Math.PI * 2)
    ctx.closePath()
    ctx.stroke()

    ctx.save()
    ctx.translate(250, 250)
    fun()
    ctx.restore()
    }
    clock()

    //数字刻度

    function fun() {
    var arr = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 1, 2]
    ctx.font = "40px Arial";
    ctx.textAlign = 'center';
    ctx.textBaseline = 'middle';
    for (var j = 0; j < arr.length; j++) {
    var rad = Math.PI * 2 / 12 * j;
    var x = Math.cos(rad) * (160);
    var y = Math.sin(rad) * (160);
    ctx.fillText(arr[j], x, y);
    }
    }

    setInterval(clock, 1000)

  • 相关阅读:
    firefox ajax async 弹出窗口提示阻止
    Spring加载resource时classpath*:与classpath:的区别(转)
    超链接的href属性 js调用
    jquery easyui tabs layout 记录
    PostgreSQL 中的递归查询 与oracle 的比较
    将字符串中的中文(英文)字符串转化为阿拉伯数字
    用CSS控制文字的宽度,超过省略号代替(转)
    制作gridview 固定列的2种简单解决方案
    自制树型控件(DataGrid) 支持多种响应
    备忘: select 对象的操作js(转)
  • 原文地址:https://www.cnblogs.com/zhanghaifeng123/p/12105133.html
Copyright © 2011-2022 走看看