-
-
Save carlosgruiz-dev/daa84671cc10dd02f5f111c309724b3b to your computer and use it in GitHub Desktop.
Playing with Scala.js and a canvas (code)
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 scala.scalajs.js.annotation.JSExport | |
import org.scalajs.dom | |
import scala.util.Random | |
object Main { | |
@JSExport | |
def main(args: Array[String]): Unit = { | |
var canvas = dom.document.createElement("canvas") | |
.asInstanceOf[dom.html.Canvas] | |
dom.document.body.appendChild(canvas) | |
val height = dom.window.innerHeight.toInt | |
val width = dom.window.innerWidth.toInt | |
canvas.height = height | |
canvas.width = width | |
val ctx = canvas.getContext("2d") | |
.asInstanceOf[dom.CanvasRenderingContext2D] | |
def reColor() = { | |
val r = Random.nextInt(255) | |
val g = Random.nextInt(255) | |
val b = Random.nextInt(255) | |
ctx.fillStyle = s"rgb($r, $g, $b)" | |
ctx.fillRect(0, 0, width, height) | |
} | |
dom.window.setInterval(() => reColor, 1000) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment