When I click the top left. The output is not x: 0, y: 0. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

When I click the top left. The output is not x: 0, y: 0.

https://code.sololearn.com/WnG4ZVkKIg5C/?ref=app

6th Mar 2017, 9:45 AM
K.C. Leung
K.C. Leung - avatar
10 Answers
+ 8
your code takes the location clicked on the entire screen rather than the canvas alone so when you click on the canvas, it takes the location clicked relative to the entire screen you can notice that better if you set margin-left:30%; margin-top:30%; to the canvas in the css. you will notice it then what you need to do is find the offset of the screen edge to the canvas and play with that good luck :)
6th Mar 2017, 10:03 AM
Burey
Burey - avatar
+ 8
hmm you made some changes and now the touch aint working well on my phone ;_; edit getting undefined for x and y
6th Mar 2017, 10:14 AM
Burey
Burey - avatar
+ 8
try playing with: pos=c.getBoundingClientRect(); var x = evt.clientX-pos.left ; var y = evt.clientY-pos.top ;
6th Mar 2017, 10:25 AM
Burey
Burey - avatar
+ 7
try: var pos=c.getBoundingClientRect(); touch=evt.touches[0]; var x = touch.clientX-pos.left ; var y = touch.clientY-pos.top ;
6th Mar 2017, 12:37 PM
Burey
Burey - avatar
+ 6
at last xD good job >:p
6th Mar 2017, 12:50 PM
Burey
Burey - avatar
+ 6
also, add this at the start of the handler evt.preventDefault(); it will prevent scrolling down when you move around
6th Mar 2017, 12:53 PM
Burey
Burey - avatar
+ 1
Burey: How can I do if I want x: 0, y: 0 when the mouse is hovering the top left of the canvas?
6th Mar 2017, 10:09 AM
K.C. Leung
K.C. Leung - avatar
+ 1
Yes, I've modified the event from 'click' to 'ontouchmove'
6th Mar 2017, 10:19 AM
K.C. Leung
K.C. Leung - avatar
0
I think there is no clientX/Y property at the evt.
6th Mar 2017, 12:19 PM
K.C. Leung
K.C. Leung - avatar
0
Yes! perfect! thank you very much.
6th Mar 2017, 12:46 PM
K.C. Leung
K.C. Leung - avatar