Skip to content

Commit c69d965

Browse files
committed
stage 8
1 parent a949be8 commit c69d965

File tree

3 files changed

+37
-38
lines changed

3 files changed

+37
-38
lines changed

stages/classes.mjs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
export class Shape {
2+
constructor(x, y, col) {
3+
this.x = x;
4+
this.y = y;
5+
this.col = col;
6+
}
7+
}
8+
9+
export class Rectangle extends Shape {
10+
constructor(x, y, width, height, col) {
11+
super(x, y, col);
12+
this.width = width;
13+
this.height = height;
14+
}
15+
16+
draw(ctx) {
17+
ctx.fillStyle = this.col;
18+
ctx.fillRect(this.x, this.y, this.width, this.height);
19+
}
20+
}
21+
22+
export class Circle extends Shape {
23+
constructor(x, y, r, col) {
24+
super(x, y, col);
25+
this.r = r;
26+
}
27+
28+
draw(ctx) {
29+
ctx.fillStyle = this.col;
30+
ctx.beginPath();
31+
ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
32+
ctx.fill();
33+
}
34+
}

stages/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,4 +4,4 @@
44
<h1>Example</h1>
55
<canvas width=800 height=800></canvas>
66

7-
<script src="index.js"></script>
7+
<script src="index.js" type="module"></script>

stages/index.js

Lines changed: 2 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,9 @@
11
'use strict';
22
/**
3-
* We refactor the `Rectangle` and `Circle` classes,
4-
* taking the common properties and functions and
5-
* moving them to a new _superclass_ called `Shape`.
3+
* We moved the classes to a separate file called `classes.mjs`.
64
*/
75

8-
class Shape {
9-
constructor(x, y, col) {
10-
this.x = x;
11-
this.y = y;
12-
this.col = col;
13-
}
14-
}
15-
16-
class Rectangle extends Shape {
17-
constructor(x, y, width, height, col) {
18-
super(x, y, col);
19-
this.width = width;
20-
this.height = height;
21-
}
22-
23-
draw(ctx) {
24-
ctx.fillStyle = this.col;
25-
ctx.fillRect(this.x, this.y, this.width, this.height);
26-
}
27-
}
28-
29-
class Circle extends Shape {
30-
constructor(x, y, r, col) {
31-
super(x, y, col);
32-
this.r = r;
33-
}
34-
35-
draw(ctx) {
36-
ctx.fillStyle = this.col;
37-
ctx.beginPath();
38-
ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI);
39-
ctx.fill();
40-
}
41-
}
6+
import { Circle, Rectangle } from './classes.mjs';
427

438
// create circle and rectangle objects
449
const rect1 = new Rectangle(100, 50, 100, 200, 'crimson');

0 commit comments

Comments
 (0)