Sprite
-like classes and handling physics/collision detection/sound etc... - canvas won't do any of that for us.<script> // #1 call the init function after the pages loads window.onload = init; function init(){ // #2 Now that the page has loaded, start drawing! // A - canvas variable points at <canvas> tag var canvas = document.querySelector('canvas'); // B - the ctx variable points at a "2D drawing context" var ctx = canvas.getContext('2d'); // C - all fill operations are now in red ctx.fillStyle = 'red'; // D - fill a rectangle with the current fill color ctx.fillRect(20,20,600,440); } </script>
ctx
variable is a reference to the "2D drawing context" - which gives us access to the entire canvas drawing API.ctx.fillStyle
is one property of the drawing context. This property sets the color of all future "fill" operations.ctx.fillRect()
is one of the methods of the drawing context. This method "fills" a specified rectangle with current fill color.Note: We have to wait until the HTML page has loaded before we run the init()
, or the code will fail.
Go download the source (first-canvas.html) from mycourses.rit.edu so that we can make some changes to the drawing code and also "break" (and fix) the page. We'll also take a quick look at debugging your JavaScript.
Be sure to carefully read and absorb the material in the ICE - don't just quickly blast through it. Most of the course material will be contained in the ICE's and Study Guides, rather than in the slides like you might expect.