-
-
Save paweljw/4bdd4c003b0aef21498b2d13cdb42b0d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Alien { | |
constructor (attachTo, size = 600, parts = 16, frequency = 0.5) { | |
this.two = new Two({ width: size, height: size }).appendTo(attachTo) | |
this.size = size | |
this.parts = parts | |
this.partSize = this.size / this.parts | |
this.frequency = frequency | |
} | |
draw () { | |
const fill = Color.random().toPastel().toString() | |
this.two.clear() | |
for (var i = 0; i < this.parts / 2; i++) { | |
for (var j = 0; j < this.parts; j++) { | |
if (Math.random() < this.frequency) { | |
var rect = this.two.makeRectangle(this._x(i), this._y(j), this.partSize, this.partSize) | |
var rect2 = this.two.makeRectangle(this._mirroredX(i), this._y(j), this.partSize, this.partSize) | |
rect.fill = rect2.fill = fill | |
rect.noStroke() | |
rect2.noStroke() | |
} | |
} | |
} | |
this.two.update() | |
} | |
_x (i) { | |
return i * this.partSize + this.partSize / 2 | |
} | |
_y (j) { | |
return this._x(j) | |
} | |
_mirroredX (i) { | |
return this.size - this._x(i) - this.partSize | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment