cairo


quickest way to get started with cairo


I have taken passing shots at learning Cairo in the past, but always moved on in favor of some other graphics library. My problem is that I can't find a good tutorial that gives me a simple display for my surface. I have always ended up digging through GTK or QT documentation about things that have nothing to do with what I want to do. I want to learn Cairo, not a massive OO architecture.
What is a bare bones wrapper to give me a cross-platform window with a Cairo canvas to draw on?
I have used cairo for virtually anything involving drawing. I work at a medical software company, so I prototype scientific data visualization and other things.
I have usually three ways to display my drawings:
A GTK drawing area created with a Python script and GTK;
A PNG image displayed directly on screen using Python Image Library show() method;
A PNG image saved to disk, also via Python Image Library.
A simple script derived from cairographics examples, which actually I use as a template for any new project, is:
import gtk
class Canvas(gtk.DrawingArea):
def __init__(self):
super(Canvas, self).__init__()
self.connect("expose_event", self.expose)
self.set_size_request(800,500)
def expose(self, widget, event):
cr = widget.window.cairo_create()
rect = self.get_allocation()
# you can use w and h to calculate relative positions which
# also change dynamically if window gets resized
w = rect.width
h = rect.height
# here is the part where you actually draw
cr.move_to(0,0)
cr.line_to(w/2, h/2)
cr.stroke()
window = gtk.Window()
canvas = Canvas()
window.add(canvas)
window.set_position(gtk.WIN_POS_CENTER)
window.show_all()
gtk.main()
Or if you prefer not to deal with GUI toolkits, you can create and display an image on screen, and optionally save it to file:
import cairo, Image
width = 800
height = 600
surface = cairo.ImageSurface(cairo.FORMAT_ARGB32, width, height)
cr = cairo.Context(surface)
# optional conversion from screen to cartesian coordinates:
cr.translate(0, height)
cr.scale(1, -1)
# something very similar to Japanese flag:
cr.set_source_rgb(1,1,1)
cr.rectangle(0, 0, width, height)
cr.fill()
cr.arc(width/2, height/2, 150, 0, 6.28)
cr.set_source_rgb(1,0,0)
cr.fill()
im = Image.frombuffer("RGBA",
(width, height),
surface.get_data(),
"raw",
"BGRA",
0,1) # don't ask me what these are!
im.show()
# im.save('filename', 'png')
An answer to a related question demonstrates a very simple setup in Gtk2HS to draw on a drawingArea with Cairo.
import Graphics.UI.Gtk
import Graphics.Rendering.Cairo
main :: IO ()
main = do
initGUI
window <- windowNew
drawingArea <- drawingAreaNew
containerAdd window drawingArea
drawingArea `onExpose` (\_ -> renderScene drawingArea)
window `onDestroy` mainQuit
windowSetDefaultSize window 640 480
widgetShowAll window
mainGUI
renderScene :: DrawingArea -> IO Bool
renderScene da = do
dw <- widgetGetDrawWindow da
renderWithDrawable dw $ do setSourceRGBA 0.5 0.5 0.5 1.0
moveTo 100.0 100.0
showText "HelloWorld"
return True
Simply pass your Cairo animation routine to renderWithDrawable dw in renderScene.

Related Links

Implement character caret in Cairo
Creating a copy of the cairo_t
Get Cairo output stream content when using PyCairo/CairoSharp/Inkscape
How to clear cairo_region_t
Filling a custom-shaped Clutter Actor with a Cairo-drawn canvas
Why does Cairo.set_source_rgb paint the whole canvas?
Howto sub class a Clutter.Actor (involves Cairo/Clutter.Canvas)
'CAIRO_GOBJECT_TYPE_SURFACE' undeclared
Drawing in a Clutter Texture with Cairo
can't render text in cairo
Why when painting one source to another am i getting the wrong colors
Can I automatically flow two font styles in a Pango Cairo Layout?
Cairo Radial Gradient
How to create gradient between two curves?
Cairo in scrollable drawing area
Can cairo load a PDF?

Categories

HOME
xbox-live
cil
windows-7
agile
thunderbird-addon
memory-leaks
mapbox-gl-js
travis-ci
transparent
csvhelper
histogrammar
schemacrawler
proguard
goutte
structuremap
constructor
symfony-forms
react-leaflet
jacoco
rebol2
rapidjson
riak
opera-mini
pyephem
multiple-columns
koa
statusbar
logarithm
badge
non-deterministic
opencart2.3
rhandsontable
pptp
hibernate-cache
vegan
crt
android-vpn-service
fractals
idl
silverlight-3.0
stereo-3d
pdftk
walmart-electrode
gd
decompress
mcrypt
nuget-server
pagefile
teiid
date-range
verbose
push-diffusion
audioqueue
mongoose-populate
coordinate-transformation
spring-repositories
gemini
fill
persistence.xml
wif
harp
debian-based
swift2.1
httpie
consensus
odftoolkit
ibmsbt
livechat
funq
power-law
planetary.js
voldemort
opensocial
real-time-updates
backbone-relational
nuspec
symphony-cms
soa-suite
fraud-prevention
qss
mvs
cadisplaylink
applicationcontext
concurrent-programming
tcxgrid
nagle
sitemappath
internals
dirty-data

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App