The canvas is in the center of the screen and when I add event click and call console.log(e.clientX, e.client.Y) it give e.g. 530, 483 but my shape is 50,50 from canvas.
how do i know i've click on shape not the canvas on the background.
let shapes:any = [];
shapes.push({ x:50, y:50, width: this.qrSize, height: this.qrSize, color:'lightgreen' })
let is_mouse_in_shape = function(x:any, y:any, shape:any){
let shape_left = shape.x;
let shape_right = shape.x + shape.width;
let shape_top = shape.y;
let shape_bottom = shape.y + shape.height;
console.log(x,y) // x = 415, y = 467
console.log(shape_left, shape_top) // x = 50, y = 50
if( x > shape_left && x < shape_right && y > shape_top && y > shape_bottom ){
return true;
}
return false; // It's return false
}
let mouse_down = function(event:any){
event.preventDefault();
startX = parseInt(event.clientX);
startY = parseInt(event.clientY);
console.log(event)
let index = 0;
for(let shape of shapes){
if(is_mouse_in_shape(startX, startY, shape)){
console.log('yes');
current_shape_index = index;
} else {
console.log('no')
}
index++;
}
}
let draw_shapes = function(){
context.clearRect(0,0, canvas_width, canvas_height);
for (let shape of shapes){
context.fillStyle = shape.color;
context.fillRect(shape.x, shape.y, shape.width, shape.height);
}
}
draw_shapes();
canvas.onmousedown = mouse_down;
