【问题标题】:HTML5 canvas - rotate object without moving coordinatesHTML5 画布 - 旋转对象而不移动坐标
【发布时间】:2013-06-15 16:23:40
【问题描述】:

我拥有的是:

我想要的是旋转 red 矩形,例如20度,但这就是我最终的结果:

如您所见,矩形已完美旋转,但已移动,不再匹配黑色对象。

我的代码是:

context.save();
context.rotate(angle * Math.PI / 180); // in the screenshot I used angle = 20
context.translate(angle * 4, 2);
context.fillStyle = "red";
context.fillRect(x + 14, y - 16, 5, 16);
context.restore();

我想让矩形在不从其位置移动的情况下转动。

谢谢。

【问题讨论】:

标签: javascript html5-canvas


【解决方案1】:

听起来你想围绕它的中心点旋转矩形。

红色矩形是原始矩形,黄色矩形围绕中心点旋转。

为此,您需要先context.translate 到矩形的中心点,然后再旋转。

// move the rotation point to the center of the rect

    ctx.translate( x+width/2, y+height/2 );

// rotate the rect

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

请注意,上下文现在处于旋转状态。

这意味着绘图位置 [0,0] 在视觉上位于 [ x+width/2, y+height/2 ]。

因此,您必须在 [ -width/2, -height/2 ] 处绘制旋转的矩形(而不是在原始未旋转的 x/y 处)。

// draw the rect on the transformed context
// Note: after transforming [0,0] is visually [-width/2, -height/2]
//       so the rect needs to be offset accordingly when drawn

    ctx.rect( -width/2, -height/2, width,height);

这是代码和小提琴:http://jsfiddle.net/m1erickson/z4p3n/

<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>

<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>

<script>
$(function(){

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

    var startX=50;
    var startY=80;

    // draw an unrotated reference rect
    ctx.beginPath();
    ctx.rect(startX,startY,100,20);
    ctx.fillStyle="blue";
    ctx.fill();

    // draw a rotated rect
    drawRotatedRect(startX,startY,100,20,45);

    function drawRotatedRect(x,y,width,height,degrees){

        // first save the untranslated/unrotated context
        ctx.save();

        ctx.beginPath();
        // move the rotation point to the center of the rect
        ctx.translate( x+width/2, y+height/2 );
        // rotate the rect
        ctx.rotate(degrees*Math.PI/180);

        // draw the rect on the transformed context
        // Note: after transforming [0,0] is visually [x,y]
        //       so the rect needs to be offset accordingly when drawn
        ctx.rect( -width/2, -height/2, width,height);

        ctx.fillStyle="gold";
        ctx.fill();

        // restore the context to its untranslated/unrotated state
        ctx.restore();

    }

}); // end $(function(){});
</script>

</head>

<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>

【讨论】:

  • 不完全是:puu.sh/3gnZT.png 我需要它不要在中心点,因为它是一个“坦克”游戏(你射击另一辆坦克),这是你用来瞄准的“枪”,所以它必须固定在水箱上(黑色物体)
  • @user2479365 markE 提供了一个出色而详细的答案,并带有一个很好的注释代码。只需根据您的需要调整translate()。你可能想要-height 而不是-height/2 或类似的东西。
  • 您可能希望使用距坦克中心的半径偏移平移。您可能还会发现使用转换矩阵可能更适合您的需求。
  • 正如 Vedran Šego 和 GoldfishSandwich 上面所说的那样,您只需将旋转点调整到所需的枢轴点 - translate(x,y+height/2) 和 rotate(0,-height/ 2,宽度,高度)。
  • @markE 这是我自己的编程问题的答案。谢谢。
【解决方案2】:

您可以使用一个简单的函数来代替平移和旋转:

这个函数可以让你画一条线,从 x 和 y 开始,以一定角度结束:

function lineToAngle(ctx, x1, y1, length, angle) {

    angle *= Math.PI / 180;

    var x2 = x1 + length * Math.cos(angle),
        y2 = y1 + length * Math.sin(angle);

    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);

    return {x: x2, y: y2};
}

用法:

lineToAngle(ctx, x, y, length, angle);

演示

var canvas = document.querySelector('canvas'),
    ctx = canvas.getContext('2d'),
    x = 100, y =50, length = 40, angle = 0, dlt = -2;

(function animate() {
    
    //clear
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    
    ctx.beginPath();
    lineToAngle(ctx, x, y, length, angle);
    ctx.lineWidth = 10;
    ctx.stroke();

    angle += dlt;
    if (angle < -90 || angle > 0) dlt = -dlt;
    
    requestAnimationFrame(animate);
})();

function lineToAngle(ctx, x1, y1, length, angle) {

    angle *= Math.PI / 180;
    
    var x2 = x1 + length * Math.cos(angle),
        y2 = y1 + length * Math.sin(angle);
    
    ctx.moveTo(x1, y1);
    ctx.lineTo(x2, y2);

    return {x: x2, y: y2};
}
body {background-color:#555557}
canvas {background:#ddd;border:1px solid #000}
&lt;canvas&gt;&lt;/canvas&gt;

【讨论】:

    【解决方案3】:

    在矩形的特定情况下,您可以只画一条线。

    在 HTML5 画布中,线条的绘制与矩形完全一样,因此,您可以使用此功能:

    let canvas = document.getElementById('myCanvas')
    
    let ctx = canvas.getContext('2d')
    
    function drawRotatedRect(ctx, x, y, width, height, angle) {
      angle *= Math.PI / 180
      ctx.lineWidth = height / 2
      ctx.beginPath()
      ctx.moveTo(x, y)
      ctx.lineTo(x + width * Math.cos(angle), y + width * Math.sin(angle))
      ctx.stroke()
    }
    
    
    drawRotatedRect(ctx, 30, 30, 200, 100, 20)
    canvas {
      border: 1px solid black;
      width: 100%;
    }
    <!DOCTYPE html>
    <html>
    <head>
      <meta charset="UTF-8">
      <title>Rotated Rectangle</title>
    </head>
    <body>
      <canvas id='myCanvas'></canvas>
    </body>
    </html>

    【讨论】:

      猜你喜欢
      • 2013-01-09
      • 2013-02-23
      • 1970-01-01
      • 2011-10-15
      • 2015-05-25
      • 2014-07-11
      • 1970-01-01
      • 2014-09-23
      • 2014-11-12
      相关资源
      最近更新 更多