GIF speed not as expected! Help!

160 views (last 30 days)
Valerio Biscione
Valerio Biscione on 15 Mar 2015
Commented: DGM on 23 Apr 2023
Hello. I am trying to generate a GIF of a growing bar. However, the speed of the generating gif is really slow, and I don't understand how to make it faster. The 'DelayTime' is set to 0, and when the animation is generated in MATLAB is looks really fine. However, when I open the bar.gif, the animation is not fluid at all. This is the code
%draw rectangle and lambda
clear;
figure();
rectangle('Position',[0 2 10 1]);
axis off
set(gcf,'Position',[ -901 789 579 70])
t=0:0.05:3
l=1; a=0.5
p=@(t,l,a) 1-(1-a).*exp(-l.*t);
x=[0 0 10 10];
y=[2 3 3 2];
patch(x,y,'white');
frame=getframe(gca); im=frame2im(frame);
[imind,cm] = rgb2ind(im,256);
filename='bar.gif';
imwrite(imind,cm,filename,'gif','DelayTime',0.5, 'Loopcount',inf);
for tt=1:length(t)
pt=p(t(tt),l,a);
x=[0 0 10.*pt 10.*pt];
patch(x,y,'green');
% pause(.01); drawnow;
frame(tt)=getframe(gca);
end
for tt=1:length(t)
im=frame2im(frame(tt));
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,filename, 'gif','DelayTime',0,'WriteMode','append');
end
  1 Comment
John D'Errico
John D'Errico on 14 Nov 2017
Edited: John D'Errico on 14 Nov 2017
@DA Huang: Please don't answer a question with your own question. Use comments to make a comment.
Moved an answer by Da Huang into a comment:
"Have you solve this problem?"

Sign in to comment.

Accepted Answer

Utpal Kumar
Utpal Kumar on 7 Jul 2017
Edited and the delay time has been set to 0.1 seconds:
%draw rectangle and lambda
clear;
figure();
rectangle('Position',[0 2 10 1]);
axis off
set(gcf,'Position',[ -901 789 579 70])
t=0:0.05:3
l=1; a=0.5
p=@(t,l,a) 1-(1-a).*exp(-l.*t);
x=[0 0 10 10];
y=[2 3 3 2];
patch(x,y,'white');
frame=getframe(gca); im=frame2im(frame);
[imind,cm] = rgb2ind(im,256);
filename='bar.gif';
imwrite(imind,cm,filename,'gif','DelayTime',0.1, 'Loopcount',inf);
for tt=1:length(t)
pt=p(t(tt),l,a);
x=[0 0 10.*pt 10.*pt];
patch(x,y,'green');
% pause(.01); drawnow;
frame(tt)=getframe(gca);
end
for tt=1:length(t)
im=frame2im(frame(tt));
[imind,cm] = rgb2ind(im,256);
imwrite(imind,cm,filename, 'gif','DelayTime',0.1,'WriteMode','append');
end
  3 Comments
Diptangshu Paul
Diptangshu Paul on 14 Sep 2022
How is delaytime 0.1 faster than delaytime 0?
DGM
DGM on 23 Apr 2023
Short delay times depend entirely on the application to render them at the specified rate. It should be no surprise that at some point, the specified delay time fails to be met. While an exact zero delay time might seem a bit of a stretch, in practice it's worse than that. While most web browsers support delay times below 0.1s, most will treat 0 delay time as 0.1s. In other words, if you try to go too fast, you'll actually get slower results.
A number of people have surveyed this experimentally over the years. Here are two old ones that I found.
From the conclusion on the first link:
The first finding to note is that no modern browser surveyed supports frame delays below 0.02 seconds. Therefore, when creating an animated GIF, one should never use a frame delay below this threshold as it will be entirely ineffectual.
From the documentation for imwrite():
To achieve a high animation rate, set DelayTime to 0.02. Setting DelayTime to a lower value will slow down the actual animation rate in many image viewers and web browsers.

Sign in to comment.

More Answers (0)

Categories

Find more on Animation in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!