1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
|
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)
}
// Seems like you need this for sbt ~fastOptJS to work
// mkdir ~/.sbt/0.13/plugins/target/scala-2.10/sbt-0.13/classes
@JSExport
object ScalaJSExample {
val ctx = dom.document
.getElementById("canvas")
.asInstanceOf[dom.HTMLCanvasElement]
.getContext("2d")
.asInstanceOf[dom.CanvasRenderingContext2D]
var p = Point(128, 128)
def color = "black"
var enemies = List.fill(10)(Point(util.Random.nextInt(255), util.Random.nextInt(255)))
def clear() = {
ctx.fillStyle = color
ctx.fillRect(0, 0, 255, 255)
}
def run = {
clear()
ctx.fillStyle = "cyan"
p = Point(p.x, (p.y + 2) % 255)
ctx.fillRect(p.x, p.y, 5, 20)
enemies = for (enemy <- enemies) yield {
ctx.fillStyle = "red"
ctx.fillRect(enemy.x, enemy.y, 10, 10)
Point((enemy.x + 1) % 255, (enemy.y + 1) % 255)
}
}
@JSExport
def main(): Unit = {
dom.console.log("main")
dom.setInterval(() => run, 10)
}
}
|