hello, canvas
DESCRIPTION
Gentle introduction to using HTML5 Canvas. Class slides for Year Up elective.TRANSCRIPT
![Page 1: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/1.jpg)
Hello, Canvas.Hello, Canvas.
![Page 2: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/2.jpg)
What is a canvas?
![Page 3: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/3.jpg)
![Page 4: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/4.jpg)
![Page 5: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/5.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/6.jpg)
<!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](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/7.jpg)
![Page 8: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/8.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/9.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/10.jpg)
![Page 11: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/11.jpg)
0, 0x
y
![Page 12: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/12.jpg)
x0, 0
y
3, 1
![Page 13: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/13.jpg)
x0, 0
y
3, 1
2, 5
![Page 14: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/14.jpg)
ctx.fillRect(5, 5, 3, 3);
start x coordinate
start y coordinate
width
height
![Page 15: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/15.jpg)
x0, 0
y
ctx.fillRect(5, 5, 3, 3);
![Page 16: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/16.jpg)
x0, 0
y
ctx.fillRect(5, 5, 3, 3);
5, 5
![Page 17: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/17.jpg)
x0, 0
y
ctx.fillRect(5, 5, 3, 3);
5, 5
3
3
![Page 18: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/18.jpg)
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](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/19.jpg)
Code Sampleshttps://github.com/sethmcl/yearup_intro_to_programming/tree/master/hello_canvas/part_1
Resources
![Page 20: Hello, Canvas](https://reader033.vdocuments.mx/reader033/viewer/2022052321/554f9fd8b4c9057b298b4840/html5/thumbnails/20.jpg)
Use canvas to draw a picture like this in the browser.BONUS: Can you animate it?
Homework