zoukankan      html  css  js  c++  java
  • 动画原理——旋转

    书籍名称: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.在有角度的情况下反弹

  • 相关阅读:
    vant toast 样式引入
    Error: Can‘t resolve ‘swiper/css/swiper.css‘ 解决方案
    Vue中使用provide/inject实现页面reload的方法
    vue 中 关于路径 @ 以及 ~的意义
    ValidationError: Invalid options object. Stylus Loader has been initialized using an options object that does not match the API schema.
    vue 去除链接后的#
    centos7安装elk
    Flask+Celery 异步任务
    centos7用zimg搭建图片存储服务器
    centos7部署nginx与vue搭配及403排错
  • 原文地址:https://www.cnblogs.com/winderby/p/4263845.html
Copyright © 2011-2022 走看看