Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JQuery - How can i smoothly resize an image to create a pulsating effect?

Hey, Im working on a website with a friend of mine, and from the start, our client has been unhappy with the quality of one of our animations. This animation takes an image and makes it larger, it then makes it smaller, and repeats to get a pulsating effect. The opacity also changes throughout the animation.

The current animation is on the home page of the site http://laveryrowe.com. The animation in question is the 75% image that you can see immediately upon arriving at the site.

I have tested in safari, firefox and internet explorer. The animation only just about makes the cut in firefox, however safari and internet explorer do not produce smooth enough resizes for our client.

Does anyone know of a better method of animating than the one i have used? (see code below and check out the site for an example).

function pulse() {
    $('#seventyfive').animate({
        marginTop: 175, 
        marginLeft: 25, 
        width: 261, height: 98, 
        opacity: 0.5
    }, 700, function() {
        $('#seventyfive').animate({
            marginTop: 161.95, 
            marginLeft: 15.2, 
            width: 287.1, height: 107.8, 
            opacity: 1
        }, 700, function() {
            pulse();
        });
    }); 
};

Many thanks in advance, We could really use a hand,

Edit: The issue isn't with the positioning, (or at least i don't think it is) instead, its more to do with the way the image is resized, you can notice the jittery edges as it gets bigger. It seems to look better as the opacity is increased, but i need the same quality for when it is opaque.

Jai

like image 812
Jai Avatar asked Apr 28 '11 17:04

Jai


4 Answers

Your animation isn't smooth because your marginLeft gets rounded down (image moves to the left one pixel) and then your width gets rounded up (image pixels are moved right just a little because they are re-sampled to a larger width.) Even though the image didn't move right your eyes tell you that it did because they perceive the middle of the image as being slightly to the right. This along with doing the same thing vertically makes the animation appear to jump around.

Here's an example of why I think the edges seem to flicker or shake. Below Are two images both 3 by 1 pixels. They are both resized to 5 by 1 and moved 4 pixels to the left. The blue one is what you are seeing where the size and location change independently. The red one only allows the size to change when the location changes and should appear to be a smooth animation.

enter image description here

like image 178
gradbot Avatar answered Oct 19 '22 18:10

gradbot


Have you looked into using jQuery UI effects?

http://jqueryui.com/demos/effect/#default

Another alternative is to talk them out of the pulsing 75% and into some yellow blinking banners and the tasteful use of frames.

like image 32
Jason Avatar answered Oct 19 '22 18:10

Jason


And what about animating the font size? I.e. => http://jsfiddle.net/steweb/D3X7R/

js/jQuery

(function pulse(back) {
    $('#seventyfive').animate(
        {
            'font-size': (back) ? '100px' : '140px',
            opacity: (back) ? 1 : 0.5
        }, 700, function(){pulse(!back)});
})(false);

Markup:

<div id="seventyfive">75%</div>

Css:

#seventyfive{
    position:absolute;
    font-size:100px;
    font-weight:bold;
}
like image 2
stecb Avatar answered Oct 19 '22 17:10

stecb


You might try using += some quantity instead of animating to a specific value each time. I've used it before and never noticed any problems.

like image 1
Thomas Shields Avatar answered Oct 19 '22 17:10

Thomas Shields