Jump to content

How to implement fixed time animations


jamiri
 Share

Recommended Posts

Hi everyone,

 

I'm going to play an animation on the web (desktop/mobile) using Pixi/Phaser. The performance varies from device to device. My problem is that I want the animation to be played in a fixed duration even if I have to skip some intermediate frames. To make it clear, suppose that I have a sound file with 2 minute duration and I want my animation to be played exactly at the same time to stay synchronized with sound. How can I achieve that on all devices (I assume skipping frames as a compensation for synchronization). Any ideas are welcome. 

Link to comment
Share on other sites

requestAnimationFrame does that for you, just dont take into account number of frames, work only with timestamps. PIXI code does not depend on number of frames, there's only some stuff based on current time in pixi-spine.

 

Thanks for reply. I'm using requestAnimationFrame but still problem exists.  Based on PIXI's flying dragon example I wrote a simple animation to test the concept which is accessible here:

 

https://jsfiddle.net/jamiri/c96bd0zm/1/

 

You can see from code that position update is done in updatePosition function which is run several times for a duration of 2 seconds and updating the view is done via requestAnimationFrame in doAnimation function. If I run it three times on a desktop computer, and watch fo total time

shown using alert() (line 74 in code), I see nearly perfect timing e.g. 2.003, 2.006, 1.096 which all are close enough to 2000 milliseconds, but on a not so good mobile device running three times yields 7.04, 6.578, 6.829 :blink:. I want latter to finish on time even if it skips many frames.

 

If you mean something different, then can you provide an example? 

 

I look forward to your reply.

Link to comment
Share on other sites

sorry, made a error in beginning:

var time = Date.now()/1000;

everything works.

 

also your updateTime is wrong too. If you dont want to depend on FPS why do your increments depend on number of times setTimeout was called? Its very easy technique: calculate delta every frame and pass it everywhere, including updateTime()

 

http://jsfiddle.net/z24rp0qg/

Link to comment
Share on other sites

You could also use the requestanimationframe -callbacks parameter to get the time instead of Date.now().

 

 


var start = null;function step(timestamp) {  if (!start) start = timestamp;  var progress = timestamp - start;  dragon.update(progress);  window.requestAnimationFrame(step);}window.requestAnimationFrame(step);
Link to comment
Share on other sites

 

You could also use the requestanimationframe -callbacks parameter to get the time instead of Date.now().

 

 

var start = null;function step(timestamp) {  if (!start) start = timestamp;  var progress = timestamp - start;  dragon.update(progress);  window.requestAnimationFrame(step);}window.requestAnimationFrame(step);

 

Thank you, but not working again. Total time again totally depends on device performance.

Link to comment
Share on other sites

All performance improvements of that patch are focused in FFD timelines and mesh rendering.

 

Thanks, I used your solution but didn't see any difference.

 

I have found a solution which is a mixture of variable rate for updating dragons i.e. dragons.update(variableRate); and variable value for position of each dragon based on remaining distance in every requestAnimationFrame call. However it has minor problems and I will come back here as soon as I found a intact solution. Still any ideas are welcome  :) .

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...