diff --git a/artworld/drawable.js b/artworld/drawable.js index 0eb0855..9e04729 100644 --- a/artworld/drawable.js +++ b/artworld/drawable.js @@ -15,7 +15,7 @@ export class Drawable { _register() { if (this._parent) { - this._parent.push(this); + this._parent._register(this); } artworld.register(this); } @@ -26,6 +26,12 @@ export class Drawable { return obj; } + draw() { + // TODO: log/raise? + } + + // setters + fill(color) { this._fill = color; return this; @@ -73,7 +79,10 @@ export class Drawable { // getters ///////////////////////////// get worldPos() { - return this._parent ? this._parent.worldPos.add(this._pos) : this._pos; + // offset from parent if needed + return this._parent + ? this._parent.worldPos.add(this._posVec) + : this._posVec; } } diff --git a/artworld/index.js b/artworld/index.js index f570896..31f3677 100644 --- a/artworld/index.js +++ b/artworld/index.js @@ -1,4 +1,7 @@ export { artworld } from "./world.js"; export { Color } from "./color.js"; -export { Vector2 } from "./math.js"; +export { Vector2, degToRad, radToDeg } from "./math.js"; export { Line } from "./shapes.js"; +export { Group } from "./drawable.js"; +export { Random } from "./random.js"; +console.log("here"); diff --git a/artworld/math.js b/artworld/math.js index b9ccc21..c4e9c2c 100644 --- a/artworld/math.js +++ b/artworld/math.js @@ -1,4 +1,12 @@ -export const TO_RADIANS = 180 / Math.PI; +const PI_180 = Math.PI / 180; + +export function degToRad(degrees) { + return degrees * PI_180; +} + +export function radToDeg(rad) { + return rad / PI_1i0; +} export class Vector2 { constructor(x, y) { @@ -7,15 +15,15 @@ export class Vector2 { } add(other) { - return Vector2(this.x + other.x, this.y + other.y); + return new Vector2(this.x + other.x, this.y + other.y); } sub(other) { - return Vector2(this.x - other.x, this.y - other.y); + return new Vector2(this.x - other.x, this.y - other.y); } scale(s) { - return Vector2(s * this.x, s * this.y); + return new Vector2(s * this.x, s * this.y); } static random(x, y) { @@ -28,10 +36,10 @@ export class Vector2 { if (y === undefined) { y = x; } - return Vector2(x * cos(theta), y * sin(theta)); + return new Vector2(x * cos(theta), y * sin(theta)); } static polar(mag, angle) { - return Vector2(mag * Math.cos(angle), mag * Math.sin(angle)); + return new Vector2(mag * Math.cos(angle), mag * Math.sin(angle)); } } diff --git a/artworld/random.js b/artworld/random.js new file mode 100644 index 0000000..1dcd910 --- /dev/null +++ b/artworld/random.js @@ -0,0 +1,5 @@ +export class Random { + static chance(odds) { + return Math.random() < odds; + } +} diff --git a/artworld/shapes.js b/artworld/shapes.js index aa58bf9..eaccebf 100644 --- a/artworld/shapes.js +++ b/artworld/shapes.js @@ -10,7 +10,7 @@ export class Line extends Drawable { draw() { artworld.setStrokeColor(this._stroke); artworld.setStrokeWeight(this._strokeWeight); - artworld.drawLine(this._posVec, this._offsetVec); + artworld.drawLine(this.worldPos, this.worldPos.add(this._offsetVec)); } to(vec) { @@ -18,10 +18,6 @@ export class Line extends Drawable { return this; } - polar(mag, angle) { - return this.to(Vector2.polar(mag, angle)); - } - angle(angle) { let mag = this._offsetVec.magnitude; return this.to(Vector2.polar(mag, angle)); diff --git a/artworld/world.js b/artworld/world.js index 42d2e66..b98507f 100644 --- a/artworld/world.js +++ b/artworld/world.js @@ -25,11 +25,13 @@ export class World { } setStrokeColor(color) { - this.ctx.strokeStyle = color.toStr(); + if (color && color.toStr) this.ctx.strokeStyle = color.toStr(); + else this.ctx.strokeStyle = color; } setFillColor(color) { - this.ctx.fillStyle = color.toStr(); + if (color && color.toStr) this.ctx.fillStyle = color.toStr(); + else this.ctx.fillStyle = color; } fillRect(rect) { diff --git a/examples/spiral.html b/examples/spiral.html new file mode 100644 index 0000000..807d985 --- /dev/null +++ b/examples/spiral.html @@ -0,0 +1,58 @@ + +
+ + + + +