Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3.3
- from gi.repository import Gtk, Gdk, GLib
- import cairo, math, time
- class MyWindow (Gtk.Window):
- def __init__ (self):
- super().__init__(title="CairoAnim Test")
- self.set_default_size(200, 220)
- self.connect_after('destroy', self.destroy)
- self.canvas = Gtk.DrawingArea()
- self.canvas.set_size_request(200, 200)
- self.canvas.connect('draw', self.draw)
- self.fpslabel = Gtk.Label()
- self.box = Gtk.VBox()
- self.box.pack_start(self.canvas, False, False, 0)
- self.box.pack_start(self.fpslabel, True, False, 0)
- self.add(self.box)
- self.show_all()
- self.frames = 0
- self.lastFrame = 0
- self.fps = 0
- self.lastFps = time.time()
- self.getFps()
- GLib.timeout_add(16, self.update) # 16 ms per frame = 60 frames per second
- def getFps (self):
- if time.time() - self.lastFps > 1:
- self.fpslabel.set_markup("FPS: <b>{0}</b>".format(self.fps))
- self.fps = 0
- self.lastFps += 1
- self.fps += 1
- def destroy (window, self):
- Gtk.main_quit()
- def update (self):
- self.canvas.queue_draw()
- self.frames += 1
- self.getFps()
- return True
- def draw (self, da, ctx):
- ctx.set_line_width(2)
- ctx.set_source_rgb(0.8, 0, 0)
- ctx.arc(100, 100, abs(math.sin(time.time()))*90, 0, 2 * math.pi)
- ctx.stroke()
- ctx.set_source_rgb(0, 0.8, 0)
- ctx.arc(100, 100, abs(math.sin(time.time()*2+90))*90, 0, 2 * math.pi)
- ctx.stroke()
- ctx.set_source_rgb(0, 0, 0.8)
- ctx.arc(100, 100, abs(math.sin(time.time()+180))*90, 0, 2 * math.pi)
- ctx.stroke()
- if __name__ == "__main__":
- app = MyWindow()
- Gtk.main()
Advertisement
Add Comment
Please, Sign In to add comment