diff --git a/artworld/drawable.js b/artworld/drawable.js index bd50e4f..e280c92 100644 --- a/artworld/drawable.js +++ b/artworld/drawable.js @@ -12,6 +12,7 @@ export class Drawable { this._strokeWeight = parent ? parent._strokeWeight : null; this._z_index = parent ? parent._z_index : null; this._posVec = new Vector2(0, 0); + this._animations = []; if (this._parent) { this._parent.addChild(this); } @@ -26,8 +27,16 @@ export class Drawable { throw new Error("draw() must be implemented on child class"); } - // do nothing by default - update() {} + animate(setter, func, args) { + this._animations.push({ setter, func, args }); + return this; + } + + update(t) { + for (let anim of this._animations) { + this[anim.setter](anim.func(t), anim.args); + } + } // setters diff --git a/artworld/math.js b/artworld/math.js index 8ea4263..b3d8609 100644 --- a/artworld/math.js +++ b/artworld/math.js @@ -33,6 +33,10 @@ export class Vector2 { ); } + get magnitude() { + return Math.sqrt(this.x * this.x + this.y * this.y); + } + static random(x, y) { let theta = Random.radians(); // if neither specified, use (1, 1) diff --git a/artworld/shapes.js b/artworld/shapes.js index e57dda4..96e8387 100644 --- a/artworld/shapes.js +++ b/artworld/shapes.js @@ -206,11 +206,11 @@ export class Polygon extends Drawable { point(vector, to_modify = null) { if (to_modify !== null) { - self._points[to_modify] = vector; + this._points[to_modify] = vector; } else { - self._points.push(point); + this._points.push(point); } - return self; + return this; } // TODO // contains(x, y) { diff --git a/artworld/world.js b/artworld/world.js index 912f57a..7e2d1e4 100644 --- a/artworld/world.js +++ b/artworld/world.js @@ -9,6 +9,7 @@ export class World { this.targetFrameRate = 60; this.stepSize = 1000 / this.targetFrameRate; this.lastTick = this.timer.time(); + this.numTicks = 0; } bindCanvas(id) { @@ -39,8 +40,9 @@ export class World { } tick() { + this.numTicks++; for (let d of this.drawables) { - d.update(); + d.update(this.numTicks); } } diff --git a/examples/clock.html b/examples/clock.html new file mode 100644 index 0000000..78e7f02 --- /dev/null +++ b/examples/clock.html @@ -0,0 +1,51 @@ + +
+ + + + +