Animated GIF's play only the first time their scene is visited

(David Howard) #1


I have an animated GIF that is set to play just once rather than loop. Placed within a scene it plays as you would expect. When I navigate away from the scene, and then back again, the GIF does not play again, it just displays the last frame.

This could be useful for some situations but I would ideally like it to trigger every time the scene is visited (which I would have expected to be the default). Is there a way to achieve this without any clever timeline jumping to force it?


Animated gif's - reload on scene change
Gif Animation question

(EDIT: This won’t work – see below )

You would need to set a On Scene load action to set the inner HTML of an element to contain the image. When this is set, the image will be re-added to the DOM and will play from the beginning:

document.getElementById("imgcontainer").innerHTML = '<img src=" ${resourcesFolderName}/img.gif">';

(David Howard) #3

Thanks Daniel.

I have not had any luck with this one i’m afraid. Attached is a simple working file to test the process. Are you able to shed light as to where I am going wrong? (1.0 MB)

(Mark Hunte) #4

There is a Typo on @Daniel 's code.

It has an extra ‘=’

It should be;

document.getElementById("imgcontainer").innerHTML = '<img src="${resourcesFolderName}/test.gif">';

Although this method does not work how we would expect to, which may be down to browser caching

(David Howard) #5

Thanks Mark. Yes that has fixed the pulling in of the image, but as you say, the refreshing of the GIF does not occur. Do you have any other ideas how I might achieve this?

Thank you.

(Mark Hunte) #6

It sometimes works in Safari, even with just the gif and no code. Chrome no go either way.

Bu still looking


This is a good thread on this topic:

The query string option seems like the best method.

(Mark Hunte) #8

just came back to this.

I got one of the ideas working. I actually tried this this morning but used the pure date instead of the just the time, which did not work.

	var d = new Date();
	hypeDocument.getElementById("imgcontainer").innerHTML = '<img src="' + '${resourcesFolderName}/test.gif?dummy='  + d.getTime() + '">'; (551.3 KB)

I this possible: Direct loading animated GIFs with timeline only vs. on sceneload?
Using GIFs/Controlling GIFs
(David Howard) #9

Thanks Mark that’s great.

Does this method require the .gif to be reloaded every time? Don’t mind either way, just wondered.

Thanks again.

(Mark Hunte) #10

Yes it does.

The transfer MB goes up each time the gif loads. Which is about 526kb each time.

I broke the gif out into 101 frames and added each separated image to the resources.

Then ran a new javascript to load them into a rect as the background image.

var imageNumber = 1;
	var imgcontainer = hypeDocument.getElementById("imgcontainer")
	var fireImage = setInterval(function(){ 

	 if (imageNumber < 100){ //-- image total is 101 = 'url( ${resourcesFolderName}/test_' + ('0' + imageNumber).slice(-2) + '.gif)'
	 } else {

 }, 33); 

This works of the bat and does not up the load weight. (1.5 MB)

(Hans-Gerd Claßen) #11

hi marc,

you should also clear the interval when leaving the scene before the animation is complete. also may consider requestAnimationFrame insteadof setInterval :mask:


Best day


(Mark Hunte) #12

requestAnimationFrame[quote=“h_classen, post:11, topic:7157”]
leaving the scene before the animation is complete

Good call.

I have seen people mention this but not used it before. Do you have a good example of the coding ( maybe using the example above )

(Mark Hunte) #13

Ahh found a good example.

So the code ( single code used on scene load and unload ) would look something like

if (event.type === "HypeSceneLoad" ){ //- Run animation
	var imageNumber = 1;
	var imgcontainer = hypeDocument.getElementById("imgcontainer")

function repeatOften() {
   if (imageNumber < 100){ //-- image total is 101 = 'url( ${resourcesFolderName}/test_' + ('0' + imageNumber).slice(-2) + '.gif)'
	 } else {
	 cancelAnimationFrame(window.globalID);//- Stop animation call
  window.globalID = requestAnimationFrame(repeatOften);
}else {

 cancelAnimationFrame(window.globalID); //- Stop animation call

} (1.5 MB)

I assume this is better because it works on the natural frame rate of the browser which is what this example is aiming for, so thats why it is better in this case ?..

Restart GIF in Hype!
Calculating current frame number in a symbol timeline
(Siew Khim) #14

Hi MarkHunte

I tried your coding but the display of the gif seems wrong. Can you please help me to identify where i have gone wrong? (2.4 MB)

I have attached the gif file where it should be and the hype file.

Looking forward to hear from you.

Thank you

Siew Khim

(Hans-Gerd Claßen) #15

this line sets the name of the image: = 'url( ${resourcesFolderName}/test_' + ('0' + imageNumber).slice(-2) + '.gif)'

it’ll work correct up to 99.

(Mark Hunte) #16

Hi @siewkhim,

@h_classen has kindly answered your question.

If you type an @ In your post, you will be offered a selection of names of forum members. You can start typing their name to narrow it down.

When you use the @name in a post that member gets a notification…

(Siew Khim) #17

Hi @h_classen & @MarkHunte

I have tried the code! It works!

Thank you so much!

Siew Khim

(Hans-Gerd Claßen) #18

… in fact i did not answer the question :slight_smile: i wanted to, but then saw that the naming of the images was inconsistent and canceled my investigations :slight_smile:

so here is a fresh document: (510.1 KB)

(Ollie) #19

Hi @MarkHunte I have followed your example (test_MHv3) and have it running successfully with my own gif. However the gif I have created I believe is 25 fps, whereas I understand a browsers natural frame rate is 60fps so the gif is playing to fast. Is there a way I could resolve this?


Update: I have looked at test_MHv2 and changed the ‘33’ in the last section of code to a higher number (100) and this has slowed the frame rate down to nearly match the original gif.

Is this a proper solution which will work?


(Jonathan Deutsch) #20

The 33 refers to milliseconds, so if you need something at 25fps you’d do 1000ms/25fps = 40 ms per frame.

The later has a fps option where you could directly enter 25.