[pygtk] Re: [cairo] pycairo - translate question

Carl Worth cworth at cworth.org
Tue Nov 6 02:48:41 WST 2007


On Mon, 5 Nov 2007 17:38:19 +0200, Donn wrote:
> Hi, not sure if this is the appropriate list.

Yes, definitely the right list. Welcome, Donn!

> Can one, within a single cairo context, do this:
>
> while True:
>   ctx.translate(0,0)
>   <pycairo commands to draw a square at 0,0>
>   ctx.translate(10,10)
>   <pycairo commands to draw a circle at 0,0>
>   ctx.translate(50,50)
>   <pycairo commands to draw a Black Night at 11,44>

Absolutely!

> The idea is to keep all the <drawing commands> relative to 0,0 and use
> translate (and scale etc later) to actually move the results around the
> canvas.

That's a good model to use, and cairo supports it quite well.

> At the moment my tests are all over the place. Is there a way to reset the
> overall transform between each <> section?

What you're looking for is save/restore, like so:

	ctx.save()
	ctx.translate()
	scale.scale()
	draw_something()
	ctx.restore()

Often I will code all of my draw_something functions to also call
save() at the beginning and restore() at the end, which helps them to
act idempotently, (things like change to the source pattern won't
accidentally leak out, for example).

I hope that helps. And have fun with cairo!

-Carl
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 189 bytes
Desc: not available
Url : http://www.daa.com.au/pipermail/pygtk/attachments/20071105/5dc4f98c/attachment-0001.pgp


More information about the pygtk mailing list