diff --git a/stages/classes.mjs b/stages/classes.mjs new file mode 100644 index 0000000..5e1d1b8 --- /dev/null +++ b/stages/classes.mjs @@ -0,0 +1,34 @@ +export class Shape { + constructor(x, y, col) { + this.x = x; + this.y = y; + this.col = col; + } +} + +export class Rectangle extends Shape { + constructor(x, y, width, height, col) { + super(x, y, col); + this.width = width; + this.height = height; + } + + draw(ctx) { + ctx.fillStyle = this.col; + ctx.fillRect(this.x, this.y, this.width, this.height); + } +} + +export class Circle extends Shape { + constructor(x, y, r, col) { + super(x, y, col); + this.r = r; + } + + draw(ctx) { + ctx.fillStyle = this.col; + ctx.beginPath(); + ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI); + ctx.fill(); + } +} diff --git a/stages/index.html b/stages/index.html index 16a0036..543c294 100644 --- a/stages/index.html +++ b/stages/index.html @@ -4,4 +4,4 @@

Example

- + diff --git a/stages/index.js b/stages/index.js index 148474d..f532087 100644 --- a/stages/index.js +++ b/stages/index.js @@ -1,44 +1,9 @@ 'use strict'; /** - * We refactor the `Rectangle` and `Circle` classes, - * taking the common properties and functions and - * moving them to a new _superclass_ called `Shape`. + * We moved the classes to a separate file called `classes.mjs`. */ -class Shape { - constructor(x, y, col) { - this.x = x; - this.y = y; - this.col = col; - } -} - -class Rectangle extends Shape { - constructor(x, y, width, height, col) { - super(x, y, col); - this.width = width; - this.height = height; - } - - draw(ctx) { - ctx.fillStyle = this.col; - ctx.fillRect(this.x, this.y, this.width, this.height); - } -} - -class Circle extends Shape { - constructor(x, y, r, col) { - super(x, y, col); - this.r = r; - } - - draw(ctx) { - ctx.fillStyle = this.col; - ctx.beginPath(); - ctx.arc(this.x, this.y, this.r, 0, 2 * Math.PI); - ctx.fill(); - } -} +import { Circle, Rectangle } from './classes.mjs'; // create circle and rectangle objects const rect1 = new Rectangle(100, 50, 100, 200, 'crimson');