Getting started with Cairo
Thursday, November 10th, 2005Using ghci is a great way of experimenting with Cairo. You might experience problems due to the way ghci and Gtk2Hs both require to be run concurrently. I’ve created a little code snippet that opens a dialog box, displays the given Cairo action and waits for you to click the Close button.
The run function will execute the given Cairo statements and will then continue to run the Gtk main loop until the dialog is actually removed from screen. This function can conveniently be used to test various Cairo commands. For example, try:
run (moveTo 200 200 >> lineTo 0 0 >> stroke)
If straight lines are too boring, you can do this:
run (moveTo 200 200 >> setLineWidth 20 >> curveTo 200 0 100 200 0 0 >> stroke)
To render text, you need to create a layout first:
run (moveTo 20 20 >> rotate (pi/4) >> createLayout "Haskell and Cairo is cool." >>= showLayout)
Note that the return value of createLayout is passed to showLayout.