代码之家  ›  专栏  ›  技术社区  ›  Wolfr

如何使用画布围绕自身旋转图像?

  •  3
  • Wolfr  · 技术社区  · 17 年前

    我很难用画布在图像周围漫游。

    由于无法旋转图像,因此必须旋转画布:如果将画布旋转一定程度,则要旋转的原点将发生更改。我不知道如何跟踪这个变化。

    这是我当前的代码: http://pastie.org/669023

    演示会在 http://preview.netlashproject.be/cog/

    如果你想试一试,压缩后的代码和图片就在 http://preview.netlashproject.be/cog/cog.zip

    2 回复  |  直到 16 年前
        1
  •  11
  •   DaMacc    17 年前

    我修改了你的密码:

    var rotation = 0;
    function draw(){
    
      var ctx = document.getElementById('myCanvas').getContext('2d');
      ctx.globalCompositeOperation = 'destination-over';
    
      ctx.save();
      ctx.clearRect(0,0,200,200);
      ctx.translate(100,100); // to get it in the origin
      rotation +=1;
      ctx.rotate(rotation*Math.PI/64); //rotate in origin
      ctx.translate(-100,-100); //put it back
      ctx.drawImage(cog,0,0);
      ctx.restore();
    }
    

    重要的是,在旋转之前必须先将图像转换为原点,然后再将其转换回来!

        2
  •  1
  •   John Boker    16 年前

    看起来这可能是你可以使用的东西: http://wilq32.googlepages.com/wilq32.rollimage222

    这是一个测试: http://www.antiyes.com/test/index.php