Created
June 14, 2014 15:38
-
-
Save hugoferreira/7002ae968d95c21654ac to your computer and use it in GitHub Desktop.
Artistic Sierpinski Triangle (Playing with Scala.JS)
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
import org.scalajs.dom | |
final case class Pt(x: Double, y: Double) | |
@JSExport | |
object SierpinskiTriangle { | |
val corners = Seq( | |
Pt(canvas.width/2, 0), | |
Pt(0, canvas.height), | |
Pt(canvas.width, canvas.height) | |
) | |
var p = corners(0) | |
val (w, h) = (canvas.height.toDouble, canvas.height.toDouble) | |
@JSExport | |
def main(args: Array[String]): Unit = { | |
dom.setInterval(() => for(_ <- 0 until 10){ | |
val c = corners(util.Random.nextInt(3)) | |
val s = util.Random.nextInt(5) | |
p = Pt((p.x + c.x) / 2, (p.y + c.y) / 2) | |
val m = (p.y / h) | |
val r = 255 - (p.x / w * m * 255).toInt | |
val g = 255 - ((w-p.x) / w * m * 255).toInt | |
val b = 255 - ((h - p.y) / h * 255).toInt | |
renderer.fillStyle = s"rgba($r, $g, $b, 0.02)" | |
renderer.beginPath() | |
renderer.arc(p.x, p.y, s, 0, Math.PI*2, true) | |
renderer.closePath() | |
renderer.fill() | |
}, 5) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment