JavaScript Events Flashcards
What is the event for clicking on a button?
click
button.addEventListener(‘click’, cb);
What is the event for losing focus?
blur
canvas.addEventListener(‘blur’, cb);
(on the window it’s - onblur)
window.onblur
What is the event for gaining focus?
focus
canvas.addEventListener(‘focus’, cb);
(on the window it’s - onfocu)
window.onfocus
Which events do you use to pause a game - if the tabs or changed or the browser is no longer the main app?
blur / focus
What event do you use to determine where the mouse is on screen?
mousemove
You can do this on the canvas. NOTE: it returns screen coords - so make sure you subtract the mouse coordinates from the canvas coords.
var rect = canvas.getBoundingClientRect(); mousex = evt.x - rect.left; mousey = evt.y - rect.top;
What is the event for starting a ‘touch’?
touchstart
canvas.addEventListener(‘touchstart’, callback);
What is the event for ending a touch?
touchend
canvas.addEventListener(‘touchend’, callback);
How would you prevent a user from dragging the canvas?
In the touchstart event handler - make sure you call:
evt.preventDefault
(NOTE: This probably isn’t an issue in something like Intel XDK)
What is the event for detecting a touch has moved?
touchmove
How would you prevent users from double tapping, to zoom into the canvas?
have a evt.preventDefault call in the touchend event