画布从底部中心图像角度旋转?

发布于 2024-07-09 13:51:47 字数 959 浏览 12 评论 0原文

如何使用canvas html5元素从底部中心角度旋转图像?

<html>
    <head>
        <title>test</title>
        <script type="text/javascript">
            function startup() {
                var canvas = document.getElementById('canvas');
                var ctx = canvas.getContext('2d');
                var img = new Image();
                img.src = 'player.gif';
                img.onload = function() {
                    ctx.translate(185, 185);
                    ctx.rotate(90 * Math.PI / 180);
                    ctx.drawImage(img, 0, 0, 64, 120);
                }
            }
        </script>
    </head>
    <body onload='startup();'>
        <canvas id="canvas" style="position: absolute; left: 300px; top: 300px;" width="800" height="800"></canvas>
    </body>
</html>

不幸的是,这似乎是从图像的左上角旋转它。 任何想法?

编辑:最后,物体(太空飞船)必须像时钟指针一样旋转,就像它在右转/左转一样。

How do you rotate an image with the canvas html5 element from the bottom center angle?

<html>
    <head>
        <title>test</title>
        <script type="text/javascript">
            function startup() {
                var canvas = document.getElementById('canvas');
                var ctx = canvas.getContext('2d');
                var img = new Image();
                img.src = 'player.gif';
                img.onload = function() {
                    ctx.translate(185, 185);
                    ctx.rotate(90 * Math.PI / 180);
                    ctx.drawImage(img, 0, 0, 64, 120);
                }
            }
        </script>
    </head>
    <body onload='startup();'>
        <canvas id="canvas" style="position: absolute; left: 300px; top: 300px;" width="800" height="800"></canvas>
    </body>
</html>

Unfortunately this seems to rotate it from the top left angle of the image. Any idea?

Edit: in the end the object (space ship) has to rotate like a clock pointer, as if it is turning right/left.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

梦在夏天 2024-07-16 13:51:47

首先,您必须平移到您想要旋转的点。 在本例中,图像尺寸为 64 x 120。要围绕底部中心旋转,您需要将其转换为 32, 120。

ctx.translate(32, 120);

这会将您带到图像的底部中心。 然后旋转画布:

ctx.rotate(90 * Math.PI/180);

旋转90度。

然后当你绘制图像时尝试这个:

ctx.drawImage(img, -32, -120, 64, 120);

? 那样有用吗?

First you have to translate to the point around which you would like to rotate. In this case the image dimensions are 64 x 120. To rotate around the bottom center you want to translate to 32, 120.

ctx.translate(32, 120);

That brings you to the bottom center of the image. Then rotate the canvas:

ctx.rotate(90 * Math.PI/180);

Which rotate by 90 degrees.

Then when you draw the image try this:

ctx.drawImage(img, -32, -120, 64, 120);

? Does that work?

羁拥 2024-07-16 13:51:47

正确答案当然是文森特的答案。
我对这个旋转/平移的事情有点摸索,我认为我的小实验可能会很有趣:

<html>
  <head>
    <title>test</title>
    <script type="text/javascript">
    canvasW = canvasH = 800;
    imgW = imgH = 128;
    function startup() {
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      // Just to see what I do...
      ctx.strokeRect(0, 0, canvasW, canvasH);
      var img = new Image();
      img.src = 'player.png';
      img.onload = function() {
        // Just for reference
        ctx.drawImage(img, 0, 0, 128, 128);
        ctx.drawImage(img, canvasW/2 - imgW/2, canvasH/2 - imgH/2, 128, 128);
        mark(ctx, "red");
        // Keep current context (transformations)
        ctx.save();
        // Put bottom center at origin
        ctx.translate(imgW/2, imgH);
        // Rotate
        // Beware the next translations/positions are done along the rotated axis
        ctx.rotate(45 * Math.PI / 180);
        // Mark new origin
        mark(ctx, "red");
        // Restore position
        ctx.translate(-imgW/2, -imgH);
        ctx.drawImage(img, 0, 0, imgW, imgH);
        mark(ctx, "green");
        // Draw it an wanted position
        ctx.drawImage(img, canvasW/2, canvasH/3, imgW, imgH);
        // Move elsewhere:
        ctx.translate(canvasW/2, canvasH/2);
        ctx.drawImage(img, 0, 0, imgW, imgH);
        mark(ctx, "blue");
        ctx.restore();
      }
    }
    function mark(ctx, color) {
     ctx.save();
//~      ctx.fillStyle = color;
//~      ctx.fillRect(-2, -2, 4, 4);
     ctx.strokeStyle = color;
     ctx.strokeRect(0, 0, imgW, imgH);
     ctx.restore();
    }
    </script>
  </head>
  <body onload='startup();'>
    <canvas id="canvas" style="position: absolute; left: 300px; top: 300px;" width="800" height="800"></canvas>
  </body>
</html>

我的绊脚石问题是定位旋转的图形很困难,因为它是沿着旋转轴的:要么我们必须做一些三角数学在原始原点进行绘制,或者我们必须在辅助隐藏画布上绘制,然后将其绘制在目标画布上。​​
除非其他人有更好的主意?

The correct answer is, of course, Vincent's one.
I fumbled a bit with this rotation/translation thing, and I think my little experiments could be interesting to show:

<html>
  <head>
    <title>test</title>
    <script type="text/javascript">
    canvasW = canvasH = 800;
    imgW = imgH = 128;
    function startup() {
      var canvas = document.getElementById('canvas');
      var ctx = canvas.getContext('2d');
      // Just to see what I do...
      ctx.strokeRect(0, 0, canvasW, canvasH);
      var img = new Image();
      img.src = 'player.png';
      img.onload = function() {
        // Just for reference
        ctx.drawImage(img, 0, 0, 128, 128);
        ctx.drawImage(img, canvasW/2 - imgW/2, canvasH/2 - imgH/2, 128, 128);
        mark(ctx, "red");
        // Keep current context (transformations)
        ctx.save();
        // Put bottom center at origin
        ctx.translate(imgW/2, imgH);
        // Rotate
        // Beware the next translations/positions are done along the rotated axis
        ctx.rotate(45 * Math.PI / 180);
        // Mark new origin
        mark(ctx, "red");
        // Restore position
        ctx.translate(-imgW/2, -imgH);
        ctx.drawImage(img, 0, 0, imgW, imgH);
        mark(ctx, "green");
        // Draw it an wanted position
        ctx.drawImage(img, canvasW/2, canvasH/3, imgW, imgH);
        // Move elsewhere:
        ctx.translate(canvasW/2, canvasH/2);
        ctx.drawImage(img, 0, 0, imgW, imgH);
        mark(ctx, "blue");
        ctx.restore();
      }
    }
    function mark(ctx, color) {
     ctx.save();
//~      ctx.fillStyle = color;
//~      ctx.fillRect(-2, -2, 4, 4);
     ctx.strokeStyle = color;
     ctx.strokeRect(0, 0, imgW, imgH);
     ctx.restore();
    }
    </script>
  </head>
  <body onload='startup();'>
    <canvas id="canvas" style="position: absolute; left: 300px; top: 300px;" width="800" height="800"></canvas>
  </body>
</html>

My stumbling issue was that positioning the rotated figure is hard, because it is along the rotated axis: either we have to do some trigo math to paint at the primitive origin, or we have to draw on a secondary hidden canvas and then paint it on the target one.
Unless somebody else has a better idea?

杀手六號 2024-07-16 13:51:47

当我需要从指定点(中心)开始旋转文本时,我使用过这个?

ctx.save(); 
ctx.translate(x,y);
ctx.rotate(degree*Math.PI/180);
ctx.translate(-x,-y);   
ctx.fillText(text,x,y); 
ctx.stroke();   
ctx.restore();

I have used this, when I needed to rotate texts started from their specified point (center)?

ctx.save(); 
ctx.translate(x,y);
ctx.rotate(degree*Math.PI/180);
ctx.translate(-x,-y);   
ctx.fillText(text,x,y); 
ctx.stroke();   
ctx.restore();
枕花眠 2024-07-16 13:51:47
<html>
  <head>
    <title>Canvas Pinball flippers by stirfry</title>
    <script type="application/x-javascript">
     /*THIS SCRIPT ADAPTED BY STIRFRY. SOURCE TEETHGRINDER no warranty or liability implied or otherwise. use at your own risk. No credit required. Enjoy.stirfry.thank(you)*/
    var img = new Image();
                            //img.src = "flipper.gif";//right
    img.src="http://i39.tinypic.com/k1vq0x.gif"
    var img2 = new Image();
                             //img2.src = "flipper2.gif";//left
    img2.src ="http://i42.tinypic.com/14c8wht.gif"
    var gAngle = 0;
    gAngle = 60;
    stop = false;
    inertia = .8;
    vel = 10;
    k = 0.1;

    function drawagain(){
      gAngle = 60;
      stop = false;
      inertia = .8;
      vel = 10;
      k = 0.1;
     draw()
   }

   function draw(){
     var ctx = document.getElementById('canvas').getContext('2d');
     ctx.save();

        vel = ( vel * inertia ) + ( -gAngle * k );

        gAngle += vel;

        ctx.fillStyle = 'rgb(255,255,255)';
        ctx.fillRect (0, 0, 600, 600);

        ctx.translate(380, 480);              //location of the system
        ctx.rotate( gAngle * Math.PI / 180 );//rotate first then draw the flipper
        ctx.drawImage(img, -105, -16); 
ctx.restore();
ctx.save();
        ctx.translate(120, 480);              //location of the system
        ctx.rotate( -1*gAngle * Math.PI / 180 );//rotate first then draw the flipper
        ctx.drawImage(img2, -18, -16); 

ctx.restore();

        if( !stop )
          setTimeout(draw, 30);
      }

    </script>
    <style type="text/css">
      body { margin: 20px; font-family: arial,verdana,helvetica; background: #fff;}
      h1 { font-size: 140%; font-weight:normal; color: #036; border-bottom: 1px solid #ccc; }
      canvas { border: 2px solid #000; float: left; margin-right: 20px; margin-bottom: 20px; }
      pre { float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
      .gameLayer {position: absolute; top: 0px; left: 0px;}
      #scoreLayer {font-family: arial; color: #FF0000; left: 10px; font-size: 70%; }
      #windowcontainer {position:relative; height:300px;}
    </style>
  </head>

  <body onload="draw()">
    <div id="windowcontainer">
      <canvas id="canvas" width="500" height="500"></canvas>
      <INPUT VALUE="flip" TYPE=BUTTON onClick="drawagain();"><br/>
    </div>

  </body>
</html>
<html>
  <head>
    <title>Canvas Pinball flippers by stirfry</title>
    <script type="application/x-javascript">
     /*THIS SCRIPT ADAPTED BY STIRFRY. SOURCE TEETHGRINDER no warranty or liability implied or otherwise. use at your own risk. No credit required. Enjoy.stirfry.thank(you)*/
    var img = new Image();
                            //img.src = "flipper.gif";//right
    img.src="http://i39.tinypic.com/k1vq0x.gif"
    var img2 = new Image();
                             //img2.src = "flipper2.gif";//left
    img2.src ="http://i42.tinypic.com/14c8wht.gif"
    var gAngle = 0;
    gAngle = 60;
    stop = false;
    inertia = .8;
    vel = 10;
    k = 0.1;

    function drawagain(){
      gAngle = 60;
      stop = false;
      inertia = .8;
      vel = 10;
      k = 0.1;
     draw()
   }

   function draw(){
     var ctx = document.getElementById('canvas').getContext('2d');
     ctx.save();

        vel = ( vel * inertia ) + ( -gAngle * k );

        gAngle += vel;

        ctx.fillStyle = 'rgb(255,255,255)';
        ctx.fillRect (0, 0, 600, 600);

        ctx.translate(380, 480);              //location of the system
        ctx.rotate( gAngle * Math.PI / 180 );//rotate first then draw the flipper
        ctx.drawImage(img, -105, -16); 
ctx.restore();
ctx.save();
        ctx.translate(120, 480);              //location of the system
        ctx.rotate( -1*gAngle * Math.PI / 180 );//rotate first then draw the flipper
        ctx.drawImage(img2, -18, -16); 

ctx.restore();

        if( !stop )
          setTimeout(draw, 30);
      }

    </script>
    <style type="text/css">
      body { margin: 20px; font-family: arial,verdana,helvetica; background: #fff;}
      h1 { font-size: 140%; font-weight:normal; color: #036; border-bottom: 1px solid #ccc; }
      canvas { border: 2px solid #000; float: left; margin-right: 20px; margin-bottom: 20px; }
      pre { float:left; display:block; background: rgb(238,238,238); border: 1px dashed #666; padding: 15px 20px; margin: 0 0 10px 0; }
      .gameLayer {position: absolute; top: 0px; left: 0px;}
      #scoreLayer {font-family: arial; color: #FF0000; left: 10px; font-size: 70%; }
      #windowcontainer {position:relative; height:300px;}
    </style>
  </head>

  <body onload="draw()">
    <div id="windowcontainer">
      <canvas id="canvas" width="500" height="500"></canvas>
      <INPUT VALUE="flip" TYPE=BUTTON onClick="drawagain();"><br/>
    </div>

  </body>
</html>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文