def cairo_draw(canvas, cr): # Rather than using GTimeDate (which I couldn't find), we'll use Python time. t = time.localtime() s = t.tm_sec * math.pi / 30.0 m = t.tm_min * math.pi / 30.0 h = t.tm_hour * math.pi / 6.0 # Clear the texture contents, so that our drawing doesn't "stack." # If you don't call this, you'll notice lots of aliasing artifacts. canvas.clear() # Scale the modelview to the size of the surface. cr.scale(*canvas.get_surface_size()) cr.set_line_cap(cairo.LINE_CAP_ROUND) cr.set_line_width(0.1) # The black rail that holds the seconds indicator. cr.set_source_rgb(0.0, 0.0, 0.0) cr.translate(0.5, 0.5) cr.arc(0.0, 0.0, 0.4, 0.0, math.pi * 2.0) cr.stroke() # The seconds indicator. cr.set_source_rgb(1.0, 1.0, 1.0) cr.move_to(0.0, 0.0) cr.arc(math.sin(s) * 0.4, -math.cos(s) * 0.4, 0.05, 0.0, math.pi * 2) cr.fill() # The minutes hand. Here we want to assign a Clutter-based color to a # cairo context, so we need to use Clutter wrappers. Clutter.cairo_set_source_color( cr, Clutter.Color.get_static(Clutter.StaticColor.CHAMELEON_DARK) ) cr.move_to(0.0, 0.0) cr.line_to(math.sin(m) * 0.4, -math.cos(m) * 0.4) cr.stroke() # The hours hand. cr.move_to(0.0, 0.0) cr.line_to(math.sin(h) * 0.2, -math.cos(h) * 0.2) cr.stroke() return True
def cairo_draw(canvas, cr): # Rather than using GTimeDate (which I couldn't find), we'll use Python time. t = time.localtime() s = t.tm_sec * math.pi / 30.0 m = t.tm_min * math.pi / 30.0 h = t.tm_hour * math.pi / 6.0 # Clear the texture contents, so that our drawing doesn't "stack." # If you don't call this, you'll notice lots of aliasing artifacts. canvas.clear() # Scale the modelview to the size of the surface. cr.scale(*canvas.get_surface_size()) cr.set_line_cap(cairo.LINE_CAP_ROUND) cr.set_line_width(0.1) # The black rail that holds the seconds indicator. cr.set_source_rgb(0.0, 0.0, 0.0) cr.translate(0.5, 0.5) cr.arc(0.0, 0.0, 0.4, 0.0, math.pi * 2.0) cr.stroke() # The seconds indicator. cr.set_source_rgb(1.0, 1.0, 1.0) cr.move_to(0.0, 0.0) cr.arc(math.sin(s) * 0.4, -math.cos(s) * 0.4, 0.05, 0.0, math.pi * 2) cr.fill() # The minutes hand. Here we want to assign a Clutter-based color to a # cairo context, so we need to use Clutter wrappers. Clutter.cairo_set_source_color( cr, Clutter.Color.get_static(Clutter.StaticColor.CHAMELEON_DARK)) cr.move_to(0.0, 0.0) cr.line_to(math.sin(m) * 0.4, -math.cos(m) * 0.4) cr.stroke() # The hours hand. cr.move_to(0.0, 0.0) cr.line_to(math.sin(h) * 0.2, -math.cos(h) * 0.2) cr.stroke() return True
def draw_clock(canvas, cr, width, height): # Clear the canvas cr.save() cr.set_operator(cairo.OPERATOR_CLEAR) cr.paint() cr.restore() # Get the current time, and compute the angles now = GLib.DateTime.new_now_local() seconds = now.get_second() * math.pi / 30 minutes = now.get_minute() * math.pi / 30 hours = now.get_hour() * math.pi / 6 # Normalize the coordinate space cr.scale(width, height) cr.set_line_cap(cairo.LINE_CAP_ROUND) cr.set_line_width(0.1) # The black rail that holds the seconds indicator color = Clutter.Color.get_static(Clutter.StaticColor.BLACK) Clutter.cairo_set_source_color(cr, color) cr.translate(0.5, 0.5) cr.arc(0, 0, 0.4, 0, math.pi * 2) cr.stroke() # The seconds indicator color = Clutter.Color.get_static(Clutter.StaticColor.WHITE) color.alpha = 196 Clutter.cairo_set_source_color(cr, color) cr.move_to(0, 0) cr.arc(math.sin(seconds) * 0.4, - math.cos(seconds) * 0.4, 0.05, 0, math.pi * 2) cr.fill() # The minutes hand color = Clutter.Color.get_static(Clutter.StaticColor.CHAMELEON_DARK) color.alpha = 196 Clutter.cairo_set_source_color(cr, color) cr.move_to(0, 0) cr.line_to(math.sin(minutes) * 0.4, - math.cos(minutes) * 0.4) cr.stroke() # The hours hand cr.move_to(0, 0) cr.line_to(math.sin(hours) * 0.2, - math.cos(hours) * 0.2) cr.stroke() return True
def draw_clock(canvas, cr, width, height): # Clear the canvas cr.save() cr.set_operator(cairo.OPERATOR_CLEAR) cr.paint() cr.restore() # Get the current time, and compute the angles now = GLib.DateTime.new_now_local() seconds = now.get_second() * math.pi / 30 minutes = now.get_minute() * math.pi / 30 hours = now.get_hour() * math.pi / 6 # Normalize the coordinate space cr.scale(width, height) cr.set_line_cap(cairo.LINE_CAP_ROUND) cr.set_line_width(0.1) # The black rail that holds the seconds indicator color = Clutter.Color.get_static(Clutter.StaticColor.BLACK) Clutter.cairo_set_source_color(cr, color) cr.translate(0.5, 0.5) cr.arc(0, 0, 0.4, 0, math.pi * 2) cr.stroke() # The seconds indicator color = Clutter.Color.get_static(Clutter.StaticColor.WHITE) color.alpha = 196 Clutter.cairo_set_source_color(cr, color) cr.move_to(0, 0) cr.arc(math.sin(seconds) * 0.4, -math.cos(seconds) * 0.4, 0.05, 0, math.pi * 2) cr.fill() # The minutes hand color = Clutter.Color.get_static(Clutter.StaticColor.CHAMELEON_DARK) color.alpha = 196 Clutter.cairo_set_source_color(cr, color) cr.move_to(0, 0) cr.line_to(math.sin(minutes) * 0.4, -math.cos(minutes) * 0.4) cr.stroke() # The hours hand cr.move_to(0, 0) cr.line_to(math.sin(hours) * 0.2, -math.cos(hours) * 0.2) cr.stroke() return True
def draw_clock(self, canvas, cr, width, height): now = GLib.DateTime.new_now_local() seconds = GLib.DateTime.get_second(now) * pi / 30 minutes = GLib.DateTime.get_minute(now) * pi / 30 hours = GLib.DateTime.get_hour(now) * pi / 6 cr.save() # clear the contents of the canvas, to avoid painting # over the previous frame cr.set_operator(cairo.OPERATOR_CLEAR) cr.paint() cr.restore() cr.set_operator(cairo.OPERATOR_OVER) # scale the modelview to the size of the surface cr.scale(width, height) cr.set_line_cap(cairo.LINE_CAP_ROUND) cr.set_line_width(0.1) # the black rail that holds the seconds indicator Clutter.cairo_set_source_color(cr, self.colors['white']) cr.translate(0.5, 0.5) cr.arc(0, 0, 0.4, 0, pi * 2) cr.stroke() # the seconds indicator Clutter.cairo_set_source_color(cr, self.colors['black']) cr.move_to(0, 0) cr.arc(sin(seconds) * 0.4, -cos(seconds) * 0.4, 0.05, 0, pi * 2) cr.fill() # the minutes hand Clutter.cairo_set_source_color(cr, self.colors['hand']) cr.move_to(0, 0) cr.line_to(sin(minutes) * 0.4, -cos(minutes) * 0.4) cr.stroke() # the hours hand cr.move_to(0, 0) cr.line_to(sin(hours) * 0.2, -cos(hours) * 0.2) cr.stroke() # we're done drawing return True
def draw_clock (self, canvas, cr, width, height): now = GLib.DateTime.new_now_local () seconds = GLib.DateTime.get_second (now) * pi / 30 minutes = GLib.DateTime.get_minute (now) * pi / 30 hours = GLib.DateTime.get_hour (now) * pi / 6 cr.save () # clear the contents of the canvas, to avoid painting # over the previous frame cr.set_operator (cairo.OPERATOR_CLEAR) cr.paint () cr.restore () cr.set_operator (cairo.OPERATOR_OVER) # scale the modelview to the size of the surface cr.scale (width, height) cr.set_line_cap (cairo.LINE_CAP_ROUND) cr.set_line_width (0.1) # the black rail that holds the seconds indicator Clutter.cairo_set_source_color (cr, self.colors['white']) cr.translate (0.5, 0.5) cr.arc (0, 0, 0.4, 0, pi * 2) cr.stroke () # the seconds indicator Clutter.cairo_set_source_color (cr, self.colors['black']) cr.move_to (0, 0) cr.arc (sin (seconds) * 0.4, - cos (seconds) * 0.4, 0.05, 0, pi * 2) cr.fill () # the minutes hand Clutter.cairo_set_source_color (cr, self.colors['hand']) cr.move_to (0, 0) cr.line_to (sin (minutes) * 0.4, -cos (minutes) * 0.4) cr.stroke () # the hours hand cr.move_to (0, 0) cr.line_to (sin (hours) * 0.2, -cos (hours) * 0.2) cr.stroke () # we're done drawing return True