Skip to content

Commit 8ed63ed

Browse files
committed
stage 2
1 parent 4e30d7b commit 8ed63ed

File tree

1 file changed

+13
-10
lines changed

1 file changed

+13
-10
lines changed

stages/index.js

+13-10
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
'use strict';
22
/**
3-
* If we want a _second_ rectangle (and simultaneously being
4-
* deliberately naïve) we might just create five additional
5-
* variables and duplicate our drawing code.
3+
* We can remove the repetition in the drawing
4+
* logic by making a function for it.
5+
*
6+
* The `drawRect` function has six parameters: the
7+
* drawing context and the five rectangle parameters.
68
*/
79

810
// create five variables
@@ -19,13 +21,14 @@ const rect2width = 100;
1921
const rect2height = 200;
2022
const rect2col = 'steelblue';
2123

24+
// draw a rectangle
25+
function drawRect(c, x, y, w, h, col) {
26+
c.fillStyle = col;
27+
c.fillRect(x, y, w, h);
28+
}
29+
2230
// get a handle on the drawing canvas
2331
const ctx = document.querySelector('canvas').getContext('2d');
2432

25-
// draw the rectangle
26-
ctx.fillStyle = rect1col;
27-
ctx.fillRect(rect1x, rect1y, rect1width, rect1height);
28-
29-
// draw it in the new position, with a different colour
30-
ctx.fillStyle = rect2col;
31-
ctx.fillRect(rect2x, rect2y, rect2width, rect2height);
33+
drawRect(ctx, rect1x, rect1y, rect1width, rect1height, rect1col);
34+
drawRect(ctx, rect2x, rect2y, rect2width, rect2height, rect2col);

0 commit comments

Comments
 (0)