hopefully final bugfix, new shape - triangle, menu suppose to work now
This commit is contained in:
parent
619dbc5a42
commit
506e0a25a1
4 changed files with 83 additions and 16 deletions
|
@ -1,15 +1,31 @@
|
|||
import Rectangle from "./rectangle.js";
|
||||
import Circle from "./circle.js";
|
||||
import Triangle from "./triangle.js";
|
||||
|
||||
const options = [
|
||||
Rectangle,
|
||||
Circle
|
||||
const options = {
|
||||
rectangle: Rectangle,
|
||||
circle : Circle,
|
||||
triangle : Triangle
|
||||
};
|
||||
|
||||
let collection = [
|
||||
Circle, Rectangle, Triangle
|
||||
];
|
||||
|
||||
export default {
|
||||
createRandomShape: function() {
|
||||
let Shape = options[Math.floor(Math.random() * options.length)];
|
||||
let Shape = collection[Math.floor(Math.random() * collection.length)];
|
||||
|
||||
return Shape.fromOptions(Shape.randomOptions());
|
||||
},
|
||||
setCollection(obj) {
|
||||
collection = [];
|
||||
let keys = Object.keys(obj);
|
||||
for (let i = 0; i < keys.length; ++i) {
|
||||
let key = keys[i];
|
||||
if (obj[key]) {
|
||||
collection.push(options[key]);
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
|
|
37
painting/triangle.js
Normal file
37
painting/triangle.js
Normal file
|
@ -0,0 +1,37 @@
|
|||
import Shape from "./shape.js";
|
||||
|
||||
class Triangle extends Shape {
|
||||
constructor(x, y, color, x1, y1, x2, y2) {
|
||||
super(x, y, color);
|
||||
|
||||
this._x1 = x1;
|
||||
this._y1 = y1;
|
||||
this._x2 = x2;
|
||||
this._y2 = y2;
|
||||
}
|
||||
draw(ctx) {
|
||||
ctx.beginPath();
|
||||
ctx.moveTo(this._x, this._y);
|
||||
ctx.lineTo(this._x1, this._y1);
|
||||
ctx.lineTo(this._x2, this._y2);
|
||||
ctx.fill();
|
||||
}
|
||||
|
||||
static randomOptions() {
|
||||
let opts = super.randomOptions();
|
||||
let opts1 = super.randomOptions();
|
||||
let opts2 = super.randomOptions();
|
||||
|
||||
opts.x1 = opts1.x;
|
||||
opts.y1 = opts1.y;
|
||||
opts.x2 = opts2.x;
|
||||
opts.y2 = opts2.y;
|
||||
|
||||
return opts;
|
||||
}
|
||||
static fromOptions(o) {
|
||||
return new Triangle(o.x, o.y, o.color, o.x1, o.y1, o.x2, o.y2);
|
||||
}
|
||||
}
|
||||
|
||||
export default Triangle;
|
Loading…
Add table
Add a link
Reference in a new issue