5.10.2_动画计时器

5.10.2_动画计时器

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>动画计时器</title>
        <style>
            body{
                background: #fff;
            }
            #canvas{
                background: #eee;
            }
            #controls{
                position: absolute;
                left: 25px;
                top: 25px;
            }
        </style>
    </head>
    <body>
        <div id="controls">
            <input id="startStopButton" type="button" value="开始"  />
        </div>
        <canvas id="canvas" width="1000" height="600"></canvas>
    </body>
    <!-- 秒表的构造函数 -->
    <script>
        StopWatch = function(){};
        StopWatch.prototype = {
            startTime: 0,
            running: false,
            elapsed: undefined, //过去的时间

            start:function(){
                this.startTime = +new Date();
                this.elapsed = undefined;
                this.running = true;
            },
            stop:function(){
                this.elapsed = (+new Date()) - this.startTime;
                this.running = false;
            },
            getElapsedTime:function(){
                if(this.running){
                    return (+new Date()) - this.startTime;
                }else{
                    return this.elapsed;
                }
            },
            isRunning:function(){
                return this.running;
            },
            reset:function(){
                this.elapsed = 0;
            }

        }

        AnimationTimer = function(duration){
            this.duration = duration;
        }

        AnimationTimer.prototype ={
            duration:undefined,
            stopwatch:new StopWatch(),

            start:function(){
                this.stopwatch.start();
            },
            stop:function(){
                this.stopwatch.stop();
            },
            getElapsedTime:function(){
                var elapsedTime = this.stopwatch.getElapsedTime();
//              if(!this.stopwatch.running){
//                  return undefined;
//              }else{
                    return elapsedTime;
//              }
            },
            isRunning:function(){
                return this.stopwatch.isRunning();
            },
            isOver:function(){
                return this.stopwatch.getElapsedTime()>this.duration;
            }
        }
    </script>
    <script>
        var canvas = document.getElementById('canvas'),
            context = canvas.getContext('2d'),
            startStopButton = document.getElementById('startStopButton'),
            animationTimer = new AnimationTimer(5000);

        startStopButton.onclick = function(){
            if(this.value == '开始'){
                this.value = '结束';
                animationTimer.start();
                requestAnimationFrame(animate);
            }else{
                this.value = '开始';
                animationTimer.stop();

            }
        }
        function animate(){
            if(animationTimer.isRunning()&&!animationTimer.isOver()){
                console.log('动画还在持续中,当前已进行了'+animationTimer.getElapsedTime());
                requestAnimationFrame(animate);
            }else if(animationTimer.isOver()){
                animationTimer.stop();
                startStopButton.value = '开始';
                console.log('自动结束了,时间为'+animationTimer.getElapsedTime());
            }else{

                console.log('手动停止了动画,停止时已运行动画时间为'+animationTimer.getElapsedTime());
            }
        }
    </script>
</html>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值