FLamparski

Simple Cairo animation

Jan 16th, 2013
47
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.58 KB | None | 0 0
  1. #!/usr/bin/env python3.3
  2.  
  3. from gi.repository import Gtk, Gdk, GLib
  4. import cairo, math, time
  5.  
  6.  
  7. class MyWindow (Gtk.Window):
  8.     def __init__ (self):
  9.         super().__init__(title="CairoAnim Test")
  10.         self.set_default_size(200, 220)
  11.         self.connect_after('destroy', self.destroy)
  12.  
  13.         self.canvas = Gtk.DrawingArea()
  14.         self.canvas.set_size_request(200, 200)
  15.         self.canvas.connect('draw', self.draw)
  16.  
  17.         self.fpslabel = Gtk.Label()
  18.        
  19.         self.box = Gtk.VBox()
  20.         self.box.pack_start(self.canvas, False, False, 0)
  21.         self.box.pack_start(self.fpslabel, True, False, 0)
  22.         self.add(self.box)
  23.  
  24.         self.show_all()
  25.        
  26.         self.frames = 0
  27.         self.lastFrame = 0
  28.         self.fps = 0
  29.         self.lastFps = time.time()
  30.         self.getFps()
  31.         GLib.timeout_add(16, self.update) # 16 ms per frame = 60 frames per second
  32.  
  33.     def getFps (self):
  34.         if time.time() - self.lastFps > 1:
  35.             self.fpslabel.set_markup("FPS: <b>{0}</b>".format(self.fps))
  36.             self.fps = 0
  37.             self.lastFps += 1
  38.         self.fps += 1
  39.    
  40.     def destroy (window, self):
  41.         Gtk.main_quit()
  42.  
  43.     def update (self):
  44.         self.canvas.queue_draw()
  45.         self.frames += 1
  46.         self.getFps()
  47.         return True
  48.  
  49.     def draw (self, da, ctx):
  50.         ctx.set_line_width(2)
  51.         ctx.set_source_rgb(0.8, 0, 0)
  52.         ctx.arc(100, 100, abs(math.sin(time.time()))*90, 0, 2 * math.pi)
  53.         ctx.stroke()
  54.  
  55.         ctx.set_source_rgb(0, 0.8, 0)
  56.         ctx.arc(100, 100, abs(math.sin(time.time()*2+90))*90, 0, 2 * math.pi)
  57.         ctx.stroke()
  58.  
  59.         ctx.set_source_rgb(0, 0, 0.8)
  60.         ctx.arc(100, 100, abs(math.sin(time.time()+180))*90, 0, 2 * math.pi)
  61.         ctx.stroke()
  62.  
  63. if __name__ == "__main__":
  64.     app = MyWindow()
  65.     Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment