summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-10-26 22:54:44 -0700
committerLi Haoyi <haoyi@dropbox.com>2014-10-26 22:54:44 -0700
commit6987ac5195b0112b4bda123f0fced3ace7115932 (patch)
tree6d38c52d6a0d7876cde7f25b8ef39667bd168ef1 /examples
parentab668dead5c3123eb9fb26b9e94c6eccabaf6ab7 (diff)
downloadhands-on-scala-js-6987ac5195b0112b4bda123f0fced3ace7115932.tar.gz
hands-on-scala-js-6987ac5195b0112b4bda123f0fced3ace7115932.tar.bz2
hands-on-scala-js-6987ac5195b0112b4bda123f0fced3ace7115932.zip
stuff works, paragraph-starting-tags still accidentally capturing entire paragraph
Diffstat (limited to 'examples')
-rw-r--r--examples/src/main/scala/Example.scala32
1 files changed, 22 insertions, 10 deletions
diff --git a/examples/src/main/scala/Example.scala b/examples/src/main/scala/Example.scala
index 5d315b2..b5e77ca 100644
--- a/examples/src/main/scala/Example.scala
+++ b/examples/src/main/scala/Example.scala
@@ -8,26 +8,38 @@ object Example extends scalajs.js.JSApp{
.getElementById("example-canvas")
.asInstanceOf[dom.HTMLCanvasElement]
+ def clear() = {
+ canvas.width = canvas.parentElement.clientWidth
+ canvas.height = canvas.parentElement.clientHeight
+ }
+ clear()
+
val renderer =
canvas.getContext("2d")
.asInstanceOf[dom.CanvasRenderingContext2D]
- val (h, w) = (canvas.height, canvas.width)
+ def h = canvas.height
+ def w = canvas.width
+
+ /*example*/
var x = 0.0
- val graphs = Seq[(String, Double => Double)](
+ type Graph = (String, Double => Double)
+ val graphs = Seq[Graph](
("red", sin),
- ("green", x => 2 - abs(x % 8 - 4)),
- ("blue", x => 3 * pow(sin(x / 12), 2) * sin(x))
+ ("green", x => 1 - abs(x % 4 - 2)),
+ ("blue", x => pow(sin(x/12), 2) * sin(x))
).zipWithIndex
- dom.setInterval(() => {
+ def run() = {
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)
+ if (x == 0) clear()
+ else for (((color, f), i) <- graphs) {
+ val offset = h / 3 * (i + 0.5)
+ val y = f(x / w * 75) * h / 40
renderer.fillStyle = color
- renderer.fillRect(x, y, 3, 3)
+ renderer.fillRect(x, y + offset, 3, 3)
}
- }, 10)
+ }
+ dom.setInterval(run _, 20)
}
} \ No newline at end of file