Squeak SmalltalkJoker Squeak Smalltalk : Morphic : prevnext Drawing Morph

> It would be great if there were a way that was as easy to draw in
> Morphic as Milan is seeking.  If you draw onto the Display directly,
> Squeak is smart enough to flush things immediately as you draw.  It's
> not fast, but it's easy to use and understand.  On the other hand, if
> you play by Morphic's rules, then what can you do?

Implement a drawOn: method ;-)

> It would be great if there were some sort of CanvasMorph.  It could be
> used like this:
>
>     morph := CanvasMorph new.
>     morph openInWorld.
>
>     canvas := morph canvas.
>     canvas fillColor: (Color yellow).
>
> The response to #canvas, here, would be some kind of canvas that
> automatically sends #changed to the morph whenever a message is  sent to
> it.  You could easily implement such a canvas as subclass of
> PluggableCanvas....


Or, far simpler, just use a block:

============
Morph subclass: #CanvasMorph
    instanceVariableNames: 'drawBlock'
    classVariableNames: ''
    poolDictionaries: ''
    category: 'Bert-Bla'

initialize
    super initialize.
    self color: Color white.
    self extent: 400@400.

drawBlock: aBlock
    drawBlock := aBlock.
    self changed.

drawOn: aCanvas
    super drawOn: aCanvas.
    drawBlock ifNotNil: [
        aCanvas
            translateBy: self bounds origin
            clippingTo: self bounds
            during: [:canvas | drawBlock value: canvas]]
============

Then use like

    m := CanvasMorph new openInWorld.

to draw:

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

or

    m drawBlock: [:c |
        c line: 0@0 to: m extent width: 10 color: Color green.
        c line: 0@m height to: m width@0 width: 10 color: Color green.
    ]

With the latter you can even resize the morph ...

- Bert -