美文网首页
canvas时钟面向对象方法

canvas时钟面向对象方法

作者: 土豪码农 | 来源:发表于2017-03-23 23:13 被阅读0次

用canvas绘制一个时钟是件非常简单的事情,自己尝试用下面向对象的方式去绘制时钟,会使这段代码的复用性更好.

这是html的文件

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<canvas width="900" height="600" id="canvas" style="border: 1px solid #000;"></canvas>
<script src="js/bell.js"></script>
<script>
    var canvas = document.getElementById("canvas");
    var context = canvas.getContext("2d");
    function move() {
        context.clearRect(0,0,canvas.width,canvas.height);
        var bell = new Bell({
            time : new Date(),
            bellR:300 //在这里输入时钟半径,可以决定时钟的大小
        });
        bell.render(context,canvas);
        setTimeout(move,1000);
    }
    move();
    
</script>
</body>
</html>

这是js的文件

//首先定义一个构造函数
function Bell(option) {
    this._init(option);
}

//设置这个构造函数的原型对象
Bell.prototype = {
    constructor:Bell,
    //接受参数
    _init: function (option) {
        this.sec = option.time.getSeconds()*6 ||0;
        this.min = option.time.getMinutes()*6+this.sec/60 ||0;
        this.hour = option.time.getHours()%12*30+this.min/60*5 ||0;
        this.bellR = option.bellR ||0;
    },
    //绘制时钟
    render: function (context,canvas) {
        var cw = canvas.width;
        var ch = canvas.height;
        var r = this.bellR;
        //钟框
        context.arc(cw/2,ch/2,r,0,Math.PI*2);
        context.strokeStyle = "pink";
        context.lineWidth = 5;
        context.stroke();

        ////////////////////以下元素都是按照钟的半径的比例设置的/////////////////////

        //刻度
        for (var i = 0; i < 60; i++) {
            if(i%5==0){
                context.save();
                context.beginPath();
                context.translate(cw/2,ch/2);
                context.rotate(i*6*Math.PI/180);
                context.fillStyle = "black";
                context.fillRect(r*0.75-r/30,-r/60,r/30*2,r/30);
                context.restore();
            }else{
                context.save();
                context.beginPath();
                context.translate(cw/2,ch/2);
                context.rotate(i*6*Math.PI/180);
                context.fillStyle = "black";
                context.fillRect(r*0.75-r/60,-r/60,r/30,r/60);
                context.restore();
            }
        }


        //时针
        context.save();
        context.beginPath();
        context.translate(cw/2,ch/2);
        context.rotate((this.hour-90)*Math.PI/180);
        context.fillStyle = "black";
        context.fillRect(-r/30,-r/30,r/3*2,r/30*2);
        context.restore();

        //分针
        context.save();
        context.beginPath();
        context.translate(cw/2,ch/2);
        context.rotate((this.min-90)*Math.PI/180);
        context.fillStyle = "black";
        context.fillRect(-r/60,-r/60,r/3*2,r/30);
        context.restore();

        //秒针

        context.save();
        context.beginPath();
        context.translate(cw/2,ch/2);
        context.rotate((this.sec-90)*Math.PI/180);
        context.fillStyle = "red";
        context.fillRect(-r/120,-r/120,r/3*2,r/60);
        context.restore();
    }
};

这个时钟默认出现在画布的中央,根据你输入的时钟半径会有大小不同的时钟出现

可以这么小

bell1.png

可以这么大

bell2.png

画布是一个非常强大且有意思的工具,还要多加学习

相关文章

  • canvas时钟面向对象方法

    用canvas绘制一个时钟是件非常简单的事情,自己尝试用下面向对象的方式去绘制时钟,会使这段代码的复用性更好. 这...

  • 构造函数&&类--canvas小球碰撞

    1. 构造函数 canvas小球碰撞 2. 类 canvas小球碰撞 有上可以看出,用类写面向对象的方法,...

  • 02Canvas

    【canvas】 《2.6.5 面向对象基础复习补充:》 创建对象的方式: * var o = { name: '...

  • 类的继承

    继承是面向对象编程的三大特征之一,也是面向对象编程中代码复用的重要方法。在前文中分析的图形化时钟程序时,仅定义了一...

  • canvas

    canvas动画思想 canvas 面向对象思想 绘制矩形、直线 填充矩形 矩形边框 擦除画布 绘制路径(逆时针绘...

  • 面向对象

    面向对象方法概论 对象、结构 概念呢 特征 1.1 什么是面向对象 面向对象方法的基本思想 对象 属性-静态特征 ...

  • JS高级

    1.面向对象的概念 1.1什么是面向对象: 1 .面向对象是一种思维方法2.面向对象是一种编程方法3.面向对象并不...

  • 2月9日

    canvas实现炫酷小时钟(二) ctx = myCanvas.getcontext('2d'); 获取上下文对象...

  • 面向对象与构造函数

    面向对象是一种思维方法 面向对象是一种编程方法 面向对象并不只针对某一种编程语言 什么是面向对象? 面向对...

  • 完全面向对象的canvas画图程序

    面向对象的canvas画图程序 项目简介 整个项目分为两大部分 场景场景负责canvas控制,事件监听,动画处理 ...

网友评论

      本文标题:canvas时钟面向对象方法

      本文链接:https://www.haomeiwen.com/subject/awqxottx.html