diff options
author | Haoyi Li <haoyi@haoyi-mbp.corp.dropbox.com> | 2014-11-26 00:44:45 -0800 |
---|---|---|
committer | Haoyi Li <haoyi@haoyi-mbp.corp.dropbox.com> | 2014-11-26 00:44:45 -0800 |
commit | b89ce9cbf79363f8cab09186a5d7ba94bc0af02a (patch) | |
tree | f68b3f02bad0375961bc99b972f6dc0f1f5dc5e7 /examples/workbench-example-app/src/main/scala/example/ScalaJSExample.scala | |
parent | c50f0d417b34d7a8be432c34bac92088734a14ca (diff) | |
parent | f2f03076c55939e454b8e3ce71408c1fd9cd1994 (diff) | |
download | hands-on-scala-js-b89ce9cbf79363f8cab09186a5d7ba94bc0af02a.tar.gz hands-on-scala-js-b89ce9cbf79363f8cab09186a5d7ba94bc0af02a.tar.bz2 hands-on-scala-js-b89ce9cbf79363f8cab09186a5d7ba94bc0af02a.zip |
Merge commit 'f2f03076c55939e454b8e3ce71408c1fd9cd1994' as 'examples/workbench-example-app'
Diffstat (limited to 'examples/workbench-example-app/src/main/scala/example/ScalaJSExample.scala')
-rw-r--r-- | examples/workbench-example-app/src/main/scala/example/ScalaJSExample.scala | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/examples/workbench-example-app/src/main/scala/example/ScalaJSExample.scala b/examples/workbench-example-app/src/main/scala/example/ScalaJSExample.scala new file mode 100644 index 0000000..e7e5326 --- /dev/null +++ b/examples/workbench-example-app/src/main/scala/example/ScalaJSExample.scala @@ -0,0 +1,43 @@ +package example +import scala.scalajs.js.annotation.JSExport +import org.scalajs.dom +import scala.util.Random + +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) +} + +@JSExport +object ScalaJSExample { + @JSExport + def main(canvas: dom.HTMLCanvasElement): Unit = { + val ctx = canvas.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)) + + def clear() = { + ctx.fillStyle = "black" + ctx.fillRect(0, 0, 255, 255) + } + + def run = for (i <- 0 until 10){ + if (count % 3000 == 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) + } + + dom.setInterval(() => run, 50) + } +} |