J
J
Jedi2017-10-05 00:31:49
JavaScript
Jedi, 2017-10-05 00:31:49

How to create a click on CANVAS?

How can I click XY coordinates on an element that is inside CANVAS ? This is not quite an element, getting to it somehow will not work. (canvas.el.click()) . How, knowing the coordinates, can I click ?

Question... I'm still a junior in JS, the question may be silly, but try to answer it :)
Is it necessary to click ? What if we just create an event ? Should this work?

Explain, please.

1372 324

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Konstantin Kitmanov, 2017-10-05
@k12th

A click on an element in the canvas is caught in general terms like this: we take the coordinates of the click from the upper left corner in the canvas and compare it with the coordinates of the element (do not forget to take into account scaling). Well, if it's a circle. Slightly worse if a rectangle. It's even worse if it's a polygon , especially if it's not convex either :) Perhaps it will be easier to take some ready-made library , there are interactive elements almost everywhere.
You can create an event yourself (but there may be cross-browser nuances), but it's usually easier to immediately call the handler with the necessary parameters.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question