24
Get Moving! with canvas

Get Moving! (with HTML5 canvas)

Embed Size (px)

DESCRIPTION

Introduction to animation with HTML5 canvas.

Citation preview

Page 1: Get Moving! (with HTML5 canvas)

Get Moving!with canvas

Page 2: Get Moving! (with HTML5 canvas)

The <canvas> Element

<!doctype html>

<html>

<head>

<title>Canvas is awesome!</title>

</head>

<body>

<canvas width="500" height="500">

Sorry, your browser does not support canvas :(

</canvas>

</body>

</html>

Page 3: Get Moving! (with HTML5 canvas)

The illusion of motion

Page 4: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 1

Page 5: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 2

Page 6: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 3

Page 7: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 4

Page 8: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 5

Page 9: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 6

Page 10: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 7

Page 11: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 8

Page 12: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 9

Page 13: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 10

Page 14: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 11

Page 15: Get Moving! (with HTML5 canvas)

The illusion of motion

Frame 12

Page 16: Get Moving! (with HTML5 canvas)

Logic of motion

clear screen

draw square

pause

Page 17: Get Moving! (with HTML5 canvas)

In code

function draw() {

// clear screen

// draw square

// pause

}

draw();

Page 18: Get Moving! (with HTML5 canvas)

In code

function draw() {

clearScreen();

drawSquare();

// pause

}

draw();

Page 19: Get Moving! (with HTML5 canvas)

In code

function draw() {

clearScreen();

drawSquare();

setTimeout(draw, 50);

}

draw();

Page 20: Get Moving! (with HTML5 canvas)

In code

function clearScreen() {

ctx.clearRect(0,

0,

ctx.canvas.width,

ctx.canvas.height);

}

Page 21: Get Moving! (with HTML5 canvas)

In code

function drawSquare() {

ctx.fillRect(50, 50, 100, 100);

}

Page 22: Get Moving! (with HTML5 canvas)

In code

function drawSquare(x, y) {

ctx.fillRect(x, y, 100, 100);

}

parameterize the function so that we can draw the square in different locations

Page 23: Get Moving! (with HTML5 canvas)

In code

var x = 50, y = 50;

function clearScreen() {...}

function drawSquare(x, y) {...}

function draw() {

clearScreen();

drawSquare(x, y);

x = x + 5;

setTimeout(draw, 50);

}

draw();

Page 24: Get Moving! (with HTML5 canvas)

Putting it all together

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