Skip to content

Commit

Permalink
stage 8
Browse files Browse the repository at this point in the history
  • Loading branch information
jacekkopecky committed Mar 26, 2020
1 parent a949be8 commit c69d965
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
34 changes: 34 additions & 0 deletions stages/classes.mjs
Original file line number Diff line number Diff line change
@@ -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();
}
}
2 changes: 1 addition & 1 deletion stages/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
<h1>Example</h1>
<canvas width=800 height=800></canvas>

<script src="index.js"></script>
<script src="index.js" type="module"></script>
39 changes: 2 additions & 37 deletions stages/index.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down

0 comments on commit c69d965

Please sign in to comment.