random() fixes and liskov port
This commit is contained in:
parent
6b8b20d286
commit
627ce43e4f
@ -3,12 +3,6 @@ import { Vector2 } from "./math.js";
|
|||||||
import { Color } from "./color.js";
|
import { Color } from "./color.js";
|
||||||
import { Random } from "./random.js";
|
import { Random } from "./random.js";
|
||||||
|
|
||||||
function cloneObject(obj) {
|
|
||||||
for (let [k, v] of Object.entries(obj)) {
|
|
||||||
console.log(k, v);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export class Drawable {
|
export class Drawable {
|
||||||
constructor(parent) {
|
constructor(parent) {
|
||||||
this._parent = parent;
|
this._parent = parent;
|
||||||
|
@ -16,4 +16,8 @@ export class Random {
|
|||||||
static radians() {
|
static radians() {
|
||||||
return Math.random() * Math.PI * 2;
|
return Math.random() * Math.PI * 2;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static choice(array) {
|
||||||
|
return array[Math.floor(Random.under(array.length))];
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -24,9 +24,14 @@ export class Line extends Drawable {
|
|||||||
return makeCopy(Line, this, overrides);
|
return makeCopy(Line, this, overrides);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
random(range = 100) {
|
||||||
|
super.random();
|
||||||
|
this._offsetVec = Vector2.random(range, range);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {
|
||||||
artworld.setStrokeColor(this._stroke);
|
artworld.prepareDraw(this);
|
||||||
artworld.setStrokeWeight(this._strokeWeight);
|
|
||||||
artworld.drawLine(this.worldPos, this.worldPos.add(this._offsetVec));
|
artworld.drawLine(this.worldPos, this.worldPos.add(this._offsetVec));
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -48,10 +53,18 @@ export class Arc extends Drawable {
|
|||||||
this._startAngle = 0;
|
this._startAngle = 0;
|
||||||
this._endAngle = 180;
|
this._endAngle = 180;
|
||||||
}
|
}
|
||||||
|
|
||||||
copy(overrides) {
|
copy(overrides) {
|
||||||
return makeCopy(Arc, this, overrides);
|
return makeCopy(Arc, this, overrides);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
random(range = 100) {
|
||||||
|
super.random();
|
||||||
|
this._r = Random.between(20, range);
|
||||||
|
this._startAngle = Random.radians();
|
||||||
|
this._endAngle = Random.radians();
|
||||||
|
return this;
|
||||||
|
}
|
||||||
draw() {
|
draw() {
|
||||||
artworld.prepareDraw(this);
|
artworld.prepareDraw(this);
|
||||||
artworld.drawArc(this.worldPos, this._r, this._startAngle, this._endAngle);
|
artworld.drawArc(this.worldPos, this._r, this._startAngle, this._endAngle);
|
||||||
@ -80,6 +93,12 @@ export class Circle extends Drawable {
|
|||||||
return makeCopy(Circle, this, overrides);
|
return makeCopy(Circle, this, overrides);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
random(range = 100) {
|
||||||
|
super.random();
|
||||||
|
this._r = Random.between(20, range);
|
||||||
|
return this;
|
||||||
|
}
|
||||||
|
|
||||||
draw() {
|
draw() {
|
||||||
artworld.prepareDraw(this);
|
artworld.prepareDraw(this);
|
||||||
artworld.drawArc(this.worldPos, this._r, 0, 2 * Math.PI);
|
artworld.drawArc(this.worldPos, this._r, 0, 2 * Math.PI);
|
||||||
@ -98,7 +117,7 @@ export class Rect extends Drawable {
|
|||||||
this._height = 50;
|
this._height = 50;
|
||||||
}
|
}
|
||||||
|
|
||||||
random(range) {
|
random(range = 100) {
|
||||||
super.random();
|
super.random();
|
||||||
this._width = Random.between(20, range);
|
this._width = Random.between(20, range);
|
||||||
this._height = Random.between(20, range);
|
this._height = Random.between(20, range);
|
||||||
|
31
examples/liskov.html
Normal file
31
examples/liskov.html
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
<html>
|
||||||
|
<head> </head>
|
||||||
|
<body>
|
||||||
|
<canvas id="mainCanvas" width="500" height="500"></canvas>
|
||||||
|
<script type="module">
|
||||||
|
import {
|
||||||
|
artworld,
|
||||||
|
Color,
|
||||||
|
Pico8,
|
||||||
|
Group,
|
||||||
|
Rect,
|
||||||
|
Arc,
|
||||||
|
Line,
|
||||||
|
Circle,
|
||||||
|
Random,
|
||||||
|
Vector2,
|
||||||
|
degToRad,
|
||||||
|
} from "../artworld/index.js";
|
||||||
|
artworld.bindCanvas("mainCanvas");
|
||||||
|
window.artworld = artworld;
|
||||||
|
|
||||||
|
let types = [Rect, Line, Circle, Arc];
|
||||||
|
|
||||||
|
for (let i = 0; i < 100; i++) {
|
||||||
|
let Cls = Random.choice(types);
|
||||||
|
new Cls().random().strokeWeight(2);
|
||||||
|
}
|
||||||
|
artworld.draw();
|
||||||
|
</script>
|
||||||
|
</body>
|
||||||
|
</html>
|
Loading…
Reference in New Issue
Block a user