ECMAScript6 面向对象 时钟效果

<!DOCTYPE html>
<html>
<head lang="en">
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width,initial-scale=1.0">
    <title></title>
    <style>
        body{
            text-align:center;
        }
        #canvas{
            border:1px solid #ccc;
        }
    </style>
</head>
<body>
<canvas id="canvas" width="1300" height="600"></canvas>
<script src="js/digit.js" type="text/javascript"></script>
<!--<script src="http://google.github.io/traceur-compiler/bin/traceur.js" type="text/javascript"></script>-->
<!--<script src="http://google.github.io/traceur-compiler/src/bootstrap.js" type="text/javascript"></script>-->
<script src="js/traceur.js"></script>
<script src="js/bootstrap.js"></script>
<script type="module">
    class clock{
    constructor(id,nowDate,x,y,r){
        this.canvas=document.getElementById(id);
        this.context=this.canvas.getContext("2d");
        this.x= x;
        this.y=y;
        this.R=r;
        this.balls=[];
        this.colors=["#008000","#FF6600","#f92672","#e67e22","#960050","#aaffaa","#ae81ff","#a3dbec","#c7254e","#00A000"];
        this.hours= 0;
        this.minutes= 0;
        this.seconds= 0;
        this.lastSeconds= nowDate.getSeconds();
        this.lastMinutes=  nowDate.getMinutes();
        this.lastHours=nowDate.getHours();
    }

    init() {
        let nowDate = new Date();
        this.hours = nowDate.getHours();
        this.minutes = nowDate.getMinutes();
        this.seconds = nowDate.getSeconds();
        this.update();
        this.time();
    }

    time() {
        if (this.seconds != this.lastSeconds) {
            if (parseInt(this.seconds / 10) != parseInt(this.lastSeconds / 10)) {
                this.addBalls(this.x + 79 * (this.R + 1), this.y, parseInt(this.seconds / 10));
            }
            if (parseInt(this.seconds % 10) != parseInt(this.lastSeconds % 10)) {
                this.addBalls(this.x + 96 * (this.R + 1), this.y, parseInt(this.seconds % 10));
            }
            this.lastSeconds = this.seconds;
        }
        if (this.minutes != this.lastMinutes) {
            if (parseInt(this.minutes / 10) != parseInt(this.lastMinutes / 10)) {
                this.addBalls(this.x + 38 * (this.R + 1), this.y, parseInt(this.minutes / 10));
            }
            if (parseInt(this.minutes % 10) != parseInt(this.lastMinutes % 10)) {
                this.addBalls(this.x + 55 * (this.R + 1), this.y, parseInt(this.minutes % 10));
            }
            this.lastMinutes = this.minutes;
        }
        if (this.hours != this.lastHours) {
            if (parseInt(this.hours / 10) != parseInt(this.lastHours / 10)) {
                this.addBalls(this.x, this.y, parseInt(this.hours / 10));
            }
            if (parseInt(this.hours % 10) != parseInt(this.lastHours % 10)) {
                this.addBalls(this.x + 15 * (this.R + 1), this.y, parseInt(this.hours % 10));
            }
            this.lastHours =this.hours;
        }

        this.updateBall();
        this.clearBall();
    }

    update(){
        this.context.clearRect(0,0,this.canvas.width,this.canvas.height);

        this.drawArc(this.x,this.y,parseInt(this.hours/10));
        this.drawArc(this.x+15*(this.R+1),this.y,this.hours%10);
        this.drawArc(this.x+29*(this.R+1),this.y,10);
        this.drawArc(this.x+38*(this.R+1),this.y,parseInt(this.minutes/10));
        this.drawArc(this.x+55*(this.R+1),this.y,this.minutes%10);
        this.drawArc(this.x+70*(this.R+1),this.y,10);

        this.drawArc(this.x+79*(this.R+1),this.y,parseInt(this.seconds/10));
        this.drawArc(this.x+96*(this.R+1),this.y,this.seconds%10);

        for(let i= 0;i<this.balls.length;i++) {
            this.context.beginPath();
            this.context.arc(this.balls[i].x, this.balls[i].y, this.R, 0, 2 * Math.PI, true);
            this.context.fillStyle = this.balls[i].color;
            this.context.closePath();
            this.context.fill();
        }
    }

    drawArc(sx,sy,num){
        for(let i=0;i<digit[num].length;i++){
            for(let j=0;j<digit[num][i].length;j++) {
                if (digit[num][i][j] == 1) {
                    let centerX = sx + j * 2 * (this.R + 1) + (this.R + 1);
                    let centerY = sy + i * 2 * (this.R + 1) + (this.R + 1);
                    this.context.beginPath();
                    this.context.arc(centerX, centerY, this.R, 0, 2 * Math.PI, true);
                    this.context.fillStyle = "#445588";
                    this.context.closePath();
                    this.context.fill();
                }
            }
        }
    }

    addBalls(sx,sy,num){
        for(let i=0;i<digit[num].length;i++){
            for(let j=0;j<digit[num][i].length;j++) {
                if (digit[num][i][j] == 1) {
                    var centerX = sx + j * 2 * (this.R + 1) + (this.R + 1);
                    var centerY = sy + i * 2 * (this.R + 1) + (this.R + 1);
                    var ball = {
                        x: centerX,
                        y: centerY,
                        g: 3.5 + Math.random(),
                        vx: Math.pow(-1, Math.ceil(Math.random() * 1000)) * 4,
                        vy: -2,
                        color: this.colors[Math.floor(Math.random() * this.colors.length)]
                    };
                    this.balls.push(ball);
                }
            }
        }
    }

    updateBall(){
        for(let i= 0;i<this.balls.length;i++) {
            this.balls[i].x+=this.balls[i].vx;
            this.balls[i].y+=this.balls[i].vy;
            this.balls[i].vy+=this.balls[i].g;
            if(this.balls[i].y>=this.canvas.height-this.R) {
                this.balls[i].y = this.canvas.height - this.R;
                this.balls[i].vy = -this.balls[i].vy * 0.75;
            }
        }
    }

    clearBall() {
        let cnt = 0;
        for (let i = 0; i < this.balls.length; i++) {
            if (this.balls[i].x + this.R > 0 && this.balls[i].x - this.R < this.canvas.width) {
                this.balls[cnt++] = this.balls[i];
            }
        }
        while (this.balls.length > cnt) {
            this.balls.pop();
        }
    }
}

var clock1=new clock("canvas",new Date(),250,100,5);
clock1.init();
setInterval(function(){
    clock1.init();
},50);
</script>
</body>
</html>

  

posted on 2016-04-15 14:25  Tadpole  阅读(146)  评论(0编辑  收藏  举报

导航