zoukankan      html  css  js  c++  java
  • 动画原理——速率

    书籍名称:HTML5-Animation-with-JavaScript

    书籍源码:https://github.com/lamberta/html5-animation

    1.在某一方向的方向的速率

    本例掩饰水平的vx

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Velocity 1</title>
        <link rel="stylesheet" href="../include/style.css">
      </head>
      <body>
        <header>
          Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
        </header>
        <canvas id="canvas" width="400" height="400"></canvas>
    
        <script src="../include/utils.js"></script>
        <script src="./classes/ball.js"></script>
        <script>
        window.onload = function () {
          var canvas = document.getElementById('canvas'),
              context = canvas.getContext('2d'),
              ball = new Ball(),
              vx = 1;
    
          ball.x = 50;
          ball.y = 100;
    
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
              
            ball.x += vx;
            ball.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    2.在两个方向的速率

    和第一个差不多,但有两个速率.

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Velocity 2</title>
        <link rel="stylesheet" href="../include/style.css">
      </head>
      <body>
        <header>
          Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
        </header>
        <canvas id="canvas" width="400" height="400"></canvas>
    
        <script src="../include/utils.js"></script>
        <script src="./classes/ball.js"></script>
        <script>
        window.onload = function () {
          var canvas = document.getElementById('canvas'),
              context = canvas.getContext('2d'),
              ball = new Ball(),
              vx = 1,
              vy = 1;
    
          ball.x = 50;
          ball.y = 100;
            
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
    
            ball.x += vx;
            ball.y += vy;
            ball.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    3.用三角函数生成的各个方向的速率

    根据向量加法,和三角函数可以将某个方向的速率分解成两个方向。

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Velocity Angle</title>
        <link rel="stylesheet" href="../include/style.css">
      </head>
      <body>
        <header>
          Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
        </header>
        <canvas id="canvas" width="400" height="400"></canvas>
    
        <script src="../include/utils.js"></script>
        <script src="./classes/ball.js"></script>
        <script>
        window.onload = function () {
          var canvas = document.getElementById('canvas'),
              context = canvas.getContext('2d'),
              ball = new Ball(),
              angle = 45,
              speed = 1;
    
          ball.x = 50;
          ball.y = 100;
    
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
    
            var radians = angle * Math.PI / 180,
                vx = Math.cos(radians) * speed,
                vy = Math.sin(radians) * speed;
    
            ball.x += vx;
            ball.y += vy;
            ball.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    4.箭头跟随程序

    关键点:1.根据箭头的位置和鼠标的位置判断箭头倾斜的角度。

    2.根据角度将速率分解X,Y方向的速率。

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Follow Mouse 1</title>
        <link rel="stylesheet" href="../include/style.css">
      </head>
      <body>
        <header>
          Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
        </header>
        <canvas id="canvas" width="400" height="400"></canvas>
        <aside>Move mouse on canvas element.</aside>
    
        <script src="../include/utils.js"></script>
        <script src="./classes/arrow.js"></script>
        <script>
        window.onload = function () {
          var canvas = document.getElementById('canvas'),
              context = canvas.getContext('2d'),
              mouse = utils.captureMouse(canvas),
              arrow = new Arrow(),
              speed = 3;
            
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
    
            var dx = mouse.x - arrow.x,
                dy = mouse.y - arrow.y,
                angle = Math.atan2(dy, dx),
                vx = Math.cos(angle) * speed,
                vy = Math.sin(angle) * speed;
    
            arrow.rotation = angle; //radians
            arrow.x += vx;
            arrow.y += vy;
            arrow.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    5.旋转

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Rotational Velocity</title>
        <link rel="stylesheet" href="../include/style.css">
      </head>
      <body>
        <header>
          Example from <a href="http://amzn.com/1430236655?tag=html5anim-20"><em>Foundation HTML5 Animation with JavaScript</em></a>
        </header>
        <canvas id="canvas" width="400" height="400"></canvas>
    
        <script src="../include/utils.js"></script>
        <script src="./classes/arrow.js"></script>
        <script>
        window.onload = function () {
          var canvas = document.getElementById('canvas'),
              context = canvas.getContext('2d'),
              mouse = utils.captureMouse(canvas),
              arrow = new Arrow(),
              vr = 2; //degrees
    
          arrow.x = canvas.width / 2;
          arrow.y = canvas.height / 2;
    
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
    
            arrow.rotation += vr  * Math.PI / 180; //to radians
            arrow.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code
  • 相关阅读:
    用pygame实现打飞机游戏-3-显示飞机和控制飞机移动
    用pygame实现打飞机游戏-2-检测键盘
    最好听的钢琴曲排行榜 世界上最好听的钢琴曲
    使用gulp构建nodejs,你只需要记住5个函数
    Linux删除文件夹命令
    前端构建工具gulpjs的使用介绍及技巧
    HTML5 LocalStorage 本地存储
    jquery新窗口打开链接
    Sublime text 3 如何格式化HTML代码
    jquery滚动条加载数据
  • 原文地址:https://www.cnblogs.com/winderby/p/4253121.html
Copyright © 2011-2022 走看看