[cairo] Drawing Shape on Image with Transparency: HOW?
darethehair
darethehair at gmail.com
Thu Mar 19 13:31:10 PDT 2009
Hello, Newbie here...
I am able to do the following using PyGTK and Cairo:
1) Load a PNG image (of the full moon)
2) Display it in a window
3) Rotate and scale the image as I desire
4) Generate an enclosed shape (using a series of 'context.line_to(x,y)'
commands)
5) Paint (overlay) the shape drawing onto my PNG image display
....however, I am NOT able to do the last step:
6) Make the shape overlay semi-transparent (I am trying to simulate the
'dimming' of an unlit display of the moon i.e. quarter moon).
What happens is that the shape is opaque, even if I use
'context.set_source_rgba(x,y,z,t)'. I think I am very close, but all my
attempts have resulted in the loss of hair :)
Can anyone help?
My 'kluge' doesn't work properly i.e. painting a copy of the PNG surface
onto the original with alpha, since the copy of the image is not
scaled. Plus, this seems silly.
Thanks!
Here are some snippets of my code:
self.context = widget.window.cairo_create()
self.context.set_operator(cairo.OPERATOR_SOURCE)
self.context.set_source_rgba(1.0, 1.0, 1.0, 0.0)
self.context.paint()
scaler = cairo.Matrix()
if window_in_fullscreen == False:
scaler.scale(1.0,1.0)
else:
scaler.scale(800.0/screen_width,480.0/screen_height)
scaler.translate(400,240)
scaler.rotate(-moon_zenith_angle *pi/180)
scaler.translate(-400,-240)
image_pattern.set_matrix(scaler)
image_pattern.set_filter(cairo.FILTER_BEST)
self.context.set_source(image_pattern)
# set a clip region for the expose event
self.context.rectangle(event.area.x, event.area.y,
event.area.width, event.area.height)
self.context.clip()
self.context.translate(400,240)
self.context.rotate(moon_zenith_angle *pi/180)
self.context.translate(-400,-240)
image = cairo.ImageSurface.create_from_png(moon_file)
image_width = image.get_width()
image_height = image.get_height()
image_pattern = cairo.SurfacePattern(image)
...
context.line_to(x,y)
...
context.paint()
context.set_source_rgba(0.0, 0.0, 0.0, 1.0)
context.fill()
context.stroke()
# this is my silly kluge that is ugly and does not work 100%
s2 = cairo.ImageSurface.create_from_png(moon_file)
s2_pattern = cairo.SurfacePattern(s2)
context.set_source_surface(s2, 0, 0)
context.paint_with_alpha(0.4)
Dare
More information about the cairo
mailing list