diff options
author | Li Haoyi <haoyi@dropbox.com> | 2014-10-26 12:28:32 -0700 |
---|---|---|
committer | Li Haoyi <haoyi@dropbox.com> | 2014-10-26 12:28:32 -0700 |
commit | ab668dead5c3123eb9fb26b9e94c6eccabaf6ab7 (patch) | |
tree | f6f538f84db0cb10076c26d9ae6ed16a5de436ba /examples | |
download | hands-on-scala-js-ab668dead5c3123eb9fb26b9e94c6eccabaf6ab7.tar.gz hands-on-scala-js-ab668dead5c3123eb9fb26b9e94c6eccabaf6ab7.tar.bz2 hands-on-scala-js-ab668dead5c3123eb9fb26b9e94c6eccabaf6ab7.zip |
first commit
Diffstat (limited to 'examples')
-rw-r--r-- | examples/src/main/scala/Example.scala | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/examples/src/main/scala/Example.scala b/examples/src/main/scala/Example.scala new file mode 100644 index 0000000..5d315b2 --- /dev/null +++ b/examples/src/main/scala/Example.scala @@ -0,0 +1,33 @@ +import Math._ +import org.scalajs.dom + +object Example extends scalajs.js.JSApp{ + def main() = { + val canvas = + dom.document + .getElementById("example-canvas") + .asInstanceOf[dom.HTMLCanvasElement] + + val renderer = + canvas.getContext("2d") + .asInstanceOf[dom.CanvasRenderingContext2D] + + val (h, w) = (canvas.height, canvas.width) + var x = 0.0 + val graphs = Seq[(String, Double => Double)]( + ("red", sin), + ("green", x => 2 - abs(x % 8 - 4)), + ("blue", x => 3 * pow(sin(x / 12), 2) * sin(x)) + ).zipWithIndex + dom.setInterval(() => { + x = (x + 1) % w + if (x == 0) renderer.clearRect(0, 0, w, h) + else for (((color, func), i) <- graphs) { + val y = func(x/w * 75) * h/40 + h/3 * (i+0.5) + renderer.fillStyle = color + renderer.fillRect(x, y, 3, 3) + } + }, 10) + + } +}
\ No newline at end of file |