Skip to content

Commit 4ed5f39

Browse files
committed
setup basic scalajs-threejs project
0 parents  commit 4ed5f39

File tree

9 files changed

+224
-0
lines changed

9 files changed

+224
-0
lines changed

.gitignore

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# scala
2+
target/
3+
.classpath
4+
*.class
5+
*.log
6+
7+
# idea
8+
.idea
9+
.idea_modules
10+
11+
# sbt specific
12+
.cache
13+
.history
14+
.lib/
15+
dist/*
16+
target/
17+
lib_managed/
18+
src_managed/
19+
project/project/
20+
project/target/
21+
project/project/
22+
project/plugins/project/
23+
24+
# Scala-IDE specific
25+
.project
26+
.settings/
27+
.scala_dependencies
28+
.worksheet

build.sbt

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import scalajs.sbtplugin.ScalaJSPlugin._
2+
import ScalaJSKeys._
3+
4+
lazy val sharedSettings = Seq(
5+
unmanagedSourceDirectories in Compile +=
6+
baseDirectory.value / "shared" / "main" / "scala",
7+
libraryDependencies ++= Seq(
8+
"com.scalatags" %%% "scalatags" % "0.4.2"
9+
, "com.lihaoyi" %%% "upickle" % "0.2.5"
10+
, "com.lihaoyi" %%% "autowire" % "0.2.3"
11+
),
12+
scalaVersion := "2.11.4",
13+
scalacOptions ++= Seq("-deprecation", "-feature")
14+
)
15+
16+
lazy val client = project.in(file("client"))
17+
.settings(scalaJSSettings: _*)
18+
.settings(sharedSettings: _*)
19+
.settings(bintraySettings:_*)
20+
.settings(
21+
resolvers += bintray.Opts.resolver.repo("denigma", "denigma-releases"),
22+
libraryDependencies ++= Seq(
23+
"org.scala-lang.modules.scalajs" %%% "scalajs-dom" % "0.6"
24+
, "com.github.japgolly.scalajs-react" %%% "core" % "0.7.0"
25+
// , "com.github.japgolly.scalajs-react" %%% "test" % "0.7.0" % "test"
26+
// , "com.github.japgolly.scalajs-react" %%% "ext-scalaz71" % "0.7.0"
27+
// , "com.github.japgolly.scalajs-react" %%% "ext-monocle" % "0.7.0"
28+
, "com.github.japgolly.scalajs-react" %%% "extra" % "0.7.0"
29+
, "org.scalajs" %%% "threejs" % "0.0.68-0.1.1"
30+
),
31+
ScalaJSKeys.jsDependencies ++= Seq(
32+
"org.webjars" % "react" % "0.12.1" / "react-with-addons.js" commonJSName "React",
33+
"org.webjars" % "showdown" % "0.3.1" / "showdown.js" commonJSName "Showdown",
34+
"org.webjars" % "three.js" % "r68" / "three.js" commonJSName "Three",
35+
scala.scalajs.sbtplugin.RuntimeDOM),
36+
// creates single js resource file for easy integration in html page
37+
skip in packageJSDependencies := false
38+
)
39+
40+
lazy val server = project.in(file("server"))
41+
.settings(sharedSettings: _*)
42+
.settings(
43+
libraryDependencies ++= Seq(
44+
"com.typesafe.akka" %% "akka-actor" % "2.3.8"
45+
, "com.typesafe.akka" % "akka-stream-experimental_2.11" % "1.0-M2"
46+
, "com.typesafe.akka" % "akka-http-experimental_2.11" % "1.0-M2"
47+
, "com.typesafe.akka" % "akka-http-core-experimental_2.11" % "1.0-M2"
48+
),
49+
(resources in Compile) += {
50+
(fastOptJS in (client, Compile)).value
51+
(artifactPath in (client, Compile, fastOptJS)).value
52+
},
53+
(resources in Compile) += {
54+
(packageJSDependencies in (client, Compile)).value
55+
(artifactPath in (client, Compile, packageJSDependencies)).value
56+
}
57+
)
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package reacttest
2+
3+
case class FileData(name: String, size: Long)
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package client
2+
3+
import scalajs.js
4+
import js.annotation.JSExport
5+
import org.scalajs.threejs._
6+
//import org.scalajs.threejs.THREE._
7+
8+
import org.scalajs.dom
9+
import dom. { document, window }
10+
//import scalatags.JsDom.all._
11+
12+
13+
@JSExport
14+
object Threejs {
15+
@JSExport
16+
def main(container: dom.HTMLDivElement) {
17+
18+
document.body.style.margin = "0"
19+
20+
val scene = new Scene
21+
22+
val camera = new PerspectiveCamera( 75, window.innerWidth / window.innerHeight, 0.1, 1000)
23+
24+
val renderer = new WebGLRenderer
25+
26+
renderer.setSize(window.innerWidth, window.innerHeight)
27+
28+
document.body.appendChild( renderer.domElement )
29+
30+
val geometry = new BoxGeometry(1, 1, 1)
31+
32+
val material = new MeshBasicMaterial(js.Dynamic.literal(
33+
color = new Color().setHex(0x00ff00)
34+
).asInstanceOf[MeshBasicMaterialParameters])
35+
36+
val cube = new Mesh( geometry, material )
37+
38+
scene.add(cube)
39+
40+
camera.position.z = 5
41+
42+
def render(param: Double): Unit = {
43+
44+
dom.requestAnimationFrame( render _)
45+
46+
cube.rotateX(0.1)
47+
cube.rotateY(0.1)
48+
49+
renderer.render( scene, camera )
50+
}
51+
52+
render(2)
53+
54+
}
55+
}

project/build.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version=0.13.7

project/plugins.sbt

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
resolvers += Resolver.url(
2+
"bintray-sbt-plugin-releases",
3+
url("http://dl.bintray.com/content/sbt/sbt-plugin-releases"))(
4+
Resolver.ivyStylePatterns)
5+
6+
addSbtPlugin("org.scala-lang.modules.scalajs" % "scalajs-sbt-plugin" % "0.5.6")
7+
8+
addSbtPlugin("me.lessis" % "bintray-sbt" % "0.1.2")
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package reacttest
2+
3+
case class FileData(name: String, size: Long)
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package server
2+
3+
import akka.actor.ActorSystem
4+
import akka.http.server.directives.AuthenticationDirectives._
5+
import akka.http.Http
6+
import akka.http.server.Directives
7+
import akka.stream.FlowMaterializer
8+
import akka.util.Timeout
9+
import akka.http.unmarshalling._
10+
11+
import akka.stream.scaladsl.Sink
12+
13+
import scala.concurrent.duration._
14+
15+
import akka.http.model.{ HttpEntity, MediaTypes }
16+
17+
import reacttest.FileData
18+
19+
object HttpServer extends App {
20+
21+
implicit val system = ActorSystem()
22+
implicit val materializer = FlowMaterializer()
23+
implicit val ec = system.dispatcher
24+
implicit val askTimeout: Timeout = 500.millis
25+
26+
27+
import Directives._
28+
29+
val binding = Http().bind(interface = "localhost", port = 8080)
30+
31+
val materializedMap = binding startHandlingWith {
32+
get {
33+
path("threejs") {
34+
complete {
35+
HttpEntity (
36+
MediaTypes.`text/html`,
37+
Page.htmlContent("Threejs")
38+
)
39+
}
40+
} ~
41+
getFromResourceDirectory("")
42+
}
43+
}
44+
45+
println(s"Server online at http://localhost:8080/\nPress RETURN to stop...")
46+
Console.readLine()
47+
48+
binding.unbind(materializedMap).onComplete(_ system.shutdown())
49+
50+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package server
2+
3+
import scalatags.Text.all._
4+
5+
object Page {
6+
7+
def htmlContent(client: String = "Client", elementId: String = "content") = html(
8+
head(
9+
meta(charset := "UTF-8"),
10+
script(src:= "/client-fastopt.js"),
11+
script(src:= "/client-jsdeps.js")
12+
),
13+
body(
14+
style:= "canvas: { height: 100%; width: 100% }",
15+
onload:= s"$client().main(document.getElementById('$elementId'))",
16+
div(id:=elementId)
17+
)
18+
).render
19+
}

0 commit comments

Comments
 (0)