From: Antonio Ospite Date: Fri, 8 May 2015 09:37:57 +0000 (+0200) Subject: cairo-gtk-animation.py: add a more efficient animation strategy X-Git-Url: https://git.ao2.it/experiments/cairo-gtk.git/commitdiff_plain/refs/heads/master?ds=sidebyside cairo-gtk-animation.py: add a more efficient animation strategy Follow what explained in http://wiki.laptop.org/go/PyGTK/Smooth_Animation_with_PyGTK about "Taking over the Event loop". --- diff --git a/cairo-gtk-animation.py b/cairo-gtk-animation.py index d6d67fe..d85f986 100755 --- a/cairo-gtk-animation.py +++ b/cairo-gtk-animation.py @@ -24,6 +24,7 @@ import gi gi.require_version('Gtk', '3.0') from gi.repository import Gtk, GObject, Gio +import time import math import colorsys @@ -47,9 +48,27 @@ class AnimatedCanvas(Gtk.DrawingArea): self.set_size_request(width, height) self.connect('draw', self.do_draw_cb) self.connect('configure-event', self.do_configure_event_cb) + self.connect('destroy', self.on_destroy) - fps = 30 - GObject.timeout_add(1000 / fps, self.update) + self.fps = 30 + GObject.timeout_add(50, self.mainloop) + + def on_destroy(self, event): + self.running = False + + def tick(self): + self.update() + self.queue_draw() + + def mainloop(self): + self.running = True + while self.running: + time.sleep(1.0 / self.fps) + self.tick() + while Gtk.events_pending(): + Gtk.main_iteration() + + return False def update(self): if self.x - self.radius - self.border < 0 or \ @@ -63,9 +82,6 @@ class AnimatedCanvas(Gtk.DrawingArea): self.x += self.x_offset self.y += self.y_offset - self.queue_draw() - return True - def do_configure_event_cb(self, widget, event): self.width = event.width self.height = event.height