javascript - 更改原点中心。 (Javascript)

标签 javascript html

我一直在绞尽脑汁解决这个问题。请问有什么方法可以将原点从左上角坐标更改为左下角坐标吗?下面是代码。请注意,我希望相对于左下坐标逆时针方向旋转最后一个矩形。

var canvas = document.getElementById("DemoCanvas");
if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    for (i = 0; i < 500; i += 50) {
        ctx.moveTo(0, i);
        ctx.strokeStyle = "#E8D8D8";
        ctx.lineTo(canvas.width, i);
        ctx.stroke();

    }
    for (i = 0; i < 511; i += 50) {
        ctx.moveTo(i, 0);
        ctx.strokeStyle = "#E8D8D8";
        ctx.lineTo(i, canvas.height);
        ctx.stroke();
    }
    // Save the unrotated context of the canvas.   
    ctx.save();
    ctx.fillStyle = "#ff0000";
    ctx.fillRect(2, 5, 100, 150);
    //Move to the center of the canvas to (50,10) point.   
    ctx.translate(100, 5);
    // Rotate the canvas by 30 degrees.
    ctx.rotate((Math.PI / 180) * 30);
    ctx.fillStyle = "#ff0000";
    // Draw another rectangle
    ctx.fillRect(0, 0, 100, 150);

    ctx.translate(100, 0);
    // Rotate the canvas by 30 degrees.
    ctx.rotate((Math.PI / 180) * 30);
    ctx.fillStyle = "#ff0000";
    // Draw another rectangle
    ctx.fillRect(0, 0, 100, 150);
    // Restore the unrotated context   

    ctx.translate(100, 0);
    ctx.rotate((Math.PI / 180) * -30);
    ctx.fillStyle = "#ff0000";
    // Draw another rectangle
    ctx.fillRect(0, 0, 100, 150);

    ctx.restore();

}
<!DOCTYPE html>
<html>

<head>
    <title>Road</title>
</head>

<body>
    <canvas id="DemoCanvas" width="300" height="400"></canvas>
</body>

</html>

我想用这个方法来绘制一条高速公路路线。非常感谢任何帮助。请帮助我谢谢。

最佳答案

使用以下内容

ctx.translate(0, canvas.height);
ctx.scale(1, -1);

    var canvas = document.getElementById("DemoCanvas");
    if (canvas.getContext) {
    var ctx = canvas.getContext('2d');
    for (i = 0; i < 500; i += 50) {
        ctx.moveTo(0, i);
        ctx.strokeStyle = "#E8D8D8";
        ctx.lineTo(canvas.width, i);
        ctx.stroke();         
    }
    
    for (i = 0; i < 511; i += 50) {
        ctx.moveTo(i, 0);
        ctx.strokeStyle = "#E8D8D8";
        ctx.lineTo(i, canvas.height);
        ctx.stroke();
    }

    // Save the unrotated context of the canvas.   
    ctx.save();
    ctx.translate(0, canvas.height);
    ctx.scale(1, -1);
    ctx.fillStyle = "#ff0000";
    ctx.fillRect(2, 5, 100, 150);
    
    //Move to the center of the canvas to (50,10) point.   
    ctx.translate(100, 5);
    // Rotate the canvas by 30 degrees.
    ctx.rotate((Math.PI / 180) * 30);
    ctx.fillStyle = "#ff0000";
    // Draw another rectangle
    ctx.fillRect(0, 0, 100, 150);

    ctx.translate(100, 0);
    // Rotate the canvas by 30 degrees.
    ctx.rotate((Math.PI / 180) * 30);
    ctx.fillStyle = "#ff0000";
    // Draw another rectangle
    ctx.fillRect(0, 0, 100, 150);
    // Restore the unrotated context   

    ctx.translate(100, 0);
    ctx.rotate((Math.PI / 180) * -30);
    ctx.fillStyle = "#ff0000";
    // Draw another rectangle
    ctx.fillRect(0, 0, 100, 150);

    ctx.restore();    
}
<!DOCTYPE html>
<html>

<head>
    <title>Road</title>
</head>

<body>
    <canvas id="DemoCanvas" width="300" height="400"></canvas>
</body>

</html>

关于javascript - 更改原点中心。 (Javascript),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47348716/

相关文章:

html - 在查询字符串中传递电子邮件地址

html - CSS Vertical Align Staggered ...应该在顶部

c# - MVC ASP & Javascript 变量共享

javascript - 正则表达式在任何空白字符处拆分字符串但隔离任何换行符

javascript - ImmutableJS fromJS() 和 Map() "ownerID"不匹配

javascript - 使用 navigator.share() api,如何仅共享一个对象的信息?

javascript - 在没有反应插件的情况下使用 eslint-config-airbnb

html - 是否有背景大小 :cover? 的替代方案

javascript - Google 图表工具提示选项

java - 当没有 id 时,如何在 Selenium 中单击此图像链接?