3 # An example of an animation done with cairo and Gtk+
5 # Copyright (C) 2013 Antonio Ospite <ospite@studenti.unina.it>
7 # This program is free software: you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation, either version 3 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 # http://wiki.laptop.org/go/PyGTK/Smooth_Animation_with_PyGTK
25 gi.require_version('Gtk', '3.0')
26 from gi.repository import Gtk, GObject, Gio
32 class AnimatedCanvas(Gtk.DrawingArea):
33 def __init__(self, width, height, *args, **kwargs):
34 super(AnimatedCanvas, self).__init__(*args, **kwargs)
44 self.x = self.radius + self.border
45 self.y = self.radius + self.border
47 self.set_size_request(width, height)
48 self.connect('draw', self.do_draw_cb)
49 self.connect('configure-event', self.do_configure_event_cb)
52 GObject.timeout_add(1000 / fps, self.update)
55 if self.x - self.radius - self.border < 0 or \
56 self.x + self.radius + self.border > self.width:
59 if self.y - self.radius - self.border < 0 or \
60 self.y + self.radius + self.border > self.height:
63 self.x += self.x_offset
64 self.y += self.y_offset
69 def do_configure_event_cb(self, widget, event):
70 self.width = event.width
71 self.height = event.height
73 def do_draw_cb(self, widget, cr):
75 cr.rectangle(0, 0, self.width, self.height)
76 cr.set_source_rgb(1.0, 0.5, 0.5)
79 cr.arc(self.x, self.y, self.radius, 0, 2 * math.pi)
81 cr.set_line_width(self.border)
83 _h = self.x / float(self.width)
85 _s = self.y / float(self.height)
86 _r, _g, _b = colorsys.hls_to_rgb(_h, _l, _s)
87 cr.set_source_rgb(_r, _g, _b)
90 cr.set_source_rgb(0, 0, 0)
94 class AnimatedApp(Gtk.Application):
95 def __init__(self, title, width, height, *args, **kwargs):
96 super(AnimatedApp, self).__init__(*args, **kwargs)
97 self.connect("activate", self.on_activate)
102 def on_activate(self, data=None):
103 window = Gtk.Window(type=Gtk.WindowType.TOPLEVEL)
104 window.set_title(self.title)
106 canvas = AnimatedCanvas(self.width, self.height)
109 self.add_window(window)
112 if __name__ == "__main__":
113 app = AnimatedApp("AnimatedApp", 640, 480,
114 application_id="apps.test.animatedapp",
115 flags=Gio.ApplicationFlags.FLAGS_NONE)