[racket] Drawing in the Canvas from outside

From: Matthew Flatt (mflatt at cs.utah.edu)
Date: Tue Jan 27 13:23:29 EST 2015

You can draw on the canvas from the outside using `get-dc`. Possibly,
though, you're seeing your drawing erased when the canvas receives a
refresh event from the window system.

See

 http://docs.racket-lang.org/gui/canvas___.html

for more about `on-paint` and the impermanence of drawing from the
outside.

If you need to draw from the outside and there's not a good way to
reconstruct the drawing at any time within `paint-callback`, then one
common strategy is: Draw to a bitmap, copy the bitmap to the canvas
whenever the bitmap content is changed, and also have `on-paint` copy
the bitmap to the canvas.




At Tue, 27 Jan 2015 20:08:17 +0200, Alexandr M wrote:
> Thanks,
> 
> I copied your example and it seems it doesn't work ..
> 
> If I comment the code inside [paint-callback .. it also doesn't draw
> anything.
> 
> In general I need to send 'messages'  from 'outside' to the canvas object
> to visualize numerical experiment performed by separate function.
> 
> I can implement experiment itself inside [pain-callback (lambda ...
> functions ) under definition of canvas, but it looks strange and clumsy.
> Sometimes I need just to run computations, but sometimes I need also to do
> visualization with animation.
> 
> How could I achieve this?
> 
> Best regards,
> Alex
> 
> On 27 January 2015 at 19:54, Matthias Felleisen <matthias at ccs.neu.edu>
> wrote:
> 
> >
> > Do you want something like this:
> >
> > #lang racket/gui
> >
> > (define frame (new frame% [label ""] [width 700] [height 500]))
> > (define canvas
> >  (new canvas%
> >       [parent frame]
> >       [paint-callback
> >        (lambda (c dc)
> >          (send dc draw-line 1 1 100 100))]))
> > (send frame show #true)
> >
> > (define (my-draw)
> >   (send (send canvas get-dc) draw-line 100 100 0 200))
> >
> >
> > -- Matthias
> >
> >
> >
> > On Jan 27, 2015, at 12:09 PM, Alexandr M wrote:
> >
> > > Hello,
> > >
> > > How can I draw the line (for example) in the canvas object defined as:
> > >
> > > (define frame (new frame% [label ""] [width 700] [height 500]))
> > > (define canvas (new canvas% [parent frame]
> > >   ;;;; - call from outside:  [paint-callback (lambda (c dc) (send dc
> > draw-line 1 1 10 10) )]
> > >   ))
> > >
> > > by calling a function like
> > >
> > > (define (MyDraw canvas-obj) .... (draw-line 1 1 10 10))
> > >
> > > ?
> > >
> > > --
> > > Best regards,
> > > Alex
> > > ____________________
> > >  Racket Users list:
> > >  http://lists.racket-lang.org/users
> >
> >
> 
> 
> -- 
> Best regards,
> Alexander Maslov
> ____________________
>   Racket Users list:
>   http://lists.racket-lang.org/users

Posted on the users mailing list.