1.7 Simple Drawing: "draw.rkt"
(require htdp/draw) |
The teachpack provides two sets of functions: one for drawing into a canvas and one for reacting to canvas events.
NOTE: This library is deprecated. Use 2htdp/image (probably in conjunction with 2htdp/universe) instead. You may continue to use the library for solving exercises from How To Design Programs, First Edition but do consider switching to How To Design Programs, Second Edition instead.
1.7.1 Drawing on a Canvas
DrawColor: (and/c symbol? (one-of/c 'white 'yellow 'red 'blue 'green 'black)) These six colors are definitely provided. If you want other colors, guess! For example, 'orange works, but 'mauve doesn’t. If you apply the function to a symbol that it doesn’t recognize as a color, it raises an error.
procedure
(start/cartesian-plane width height) → true
width : number? height : number?
procedure
(draw-circle p r c) → true
p : posn? r : number? c : DrawColor
procedure
(draw-solid-disk p r c) → true
p : posn? r : number? c : DrawColor
procedure
(draw-solid-rect ul width height c) → true
ul : posn? width : number? height : number? c : DrawColor
procedure
(draw-solid-line strt end c) → true
strt : posn? end : posn? c : DrawColor
procedure
(draw-solid-string p s) → true
p : posn? s : string?
procedure
(sleep-for-a-while s) → true
s : number?
The teachpack also provides clear- functions for each draw- function:
procedure
(clear-circle p r c) → true
p : posn? r : number? c : DrawColor
procedure
(clear-solid-disk p r c) → true
p : posn? r : number? c : DrawColor
procedure
(clear-solid-rect ul width height c) → true
ul : posn? width : number? height : number? c : DrawColor
procedure
(clear-solid-line strt end c) → true
strt : posn? end : posn? c : DrawColor
procedure
(clear-solid-string p s) → true
p : posn? s : string?
1.7.2 Interactions with Canvas
procedure
(wait-for-mouse-click) → posn?
procedure
(get-key-event) → (or/c false DrawKeyEvent)
DrawWorld: For proper interactions, using the teachpack requires that you provide a data definition for DrawWorld . In principle, there are no constraints on this data definition. You can even keep it implicit, even if this violates the Design Recipe.
The following functions allow programs to react to events from the canvas.
procedure
(on-key-event change) → true
change : (-> DrawKeyEvent DrawWorld DrawWorld)
procedure
(on-tick-event tock) → true
tock : (-> DrawWorld DrawWorld)
procedure
(end-of-time) → DrawWorld