Skip to content

Commit e15cc10

Browse files
committed
stage 6
1 parent d84ce2b commit e15cc10

File tree

1 file changed

+24
-8
lines changed

1 file changed

+24
-8
lines changed

stages/index.js

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,8 @@
11
'use strict';
22
/**
3-
* Objects can contain methods.
4-
*
5-
* Here, we move the `drawRect` function into the class
6-
* definition as a `draw` method.
7-
*
8-
* Methods have access to instance
9-
* properties by using `this`.
3+
* We add a `Circle` class to complement our `Rectangle`.
4+
* Circles have `x` and `y` position as well
5+
* as a radius (`r`).
106
*/
117

128
class Rectangle {
@@ -24,12 +20,32 @@ class Rectangle {
2420
}
2521
}
2622

27-
// create the two rectangle objects
23+
class Circle {
24+
constructor(x, y, r, col) {
25+
this.x = x;
26+
this.y = y;
27+
this.r = r;
28+
this.col = col;
29+
}
30+
31+
draw(ctx) {
32+
ctx.fillStyle = this.col;
33+
ctx.beginPath();
34+
ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
35+
ctx.fill();
36+
}
37+
}
38+
39+
// create circle and rectangle objects
2840
const rect1 = new Rectangle(100, 50, 100, 200, 'crimson');
2941
const rect2 = new Rectangle(300, 150, 100, 200, 'steelblue');
42+
const circ1 = new Circle(150, 350, 50, 'crimson');
43+
const circ2 = new Circle(350, 450, 50, 'steelblue');
3044

3145
// get a handle on the drawing canvas
3246
const ctx = document.querySelector('canvas').getContext('2d');
3347

3448
rect1.draw(ctx);
3549
rect2.draw(ctx);
50+
circ1.draw(ctx);
51+
circ2.draw(ctx);

0 commit comments

Comments
 (0)