Skip to content

Instantly share code, notes, and snippets.

@paweljw
Created May 28, 2017 15:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save paweljw/4bdd4c003b0aef21498b2d13cdb42b0d to your computer and use it in GitHub Desktop.
Save paweljw/4bdd4c003b0aef21498b2d13cdb42b0d to your computer and use it in GitHub Desktop.
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