I am attempting to recreate a painting API using canvas. I am facing an issue where JavaScript only seems to retain one coordinate instead of two for drawing a rectangle.
Below is my code snippet:
var x_1;
var y_1;
var x_2;
var y_2;
var tab_coord;
function coord(evt) {
x_1 = evt.clientX;
y_1 = evt.clientY;
console.log("coordinate x_1: " + x_1 + " y: " + y_1);
tab_coord={'x_1': x_1, 'x_2':y_1};
canvas.addEventListener('click', second_coord);
}
function second_coord(evt) {
console.log(tab_coord);
x_2 = evt.clientX;
y_2 = evt.clientY;
console.log("coordinate x_2:" + x_2 + " y: " + y_2);
}
Even though the
console.log("coordinate x_1: " + x_1 + " y: " + y_1)
displays the first coordinate properly, JavaScript fails to store it in the tab_coord
. Therefore, the console.log(tab_coord)
shows the coordinates of x_2
and y_2
instead of x_1
and y_1
as intended.