7.5_以扭曲后的帧速率播放动画

本文介绍了一种通过扭曲帧速率实现不同动画效果的技术。利用自定义的时间扭曲函数,可以实现缓入、缓出、弹簧和弹跳等动画效果,同时提供了完整的HTML与JavaScript代码示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

7.5_以扭曲后的帧速率播放动画

<!DOCTYPE html>
<html>
    <head>
        <meta charset="UTF-8">
        <title>以扭曲后的帧速率播放动画</title>
        <style>
            body{
                background: #cdcdcd;
            }
            .controls{
                position: absolute;
                left: 150px;
                top: 10px;
                font: 12px arial;
            }

            #canvas{
                position: absolute;
                left: 0;
                top: 20px;
                margin: 20px;
                border: thin inset rgba(100,150,230,0.8);
                background: #efefef;
            }
            #animateButton{
                margin-left: 15px;
                margin-bottom: 10px;
            }
        </style>
    </head>
    <body>
        <input id="animateButton" type="button" value="Animate" />
        <canvas id="canvas" width="420" height="100" ></canvas>
        <div id="motionControls" class="controls">
            <div id="motionRadios">
                <input type="radio" name="motion" id="linearRadio" checked="" />匀速
                <input type="radio" name="motion" id="easeInRadio"/>缓入
                <input type="radio" name="motion" id="easeOutRadio"/>缓出
                <input type="radio" name="motion" id="easeInOutRadio"/>缓入缓出
            </div>
        </div>
    </body>
    <!-- 精灵对象 -->
    <script>
        var Sprite = function(name,painter,behaviors){
            if(name !== undefined){ this.name = name; }
            if(painter !== undefined){ this.painter = painter; }

            this.top = 0;
            this.left = 0;
            this.width = 10;
            this.height = 10;
            this.velocityX = 0;
            this.velocityY = 0;
            this.visible = true;
            this.animating = false;
            this.behaviors = behaviors || [];

        }

        Sprite.prototype = {
            paint:function(context){
                if(this.painter !== undefined && this.visible){
                    this.painter.paint(this,context);
                }
            },
            update:function(context,time){
                for(var i=0;i<this.behaviors.length;i++){
                    this.behaviors[i].execute(this,context,time);
                }
            }
        }
    </script>
    <!-- 重构后的支持时间轴扭曲功能的AnimationTimer对象(动画计时器) -->
    <script>
        StopWatch = function(){};
        StopWatch.prototype = {
            startTime:0,
            running:false,
            elapsed:undefined,

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

        AnimationTimer = function(duration,timeWarp){
            if(duration !== undefined){
                this.duration = duration;
            };
            if(timeWarp !== undefined){
                this.timeWarp = timeWarp;
            }
            this.stopWatch = new StopWatch();
        }

        AnimationTimer.prototype = {
            default_elastic_passes:3,
            start:function(){
                this.stopWatch.start();
            },
            stop:function(){
                this.stopWatch.stop();
            },
            getElapsedTime:function(){
                var elapsedTime = this.stopWatch.getElapsedTime();
                var percentComplete = elapsedTime/this.duration;

                if(!this.stopWatch.running){
                    return undefined;
                };
                if(this.timeWarp == undefined){
                    return elapsedTime;
                };
                //console.log(this.timeWarp);
                return elapsedTime*(this.timeWarp(percentComplete)/percentComplete);
            },
            isRunning:function(){
                return this.stopWatch.isRunning();
            },
            isOver:function(){
                return this.stopWatch.getElapsedTime() > this.duration;
            },
            //运动形式
            makeEaseIn:function(strength){  //缓入动画
                return function (percentComplete){
                    console.log('进入缓入动画了');
                    return Math.pow(percentComplete,strength*2)
                }
            },
            makeEaseOut:function(strength){
                return function(percentComplete){  //缓出动画
                    console.log('进入缓出动画了');
                    return 1-Math.pow(1-percentComplete,strength*2);
                };
            },
            makeEaseInOut:function(){  //缓入缓出动画
                return function(percentComplete){  //第一个return是timeWarp函数
                    console.log('进入缓入缓出动画了');
                    return percentComplete - Math.sin(percentComplete*2*Math.PI)/(2*Math.PI);
                };
            },
            makeElastic:function(passes){ //弹簧运动
                var passes = passes || this.default_elastic_passes;
                return function(percentComplete){
                    console.log('进入弹簧运动了');
                    return ((1-Math.cos(percentComplete*Math.PI*passes))*(1-percentComplete))+ percentComplete;
                };
            },
            makeBounce:function(bounces){ //弹跳运动
                var fn = this.makeElastic(bounces);
                return function(percentComplete){
                    console.log('进入弹跳运动了');
                    percentComplete = fn(percentComplete);
                    return percentComplete <=1 ?percentComplete:2-percentComplete;
                }
            },
            makeLinear:function(){  //匀速运动
                return function(percentComplete){
                    console.log('进入匀速运动了');
                    return percentComplete;
                }
            }
        };
    </script>
    <!-- 精灵表绘制器 -->
    <script>
        var SpriteSheetPainter = function(cells){
            this.cells = cells || [];
            this.cellIndex = 0;
        }

        SpriteSheetPainter.prototype = {
            advance:function(){
                if(this.cellIndex == (this.cells.length-1)){
                    this.cellIndex = 0;
                }else{
                    this.cellIndex++;
                }
            },
            paint:function(sprite,context){
                var cell = this.cells[this.cellIndex];
                context.drawImage(spritesheet,cell.x,cell.y,cell.w,cell.h,sprite.left,sprite.top,cell.w,cell.h);
            }
        }
    </script>
    <script>
        var canvas = document.getElementById('canvas');
        var context = canvas.getContext('2d');

        var linearRadio = document.getElementById('linearRadio');
        var easeInRadio = document.getElementById('easeInRadio');
        var easeOutRadio = document.getElementById('easeOutRadio');
        var easeInOutRadio = document.getElementById('easeInOutRadio');

        var animateButton = document.getElementById('animateButton');
        var spritesheet = new Image();

        var runnerCells =[ //精灵图表中的每幅图的定位
                        {x:0,y:0,w:47,h:64},
                        {x:55,y:0,w:44,h:64},
                        {x:107,y:0,w:39,h:64},
                        {x:150,y:0,w:46,h:64},
                        {x:208,y:0,w:49,h:64},
                        {x:265,y:0,w:46,h:64},
                        {x:320,y:0,w:42,h:64},
                        {x:380,y:0,w:35,h:64},
                        {x:425,y:0,w:35,h:64}
                    ];
        var interval;
        var lastAdvance = 0;
        var sprite_left = canvas.width - runnerCells[0].w;
        var sprite_top = 10;

        var pageFlip_interval = 100;//换帧频率
        var animation_duration = 3900; //运动的总时长

        //设置一个计时器
        //console.log(AnimationTimer.makeLinear(1));

        var animationTimer = new AnimationTimer(animation_duration);
        animationTimer.timeWarp = animationTimer.makeLinear(1);

        //坐标系
        var left = 1.5;
        var right = canvas.width - runnerCells[0].w;
        var tick_height = 8.5;
        var baseline = canvas.height - 9.5;
        var width = right - left;

        //人物原定换帧
        var runInPlace = {
            execute:function(sprite,context,time){
                var elapsed = animationTimer.getElapsedTime();

                if(lastAdvance ===0){ //跳过第一次
                    lastAdvance = elapsed;
                }else if(lastAdvance!=0 && (elapsed - lastAdvance) >pageFlip_interval){
                    sprite.painter.advance();

                    lastAdvance = elapsed;
                }
            }
        };

        var moveRightToLeft = {
            lastMove : 0,
            reset:function(){
                this.lastMove = 0;
            },
            execute:function(sprite,context,time){
                var elapsed = animationTimer.getElapsedTime();

                if(this.lastMove == 0){
                    console.log(elapsed+'lllll')
                    this.lastMove = elapsed;
                }else{
                    sprite.left -= (elapsed - this.lastMove)/1000*sprite.velocityX;

                    this.lastMove = elapsed;
                }
            }

        };

        var sprite = new Sprite('runner',new SpriteSheetPainter(runnerCells),[moveRightToLeft,runInPlace])


        //初始化
        spritesheet.src = 'img/running-sprite-sheet.png';
        sprite.left = sprite_left;
        sprite.top = sprite_top;
        sprite.velocityX = 100;

        drawAxis();
        spritesheet.onload = function(){
            sprite.paint(context);
        }

        //事件
        animateButton.onclick = function(){
//          if(animateButton.value =='Animate'){
                startAnimation();
//          }else{
//              endAnimation()
//          }
        };
        //变换运动方式
        linearRadio.onclick = function(){
            animationTimer.timeWarp = animationTimer.makeLinear(1);
        }
        easeInRadio.onclick = function(){
            animationTimer.timeWarp = animationTimer.makeEaseIn(1);
        }
        easeOutRadio.onclick = function(){
            animationTimer.timeWarp = animationTimer.makeEaseOut(1);
        }
        easeInOutRadio.onclick = function(){
            animationTimer.timeWarp = animationTimer.makeEaseInOut();
        }

        //函数
        //开始动画
        function startAnimation(){
            animationTimer.start();
            animateButton.style.display = 'none';
            window.requestAnimationFrame(animate);
        }
        //动画结束
        function endAnimation(){
            animateButton.value = 'Animate';
            animateButton.style.display = 'inline';
            animationTimer.stop();
            lastAdvance = 0;
            sprite.painter.cellIndex =0;
            sprite.left = sprite_left;
            moveRightToLeft.reset();

        }
        //执行动画
        function animate(time){
            if(animationTimer.isRunning()){
                context.clearRect(0,0,canvas.width,canvas.height);
                sprite.update(context,time);
                sprite.paint(context);
                drawTimeLine()
                drawAxis();

                if(animationTimer.isOver()){ //计时器到点了,停止动画
                    console.log('动画时间到了,已停止');
                    endAnimation();
                }
                window.requestAnimationFrame(animate);
            }
        }
        //绘制坐标系
        function drawAxis(){
            context.lineWidth = 0.5;
            context.strokeStyle = 'cornflowerblue';
            context.moveTo(left,baseline);
            context.lineTo(right,baseline);
            context.stroke();

            //长间隔线
            for(var i =0 ;i<width;i+=width/20){
                context.beginPath();
                context.moveTo(left+i,baseline - tick_height/2);
                context.lineTo(left+i,baseline+tick_height/2);
                context.stroke();
            }

            //短间隔线
            for(var i =0 ;i<width;i+=width/4){
                context.beginPath();
                context.moveTo(left+i,baseline - tick_height);
                context.lineTo(left+i,baseline+tick_height);
                context.stroke();
            }

            context.beginPath();
            context.moveTo(right,baseline - tick_height);
            context.lineTo(right,baseline+tick_height);
            context.stroke();
        }

        //绘制正常时间线
        function drawTimeLine(){
            var realElapsed = animationTimer.stopWatch.getElapsedTime()
            var realPercent = realElapsed/animation_duration;

            context.lineWidth = 0.5;
            context.strokeStyle = 'rgba(0,0,255,0.5)';
            context.beginPath();
            context.moveTo(width - realPercent*width,0);
            context.lineTo(width - realPercent*width,canvas.height);
            context.stroke();

        }

    </script>
</html>

这里写图片描述

内容概要:本文档详细介绍了在三台CentOS 7服务器(IP地址分别为192.168.0.157、192.168.0.158和192.168.0.159)上安装和配置Hadoop、Flink及其他大数据组件(如Hive、MySQL、Sqoop、Kafka、Zookeeper、HBase、Spark、Scala)的具体步骤。首先,文档说明了环境准备,包括配置主机名映射、SSH免密登录、JDK安装等。接着,详细描述了Hadoop集群的安装配置,包括SSH免密登录、JDK配置、Hadoop环境变量设置、HDFS和YARN配置文件修改、集群启动与测试。随后,依次介绍了MySQL、Hive、Sqoop、Kafka、Zookeeper、HBase、Spark、Scala和Flink的安装配置过程,包括解压、环境变量配置、配置文件修改、服务启动等关键步骤。最后,文档提供了每个组件的基本测试方法,确保安装成功。 适合人群:具备一定Linux基础和大数据组件基础知识的运维人员、大数据开发工程师以及系统管理员。 使用场景及目标:①为大数据平台建提供详细的安装指南,确保各组件能够顺利安装和配置;②帮助技术人员快速掌握Hadoop、Flink等大数据组件的安装与配置,提升工作效率;③适用于企业级大数据平台的建与维护,确保集群稳定运行。 其他说明:本文档不仅提供了详细的安装步骤,还涵盖了常见的配置项解释和故障排查建议。建议读者在安装过程中仔细阅读每一步骤,并根据实际情况调整配置参数。此外,文档中的命令和配置文件路径均为示例,实际操作时需根据具体环境进行适当修改。
在无线通信领域,天线阵列设计对于信号传播方向和覆盖范围的优化至关重要。本题要求设计一个广播电台的天线布局,形成特定的水平面波瓣图,即在东北方向实现最大辐射强度,在正东到正北的90°范围内辐射衰减最小且无零点;而在其余270°范围内允许出现零点,且正西和西南方向必须为零。为此,设计了一个由4个铅垂铁塔组成的阵列,各铁塔上的电流幅度相等,相位关系可自由调整,几何布置和间距不受限制。设计过程如下: 第一步:构建初级波瓣图 选取南北方向上的两个点源,间距为0.2λ(λ为电磁波波长),形成一个端射阵。通过调整相位差,使正南方向的辐射为零,计算得到初始相位差δ=252°。为了满足西南方向零辐射的要求,整体相位再偏移45°,得到初级波瓣图的表达式为E1=cos(36°cos(φ+45°)+126°)。 第二步:构建次级波瓣图 再选取一个点源位于正北方向,另一个点源位于西南方向,间距为0.4λ。调整相位差使西南方向的辐射为零,计算得到相位差δ=280°。同样整体偏移45°,得到次级波瓣图的表达式为E2=cos(72°cos(φ+45°)+140°)。 最终组合: 将初级波瓣图E1和次级波瓣图E2相乘,得到总阵的波瓣图E=E1×E2=cos(36°cos(φ+45°)+126°)×cos(72°cos(φ+45°)+140°)。通过编程实现计算并绘制波瓣图,可以看到三个阶段的波瓣图分别对应初级波瓣、次级波瓣和总波瓣,最终得到满足广播电台需求的总波瓣图。实验代码使用MATLAB编写,利用polar函数在极坐标下绘制波瓣图,并通过subplot分块显示不同阶段的波瓣图。这种设计方法体现了天线阵列设计的基本原理,即通过调整天线间的相对位置和相位关系,控制电磁波的辐射方向和强度,以满足特定的覆盖需求。这种设计在雷达、卫星通信和移动通信基站等无线通信系统中得到了广泛应用。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值