@@ -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
7276class  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
340346fun  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