"Hello, Kotlin" example working with the new library

This commit is contained in:
pTalanov
2012-05-31 18:07:22 +04:00
parent 527c4585a3
commit f07523df1d
@@ -1,24 +1,42 @@
/* /*
This example is just simple text floating around. If u are using chrome, there is a bug that spoil the visuals. This shows simple text floating around.
*/ */
package hello package hello
import js.* import js.dom.html5.*
import html5.* import js.dom.html.window
import jquery.* import js.jquery.*
val canvas: HTMLCanvasElement
get() {
return window.document.getElementsByTagName("canvas").item(0)!! as HTMLCanvasElement
}
val context: CanvasContext
get() {
return canvas.getContext("2d")!!
}
val width: Double
get() {
return canvas.width
}
val height: Double
get() {
return canvas.height
}
val context = getContext()
val height = getCanvas().height
val width = getCanvas().width
// class representing a floating text // class representing a floating text
class HelloKotlin() { class HelloKotlin() {
var relX = 0.2 + 0.2 * Math.random() var relX = 0.2 + 0.2 * Math.random()
var relY = 0.4 + 0.2 * Math.random() var relY = 0.4 + 0.2 * Math.random()
val absX : Double val absX: Double
get() = (relX * width) get() = (relX * width)
val absY : Double val absY: Double
get() = (relY * height) get() = (relY * height)
var relXVelocity = randomVelocity() var relXVelocity = randomVelocity()
@@ -30,7 +48,7 @@ class HelloKotlin() {
{ {
context.font = "bold ${textHeightInPixels}px Georgia, serif" context.font = "bold ${textHeightInPixels}px Georgia, serif"
} }
val textWidthInPixels = context.measureText(message).width val textWidthInPixels = context.measureText(message)!!.width
fun draw() { fun draw() {
context.save() context.save()
@@ -38,7 +56,7 @@ class HelloKotlin() {
// if you using chrome chances are good you wont see the shadow // if you using chrome chances are good you wont see the shadow
context.shadowColor = "#000000" context.shadowColor = "#000000"
context.shadowBlur = 5.0 context.shadowBlur = 5.0
context.shadowOffsetX = -4.0 context.shadowOffsetX = - 4.0
context.shadowOffsetY = 4.0 context.shadowOffsetY = 4.0
context.fillStyle = "rgb(242,160,110)" context.fillStyle = "rgb(242,160,110)"
context.fillText(message, absX.toInt(), absY.toInt()) context.fillText(message, absX.toInt(), absY.toInt())
@@ -48,21 +66,21 @@ class HelloKotlin() {
fun move() { fun move() {
val relTextWidth = textWidthInPixels / width val relTextWidth = textWidthInPixels / width
if (relX > (1.0 - relTextWidth - relXVelocity.abs) || relX < relXVelocity.abs) { if (relX > (1.0 - relTextWidth - relXVelocity.abs) || relX < relXVelocity.abs) {
relXVelocity *= -1 relXVelocity *= - 1
} }
val relTextHeight = textHeightInPixels / height val relTextHeight = textHeightInPixels / height
if (relY > (1.0 - relYVelocity.abs) || relY < relYVelocity.abs + relTextHeight) { if (relY > (1.0 - relYVelocity.abs) || relY < relYVelocity.abs + relTextHeight) {
relYVelocity *= -1 relYVelocity *= - 1
} }
relX += relXVelocity relX += relXVelocity
relY += relYVelocity relY += relYVelocity
} }
fun randomVelocity() = 0.03 * Math.random() * (if (Math.random() < 0.5) 1 else -1) fun randomVelocity() = 0.03 * Math.random() * (if (Math.random() < 0.5) 1 else - 1)
val Double.abs : Double val Double.abs: Double
get() = if (this > 0) this else -this get() = if (this > 0) this else - this
} }
fun renderBackground() { fun renderBackground() {
@@ -72,13 +90,14 @@ fun renderBackground() {
context.restore() context.restore()
} }
fun main(args : Array<String>) { fun main(args: Array<String>) {
jq {
val interval = 50 val interval = 50
// we pass a literal that constructs a new HelloKotlin object // we pass a literal that constructs a new HelloKotlin object
val logos = Array(3) { val logos = Array(3) {
HelloKotlin() HelloKotlin()
} }
jq {
setInterval({ setInterval({
renderBackground() renderBackground()
for (logo in logos) { for (logo in logos) {