|
1 | 1 | 'use strict'; |
2 | 2 | /** |
3 | | - * We moved the classes to a separate file called `classes.mjs`. |
| 3 | + * We use an array instead of four variables, |
| 4 | + * resulting in cleaner code. |
4 | 5 | */ |
5 | 6 |
|
6 | 7 | import { Circle, Rectangle } from './classes.mjs'; |
7 | 8 |
|
8 | 9 | // create circle and rectangle objects |
9 | | -const rect1 = new Rectangle(100, 50, 100, 200, 'crimson'); |
10 | | -const rect2 = new Rectangle(300, 150, 100, 200, 'steelblue'); |
11 | | -const circ1 = new Circle(150, 350, 50, 'crimson'); |
12 | | -const circ2 = new Circle(350, 450, 50, 'steelblue'); |
| 10 | +const shapes = [ |
| 11 | + new Rectangle(100, 50, 100, 200, 'crimson'), |
| 12 | + new Rectangle(300, 150, 100, 200, 'steelblue'), |
| 13 | + new Circle(150, 350, 50, 'crimson'), |
| 14 | + new Circle(350, 450, 50, 'steelblue'), |
| 15 | +]; |
13 | 16 |
|
14 | 17 | // get a handle on the drawing canvas |
15 | 18 | const ctx = document.querySelector('canvas').getContext('2d'); |
16 | 19 |
|
17 | | -rect1.draw(ctx); |
18 | | -rect2.draw(ctx); |
19 | | -circ1.draw(ctx); |
20 | | -circ2.draw(ctx); |
| 20 | +for (const s of shapes) { |
| 21 | + s.draw(ctx); |
| 22 | +} |
0 commit comments