Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/env python
from gi.repository import Clutter
import cairo
import math
color = lambda string: Clutter.color_from_string(string)[1] # shortcut
class CairoActor(Clutter.Actor):
'''a horizontal item inside a row'''
def __init__(self):
super(CairoActor, self).__init__()
self.set_background_color(color('orange'))
self.canvas = Clutter.Canvas()
self.set_content(self.canvas)
self.canvas.connect('draw', self.draw)
self.line_width = 5
def do_allocate(self, box, flags):
self.canvas.set_size(*box.get_size())
self.canvas.invalidate()
self.set_allocation(box, flags)
def draw(self, canvas, ctx, width, height):
# clear the previous frame
ctx.set_operator(cairo.OPERATOR_CLEAR)
ctx.paint()
ctx.set_operator(cairo.OPERATOR_OVER)
ctx.arc(width / 2, height / 2, height / 3, 0, 2 * math.pi)
Clutter.cairo_set_source_color(ctx, color('#33f9'))
ctx.fill_preserve()
ctx.set_line_width(self.line_width)
Clutter.cairo_set_source_color(ctx, color('red'))
ctx.stroke()
if __name__ == '__main__':
def stage_key(element, event):
if event.keyval == Clutter.Escape:
clutter_quit()
def clutter_quit(*args):
Clutter.main_quit()
Clutter.init([])
stage = Clutter.Stage()
stage.set_size(800, 800)
stage.set_title('Clutter - Cairo content')
stage.set_background_color(color('orange'))
stage.set_user_resizable(True)
# quit when the window gets closed
stage.connect('destroy', clutter_quit)
# close window on escape
stage.connect('key-press-event', stage_key)
cairo_actor = CairoActor()
stage.add_child(cairo_actor)
# bind the size of cairo_actor to the size of the stage
cairo_actor.add_constraint(Clutter.BindConstraint.new(stage, Clutter.BindCoordinate.SIZE, 0.0))
stage.show()
Clutter.main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment