在 html 画布上移动图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3057162/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 03:29:01  来源:igfitidea点击:

moving an image across a html canvas

htmlcanvas

提问by iJK

I am trying to move an image from the right to the center and I am not sure if this is the best way.

我正在尝试将图像从右侧移动到中心,但我不确定这是否是最好的方法。

var imgTag = null;
    var x = 0;
    var y = 0;
    var id;

    function doCanvas()
    {
        var canvas = document.getElementById('icanvas');
        var ctx = canvas.getContext("2d");
        var imgBkg = document.getElementById('imgBkg');
        imgTag = document.getElementById('imgTag');

        ctx.drawImage(imgBkg, 0, 0);

        x = canvas.width;
        y = 40;

        id = setInterval(moveImg, 0.25);

    }

    function moveImg()
    {
        if(x <= 250)
            clearInterval(id);

        var canvas = document.getElementById('icanvas');
        var ctx = canvas.getContext("2d");

        ctx.clearRect(0, 0, canvas.width, canvas.height);

        var imgBkg = document.getElementById('imgBkg');
        ctx.drawImage(imgBkg, 0, 0);

        ctx.drawImage(imgTag, x, y);

        x = x - 1;
    }

Any advice?

有什么建议吗?

回答by iJK

This question is 5 years old, but since we now have requestAnimationFrame, here's an approach for that using vanilla JavaScript:

这个问题已经有 5 年历史了,但由于我们现在有了 requestAnimationFrame,这里有一种使用 vanilla JavaScript 的方法:

var imgTag = new Image();
var canvas = document.getElementById('icanvas');
var ctx = canvas.getContext("2d");
var x = canvas.width;
var y = 0;

imgTag.onload = animate;
imgTag.src = "http://i.stack.imgur.com/Rk0DW.png";   // load image

function animate() {
  ctx.clearRect(0, 0, canvas.width, canvas.height);  // clear canvas
  ctx.drawImage(imgTag, x, y);                       // draw image at current position
  x -= 4;
  if (x > 250) requestAnimationFrame(animate)        // loop
}
<canvas id="icanvas" width=640 height=180></canvas>

回答by timdream

drawImage()enables to define which part of the source image to draw on target canvas. I would suggest for each moveImg()calculate the previous image position, overwrite the previous image with that part of imgBkg, then draw the new image. Supposedly this will save some computing power.

drawImage()允许定义在目标画布上绘制源图像的哪一部分。我建议为每个moveImg()计算前一个图像位置,用 的那部分覆盖前一个图像imgBkg,然后绘制新图像。据说这会节省一些计算能力。

回答by Nevin Madhukar K

For lag free animations,i generally use kinetic.js.

对于无延迟动画,我通常使用 kinetic.js。

 var stage = new Kinetic.Stage({
        container: 'container',
        width: 578,
        height: 200
      });
      var layer = new Kinetic.Layer();

      var hexagon = new Kinetic.RegularPolygon({
        x: stage.width()/2,
        y: stage.height()/2,
        sides: 6,
        radius: 70,
        fill: 'red',
        stroke: 'black',
        strokeWidth: 4
      });

      layer.add(hexagon);
      stage.add(layer);

      var amplitude = 150;
      var period = 2000;
      // in ms
      var centerX = stage.width()/2;

      var anim = new Kinetic.Animation(function(frame) {
        hexagon.setX(amplitude * Math.sin(frame.time * 2 * Math.PI / period) + centerX);
      }, layer);

      anim.start();

Here's the example,if you wanna take a look.

这是例子,如果你想看看。

http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-animate-position-tutorial/

http://www.html5canvastutorials.com/kineticjs/html5-canvas-kineticjs-animate-position-tutorial/

Why i suggest this is because,setInterval or setTimeout a particular function causes issues when large amount of simultaneous animations take place,but kinetic.Animationdeals with framerates more intelligently.

为什么我建议这样做是因为,当发生大量同步动画时,特定函数会导致 setInterval 或 setTimeout 出现问题,但kinetic.Animation更智能地处理帧率