javascript - Canvas 的旋转没有按照我的预期发生

标签 javascript jquery html

我想让我的矩形原地旋转。但目前它正在旋转,就好像有一个物体在中心,而我的物体围绕着它旋转,就像我们在太阳系中看到的一样。我希望我的对象原地旋转而不是围绕某物旋转。我该怎么做?

到目前为止,这是我的代码:

<script type="text/javascript">
    var context;
    var radian = 0.01;
    var w, h;
    $(document).ready(function () {
        w = document.width;
        h = document.height;
        var canvas = $('#canvas');
        context = canvas[0].getContext('2d');
        canvas[0].width = w;
        canvas[0].height = h;
        setInterval(startAnim, 10);
    });

    function startAnim() {

        context.save();
        context.strokeStyle = 'rgb(0,0,0)';
        context.fillStyle = 'rgb(0,0,0)';
        context.fillRect(0, 0, w, h);

        context.strokeStyle = 'rgb(0,0,0)';
        context.fillStyle = 'rgb(255,255,0)';
        context.strokeRect(0, 0, 200, 200);
        context.fillRect(0, 0, 200, 200);
        context.restore();
        context.fillStyle = 'rgb(0,255,255)';
        context.fillRect(500, 400, 200, 200);
        radian += 0.01;
    }

</script>

更新: 抱歉,我在发帖前进行了试验,在此处发帖时忘记添加旋转功能。这是实际的代码:

function startAnim() {

        context.save();
        context.strokeStyle = 'rgb(0,0,0)';
        context.fillStyle = 'rgb(0,0,0)';
        context.fillRect(0, 0, w, h);

        context.translate(350, 300);
        context.rotate(radian);
        context.translate(-350, -300);

        context.strokeStyle = 'rgb(0,0,0)';
        context.fillStyle = 'rgb(255,255,0)';
        context.strokeRect(0, 0, 200, 200);
        context.fillRect(0, 0, 200, 200);
        context.restore();
        context.fillStyle = 'rgb(0,255,255)';
        context.fillRect(500, 400, 200, 200);
        radian += 0.01;
    }

最佳答案

使用translate(x, y)rotate(r),然后绘制矩形。

function startAnim() {
    context.save();
    context.strokeStyle = 'rgb(0,0,0)';
    context.fillStyle = 'rgb(0,0,0)';
    context.fillRect(0, 0, w, h);
    //^Shouldn't clearRect() be better here?

    //Draw translated, rotated rectangle at (250, 250)
    var x = 250;
    var y = 250;
    context.save();
    context.translate(x, y);
    context.rotate(radian);
    context.fillStyle = 'rgb(255,255,0)';
    context.fillRect(0, 0, 200, 200);
    context.restore();
    //Restore context (to reverse translation and rotation)

    context.strokeStyle = 'rgb(0,0,0)';
    context.fillStyle = 'rgb(255,255,0)';
    context.strokeRect(0, 0, 200, 200);
    context.fillRect(0, 0, 200, 200);
    context.restore();
    context.fillStyle = 'rgb(0,255,255)';
    context.fillRect(500, 400, 200, 200);
    radian += 0.01;
}

jsFiddle demoSimple jsFiddle demo用一个旋转矩形

关于javascript - Canvas 的旋转没有按照我的预期发生,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7011158/

相关文章:

javascript - 当页面忙于加载时,新标签页自动打开?

javascript - 使用 WebDriver(HtmlUnit、Ruby 绑定(bind))时是否可以忽略 JavaScript 异常

javascript - React-Navigation:如何在 TabNavigation 中的选项卡之间传递数据?

javascript - jQuery:将对象 ["student.course.name"] 转换为对象 ['student' ] ['course' ] ['name' ]

javascript - 当用户单击图像时,如何使用 JavaScript 或 jQuery 读取图像的像素?

javascript - 按 Ctrl + F 将焦点更改为输入并显示搜索

javascript - 禁用 div 内的元素

html - 为什么我的导航栏不起作用?

javascript - Javascript有什么用

javascript - 如何从json中删除双引号