Skip to content

Instantly share code, notes, and snippets.

@jbarciela
Forked from anonymous/Main.scala
Last active August 29, 2015 14:16
Show Gist options
  • Select an option

  • Save jbarciela/76c1337569676aa94764 to your computer and use it in GitHub Desktop.

Select an option

Save jbarciela/76c1337569676aa94764 to your computer and use it in GitHub Desktop.
//
//for use in http://www.scala-js-fiddle.com/
//
//from http://www.scala-js-fiddle.com/gist/9443f8e0ecc68d1058ad/LandingPage.scala
// Scala-Js-Fiddle comes with the following default imports at the top of every program:
// import scalatags.JsDom.all._
// import org.scalajs.dom
// import fiddle.Page
// import Page.{red, green, blue, yellow, orange, println}
// import scalajs.js
// This imports the Scala.js js and dom namespaces ready to use, imports the Page object and
// imports the scalatags.JsDom namespace to support the helper functions in Page.
import org.scalajs.dom
object ScalaJSExample extends js.JSApp{
def main() = {
//Page.clear();
println(script(src:="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.4.2/p5.js"))
println(div(id:="playground"))
println(div(id:="playground2"))
//http://www.scala-js.org/doc/calling-javascript.html
val document = js.Dynamic.global.document
val playground = document.getElementById("playground")
val newP = document.createElement("p")
newP.innerHTML = "Hello world! <i>-- DOM</i>"
playground.appendChild(newP)
println("test test test")
val xs = Seq(1, 2, 3, 4, 5)
println(xs.toString)
val ys = Seq(4, 5, 6, 7, 8, 9)
println(ys.toString)
val zs = for{
x <- xs
y <- ys
} yield x * y
println(zs.toString)
//int[] fibs = { 144, 89, 55, 34, 21};
val fibs: Array[Int] = Array(55, 34, 21)
//https://github.com/processing/p5.js/wiki/p5.js-overview#instantiation--namespace
//@JSExport
val sk : js.Function1[js.Dynamic, Unit] = (sketch: js.Dynamic) => {
val drawCirclesBranch = () => {
var x = 0;
var y = 0;
for(i <- 0 until fibs.length) {
sketch.ellipse(x, y, 2*fibs(i), 2*fibs(i));
if (i < fibs.length - 1) {
x = x + fibs(i) + fibs(i + 1);
}
}
}
sketch.setup = () => {
sketch.createCanvas(500, 500);
sketch.background(255);
sketch.strokeWeight(5);
sketch.smooth();
}
sketch.draw = () => {
sketch.translate(250, 250);
for (i <- 0 to 6) {
drawCirclesBranch();
sketch.rotate(sketch.radians(60));
}
sketch.fill(255);
sketch.rect(0,0,50,50);
}
}
//http://www.scala-js.org/doc/calling-javascript.html
//var myp5 = new p5(s);
//val myp5 = js.Dynamic.newInstance(js.Dynamic.global.p5)(sk)
//wait for p5 to load the hacky way
dom.setTimeout(() => {
val myp5 = js.Dynamic.newInstance(js.Dynamic.global.p5)(sk, document.getElementById("playground2"))
}, 1000)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment