javascript: Raphael stops running smoothly if jQuery or CSS is animating? - javascript

My site:
jQuery Animation (`comsat.js`: `showPlayerActionView()`):
if (container == cs_characterSliderPlayerOne){
container.show("slide", {direction: "left"}, 500);
} else if (container == cs_characterSliderPlayerTwo){
container.show("slide", {direction: "right"}, 500);
}
Raphael Animation:
this.hover(function(event) {
cs_playerList[i].animate({
fill: HEX_HOVER_COLOR
}, 500);
}, function(event) {
cs_playerList[i].animate({
fill: DEFAULT_HEX_COLOR
}, 500)
});
So, each animation seems to run perfectly smoothly when the other is disabled, but when both are enabled, they seems to fight eatchother for rendering the most amount of times, resulting in a slow down. Is there a way to solve this? either by telling the animations they don't need to render at 200 frames a second (guess) and share resources so the other stuff can appear smooth?
The same slow down happens when I use any CSS3 transition animation (not shown on site). Maybe multithreading would help? How would one do that in JavaScript?

Raphael 2 was released in October 2011 with some changes to the animation code: animations now run using the new browser API "requestAnimationFrame", with a fallback for older browsers to run at approximately 60FPS.
jQuery adopted requestAnimationFrame early in the 1.6 series but removed it again when various jQuery users reported their animation queues were getting backed-up and leading to horrible catch-up animations as the window lost and regained focus.
The animations should run more smoothly with Raphael 2, but perhaps not quite as smoothly as they would if jQuery was also using requestAnimationFrame

Related

AJAX loading objects/images - makes jQuery animations choppy

I am building a website that has a few animations when you load the home page (for example, the main logo and a few menus slide in from the sides of the screen). Simultaneously, I am also using AJAX in the background to start to load some images that might be viewed later. The problem with this is that when the images are loading, the animations become quite choppy. Is there any way to stop this? Or maybe give the AJAX function a lower priority so that it doesn't try to do anything when an animation is running?
Here is the current script I'm using to load these images:
$('.lightbox-container.first').load('/images/first_set/', function(){
$('.lightbox-container.second').load('/images/second_set/', function(){
$('.lightbox-container.third').load('/images/third_set/', function(){
$('.lightbox-container.fourth').load('/images/fourth_set/', function(){
$('.lightbox-container.fifth').load('/images/fifth_set/', function(){
$('.lightbox-container.sixth').load('/images/sixth_set/');
});
});
});
});
});
An all of my animation function look something like:
$('.third-section').animate({ 'opacity': '1', 'height': '200px', 'padding-top': '20px', }, 500);
The problem
Since all your ajax and js animations run on the same browser thread, you are bound to have this problems. You are reaching the limits of your CPU, which causes the choppiness.
How to solve this
Use CSS3 transforms. Those are hardware accelerated in all modern browsers and run on a separate threads, so their performance is generally not affected by ajax calls. Since you said you only slide things around, I think this would be the ideal solution for you. There is a great article about it here:
http://www.html5rocks.com/en/tutorials/speed/high-performance-animations/
Your case
So to actually make this work for you. Leave the ugly ajax calls as they are for now. Instead of using jQuery animate, you need 2 states - the initial, which positions the slide away and one with an extra class, which positions your slide in it's target place.
All you have to do is add the class to the slide and it will nicely come in place. Theory is simple.
Sample
Your initial state could be something like this:
.slide {
transform: translate(-400px, -200px);
transition: all 5s;
}
And the one to show in place:
.slide.show {
transform: translate(0px, 0px);
}

Improving smoothness of multiple jQuery animations

I created this multi-layered animation that takes 4 images ("layers") and animates them to "zoom away". It usually runs smoothly but once in a while when the 3rd layer starts, the 2nd layer animation kind of lags. I'm not really sure why/when the lag happens - maybe due to GPU processing?
http://jsfiddle.net/3EwnB/3/
Is there anything I can do to reduce any animation lag?
And if there's a better way to achieve this effect I'm not stuck on using jQuery.animate - alternate suggestions are welcome.
The code for the jQuery animation (for 1 of the 4 layers):
setTimeout(function() {
$('#animation-layer-2').show().animate({
opacity: '0.9',
marginLeft: '-490px',
marginTop: '25px',
width: '950px'
}, { duration: 400, queue: false });
}, 500)
Also, in the JS Fiddle example, the images are 9-17kb but that actual images I'm using are 131-457kb. The image sizes don't seem to make much of a difference though.

Smoothly Fade In Entire Web Page With MooTools

I want to fade in an entire web page after all its elements finished loading. The web page includes the background image repeated left to right, and the main content area with some text and pictures. I assume I should set body opacity to 0 in CSS, and use JavaScript code to fade in the page.
I have to use MooTools, more specifically, version 1.2.6, because that library is already linked to the page (and shouldn't be upgraded to a more recent version, for a number of reasons).
One of the StackOverflow experts suggested this MooTools snippet as a solution:
window.addEvent('load', function() {
$$('body').set('morph', {duration: 300}).morph({'opacity': '1'});
});
PROBLEM: for some reason, instead of smoothly fading in the page, the snippet makes the background appear right away, and then, a second or so later, the page pops up, without any fade-in effect. Most likely it's me who's not doing things right.
I'd appreciate a bit of advice from a knowledgeable person.
The answer to your question is to do the following.
Remove the CSS opacity:0; in the stylesheet and use this code adjusted from yours
I increased from 300 to 3000 which in seconds is from .3seconds to 3seconds.
chained:
window.addEvent('load', function () {
$$('body').fade('hide').set('morph', {
duration: 3000
}).morph({
'opacity': '1'
});
});
expanded:
window.addEvent('load', function () {
var el = $$('body');
el.fade('hide'); // hide body tag
el.set('morph', {duration: 3000});
$$('body').morph({'opacity': '1'});
});
Notice:
I do agree with LifeInTheGrey about bad practice, but i said i would answer your question.

Jquery finding the right easing function

Hi all im trying to create a loading animation with html and JQuery that looks like the windows phone 7 loading animation.
I have gotten this far
http://jsfiddle.net/b6L8M/5/
But the easing function does the opposite of what i want, i want it to go fast when its on the edges and then slow down when it comes to the center.
when looking at http://jqueryui.com/demos/effect/easing.html it does not seem that there is a built-in function for that, so how would i create that function?
If you split-up your animation into two parts you can ease-in to the center and ease-out of the center:
function moveDot(dotItem, delay) {
var dotItem = $(dotItem);
dotItem.delay(delay * 200).css('left', '0%').animate({left: '50%'}, 1000, 'easeOutCirc', function() {
dotItem.animate({left : '100%'}, 1000, 'easeInCirc', function () {
moveDot(dotItem[0], 0);
});
});
}
I also cached the $(dotItem) selection so it doesn't create a hiccup mid-animation while creating another selection (not a big chance of this happening but hey :) ).
Here is a demo: http://jsfiddle.net/b6L8M/13/
Sometimes, you have to use more than one animate function to do what you want.
I don't know how the windows phone 7 animation looks but I tried this according on what you said :
$(dotItem).delay(delay * 200).css('left', '0%').animate({left: '50%'}, 1000, 'easeOutQuart', function() {
$(this).animate({left: '100%'}, 1000, 'easeInQuart', function() {
moveDot(dotItem, 0);
});
});
The first one, easeOutQuart, is fast then slow down. The second is slow then accelrate.
I used the chaining system, but it makes the elements stop during some ms. You also can use a "delay" to do so without stop.
After fiddeling around in fiddler and using this post Help with custom jquery easing function i got it to work like i wanted
http://jsfiddle.net/b6L8M/24/ it's more or less identical to the WP7 loading!

How to force jQuery animations to run simultaneously?

I have a web page with animations (JQuery Animation) all over the place. A typical animation sequence may contain three or four objects animating independently at the same time. The issue I am facing is that the queuing of the animations is not predictable. Some of the animations are running simultaneously while some others are not.
I am doing something like
setTimeout(function(){
//animations
}, delay);
in many places just to try and group the animations. Even when using this, some of the animations inside the code block are not running simultaneously.
Is there any way to force animations to be run at the same time? Is there a way to fill up the queue with animations and then execute these at the same time?
Is there any comprehensive documentation of how this thing actually works?
EDIT: Sample code Warning: The code is messy
The question is looking at the code how do you know which of the animations are going to run simultaneously?
This run simultaneously (but not if there's already an ongoing animation on the object):
$('.blabla .2').animate({opacity: 0.3, fontSize: 20}, 800);
And if you want to be 100% sure that it's animated right away (notice the queue:false):
$('.blabla .1').animate({fontSize: 20},{queue:false,duration:800});
You can also do this. (It will still run simultaneously)
$('.blabla .1')
.animate({fontSize: 20},{queue:false,duration:800})
.animate({opacity: 0.4},{queue:false,duration:800});
This runs one after the other.
$('.blabla .1').animate({opacity: 0.3}, 800).animate({fontSize: 20}, 800);
So does this
$('.blabla .2').animate({opacity: 0.3}, 800, function() {
$('.blabla .3').animate({opacity: 0.3}, 800);
});
I hope that answers your question.
Sorry for all the edits, i'm new here.
Use the step option. You give it a function, and after each step of the current animation, it will execute that function. Thus you can animate there. Link to docs.

Categories

Resources