javascript - 调整形状宽度时,旋转的形状在 y 轴上移动

标签 javascript html5-canvas

我正在尝试调整 Canvas 上旋转形状的大小。我的问题是,当我调用渲染函数时,形状开始“漂移”,具体取决于形状 Angular 。我怎样才能避免这种情况?

我制作了一个简化的 fiddle 来演示这个问题,当单击 Canvas 时,形状会变大,并且由于某种原因它会向上漂移。

这是 fiddle :https://jsfiddle.net/x5gxo1p7/

<style>
    canvas {
        position: absolute;
        box-sizing: border-box;
        border: 1px solid red;
    }
</style>
<body>
<div>
    <canvas id="canvas"></canvas>
</div>
</body>

<script type="text/javascript">
    var canvas = document.getElementById('canvas');
    canvas.width = 300;
    canvas.height= 150;
    var ctx = canvas.getContext('2d');
    var counter = 0;

    var shape = {
        top: 120,
        left: 120,
        width: 120,
        height: 60,
        rotation: Math.PI / 180 * 15
    };



    function draw() {
        var h2 = shape.height / 2;
        var w2 = shape.width / 2;

        var x = w2;
        var y = h2;


        ctx.save();
        ctx.clearRect(0, 0, canvas.width, canvas.height);
        ctx.translate(75,37.5)
        ctx.translate(x, y);

        ctx.rotate(Math.PI / 180 * 15);
        ctx.translate(-x, -y);

        ctx.fillStyle = '#000';
        ctx.fillRect(0, 0, shape.width, shape.height);
        ctx.restore();
}


canvas.addEventListener('click', function() {

    shape.width = shape.width + 15;
    window.requestAnimationFrame(draw.bind(this));
});

window.requestAnimationFrame(draw.bind(this));
</script>

在“真实”代码中,当单击并移动调整大小 handle 时,形状会调整大小,但我认为这个示例充分说明了这个问题。

编辑:更新 fiddle 以澄清问题:

https://jsfiddle.net/x5gxo1p7/9/

最佳答案

始终使用局部坐标来定义形状。

当呈现要转换的内容时,内容应该在它自己的(本地)坐标系中。想一个图像。无论在何处渲染,左上角像素始终位于图像的 0,0 处。像素位于它们的本地坐标,渲染时它们通过当前变换移动到(世界) Canvas 坐标。

因此,如果您将形状的坐标设置为其本地,使旋转点位于其本地原点 (0,0),则显示坐标将单独存储为世界坐标

var shape = {
    top: -30,   // local coordinates with rotation origin 
    left: -60,  // at 0,0
    width: 120,
    height: 60,
    world : {
         x : canvas.width / 2,
         y : canvas.height / 2,
         rot : Math.PI / 12,   // 15deg clockwise
    }
};

现在您不必再为向前和向后的翻译而烦恼了……真是太痛苦了。

只是

ctx.save();
ctx.translate(shape.world.x,shape.world.y);
ctx.rotate(shape.world.rot);
ctx.fillRect(shape.left, shape.top, shape.width, shape.height)
ctx.restore();

或事件更快,无需使用保存和恢复

ctx.setTransform(1,0,0,1,shape.world.x,shape.world.y);
ctx.rotate(shape.world.rot);
ctx.fillRect(shape.left, shape.top, shape.width, shape.height);

局部形状原点 (0,0) 是变换放置平移的位置。

这大大简化了很多必须要做的工作

var canvas = document.getElementById('canvas');
canvas.width = 300;
canvas.height= 150;
var ctx = canvas.getContext('2d');
ctx.fillStyle = "black";
ctx.strokeStyle = "red";
ctx.lineWidth = 2;  


var shape = {
    top: -30,   // local coordinates with rotation origin 
    left: -60,  // at 0,0
    width: 120,
    height: 60,
    world : {
         x : canvas.width / 2,
         y : canvas.height / 2,
         rot : Math.PI / 12,   // 15deg clockwise
     }
};

function draw() {
    ctx.setTransform(1,0,0,1,0,0); // to clear use default transform
    ctx.clearRect(0, 0, canvas.width, canvas.height);

    // you were scaling the shape, that can be done via a transform
    // once you have moved the shape to the world coordinates.
    ctx.setTransform(1,0,0,1,shape.world.x,shape.world.y);
    ctx.rotate(shape.world.rot);
 
    // after the transformations have moved the local to the world
    // you can ignore the canvas coordinates and work within the objects
    // local. In this case showing the unscaled box
    ctx.strokeRect(shape.left, shape.top, shape.width, shape.height);
    // and a line above the box
    ctx.strokeRect(shape.left, shape.top - 5, shape.width, 1);
 
    ctx.scale(0.5,0.5); // the scaling you were doing
    ctx.fillRect(shape.left, shape.top, shape.width, shape.height);
}

canvas.addEventListener('click', function() {
    shape.width += 15;
    shape.left -= 15 / 2;
    shape.world.rot += Math.PI / 45; // rotate to illustrate location 
                                     // of local origin
    var distToMove = 15/2;
    shape.world.x += Math.cos(shape.world.rot) * distToMove;
    shape.world.y += Math.sin(shape.world.rot) * distToMove;
    draw();
});
// no need to use requestAnimationFrame (RAF) if you are not animation 
// but its not wrong. Nor do you need to bind this (in this case
// this = window) to the callback RAF does not bind a context
// to the callback 
/*window.requestAnimationFrame(draw.bind(this));*/
requestAnimationFrame(draw); // functionaly identical
// or just
/*draw()*/ //will work
 body { font-family : Arial,"Helvetica Neue",Helvetica,sans-serif; font-size : 12px; color : #242729;} /* SO font currently being used */

canvas { border: 1px solid red; }
<canvas id="canvas"></canvas>
<p>Click to grow "and rotate" (I add that to illustrate the local origin)</p>
<p>I have added a red box and a line above the box, showing how using the local coordinates to define a shape makes it a lot easier to then manipulate that shape when rendering "see code comments".</p>

关于javascript - 调整形状宽度时,旋转的形状在 y 轴上移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38827788/

相关文章:

javascript - javascript 中的异步等待 sqlite

javascript - three.js 中在 SphereGeometry 的表面上从 vector3 到 vector3 绘制曲线

javascript - Three.js Material.skinning = true 导致着色器编译错误

javascript - 单击按钮时 JQuery OnClick 不起作用

javascript - html5 Canvas 颜色切换和线宽

javascript - 我想添加客户页面的屏幕截图按钮,然后希望将其保存在数据库中并将其显示在另一个页面中作为仪表板

javascript - for循环(每次迭代都会产生一个 promise )完成后如何返回单个 promise ?

javascript - 使用 WebGL 将两个 Canvas 混合到一个 Canvas 上

javascript - 从新的 HTML5 Canvas 开始

javascript - 更改 Canvas 填充样式而不与以前的颜色重叠