Skip to content

Instantly share code, notes, and snippets.

@sleepynate
Forked from anonymous/Main.scala
Created February 18, 2016 14:03
Show Gist options
  • Save sleepynate/ca947e08aa2629cc02b5 to your computer and use it in GitHub Desktop.
Save sleepynate/ca947e08aa2629cc02b5 to your computer and use it in GitHub Desktop.
Scala.js Bouncing Taco
import Math._
object ScalaJSExample extends js.JSApp{
class HTMLImageElement extends dom.raw.HTMLImageElement {
var onload: js.Function1[dom.Event, _] = ???
}
def newUnit() = 2 * Math.random
def newMutPlus() = {
val unit = newUnit()
(i:Double) => i + unit
}
def newMutMinus() = {
val unit = newUnit()
(i:Double) => i - unit
}
def main(): Unit = {
val (h, w) = (Page.canvas.height, Page.canvas.width)
var x = 0.0
var y = 0.0
val drawing = dom.document.createElement("img").asInstanceOf[HTMLImageElement]
drawing.src = "http://3.bp.blogspot.com/_HmGRbHGeQ0o/TT43TfKPwwI/AAAAAAAAGoI/NJBsM3g5OtY/s1600/taco-supreme.jpg"
var latMut:(Double => Double) = newMutPlus()
var vertMut:(Double => Double) = newMutPlus()
drawing.onload = { evt:dom.Event =>
val shrunkH = drawing.height / 2
val shrunkW = drawing.width / 2
dom.setInterval(() => {
if(x + drawing.width >= w) latMut = newMutMinus()
else if(x <= 0) latMut = newMutPlus()
if(y + drawing.height >= h) vertMut = newMutMinus()
else if(y <= 0) vertMut = newMutPlus()
Page.renderer.clearRect(0, 0, w, h)
Page.renderer.drawImage(drawing, x, y)
x = latMut(x)
y = vertMut(y)
}, 10)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment