Are we there yet?

31
Are we there yet? polling & eventing in JavaScript

description

Learn about events in JavaScript, and how to use them.

Transcript of Are we there yet?

Page 1: Are we there yet?

Are we there yet?polling & eventing in JavaScript

Page 2: Are we there yet?

Ask vs. Tell

Are we

there yet?

Page 3: Are we there yet?

Ask vs. Tell

No.

Page 4: Are we there yet?

Ask vs. Tell

Are we

there yet?

Page 5: Are we there yet?

Ask vs. Tell

No.

Page 6: Are we there yet?

Ask vs. Tell

Are we

there yet?

Page 7: Are we there yet?

Ask vs. Tell

Are we

there yet?

Page 8: Are we there yet?

Ask vs. Tell

NO!!

Page 9: Are we there yet?

Ask vs. Tell

Let me know

when we get

there!

Page 10: Are we there yet?

Ask vs. Tell

Roger that!

Page 11: Are we there yet?

Ask vs. Tell

Page 12: Are we there yet?

Ask vs. Tell

Page 13: Are we there yet?

Ask vs. Tell

Page 14: Are we there yet?

Ask vs. Tell

Page 15: Are we there yet?

Ask vs. Tell

Page 16: Are we there yet?

Ask vs. Tell

Page 17: Are we there yet?

Ask vs. Tell

Page 18: Are we there yet?

Ask vs. Tell

Page 19: Are we there yet?

Ask vs. Tell

We’re here!

Page 20: Are we there yet?

Ask vs. Tell

YAY!!!

Page 21: Are we there yet?

Tell, don’t ask.

● More efficient● Less complex to implement

Page 22: Are we there yet?

Telling with Events

Use events to get notified when “something” happens

○ key on keyboard is pressed

○ mouse is moved

○ browser window is resized

○ ...and a lot more!

Page 23: Are we there yet?

Hook up your events

document.addEventListener('mousemove', function (e) {

console.log(e.clientX, e.clientY);

});

Examplehttp://codepen.io/sethmcl/pen/zEGLy

Page 24: Are we there yet?

Hook up your events

document.addEventListener('mousemove', function (e) {

console.log(e.clientX, e.clientY);

});

global object provided by the browser, entry point to the web page which is currently loaded in the browser

Learn morehttps://developer.mozilla.org/en-US/docs/Web/API/document

Page 25: Are we there yet?

Hook up your events

document.addEventListener('mousemove', function (e) {

console.log(e.clientX, e.clientY);

});

method (function) that we can call to “register” for a particular event

Learn morehttps://developer.mozilla.org/en-US/docs/Web/API/EventTarget.addEventListener

Page 26: Are we there yet?

Hook up your events

document.addEventListener('mousemove', function (e) {

console.log(e.clientX, e.clientY);

});

name of event

Learn morehttps://developer.mozilla.org/en-US/docs/Web/Reference/Events

Page 27: Are we there yet?

Hook up your events

document.addEventListener('mousemove', function (e) {

console.log(e.clientX, e.clientY);

});

function which is called when the event is “fired”

this function is called the “event handler”

Learn morehttps://developer.mozilla.org/en-US/docs/Web/Guide/Events/Event_handlers

Page 28: Are we there yet?

Hook up your events

document.addEventListener('mousemove', function (e) {

console.log(e.clientX, e.clientY);

});

Learn morehttps://developer.mozilla.org/en-US/docs/Web/Reference/Events/mousemove

event object, which contains information about the event

Page 29: Are we there yet?

Keyboard Events

document.addEventListener('keydown', function (e) {

console.log(e.keyCode, e.keyCode);

});

Learn morehttps://developer.mozilla.org/en-US/docs/Web/Reference/Events/keydown

Page 30: Are we there yet?

Putting it all together

window.addEventListener('resize', function (e) {

canvas.width = window.innerWidth;

canvas.height = window.innerHeight;

// Note: resizing the canvas will also clear it

});

Examplehttp://codepen.io/sethmcl/pen/rsJGh

Page 31: Are we there yet?

Homework

Think about the concepts we have learned so far, and brainstorm an idea for a game that you can make.

The output of this activity is not code, but rather a list of ideas and some drawings (pen and paper, powerpoint, etc) that describes what you want to build.