Skip to content

Commit e34258e

Browse files
chore: update examples
1 parent 4f20447 commit e34258e

File tree

4 files changed

+39
-44
lines changed

4 files changed

+39
-44
lines changed

examples/02_control_flow/01_When.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ fun cases(obj: Any) {
2323
}
2424
}
2525

26-
class MyClass() { }
26+
class MyClass
2727
```
2828

2929
</div>

examples/03_special_classes/04_Object.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# Object keyword
2-
2+
33
First of all, let's start with some basic OOP concepts: a *class* is a blueprint, and an *object* is an instance of a class.
44
You define a class, then create multiple instances of that class:
55

examples/05_stdlib/02_withFunction.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,19 @@ prefix each member with the instance name.
66
<div class="language-kotlin" theme="idea" data-min-compiler-version="1.3">
77

88
```kotlin
9-
class Configuration(var host: String = "", var port: Int = 0, var isSSL: Boolean = false)
9+
class Configuration(var host: String, var port: Int)
1010

1111
fun main() {
12-
val configuration = Configuration()
13-
// TODO rewrite: make conform to code style
12+
val configuration = Configuration(host = "127.0.0.1", port = 9000)
13+
14+
//sampleStart
1415
with(configuration) {
15-
host = "127.0.0.1"
16-
port = 9000
17-
isSSL = true
16+
println("$host:$port")
1817
}
1918

20-
println(configuration.host)
19+
// instead of:
20+
println("${configuration.host}:${configuration.port}")
21+
//sampleEnd
2122
}
2223
```
2324

examples/09_Kotlin_JS/05_Canvas.md

Lines changed: 29 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -67,24 +67,30 @@ abstract class Shape() {
6767
}
6868
}
6969

70-
val Kotlin = Logo(v(250.0, 75.0))
70+
val logoImage by lazy { getImage("http://try.kotlinlang.org/static/images/kotlin_logo.svg") }
71+
72+
val logoImageSize = v(120.0, 30.0)
73+
74+
val Kotlin = Logo(v(canvas.width / 2.0 - logoImageSize.x / 2.0 - 40, canvas.height / 2.0 - logoImageSize.y / 2.0 - 20))
7175

7276
class Logo(override var pos: Vector) : Shape() {
7377
val relSize: Double = 0.18
7478
val shadowOffset = v(-3.0, 3.0)
75-
val imageSize = v(150.0, 150.0)
76-
var size: Vector = imageSize * relSize
79+
var size: Vector = logoImageSize * relSize
7780
// get-only properties like this saves you lots of typing and are very expressive
7881
val position: Vector
7982
get() = if (selected) pos - shadowOffset else pos
8083

8184

8285
fun drawLogo(state: CanvasState) {
83-
size = imageSize * (state.size.x / imageSize.x) * relSize
84-
// getKotlinLogo() is a 'magic' function here defined only for purposes of demonstration but in fact it just find an element containing the logo
85-
//state.context.drawImage(getImage("http://try.kotlinlang.org/static/images/canvas/Kotlin-logo.png"), 0.0, 0.0,
86+
if (!logoImage.complete) {
87+
state.changed = true
88+
return
89+
}
90+
91+
size = logoImageSize * (state.size.x / logoImageSize.x) * relSize
8692
state.context.drawImage(getImage("http://try.kotlinlang.org/static/images/kotlin_logo.svg"), 0.0, 0.0,
87-
imageSize.x, imageSize.y,
93+
logoImageSize.x, logoImageSize.y,
8894
position.x, position.y,
8995
size.x, size.y)
9096
}
@@ -206,15 +212,15 @@ class CanvasState(val canvas: HTMLCanvasElement) {
206212
val size: Vector
207213
get() = v(width.toDouble(), height.toDouble())
208214
val context = creatures.context
209-
var valid = false
215+
var changed = true
210216
var shapes = mutableListOf<Shape>()
211217
var selection: Shape? = null
212218
var dragOff = Vector()
213219
val interval = 1000 / 30
214220

215221
init {
216222
jq(canvas).mousedown {
217-
valid = false
223+
changed = true
218224
selection = null
219225
val mousePos = mousePos(it)
220226
for (shape in shapes) {
@@ -230,7 +236,7 @@ class CanvasState(val canvas: HTMLCanvasElement) {
230236
jq(canvas).mousemove {
231237
if (selection != null) {
232238
selection!!.pos = mousePos(it) - dragOff
233-
valid = false
239+
changed = true
234240
}
235241
}
236242

@@ -239,13 +245,13 @@ class CanvasState(val canvas: HTMLCanvasElement) {
239245
selection!!.selected = false
240246
}
241247
selection = null
242-
valid = false
248+
changed = true
243249
}
244250

245251
jq(canvas).dblclick {
246252
val newCreature = Creature(mousePos(it), this@CanvasState)
247253
addShape(newCreature)
248-
valid = false
254+
changed = true
249255
}
250256

251257
window.setInterval({
@@ -266,27 +272,27 @@ class CanvasState(val canvas: HTMLCanvasElement) {
266272

267273
fun addShape(shape: Shape) {
268274
shapes.add(shape)
269-
valid = false
275+
changed = true
270276
}
271277

272278
fun clear() {
273279
context.fillStyle = "#D0D0D0"
274-
// context.fillStyle = "#FFFFFF"
275280
context.fillRect(0.0, 0.0, width.toDouble(), height.toDouble())
276281
context.strokeStyle = "#000000"
277282
context.lineWidth = 4.0
278283
context.strokeRect(0.0, 0.0, width.toDouble(), height.toDouble())
279284
}
280285

281286
fun draw() {
282-
if (valid) return
283-
287+
if (!changed) return
288+
289+
changed = false
290+
284291
clear()
285292
for (shape in shapes.asReversed()) {
286293
shape.draw(this)
287294
}
288295
Kotlin.draw(this)
289-
valid = true
290296
}
291297
}
292298

@@ -339,24 +345,12 @@ class Vector(val x: Double = 0.0, val y: Double = 0.0) {
339345

340346
fun main(args: Array<String>) {
341347
//sampleStart
342-
// TODO
343-
// jq {
344-
// val state = CanvasState(canvas)
345-
// state.addShape(Kotlin)
346-
// state.addShape(Creature(state.size * 0.25, state))
347-
// state.addShape(Creature(state.size * 0.75, state))
348-
349-
val state = CanvasState(canvas).apply {
350-
addShape(Kotlin)
351-
addShape(Creature(size * 0.25, this))
352-
addShape(Creature(size * 0.75, this))
353-
}
354-
355-
window.setTimeout({
356-
state.valid = false
357-
}, 1000)
358-
// }
359-
//sampleEnd
348+
CanvasState(canvas).apply {
349+
addShape(Kotlin)
350+
addShape(Creature(size * 0.25, this))
351+
addShape(Creature(size * 0.75, this))
352+
}
353+
//sampleEnd
360354
}
361355
```
362356

0 commit comments

Comments
 (0)