Skip to content

Instantly share code, notes, and snippets.

@carlosgruiz-dev
Last active July 19, 2022 03:37
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 carlosgruiz-dev/daa84671cc10dd02f5f111c309724b3b to your computer and use it in GitHub Desktop.
Save carlosgruiz-dev/daa84671cc10dd02f5f111c309724b3b to your computer and use it in GitHub Desktop.
Playing with Scala.js and a canvas (code)
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