• 动画原理——旋转


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

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

    1.简单旋转

    利用三角函数进行旋转

    01-rotate-1.html

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Rotate 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(),
              vr = 0.05,
              angle = 0,
              radius = 150,
              centerX = canvas.width / 2,
              centerY = canvas.height / 2;
          
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
            
            ball.x = centerX + Math.cos(angle) * radius;
            ball.y = centerY + Math.sin(angle) * radius;
            angle += vr;
            
            ball.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    2.高级旋转

    这种方法是根据当前的角度和坐标,计算下一帧的角度和坐标
    02-rotate-2.html

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Rotate 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(),
              vr = 0.05,
              cos = Math.cos(vr),
              sin = Math.sin(vr),
              centerX = canvas.width / 2,
              centerY = canvas.height / 2;
          
          ball.x = Math.random() * canvas.width;
          ball.y = Math.random() * canvas.height;
    
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
            
            var x1 = ball.x - centerX,
                y1 = ball.y - centerY,
                x2 = cos * x1 - sin * y1,
                y2 = cos * y1 + sin * x1;
            
            ball.x = centerX + x2;
            ball.y = centerY + y2;
            
            ball.draw(context);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    多个物体

    03-rotate-3.html

    <!doctype html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Rotate 3</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 left or right on canvas element.</aside>
    
        <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'),
              mouse = utils.captureMouse(canvas),
              balls = [],
              numBalls = 10,
              vr = 0.05,
              centerX = canvas.width / 2,
              centerY = canvas.height / 2,
              cos, sin; //accessible by move and drawFrame
    
          for (var ball, i = 0; i < numBalls; i++) {
            ball = new Ball();
            ball.x = Math.random() * canvas.width;
            ball.y = Math.random() * canvas.height;
            balls.push(ball);
          }
    
          function move (ball) {
            var x1 = ball.x - centerX,
                y1 = ball.y - centerY,
                x2 = cos * x1 - sin * y1,
                y2 = cos * y1 + sin * x1;
            ball.x = centerX + x2;
            ball.y = centerY + y2;
          }
          
          function draw (ball) {
            ball.draw(context);
          }
    
          (function drawFrame () {
            window.requestAnimationFrame(drawFrame, canvas);
            context.clearRect(0, 0, canvas.width, canvas.height);
            
            var angle = (mouse.x - centerX) * 0.0005;
            cos = Math.cos(angle);
            sin = Math.sin(angle);
            balls.forEach(move);
            balls.forEach(draw);
          }());
        };
        </script>
      </body>
    </html>
    View Code

    3.在有角度的情况下反弹

  • 相关阅读:
    基于 cobbler 实现自动安装 linux 系统
    自动安装 linux 系统
    从12306网站新验证码看Web验证码设计与破解
    用java实现删除文件夹里的所有文件
    本机访问其它电脑上的oracle数据库
    powerdesigner 15 如何导出sql schema
    使用PowerDesigner建立数据库模型
    com.google.gson.stream.MalformedJsonException的解决办法
    Spring注解详解
    nginx中文手册内容说明
  • 原文地址:https://www.cnblogs.com/winderby/p/4263845.html
Copyright © 2020-2023  润新知