+ 5

How to check users click on screen (X and y ) using canvas?

No code~

5th Apr 2021, 11:44 PM
Michael
Michael - avatar
20 Answers
+ 5
https://code.sololearn.com/WNnT7t33cafL/?ref=app Made this a while ago It's simple and does exactly what you need and should be pretty easy to copy and learn from
6th Apr 2021, 1:44 AM
Pieter Edwards
Pieter Edwards - avatar
+ 5
I see some answers which would work perfectly only if there's no other HTML Element or bounding box above the canvas. Suppose you have a large divs above the canvas, those functions wont work as expected because the canvas bounding element (not the drawing area) has now been distorted. To fix this, you have to get the actual bounding context of the canvas and do some vector's manipulation to get it working. Rect = canvas.getBoundingClientRect(); posX = abs(Rect.left - e.clientX) posY = abs(Rect.top - e.clientY) "abs" is simply a function defined as const abs = x => x < 0? x : x
6th Apr 2021, 8:59 PM
Mirielle
Mirielle - avatar
6th Apr 2021, 2:01 AM
Pieter Edwards
Pieter Edwards - avatar
+ 2
Daniel Briceño what do you mean?
6th Apr 2021, 1:34 AM
Michael
Michael - avatar
+ 2
Ok and can you make an example code? Plz, Daniel Briceño
6th Apr 2021, 1:40 AM
Michael
Michael - avatar
+ 2
Daniel Briceño yes, canvas
6th Apr 2021, 1:47 AM
Michael
Michael - avatar
+ 2
Like I don't understand this document.getElementById('x-value').textContent = e.x; document.getElementById('y-value').textContent = e.y; $("#player").css("transform","translate3d( " + e.x/1.5 + "px, " + e.y/1.5 + "px, 0px)"); }); Can you like make it normal canvas no simple? Like no # and e.x and more...
6th Apr 2021, 2:03 AM
Michael
Michael - avatar
+ 1
Hard. No code. Just HTML?
6th Apr 2021, 1:34 AM
Daniel Briceño
Daniel Briceño - avatar
+ 1
Daniel Briceño no. That is not what I want, plz delete your answers to make this question unanswered.
6th Apr 2021, 1:44 AM
Michael
Michael - avatar
+ 1
.... Is "JS" allowed?
6th Apr 2021, 1:45 AM
Daniel Briceño
Daniel Briceño - avatar
+ 1
𝕄𝕚𝕔𝕙𝕒𝕖𝕝 𝕃𝕚𝕦 [🄰🄲🅃🄸🅅🄴] (っ◔◡◔)っ My code should work with canvas but if not I can help you adapt it if you DM me
6th Apr 2021, 1:48 AM
Pieter Edwards
Pieter Edwards - avatar
+ 1
Pieter Edwards I was wrong. But that's how you learn.:)
6th Apr 2021, 1:58 AM
Daniel Briceño
Daniel Briceño - avatar
+ 1
Pieter Edwards can you make the code simpler? It is a bit hard to understand in the js.
6th Apr 2021, 2:00 AM
Michael
Michael - avatar
+ 1
So basically e.x = The X axis co-ordinate e.y = The Y axis co-ordinate The document.getElementById bit is just making it so the X and Y co-ordinate is printed onto the page so is not necessary. Hope you understand now?
6th Apr 2021, 2:06 AM
Pieter Edwards
Pieter Edwards - avatar
+ 1
Pieter Edwards Try to use just JS please. JS must be taught first and then Jquery
6th Apr 2021, 2:11 AM
Daniel Briceño
Daniel Briceño - avatar
+ 1
Pieter Edwards can you use js? No jQuery
6th Apr 2021, 5:55 AM
Michael
Michael - avatar
+ 1
X = e.clientX-cnv.offsetLeft Y = e.clientY-cnv.offsetTop check line 29 & 30 where cnv is the canvas element. https://code.sololearn.com/W6d0E0j5wCMB/?ref=app
7th Apr 2021, 3:31 AM
Nor'wester 🌪️ 🇧🇩 (INACTIVE)
Nor'wester 🌪️ 🇧🇩 (INACTIVE) - avatar
0
In a course that I saw, how to do it came out and then sent it by form. Let me search....
6th Apr 2021, 1:36 AM
Daniel Briceño
Daniel Briceño - avatar
0
check in <input type = "image" alt = "blablabla" /> https://www.mclibre.org/consultar/htmlcss/html/html-formularios.html
6th Apr 2021, 1:40 AM
Daniel Briceño
Daniel Briceño - avatar
0
... Canvas does not have event support, but it can over a parent element: <div onclick = "fun (events);"> <canvas> </canvas> </div>
6th Apr 2021, 1:51 AM
Daniel Briceño
Daniel Briceño - avatar