diff options
Diffstat (limited to 'example')
-rw-r--r-- | example/build.sbt | 25 | ||||
-rw-r--r-- | example/project/build.properties | 1 | ||||
-rw-r--r-- | example/project/build.sbt | 6 | ||||
-rw-r--r-- | example/src/main/resources/index-dev.html | 19 | ||||
-rw-r--r-- | example/src/main/resources/index-opt.html | 18 | ||||
-rw-r--r-- | example/src/main/scala/example/ScalaJSExample.scala | 46 |
6 files changed, 115 insertions, 0 deletions
diff --git a/example/build.sbt b/example/build.sbt new file mode 100644 index 0000000..ceefedc --- /dev/null +++ b/example/build.sbt @@ -0,0 +1,25 @@ +// Turn this project into a Scala.js project by importing these settings +import scala.scalajs.sbtplugin.ScalaJSPlugin._ +import ScalaJSKeys._ +import com.lihaoyi.workbench.Plugin._ + +scalaJSSettings + +workbenchSettings + +name := "Example" + +scalaVersion := "2.11.2" + +version := "0.1-SNAPSHOT" + +resolvers += "Typesafe repository" at "http://repo.typesafe.com/typesafe/releases/" + +libraryDependencies ++= Seq( + "org.scala-lang.modules.scalajs" %%% "scalajs-dom" % "0.6" +) + +bootSnippet := "ScalaJSExample().main();" + +updateBrowsers <<= updateBrowsers.triggeredBy(ScalaJSKeys.fastOptJS in Compile) + diff --git a/example/project/build.properties b/example/project/build.properties new file mode 100644 index 0000000..0974fce --- /dev/null +++ b/example/project/build.properties @@ -0,0 +1 @@ +sbt.version=0.13.0 diff --git a/example/project/build.sbt b/example/project/build.sbt new file mode 100644 index 0000000..24009f8 --- /dev/null +++ b/example/project/build.sbt @@ -0,0 +1,6 @@ + +addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.3") + +lazy val root = project.in( file(".") ).dependsOn( + file("../..") +)
\ No newline at end of file diff --git a/example/src/main/resources/index-dev.html b/example/src/main/resources/index-dev.html new file mode 100644 index 0000000..b102613 --- /dev/null +++ b/example/src/main/resources/index-dev.html @@ -0,0 +1,19 @@ +<!DOCTYPE html> +<html> +<head> + <title>Example Scala.js application</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +</head> +<body style="margin: 0px"> + +<div> + <canvas style="display: block" id="canvas" width="255" height="255"/> +</div> + +<script type="text/javascript" src="../example-fastopt.js"></script> +<script type="text/javascript" src="/workbench.js"></script> +<script> + ScalaJSExample().main(); +</script> +</body> +</html> diff --git a/example/src/main/resources/index-opt.html b/example/src/main/resources/index-opt.html new file mode 100644 index 0000000..5abb478 --- /dev/null +++ b/example/src/main/resources/index-opt.html @@ -0,0 +1,18 @@ +<!DOCTYPE html> +<html> +<head> + <title>Example Scala.js application</title> + <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/> +</head> +<body style="margin: 0px"> + +<div> + <canvas style="display: block" id="canvas" width="255" height="255"/> +</div> + +<script type="text/javascript" src="../example-opt.js"></script> +<script> + ScalaJSExample().main(); +</script> +</body> +</html> diff --git a/example/src/main/scala/example/ScalaJSExample.scala b/example/src/main/scala/example/ScalaJSExample.scala new file mode 100644 index 0000000..7222143 --- /dev/null +++ b/example/src/main/scala/example/ScalaJSExample.scala @@ -0,0 +1,46 @@ +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 { + val ctx = dom.document + .getElementById("canvas") + .asInstanceOf[dom.HTMLCanvasElement] + .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 % 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) + } + @JSExport + def main(): Unit = { + dom.console.log("main") + + dom.setInterval(() => run, 50) + } +} |