简体   繁体   English

计算二维旋转 javascript HTML canvas

[英]Calculate 2D rotation javascript HTML canvas

Implemented a canvas, Drawing a square there and get the calculated coordinates,实现了一个canvas,在那里画一个正方形,得到计算出的坐标,
Can see on the following pic the drawing:可以在下图中看到图纸: 画布绘制

I'm calculating and getting the upleft point X and Y coordinates,我正在计算并获取最高点 X 和 Y 坐标,
And for the down right coordinates that i need, I'm adding the height and width, as follows:对于我需要的右下坐标,我添加了高度和宽度,如下所示:

 { upLeft: { x: position.x, y: position.y }, downRight: { x: position.x + position.width, y: position.y + position.height } },

Now i want to get the same dimensions when i'm rotating the canvas clockwise or anti-clockwise.现在我想在顺时针或逆时针旋转 canvas 时获得相同的尺寸。 So i have the angle, And i try to calculate via the following function:所以我有角度,我尝试通过以下 function 进行计算:

 function getRotatedCoordinates(cx, cy, x, y, angle) {
    let radians = (Math.PI / 180) * angle,
        cos = Math.cos(radians),
        sin = Math.sin(radians),
        nx = (cos * (x - cx)) - (sin * (y - cy)) + cx,
        ny = (cos * (y - cy)) + (sin * (x - cx)) + cy;
    return [nx, ny];
  }

And i'm calling the function via the following args and using it.我通过以下参数调用 function 并使用它。

let newCoords = getRotatedCoordinates(0, 0, position.x, position.y, angle);
position.x = newCoords[0];
position.y = newCoords[1];

So firstly, I'm not sure that the cx and cy points are correct, I'm always entering 0 for both of them.所以首先,我不确定 cx 和 cy 点是否正确,我总是为它们输入 0。 Secondly, I'm not getting the desired results, They are getting changed but i'm pretty sure that something is wrong with the x and y, So i guess that the function is wrong.其次,我没有得到想要的结果,它们正在改变,但我很确定 x 和 y 有问题,所以我猜 function 是错误的。 Thanks.谢谢。

Here is how I would do it:这是我的做法:

 function getRectangeCoordinates(x, y, width, height, angle) { let points = [ [x, y] ] let radians = (Math.PI / 180) * angle; x += Math.cos(radians) * width; y += Math.sin(radians) * width; points.push([x, y]) radians += Math.PI / 2 x += Math.cos(radians) * height; y += Math.sin(radians) * height; points.push([x, y]) radians += Math.PI / 2 x += Math.cos(radians) * width; y += Math.sin(radians) * width; points.push([x, y]) return points } let canvas = document.createElement("canvas"); canvas.width = canvas.height = 140 let ctx = canvas.getContext('2d'); document.body.appendChild(canvas); let coords = getRectangeCoordinates(20, 10, 120, 40, 15) console.log(JSON.stringify(coords)) for (let i = 0; i < 4; i++) { ctx.beginPath(); ctx.arc(coords[i][0], coords[i][1], 3, 0, 8); ctx.moveTo(coords[i][0], coords[i][1]); let next = (i + 1) % 4 ctx.lineTo(coords[next][0], coords[next][1]); ctx.stroke(); } ctx.strokeStyle = "red"; coords = getRectangeCoordinates(60, 40, 40, 50, 65) for (let i = 0; i < 4; i++) { ctx.beginPath(); ctx.arc(coords[i][0], coords[i][1], 5, 0, 8); ctx.moveTo(coords[i][0], coords[i][1]); let next = (i + 1) % 4 ctx.lineTo(coords[next][0], coords[next][1]); ctx.stroke(); } ctx.strokeStyle = "blue"; coords = getRectangeCoordinates(120, 3, 20, 20, 45) for (let i = 0; i < 4; i++) { ctx.beginPath(); ctx.arc(coords[i][0], coords[i][1], 2, 0, 8); ctx.moveTo(coords[i][0], coords[i][1]); let next = (i + 1) % 4 ctx.lineTo(coords[next][0], coords[next][1]); ctx.stroke(); }

In the getRectangeCoordinates I'm returning all corners of a rectangle and the paraments of the function are the top left corner (x, y) the height and width of the rectangle and last the angle.getRectangeCoordinates ,我返回矩形的所有角,function 的参数是左上角(x, y)矩形的高度和宽度,最后是角度。

I'm drawing a few rectangles with different shapes and angles to show how it looks like我画了几个不同形状和角度的矩形来展示它的样子


The calculations in the function are simple trigonometry here is a visual representation that could help you remember it the next time you need it: function 中的计算是简单的三角函数,这是一个直观的表示,可以帮助您在下次需要时记住它:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM