Skip to content

Instantly share code, notes, and snippets.

@carlosgruiz-dev
Last active July 19, 2022 03:37
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Embed
What would you like to do?
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