summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorLi Haoyi <haoyi@dropbox.com>2014-08-10 15:55:15 -0700
committerLi Haoyi <haoyi@dropbox.com>2014-08-10 15:55:15 -0700
commit2c178fdd460a460586e8dd8986772f5d6fe681d0 (patch)
tree994bffd4ab11bb30889b6c2ed6c18c1822909943
parent1b85755a67fef592af64d11d33ad674f8d6a56b1 (diff)
downloadworkbench-2c178fdd460a460586e8dd8986772f5d6fe681d0.tar.gz
workbench-2c178fdd460a460586e8dd8986772f5d6fe681d0.tar.bz2
workbench-2c178fdd460a460586e8dd8986772f5d6fe681d0.zip
Copy in an example project for better development
-rw-r--r--example/build.sbt25
-rw-r--r--example/project/build.properties1
-rw-r--r--example/project/build.sbt6
-rw-r--r--example/src/main/resources/index-dev.html19
-rw-r--r--example/src/main/resources/index-opt.html18
-rw-r--r--example/src/main/scala/example/ScalaJSExample.scala46
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)
+ }
+}