繁体   English   中英

在HTML5和画布中动画图像

[英]Animating images in HTML5 and canvas

我需要帮助在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>

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

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

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而不是间隔

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

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

  • 还有很多

但是,由于所有这些都会使示例过于复杂,我会保持原样并希望您在学习的同时阅读这些主题。

以下是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

要使用画布动画,您需要记录对象的位置,然后在新帧setInterval(draw, 1000 / 25);上增加它setInterval(draw, 1000 / 25); 允许您在指定的时间间隔后运行函数。 每次渲染新帧时,您都可以使用此函数更新对象在页面上的位置。

例如:

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

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

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

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM