【问题标题】:Animating images in HTML5 and canvas在 HTML5 和画布中动画图像
【发布时间】:2012-04-05 05:18:38
【问题描述】:

我需要帮助以使用 HTML5 和 JavaScript 将图像浮动到画布上。我一直在谷歌搜索,没有找到任何东西。我可以在屏幕上绘制形状,但我不知道如何为它们设置动画。 我希望几个不同的图像从不同的方向漂浮在画布上。 有人可以帮我吗?经过 4 个小时的谷歌搜索,我能做的就是这个

<script type = "Text/JavaScript">
                function Animate(){
                var canvas=document.getElementById("myCanvas"); 
                var ctx=canvas.getContext("2d"); 

                var img = new Image();

                img.onload = function ()
                {
                    ctx.drawImage(img,20,20,300,300);

                };
                img.src = "vb.png";
                }
             </script> 
    </head> 
<body> 
<canvas id="myCanvas" height="200" width="800"></canvas>
<br><button onclick="Animate();">Restart</button>

似乎有很多关于动画形状的教程,但我想加载我自己的图片并将它们飞到画布上。

【问题讨论】:

标签: javascript image html animation canvas


【解决方案1】:

试试这个非常基本的画布动画演示:

http://jsfiddle.net/bDQ6b/2/

window.addEventListener('load', function () {
  var
    img = new Image,
    ctx = document.getElementById('myCanvas').getContext('2d');

  img.src = 'http://www.gravatar.com/avatar/a1f80339c0cef95be6dc73e0ac510d5d?s=32&d=identicon&r=PG';
  img.addEventListener('load', function () {

    var interval = setInterval(function() {
      var x = 0, y = 0;

      return function () {
        ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
        ctx.drawImage(img, x, y);

        x += 1;
        if (x > ctx.canvas.width) {
          x = 0;
        }
      };
    }(), 1000/40);
  }, false);
}, false);

不过,还有很多事情可以做得更好。例如:

  • 使用requestAnimationFrame而不是间隔

  • 以更方便的方式预加载图像

  • 使用速度和时间差(从最后一帧到当前帧)而不是固定增量

  • 还有更多

但是,由于所有这些都会使示例变得过于复杂,因此我将保持原样,并希望您在学习时能够阅读这些主题。

【讨论】:

    【解决方案2】:

    要使用画布制作动画,您需要记录对象的位置,然后在新帧上增加它@9​​87654321@ 允许您在指定的时间间隔后运行函数。您可以使用此函数在每次呈现新帧时更新对象在页面上的位置。

    例如:

    function draw() { playersShip.move(); }

    移动函数增加或减少对象的 x 和/或 y 坐标的位置。此行在指定坐标处绘制指定图像(每帧渲染时更新)。

    ctx.drawImage(shipImage, playersShip.position.x, playersShip.position.y);

    如果您要构建游戏或类似游戏,最好将对象的运动与帧速率隔离开来。如果您需要,我可以提供更深入的示例。

    使用这个想法,您应该能够创建图像的动画。

    【讨论】:

    • 如何将运动与 fps 隔离开来?
    【解决方案3】:

    这是 HTML5 画布上画布内的简单弹跳球动画示例。

     <body>
    <canvas id="Canvas01" width="400" height="400" style="border:5px solid #FF9933; margin-left:10px; margin-top:10px;"></canvas>
    
    <script>
      var canvas = document.getElementById('Canvas01');
      var ctx = canvas.getContext('2d');
      var p = {x:25, y:25};
      var velo=6, corner=30, rad=20;
      var ball={x:p.x, y:p.y};
    
      var moveX=Math.cos(Math.PI/180*corner) * velo;
      var moveY=Math.sin(Math.PI/180*corner) * velo;
    
    
      function DrawMe() {
      ctx.clearRect(0,0,canvas.width, canvas.height);
    
      if(ball.x>canvas.width-rad || ball.x<rad) moveX=-moveX;
      if(ball.y>canvas.height-rad || ball.y<rad) moveY=-moveY;
    
        ball.x +=moveX;
        ball.y +=moveY;
    
      ctx.beginPath();
      ctx.fillStyle = 'blue';
      ctx.arc(ball.x, ball.y, rad, 0 , Math.PI*2,false);
      ctx.fill();
      ctx.closePath();
      }
    
      setInterval(DrawMe,30);
    
     </script>
    </body>
    

    您可以在这里自己尝试:http://codetutorial.com/examples-canvas/canvas-examples-bounce-ball

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-08-16
      • 1970-01-01
      • 2014-12-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-25
      • 1970-01-01
      相关资源
      最近更新 更多