Semi-random thoughts and tales of tinkering
Every video game starts with a screen to draw on. In this lesson, you'll create your very first web page and learn to draw shapes on it — just like the blocks in Minecraft! 🎨
HTML stands for HyperText Markup Language. It's the language used to create web pages — yes, the same kind of pages you visit every day in your browser!
An HTML file is just a text file with special tags that tell the browser what to show.
Tags look like this: <tag>stuff inside</tag>
Here's the simplest web page you can make. It has just three parts:
<!DOCTYPE html>
<html>
<head>
<title>My Game</title>
</head>
<body>
<h1>Hello, World!</h1>
</body>
</html>
<html> — Wraps everything on the page<head> — Info about the page (like its title in the browser tab)<body> — Everything you actually see on the page<h1> — A big heading (like a chapter title)
To make a game, we need something to draw on. HTML gives us a special tag called
<canvas>. Think of it as a blank piece of paper that we can draw
shapes, colors, and pictures on using code!
<canvas id="game" width="600" height="400"></canvas>
This creates a drawing area that is 600 pixels wide and 400 pixels tall.
The id="game" gives it a name so we can find it in our code.
HTML creates the canvas, but we need JavaScript to actually draw on it. JavaScript is a programming language that makes web pages interactive — it's the language that powers almost every game you play in a browser!
We write JavaScript inside a <script> tag. Here's how we draw our first rectangle:
<script>
// Step 1: Find our canvas on the page
const canvas = document.getElementById('game');
// Step 2: Get the "drawing tools" (called the context)
const ctx = canvas.getContext('2d');
// Step 3: Pick a color
ctx.fillStyle = '#5cb85c'; // A nice green color
// Step 4: Draw a filled rectangle!
// fillRect(x, y, width, height)
ctx.fillRect(50, 50, 200, 100);
</script>
x — How far from the left edge to start (in pixels)y — How far from the top edge to start (in pixels)width — How wide the rectangle isheight — How tall the rectangle isImportant: On a canvas, the position (0, 0) is the top-left corner, not the bottom-left! The y-axis goes down, not up. This is different from math class!
Here's what our code draws — a green rectangle on a dark background:
A green rectangle at position (50, 50)
We can draw multiple rectangles with different colors to make a simple scene. Let's make a sky, some ground, and a sun:
// Sky (fill the whole canvas with light blue)
ctx.fillStyle = '#87ceeb';
ctx.fillRect(0, 0, 600, 400);
// Ground (green along the bottom)
ctx.fillStyle = '#5cb85c';
ctx.fillRect(0, 300, 600, 100);
// Dirt (brown under the grass)
ctx.fillStyle = '#8b6914';
ctx.fillRect(0, 320, 600, 80);
// Sun (yellow square in the sky)
ctx.fillStyle = '#ffdd00';
ctx.fillRect(500, 40, 60, 60);
A simple scene made entirely from rectangles!
'#87ceeb' are called
hex colors. The 6 characters after the # represent how much Red, Green,
and Blue to mix together. You can also use color names like 'red',
'blue', or 'green'!
Here's the full HTML file that creates our scene. You can copy this into a text file,
save it as game.html, and open it in your browser!
<!DOCTYPE html>
<html>
<head>
<title>My First Canvas</title>
</head>
<body style="background: #222; margin: 0;">
<canvas id="game" width="600" height="400"></canvas>
<script>
const canvas = document.getElementById('game');
const ctx = canvas.getContext('2d');
// Sky
ctx.fillStyle = '#87ceeb';
ctx.fillRect(0, 0, 600, 400);
// Ground
ctx.fillStyle = '#5cb85c';
ctx.fillRect(0, 300, 600, 100);
// Dirt
ctx.fillStyle = '#8b6914';
ctx.fillRect(0, 320, 600, 80);
// Sun
ctx.fillStyle = '#ffdd00';
ctx.fillRect(500, 40, 60, 60);
</script>
</body>
</html>
'#ff6b6b' for a sunset!y position<canvas> tag gives us a drawing area<script> tagsctx.fillStyle to pick a colorctx.fillRect(x, y, width, height) to draw rectangles