Skip to content

Commit 1a6f145

Browse files
committed
Add second blog post about learning the canvas
1 parent cec9974 commit 1a6f145

File tree

1 file changed

+156
-0
lines changed

1 file changed

+156
-0
lines changed
Lines changed: 156 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,156 @@
1+
---
2+
layout: post
3+
title: "Learning the Canvas: Part 2"
4+
tags:
5+
- javascript
6+
- canvas
7+
categories:
8+
---
9+
10+
This is part of an ongoing series of posts about learning how to use the HTML canvas element. If you haven't already seen part 1, I recommend you check out [Learning the Canvas: Part 1]({{ page.previous.url }}).
11+
12+
Last time we only used one function to draw rectangles: `fillRect()`. But in fact, there are two other functions that can be used to draw rectanges as well: `strokeRect()` and `clearRect()`. All three methods take the same parameters: `x`, `y`, `width`, and `height`. For the x and y position, remember that the "starting" coordinates are the top-left corner of the rectangle.
13+
14+
Combining these three functions to form a single drawing can produce some pretty interesting results.
15+
16+
<div>
17+
<canvas id="canvas" width="200" height="200" style="border: 1px solid black">
18+
</canvas>
19+
</div>
20+
21+
<script>
22+
var canvas = document.getElementById("canvas");
23+
var context = canvas.getContext("2d");
24+
context.fillRect(0, 0, 200, 200);
25+
context.clearRect(50, 50, 100, 100);
26+
context.strokeRect(60, 60, 80, 80);
27+
context.strokeRect(70, 70, 60, 60);
28+
context.strokeRect(80, 80, 40, 40);
29+
context.strokeRect(90, 90, 20, 20);
30+
context.fillRect(95, 95, 10, 10);
31+
</script>
32+
33+
<br/>
34+
35+
When you look at the above image, what do you see?
36+
37+
Some see a bird's eye view of a tall pryamid, while others see a first-person view down long hallway.
38+
39+
The code for this example is a combination of the three methods mentioned above.
40+
41+
{% highlight html %}
42+
<div>
43+
<canvas id="canvas" width="200" height="200" style="border: 1px solid black">
44+
</canvas>
45+
</div>
46+
{% endhighlight %}
47+
48+
{% highlight html %}
49+
<script>
50+
var canvas = document.getElementById("canvas");
51+
var context = canvas.getContext("2d");
52+
context.fillRect(0, 0, 200, 200);
53+
context.clearRect(50, 50, 100, 100);
54+
context.strokeRect(60, 60, 80, 80);
55+
context.strokeRect(70, 70, 60, 60);
56+
context.strokeRect(80, 80, 40, 40);
57+
context.strokeRect(90, 90, 20, 20);
58+
context.fillRect(95, 95, 10, 10);
59+
</script>
60+
{% endhighlight %}
61+
62+
<br/>
63+
64+
You can see how these functions give us more flexibility on what we can draw, and how we can draw it.
65+
66+
<div>
67+
<canvas id="canvas-2" width="600" height="200" style="border: 1px solid black">
68+
</canvas>
69+
</div>
70+
71+
<script>
72+
var canvas = document.getElementById("canvas-2");
73+
var context = canvas.getContext("2d");
74+
context.fillRect(0, 0, 600, 200);
75+
context.clearRect(10, 10, 580, 180);
76+
77+
var initialX = 20;
78+
for (var index = 0; index <= 55; index += 1) {
79+
context.strokeRect(initialX+index*10, 20, 5, 160);
80+
}
81+
</script>
82+
83+
<br/>
84+
85+
We can leverage loop structures to draw on bigger canvases without much additional code.
86+
87+
{% highlight html %}
88+
<script>
89+
var canvas = document.getElementById("canvas-2");
90+
var context = canvas.getContext("2d");
91+
context.fillRect(0, 0, 600, 200);
92+
context.clearRect(10, 10, 580, 180);
93+
94+
var initialX = 20;
95+
for (var index = 0; index <= 55; index += 1) {
96+
context.strokeRect(initialX+index*10, 20, 5, 160);
97+
}
98+
</script>
99+
{% endhighlight %}
100+
101+
<br/>
102+
103+
Building upon that example, we can imagine all sorts of rectangular-based drawings using a combination of these three functions.
104+
105+
<div>
106+
<canvas id="canvas-3" width="400" height="400" style="border: 1px solid black">
107+
</canvas>
108+
</div>
109+
110+
<script>
111+
var canvas = document.getElementById("canvas-3");
112+
var context = canvas.getContext("2d");
113+
context.fillRect(0, 0, 400, 400);
114+
115+
var initialX = 10;
116+
var initialY = 10;
117+
var maxX = 390;
118+
var maxY = 390;
119+
120+
context.clearRect(initialX, initialY, maxX-initialX, maxY-initialY);
121+
122+
for (var y = 0; initialY + 10 * y < maxY; y += 1) {
123+
context.strokeRect(initialX, initialY*y, maxX-initialX, maxY-initialY);
124+
}
125+
for (var x = 0; initialX + 10 * x < maxX; x += 1) {
126+
context.strokeRect(initialX*x, initialY, maxX-initialX, maxY-initialY);
127+
}
128+
</script>
129+
130+
<br/>
131+
132+
Even though it appears as a board with many squares, in the code we see it's actually long rectangles rendered on top of each other to achieve the appearance of squares.
133+
134+
{% highlight html %}
135+
<script>
136+
var canvas = document.getElementById("canvas-3");
137+
var context = canvas.getContext("2d");
138+
context.fillRect(0, 0, 400, 400);
139+
140+
var initialX = 10;
141+
var initialY = 10;
142+
var maxX = 390;
143+
var maxY = 390;
144+
145+
context.clearRect(initialX, initialY, maxX-initialX, maxY-initialY);
146+
147+
for (var y = 0; initialY + 10 * y < maxY; y += 1) {
148+
context.strokeRect(initialX, initialY*y, maxX-initialX, maxY-initialY);
149+
}
150+
for (var x = 0; initialX + 10 * x < maxX; x += 1) {
151+
context.strokeRect(initialX*x, initialY, maxX-initialX, maxY-initialY);
152+
}
153+
</script>
154+
{% endhighlight %}
155+
156+
This is only the beginning of what you can do with the HTML canvas. In the next post, we'll cover paths, which are an extremely powerful set of tools for drawing almost any shape you can think of.

0 commit comments

Comments
 (0)