# PixbufSimpleAnim - looping animation

**URL:** <https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801>\
**Category:** Ruby-Gnome 2\
**Created:** [June 18, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801 "2008-06-18T18:21:45Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 18, 2008, 6:21pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/1 "2008-06-18T18:21:45Z")

</div>

Hello,

I’m trying to use the PixbufSimpleAnim, to show a animation generated by  
multiple image files. The problem is that the animation runs once, and  
then stops.

I saw in the samples that a gif file loaded using PixbufAnimation gives  
me a looping animation. How can I accomplish the same result using a  
PixbufSimpleAnim?

Thank you in advance,

AndrÃ©

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 3:10pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/2 "2008-06-19T15:10:25Z")

</div>

Anyone?

AndrÃ©

AndrÃ© Wagner wrote:

> Hello,
> 
> I’m trying to use the PixbufSimpleAnim, to show a animation generated by  
> multiple image files. The problem is that the animation runs once, and  
> then stops.
> 
> I saw in the samples that a gif file loaded using PixbufAnimation gives  
> me a looping animation. How can I accomplish the same result using a  
> PixbufSimpleAnim?
> 
> Thank you in advance,
> 
> AndrÃ©

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 4:19pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/3 "2008-06-19T16:19:11Z")

</div>

I dont even know whether this is possible

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 4:21pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/4 "2008-06-19T16:21:21Z")

</div>

> I dont even know whether this is possible

Ok, thank you. I think I’ll write my own class to do that. I can share  
it later.

AndrÃ©

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 4:53pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/5 "2008-06-19T16:53:13Z")

</div>

Ok, I wrote a little class that does the animation. You can use it as a  
simple image, just pass the frames as a array of pixbufs in the  
initialization:

class ImageAnimation \< Gtk::Image

attr\_reader :fps, :frame\_time

def initialize(frames=[], fps=60, frame\_time=1)  
super()  
@frames, @fps, @frame\_time = frames, fps, frame\_time  
start\_animation  
end

def restart(frames=[], fps=60, frame\_time=1)  
@frames, @fps, @frame\_time = frames, fps, frame\_time  
@running = false  
@t.join if @t  
start\_animation  
end

def fps=(i)  
@fps = i  
start\_animation  
end

def frame\_time=(i)  
@frame\_time = i  
start\_animation  
end

private

def start\_animation  
return if not @frames or @fps == 0 or @frame\_time \< 1  
return if @frames.length == 0

```
@running = true
i = 0
@t = Thread.new do
  while @running and not self.destroyed?
    self.pixbuf = @frames[i]
    i += 1
    i = 0 if i == @frames.length
    sleep 1.0 / @fps.to_f * @frame_time.to_f
  end
end

```

end

end

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 5:57pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/6 "2008-06-19T17:57:01Z")

</div>

Hi,

I would rather implement this without the use of threads. If you use  
threads and touch GTK+ from several threads you need to run

Gdk::Threads.init

And run the code that updates GTK+ like:

Gdk::Threads.synchronize { self.pixbuf = @frames[i] }

The usual way of doing this with GTK+ is to use the Mainloop.

Ie. instead of starting a new thread you add a GTimeout  
(GLib::Timeout.add) that is called in certain intervals to swap the  
image.

Best Regards,  
Mikael H.

19 jun 2008 kl. 16.53 skrev André Wagner:

> def initialize(frames=, fps=60, frame\_time=1)  
> end
> 
> ```
> self.pixbuf = @frames[i]
> 
> ```
> 
> * * *
> 
> Check out the new [SourceForge.net](http://SourceForge.net) Marketplace.  
> It’s the best place to buy or sell services for  
> just about anything Open Source.  
> [http://sourceforge.net/services/buy/index.php](http://sourceforge.net/services/buy/index.php)
> 
> * * *
> 
> ruby-gnome2-devel-en mailing list  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
> [https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en](https://lists.sourceforge.net/lists/listinfo/ruby-gnome2-devel-en)

–  
Mikael H.  
Imendio AB - Expert solutions in GTK+

> **[Tinybird Interactive (.se)](https://tinybird.se)**
>
> Tinybird Interactive - creating fun, useful, and inspiring apps for iPhone, iPad, and Mac.

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 6:23pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/7 "2008-06-19T18:23:10Z")

</div>

Am Donnerstag, den 19.06.2008, 17:55 +0200 schrieb Mikael H.:

> Ie. instead of starting a new thread you add a GTimeout  
> (GLib::Timeout.add) that is called in certain intervals to swap the  
> image.

That works not so well specialy for bigger images. The animation will  
not get smooth. Better you can do it in a loop like this:

Gtk.idle\_add\_priority(GLib::PRIORITY\_LOW) do  
now = Time.new  
tdiff = now - @last\_update  
@last\_update = now

# do the work

# sleep for at least 0.01 second to keep the app responsive

if @time\_per\_frame - tdiff \< 0.01  
sleep 0.01  
else  
sleep ï»¿@time\_per\_frame - tdiff  
end

---

<div class="post-metadata">

**Author:** ![andre\_nho](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/f1d935/32.png) [@andre\_nho](https://www.ruby-forum.com/u/andre_nho)\
**Post date:** [June 19, 2008, 6:49pm UTC](https://www.ruby-forum.com/t/pixbufsimpleanim-looping-animation/140801/8 "2008-06-19T18:49:48Z")

</div>

> I would rather implement this without the use of threads. If you use  
> threads and touch GTK+ from several threads you need to run
> 
> Gdk::Threads.init
> 
> And run the code that updates GTK+ like:
> 
> Gdk::Threads.synchronize { self.pixbuf = @frames[i] }
> 
> The usual way of doing this with GTK+ is to use the Mainloop.
> 
> Ie. instead of starting a new thread you add a GTimeout  
> (GLib::Timeout.add) that is called in certain intervals to swap the  
> image.

Thank you for this info!

But: why should I prefer using the mainloop instead of sychronized  
threads?

AndrÃ©
