javascript - HTML5 canvas - 旋转对象而不移动坐标

标签 javascript html5-canvas

我所拥有的是这样的: Tank

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

如您所见,矩形完美旋转,但它发生了移动并且不再与黑色对象匹配。

我的代码是:

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();

我想让矩形转动而不偏离其位置。

谢谢。

最佳答案

听起来您想围绕其中心点旋转矩形。

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

enter image description here

为此,您需要在旋转之前先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);

这是代码和 fiddle :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>

关于javascript - HTML5 canvas - 旋转对象而不移动坐标,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17125632/

相关文章:

javascript - 点击/点击通过 JQuery Mobile 中的绝对定位 div

javascript - 如何将变量从 Controller 传递到 application.js?

javascript - 当Wordpress中有多个事件类时,如何将事件类设置为选项卡?

javascript - 正则表达式排除字符

javascript - 在 ChartJS 中使 x 标签水平

javascript - 我可以使用 vh 和 vw 指定 Canvas 尺寸吗?

javascript - 如何在 JavaScript 脚本中渲染新页面

javascript - 使用双上下文获取 Canvas 上的真实鼠标位置

javascript - HTML5 Canvas 游戏开发复杂化

javascript - 如何在使用 Apple pencil 时从 HTML canvas 绘制或获取指针坐标?