+ 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?
13 odpowiedzi
+ 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)
+ 3
Take a look at how I implemented touch:
https://code.sololearn.com/Wmn12u8TO24p/?ref=app
+ 2
make it a button with onclick
+ 2
Vasiliy Thank you. Really helpful example.
+ 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 😅
+ 2
No offense tho html and css lovers 🤣
+ 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 ;)
+ 2
visph Thank you for advice.
+ 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.
+ 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.
+ 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"