diff options
Diffstat (limited to 'example/src/main/scala/example/ScalaJSExample.scala')
-rw-r--r-- | example/src/main/scala/example/ScalaJSExample.scala | 32 |
1 files changed, 16 insertions, 16 deletions
diff --git a/example/src/main/scala/example/ScalaJSExample.scala b/example/src/main/scala/example/ScalaJSExample.scala index 7a41c44..9df7b18 100644 --- a/example/src/main/scala/example/ScalaJSExample.scala +++ b/example/src/main/scala/example/ScalaJSExample.scala @@ -7,6 +7,7 @@ case class Point(x: Int, y: Int){ def +(p: Point) = Point(x + p.x, y + p.y) def /(d: Int) = Point(x / d, y / d) } + // Seems like you need this for sbt ~fastOptJS to work // mkdir ~/.sbt/0.13/plugins/target/scala-2.10/sbt-0.13/classes @JSExport @@ -17,31 +18,30 @@ object ScalaJSExample { .getContext("2d") .asInstanceOf[dom.CanvasRenderingContext2D] - var count = 0 - var p = Point(0, 0) - val corners = Seq(Point(255, 255), Point(0, 255), Point(128, 0)) + var p = Point(128, 128) + def color = "black" + var enemies = List.fill(10)(Point(util.Random.nextInt(255), util.Random.nextInt(255))) def clear() = { - ctx.fillStyle = "white" + ctx.fillStyle = color ctx.fillRect(0, 0, 255, 255) } - def run = for (i <- 0 until 10){ - if (count % 30000 == 0) clear() - count += 1 - p = (p + corners(Random.nextInt(3))) / 2 - val height = 512.0 / (255 + p.y) - val r = (p.x * height).toInt - val g = ((255-p.x) * height).toInt - val b = p.y - ctx.fillStyle = s"rgb($g, $r, $b)" - - ctx.fillRect(p.x, p.y, 1, 1) + def run = { + clear() + ctx.fillStyle = "cyan" + p = Point(p.x, (p.y + 2) % 255) + ctx.fillRect(p.x, p.y, 5, 20) + enemies = for (enemy <- enemies) yield { + ctx.fillStyle = "red" + ctx.fillRect(enemy.x, enemy.y, 10, 10) + Point((enemy.x + 1) % 255, (enemy.y + 1) % 255) + } } @JSExport def main(): Unit = { dom.console.log("main") - dom.setInterval(() => run, 50) + dom.setInterval(() => run, 10) } } |