Iterating through Diameter of a Circle Morph

398 views Asked by At

I have a CircleMorph and I want to extend its diameter, possibly to create a small animation with it.

b := CircleMorph new.
b color: Color transparent.
b borderWidth: 2.
b extent:100 @ 100.
b openInWorld.

Would it be good if I used a loop or the step method to do this? If you recommend the step method, then how do I do that?

2

There are 2 answers

3
Helene Bilbo On BEST ANSWER

You might make a subclass of CircleMorph called GrowingCircleMorph. Then implement step:

step
   self extent: (self extent) + 1.
   (self extent) > (200@200) ifTrue: [ self stopStepping ]

Now if you open an instance of your new GrowingCircleMorph in the World it will start growing up to 201@201.

To change the speed, implement stepTime and return the desired time between steps in milliseconds.

update: if you want the center to stay the same, change the bounds of your circle morph, not the extent:

step
   self bounds: ((self bounds) expandBy: 1).
   (self extent) > (200@200) ifTrue: [ self stopStepping ]
0
philippeback On

If you want to have fun with such things, make it generic:

HOCanvasMorph>>drawOn: aCanvas
    super drawOn: aCanvas.

    drawBlock ifNotNil: [
      aCanvas
        translateBy: self bounds origin
        clippingTo: self bounds
        during: [:canvas | drawBlock value: canvas]]

    HOCanvasMorph>>drawBlock: aBlock
        drawBlock := aBlock.
        self changed.

HOCanvasMorph class>>example
|m|

m := HOCanvasMorph new openInWorld.

"to draw:"

m drawBlock: [:c |
    c line: 10@10 to: 100@100 color: Color red
]

HOCanvasMorph class>>napperons
"Some mathematical fun"

| m |
m := HOCanvasMorph new openInWorld. "to draw:"
m
    drawBlock: [ :aCanvas | 
        | n r t xa xc xb yc ya yb longueur nombreDeDroites |
        longueur := 150.
        nombreDeDroites := 30.  "super drawOn: aCanvas."
        xc := 200.
        yc := 200.
        n := 15.
        r := 100.
        0 to: n - 2 do: [ :i | 
            t := 2 * Float pi * i / n.
            i + 1 to: n - 1 do: [ :j | 
                | tj |
                tj := 2 * Float pi * j / n.
                xa := xc + (r * t cos).
                ya := yc + (r * t sin).
                xb := xc + (r * tj cos).
                yb := yc + (r * tj sin).
                (aCanvas asBalloonCanvas) line: xa @ ya to: xb @ yb color: Color green ] ] ]

As the block can use about anything that changes, put those changes in a step method.