|
1 | 1 | 'use strict';
|
2 | 2 | /**
|
3 |
| - * We can describe a rectangle in terms of five |
4 |
| - * properies: the x and y position of its top left |
5 |
| - * corner, its width, its height and its colour. |
6 |
| - * |
7 |
| - * We create five variables for these properties. |
8 |
| - * |
9 |
| - * We draw the rectangle, move it, and then draw it again. |
| 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. |
10 | 6 | */
|
11 | 7 |
|
12 | 8 | // create five variables
|
13 |
| -const rectX = 100; |
14 |
| -const rectY = 50; |
15 |
| -const rectWidth = 100; |
16 |
| -const rectHeight = 200; |
17 |
| -const rectCol = 'crimson'; |
| 9 | +const rect1x = 100; |
| 10 | +const rect1y = 50; |
| 11 | +const rect1width = 100; |
| 12 | +const rect1height = 200; |
| 13 | +const rect1col = 'crimson'; |
| 14 | + |
| 15 | +// create another five variables |
| 16 | +const rect2x = 300; |
| 17 | +const rect2y = 150; |
| 18 | +const rect2width = 100; |
| 19 | +const rect2height = 200; |
| 20 | +const rect2col = 'steelblue'; |
18 | 21 |
|
19 | 22 | // get a handle on the drawing canvas
|
20 | 23 | const ctx = document.querySelector('canvas').getContext('2d');
|
21 | 24 |
|
22 | 25 | // draw the rectangle
|
23 |
| -ctx.fillStyle = rectCol; |
24 |
| -ctx.fillRect(rectX, rectY, rectWidth, rectHeight); |
| 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); |
0 commit comments