javascript state of mind

17
JavaScript state of mind

Upload: seth-mclaughlin

Post on 11-May-2015

195 views

Category:

Software


1 download

DESCRIPTION

Discover how to use objects to track state in JavaScript. Apply this concept with a simple application in HTML5 Canvas.

TRANSCRIPT

Page 1: JavaScript State of Mind

JavaScript

state of mind

Page 2: JavaScript State of Mind

What is "state"?

mood: "angry"

Page 3: JavaScript State of Mind

What is "state"?

mood: "happy"sleeping: falsehair: "brown"

mood: "sad"sleeping: falsehair: "brown"

mood: "angry"sleeping: falsehair: "brown"

Page 4: JavaScript State of Mind

How is tracking state helpful?

color: 'red'width: 100height: 100x: 323y: 100

Page 5: JavaScript State of Mind

How is tracking state helpful?

color: 'red'width: 75height: 75x: 330y: 200

Page 6: JavaScript State of Mind

Using objects to store state

Page 7: JavaScript State of Mind

Using objects to store state

keyword

Page 8: JavaScript State of Mind

Using objects to store state

identifier

Page 9: JavaScript State of Mind

Using objects to store state

assignment operator

Page 10: JavaScript State of Mind

Using objects to store state

object literal

Page 11: JavaScript State of Mind

Using objects to store state

property

Page 12: JavaScript State of Mind

Using objects to store state

value (string)

Page 13: JavaScript State of Mind

Using objects to store state

value (number)

Page 14: JavaScript State of Mind

Render (draw) the object

Page 15: JavaScript State of Mind

Putting it all together

Page 16: JavaScript State of Mind

Putting it all together

http://codepen.io/sethmcl/pen/LDgzE

Page 17: JavaScript State of Mind

Assignment

Draw a square on the screen, using a square object to track state.

Make it move to the right across the screen.

When it reaches the right edge, it should reverse direction and move to the left.

When it reaches the left edge, it should reverse direction and move to the right.

Reminder: You can get the canvas width from ctx.canvas.width