hello, canvas

20
Hello, Canvas. Hello, Canvas.

Upload: seth-mclaughlin

Post on 11-May-2015

337 views

Category:

Software


1 download

DESCRIPTION

Gentle introduction to using HTML5 Canvas. Class slides for Year Up elective.

TRANSCRIPT

Page 1: Hello, Canvas

Hello, Canvas.Hello, Canvas.

Page 2: Hello, Canvas

What is a canvas?

Page 3: Hello, Canvas
Page 4: Hello, Canvas
Page 5: Hello, 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 6: Hello, Canvas

<!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>

The <canvas> Element

Page 7: Hello, Canvas
Page 8: Hello, Canvas

JavaScript to the rescue!

<!doctype html>

<html>

<head>

<title>Canvas is awesome!</title>

</head>

<body>

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

Sorry, your browser does not support canvas :(

</canvas>

<script src="script.js"></script>

</body>

</html>

Page 9: Hello, Canvas

JavaScript to the rescue!

// script.js

var canvas = document.querySelector('canvas');

var ctx = canvas.getContext('2d');

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

Learn more: https://developer.mozilla.org/en-US/docs/Web/HTML/Canvas

Page 10: Hello, Canvas
Page 11: Hello, Canvas

0, 0x

y

Page 12: Hello, Canvas

x0, 0

y

3, 1

Page 13: Hello, Canvas

x0, 0

y

3, 1

2, 5

Page 14: Hello, Canvas

ctx.fillRect(5, 5, 3, 3);

start x coordinate

start y coordinate

width

height

Page 15: Hello, Canvas

x0, 0

y

ctx.fillRect(5, 5, 3, 3);

Page 16: Hello, Canvas

x0, 0

y

ctx.fillRect(5, 5, 3, 3);

5, 5

Page 17: Hello, Canvas

x0, 0

y

ctx.fillRect(5, 5, 3, 3);

5, 5

3

3

Page 18: Hello, Canvas

Getting fancy

ctx.fillStyle = 'red'; // Draw a red square

ctx.fillStyle = 'blue'; // Draw a blue square

ctx.fillStyle = '#8A1CDA'; // Draw a purple square

Page 19: Hello, Canvas

Code Sampleshttps://github.com/sethmcl/yearup_intro_to_programming/tree/master/hello_canvas/part_1

Resources

Page 20: Hello, Canvas

Use canvas to draw a picture like this in the browser.BONUS: Can you animate it?

Homework