Events
Instead of adding 3 (permanent) EventListener
, you could just keep the first one: square.addEventListener('mousedown'...
and when this is triggered, add the 2 other events mouseup
and mousemove
on the document (it will also enalbe the user to move quickly)
The mouseup
event should then simply remove the 2 event listeners (you will need to keep a trace of the EventListeners
).
You can then get rid off the pushed
variable.
The square.addEventListener
could be moved inside the constructor.
moveCursor
In the method moveCursor
instead of moving only if the new position is completely in the area, you could separately check the X and Y coordinates (It will then be easier to move near the edges).
Naming
I don't find the name Cursor
to be very expressive... maybe Draggable
is a better name?
- 1.9k
- 12
- 28