How do I make a object that moves with mouse pointer? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 2

How do I make a object that moves with mouse pointer?

I was learning JS and I reached the part of addEventListener and after learning it I got an idea of making a gamr.But for that game I want a object inside a html canvas that moves along mouse pointer.I thought I can do it by mouseover but I can't think of a function that makes the object move along the mouse pointer Also pls tell how to make that object stay in a boundary for e.g. inside 100px by px square Pls help

16th Aug 2020, 9:23 AM
Raghu Rao
Raghu Rao - avatar
5 Answers
+ 2
Then will it move at the position where cursor is?? Pls also anawer my query mentioned in last 4 lines of question description
16th Aug 2020, 9:32 AM
Raghu Rao
Raghu Rao - avatar
+ 2
Can you pls tell the canvas.offset thing clearly. So if my canvas dimension is Width: 200 px Height:200 px So my canvas.offset would be
16th Aug 2020, 9:42 AM
Raghu Rao
Raghu Rao - avatar
+ 2
The below statement is quite of noob .But I can't just think of this.Sorry What would be the left margin of my canvas if the width and height are 200,200.Pls tell
16th Aug 2020, 9:46 AM
Raghu Rao
Raghu Rao - avatar
+ 2
Oh thanks Now I understood the concept
16th Aug 2020, 9:50 AM
Raghu Rao
Raghu Rao - avatar