It will help if you have some knowledge of HTML and Javascript. If not,
hopefully this basic canvas template will make sense after it is
explained. Lets take a look at at our starting point:
<!DOCTYPE HTML>
<html>
<body>
<canvas id="canvas" width="200" height="200"></canvas>
<script>
var can = document.getElementById("canvas");
var ctx = can.getContext('2d');
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 200, 200);
ctx.fillStyle = "yellow";
ctx.fillRect(50, 50, 100, 50);
</script>
</body>
</html>
This code is about the minimum you must have to create a HTML canvas
and draw in it. We drew a black rectangle over the
entire canvas. This can serve as the background to a scene. Then we
drew a yellow rectangle over the black one.
In lines 1-5 we are setting up a minimal HTML page. It is minimal
because it doesn't include a head or title, but it will work fine.
Lines 7-14 are inside the script tag (in between the <script> and
</script>) which indicates it is JavaScript.
Using this template, you can start to insert your own drawing commands.
To learn the drawing commands such as circles, rectangles, and lines,
look to the w3schools tutorial at
w3schools HTML5 Canvas
. W3schools is a great site to learn commands but it dosen't guide you towards
creating more complex games or applications.
Try it out by opening up your text editor and pasting the template in. Then
save it with the .html file extension. Now when you double click it your browser
will open it up and display the canvas. In the future you can simply save your
code and hit refresh on the browser.
If you don't see anything, the browser may be prompting you to display "Active"
content. It is also a good idea to open up your browser's developer tools. Press F12
and look for the "Console". If you have any errors in the code the console will
let you know what line to look at in order to fix them. Common errors that will
keep the canvas from displaying are open parentheses, brackets, or quotes.
This is an example of what a Syntax Error looks like. It is saying that parentheses were opened but not closed on line 166.
Usually the first thing you will want to do is to make your canvas larger. You can have the canvas automatically resize to the window size inside your JavaScript. If you do this you might want to create variables that equal your width and height. The Body element will have small margins (space around the edge) by default. When you go fullscreen you probably want to set the margins to zero. It is also a good idea to turn off the scrollbars with "overflow:hidden;".
<!DOCTYPE HTML>
<html>
<body style="margin:0px; overflow:hidden;">
<canvas id="canvas"></canvas>
<script>
var can = document.getElementById("canvas");
var ctx = can.getContext('2d');
var cw = can.width = window.innerWidth;
var ch = can.height = window.innerHeight;
ctx.fillStyle = "black";
ctx.fillRect(0, 0, cw, ch);
ctx.fillStyle = "brown";
ctx.fillRect(cw/2 - 50, ch - 50, 100, 50);
ctx.beginPath();
ctx.moveTo(cw/2 - 70, ch - 50);
ctx.lineTo(cw/2, ch - 90);
ctx.lineTo(cw/2 + 70, ch - 50);
ctx.fill();
</script>
</body>
</html>
This time we set the canvas width and height to match the window. We can access the window's size
with window.innerWidth and window.innerHeight. We set the variables cw and ch so that we can
access the width and height when we draw without typing too much. You can easily place an object
in the middle of the screen by dividing the width by 2. You will learn a lot just from trial and
error, so if it looks confusing just play around with it and you will see what happens when you
make changes to your code.
We now have a little brown house in the center of the screen. You might be wondering why you don't acctually see the lines we drew. You can make the canvas draw lines with the stroke() command. Make sure to set up your lineWidth and strokeStyle (the color) before you draw the lines.
ctx.lineWidth="3";
ctx.strokeStyle="yellow";
ctx.stroke();
So far you have seen colors given by thier name, such as "black" and "yellow". It is useful to know how to specify colors in other ways. You can also specify colors using hex: #ff00ff or with red, green, and blue values: rgb(255,0,255). One advantage to using rgb is that you can specify transparency with the alpha channel: rgba(150,200,255,.5). Alpha values (the last one) range from 0 (transparent) to 1 (opaque).
<!DOCTYPE HTML>
<html>
<body style="margin:0px; overflow:hidden;">
<canvas id="canvas"></canvas>
<script>
var can = document.getElementById("canvas");
var ctx = can.getContext('2d');
var cw = can.width = window.innerWidth;
var ch = can.height = window.innerHeight;
ctx.fillStyle = "black";
ctx.fillRect(0, 0, cw, ch);
//Yellow Rectangle
ctx.fillStyle = "#ffff00";
ctx.fillRect(50, 50, 50, 100);
//Semi-transparent Red Circle
ctx.fillStyle = "rgba(255, 0, 0, .5)"
ctx.beginPath();
ctx.arc(100, 100, 50, 0, Math.PI*2);
ctx.fill();
</script>
</body>
</html>
You can see in this example that a yellow rectangle was drawn using "#ffff00" with a semi-transparent red circle drawn over the top of it. For more information on colors or shape commands look to w3schools.com.