How to determine if touchpoint is within a given canvas object's area? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

How to determine if touchpoint is within a given canvas object's area?

https://code.sololearn.com/WI4zXIFuCCuL/?ref=app What this code snippet does is basically it attaches event listener to a specific canvas area. But the more objects on canvas you have the larger and unmaintainable code becomes. Is there any better approach to this?

25th May 2021, 6:56 AM
Artur
Artur - avatar
13 Answers
+ 4
I've rewriten your code to give you a base to extend as you need: https://code.sololearn.com/W797u08XZN99/?ref=app (you could add a color property to the object, and use it to fill the rect, as example, and add as many area you want)
25th May 2021, 12:43 PM
visph
visph - avatar
+ 3
Take a look at how I implemented touch: https://code.sololearn.com/Wmn12u8TO24p/?ref=app
25th May 2021, 7:13 AM
Solo
Solo - avatar
+ 2
make it a button with onclick
25th May 2021, 7:03 AM
Oma Falk
Oma Falk - avatar
+ 2
Vasiliy Thank you. Really helpful example.
25th May 2021, 10:31 AM
Artur
Artur - avatar
+ 2
Because to be honest I'd rather write everything in js (even if it gets quite long) than jumping between horrible html css and stuff like that, if you know what I mean 😅
25th May 2021, 10:44 AM
Artur
Artur - avatar
+ 2
No offense tho html and css lovers 🤣
25th May 2021, 10:44 AM
Artur
Artur - avatar
+ 2
no downside, even better ^^ but if you call an addEventListener in a loop like (as you seems to try, even if you actually don't), then you will got problems ;P the correct solution would be to define the event listener once, and then do your loop logic independently... however, to handle many touch area inside a canvas, I will advise you to do it through a loop wich will test an array of area coordinates, rather than repeating code, nor define one event listener for each area ;)
25th May 2021, 10:54 AM
visph
visph - avatar
+ 2
visph Thank you for advice.
25th May 2021, 11:09 AM
Artur
Artur - avatar
+ 2
Yeah....js and its derivatives can only be topped by elm. Well...that was a bit fun although not completely wrong. You are on a good way.
25th May 2021, 11:09 AM
Oma Falk
Oma Falk - avatar
+ 2
Frogged visph Vasiliy Thank you very much guys. Appreciate your help.
25th May 2021, 11:17 AM
Artur
Artur - avatar
+ 2
Artur: — "Are there any downsides or advantages of implementing touch event control through html&css&js over pure js?" It all depends on the goal pursued and the task at hand. Consider my code not as a reference solution, but as a tutorial for demonstrating certain actions in different ways.
25th May 2021, 11:43 AM
Solo
Solo - avatar
+ 2
The best solution lies in your question: "Create an object constructor in which you describe the desired property once and calmly create at 1000 new objects"
25th May 2021, 12:07 PM
Solo
Solo - avatar
+ 1
Vasiliy Frogged Are there any downsides or advantages of implementing touch event control through html&css&js over pure js?
25th May 2021, 10:38 AM
Artur
Artur - avatar