Question about flushing Display on Canvas

Bert Freudenberg bert at
Tue May 3 09:53:05 UTC 2005

Am 02.05.2005 um 23:13 schrieb Lex Spoon:

> 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'

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

drawBlock: aBlock
     drawBlock := aBlock.
     self changed.

drawOn: aCanvas
     super drawOn: aCanvas.
     drawBlock ifNotNil: [
             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 at 10 to: 100 at 100 color: Color red


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

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

- Bert -

More information about the Squeak-dev mailing list