Skip to content

Instantly share code, notes, and snippets.

@paweljw
Created May 28, 2017
Embed
What would you like to do?
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