I am leaning to work with canvas
objects. I have the following coordinates:
A: [x1,y1] B: [x2,y2]
and a width
which is a number. I want to draw a rectangle on the canvas using these information. I know that the output can be two rectangles both sides of the AB.
One way can be to find two lines that are perpendicular to the AB
and then from each point find another BC
and AD
points using width
value.
Is there any better solutions for that?
Thanks
Asked
Active
Viewed 979 times
-1

Majid Hojati
- 1,740
- 4
- 29
- 61
-
Not sure to follow... Your rectangle can be rotated? Ah.. maybe I get it better now, [AB] is a segment, not a diagonal, right? Also, can you define "better"? What's wrong with your current solution? – Kaiido Sep 22 '21 at 03:55
-
@Kaiido thanks for your comment. yeah AB is a segment. By better I thought I am making things complicated and maybe there is easier ways to do it – Majid Hojati Sep 23 '21 at 01:20
1 Answers
1
You don't need width if you know the vertices of both ends of a rectangle. Width and height can be obtained by subtracting the vertices of the square. Please refer to the following code.
const CANVAS_WIDTH = 400;
const CANVAS_HEIGHT = 300;
const canvas = Object.assign(document.createElement('canvas'), {width: CANVAS_WIDTH, height: CANVAS_HEIGHT});
const ctx = canvas.getContext('2d');
const A = [10, 30];
const B = [120, 250];
const drawRectByCorner = (ctx, corner1, corner2) => {
const [x1, y1] = corner1;
const [x2, y2] = corner2;
ctx.fillRect(x1, y1, x2 - x1, y2 - y1); // x, y, width, height
};
drawRectByCorner(ctx, A, B);

lowfront
- 639
- 1
- 5
-
Hello, But I only have a segment. so AB is one of the edges. I am not sure if your method gives the correct rotation. I will try it thought – Majid Hojati Sep 23 '21 at 01:22
-
Can I ask for more explanation? You only know one corner and one edge AB, but you don't know which edge AB is between top, bottom, left and right? Please explain by using the actual variable value. – lowfront Sep 23 '21 at 10:26
-
Thanks for your help. Yes, Lets say our rectangle's vertexies are ABCD, I know the AB edge and A and B's coordinates. I also know length of BC or AD. I need to find the coordinates of C and D and I guess I can draw rectangle with it – Majid Hojati Sep 24 '21 at 04:03
-
If you haven't solved it yet, please refer to it. [Link](https://i.imgur.com/Vp2ys6E.png) – lowfront Sep 25 '21 at 09:32
-
Thanks, may I ask if this method works with any rectangular with any rotation angles? – Majid Hojati Sep 26 '21 at 21:58
-
Oops, I missed the rotation. There are many ways if rotation is added. This is because the results vary depending on which coordinates are rotated along the axis. If you know how to use the `ctx.rotate` method, you can use the above function of drawing a rectangle when there is no rotation to get the result of rotation. To accurately understand the rotate method, you need to know the image rotation algorithm or the point rotation equation on the two-dimensional coordinate plane to some extent. – lowfront Sep 28 '21 at 17:41
-
Another way in the presented situation is to obtain the remaining C and D coordinates and use the [CanvasDrawPath](https://html.spec.whatwg.org/multipage/canvas.html#canvasdrawpath) interface like to `ctx.beginPath`, `ctx.moveTo`. To obtain the C and D coordinates, you have to be able to find the horizontal and vertical lengths for the hypotenuse of a rectangle rotated by a trigonometric function of *sin* and *cos* to find the coordinates C and D. – lowfront Sep 28 '21 at 17:41
-
I have actually tried to use rotation, I managed to rotate one of rectangles but It did not work for the rest of coordinates. I guess when I applied rotation on the canvas it remains rotated and I have to update my other coordinates as well. I like to find C and D coordinates and use `drawPath` method – Majid Hojati Sep 30 '21 at 01:58
-
The `ctx.rotate` method can be applied temporarily using the `ctx.save` and `ctx.restore` methods. It works fast and is a commonly used method for rotating individual elements at different angles on the canvas. But if you want to find the coordinates, you can look at the rotational transformation formula of the point in two dimensions. But if you want to find the coordinates, you can look at the rotational transformation formula of the point in two dimensions. Please refer to this [place](https://stackoverflow.com/a/13048924/16962686)! – lowfront Oct 02 '21 at 08:45