From 2c178fdd460a460586e8dd8986772f5d6fe681d0 Mon Sep 17 00:00:00 2001 From: Li Haoyi Date: Sun, 10 Aug 2014 15:55:15 -0700 Subject: Copy in an example project for better development --- example/build.sbt | 25 ++++++++++++ example/project/build.properties | 1 + example/project/build.sbt | 6 +++ example/src/main/resources/index-dev.html | 19 +++++++++ example/src/main/resources/index-opt.html | 18 +++++++++ .../src/main/scala/example/ScalaJSExample.scala | 46 ++++++++++++++++++++++ 6 files changed, 115 insertions(+) create mode 100644 example/build.sbt create mode 100644 example/project/build.properties create mode 100644 example/project/build.sbt create mode 100644 example/src/main/resources/index-dev.html create mode 100644 example/src/main/resources/index-opt.html create mode 100644 example/src/main/scala/example/ScalaJSExample.scala 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 @@ + + + + Example Scala.js application + + + + +
+ +
+ + + + + + 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 @@ + + + + Example Scala.js application + + + + +
+ +
+ + + + + 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) + } +} -- cgit v1.2.3