Touch events
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Try using the Spinning Pacman on your phone. You won’t be able to get any Pacman to follow your finger around.
Why? Because we built Spinning Pacman by listening to mouse events! We did not support touch events yet.
First, you need to open the console
from a device that supports touch input. You can try:
There are 4 touch events:
touchstart
touchmove
touchend
touchcancel
touchstart
is like mousedown
. It triggers when you touch the screen with a finger. Each finger triggers a new touchstart
event.
touchmove
triggers when you move your finger on a screen.
touchend
is similar to mouseup
. It triggers when a finger leaves the screen. If you remove two fingers on the screen, touchend
will trigger two times.
touchcancel
triggers when a touch event gets disrupted.
I tested and found out that:
touchcancel
.touchcancel
even when I have 15 fingers on screen.Note: Navigator.maxTouchPoints
can be used to determine the maximum number of simultaneous touch points.
It’s easy to support Touch and Mouse if you want to trigger a click
event. Touch devices will trigger mousedown
,mouseup
, and click
automatically if you press the screen with the intention to click.
If you want to support other gestures like touchmove
, you have two options:
There are three touch properties:
touches
: list of Touch objects that are in contact with the surface.targetTouches
: touches
that start from the same target element.changedTouches
: touches
that have changed since the last Touch event.Each Touch object has the following properties:
target
: The element that was touched initially.identifier
: A unique identifier for this touch object.pageX
& pageY
: Position of touch relative to the documentclientX
& clientY
: Position of touch relative to the viewportscreenX
& screenY
: Position of touch relative to screenYou can also find the information like force
, radiusX
, radiusY
, and rotationAngle
in each touch object. We won’t cover them since we won’t use them in this course.