diff --git a/artworld/drawable.js b/artworld/drawable.js
index 096fe9f..27100a6 100644
--- a/artworld/drawable.js
+++ b/artworld/drawable.js
@@ -2,18 +2,17 @@ import { Vector2 } from "./math.js";
import { Color } from "./color.js";
import { Random } from "./random.js";
import { World } from "./world.js";
+import { getVal } from "./variables.js";
export class Drawable {
constructor(parent) {
this._parent = null;
this._world = null;
- // if parent is provided, set directly, then delegate to parent class
+ // if parent is provided, then delegate to parent class
if (parent instanceof World) {
- this._world = parent;
- this._world.drawable(this);
+ parent.registerDrawable(this);
} else if (parent) {
- this._parent = parent;
- this._parent.addChild(this);
+ parent.addChild(this);
}
this._fill = parent ? parent._fill : null;
this._stroke = parent ? parent._stroke : null;
@@ -110,14 +109,14 @@ export class Drawable {
get worldPos() {
// offset from parent if needed
return this._parent
- ? this._parent.worldPos.add(this._posVec)
- : this._posVec;
+ ? this._parent.worldPos.add(getVal(this._posVec))
+ : getVal(this._posVec);
}
}
export class Group extends Drawable {
- constructor() {
- super();
+ constructor(parent) {
+ super(parent);
this._children = [];
}
@@ -138,22 +137,22 @@ export class Group extends Drawable {
newGroup._parent = this._parent;
newGroup._parent.addChild(newGroup);
}
+ if (this._world) {
+ this._world.registerDrawable(newGroup);
+ }
return newGroup;
}
- draw() {
- for (let c of this._children) {
- c.draw();
- }
- }
+ draw() {}
addChild(drawable) {
this._children.push(drawable);
// probably alraedy true, but make sure
drawable._parent = this;
- // ensure we're in the same world
- drawable.world(this._world);
+ if (this._world) {
+ this._world.registerDrawable(drawable);
+ }
return this;
}
diff --git a/artworld/shapes.js b/artworld/shapes.js
index b979860..c8ef009 100644
--- a/artworld/shapes.js
+++ b/artworld/shapes.js
@@ -12,6 +12,10 @@ function makeCopy(Cls, obj, override) {
if (newObj._parent) {
newObj._parent.addChild(newObj);
}
+ // attach to world
+ if (newObj._world) {
+ newObj._world.registerDrawable(newObj);
+ }
return newObj;
}
diff --git a/artworld/world.js b/artworld/world.js
index aa29a73..3c451b0 100644
--- a/artworld/world.js
+++ b/artworld/world.js
@@ -29,9 +29,9 @@ export class World {
return v;
}
- drawable(thing) {
+ registerDrawable(thing) {
this.drawables.push(thing);
- thing._world = this; // set world for access to renderer (TODO: revisit)
+ thing._world = this;
return thing;
}
diff --git a/examples/circles.html b/examples/circles.html
index 8c5c577..b47ce57 100644
--- a/examples/circles.html
+++ b/examples/circles.html
@@ -4,7 +4,7 @@