Greensock JavaScript Animation

There are 37 comments.

Greensock is a fantastic JavaScript animation framework that performs above and beyond everything else currently available to those of us working with web design.

Though only few people know of it in the JavaScript world it’s got a huge audience in the world Flash, where it’s about as widely used as jQuery is among JavaScript-folks.

The example ’3D-flip image gallery’ has been updated to use the newly added 3D features of Greensock. I’ve also added a couple of new example links.

Differences between jQuery and Greensock

You might be thinking “I already have jQuery! I don’t need another JavaScript animation library.” … While you might feel like jQuery’s animation tools are enough, that’s roughly compared to feeling your old mobile phone is sufficient until you try a smart phone and discover the world of possibilities it gives you to walk around with the internet in your pocket.

A few of the things Greensock does that jQuery doesn’t

  • It performs extremely well. About 10 times faster than jQuery. Check out this speed test.
  • It makes it very easy to animate groups of items either by appending one animation after another or by applying staggered delay/start time for a sort of dominos effect.
  • It makes animating transforms like scale, skew and rotate easy. At the same time it handles all the cross-browser junk enabling transforms all the way back to IE6.
  • You can animate any numeric value of any object, not just CSS properties. This means that Greensock can animate custom JavaScript objects or work in conjunction with HTML5 Canvas or Raphael.js (For animated vector graphics). It’s extremely flexible in that sense.
  • Great easing modes built right in. You don’t need to include any other files, just load up TweenMax.min.js and you have everything you need.

On top of that I’d argue that Greensock has a much better syntax. You can even choose wether or not you want it to be Object Oriented.

Examples

Here are a couple of examples demonstrating the core features of the Greensock Animation Platform. If you have any questions, please leave those in the comments below.

basic syntax


<section id="canvas">
    <div id="box"></div>
</section>

var $box = $('#box');

function moveBox(e) {
	var x = e.pageX,
		y = e.pageY;

	TweenMax.to($box, 1.4, {
		css: { left: x, top: y, scale: Math.random() * 2 + 1 },
		ease:Elastic.easeOut
	});
}

$(window).on('click', moveBox);
View demo

Mouse follower

Greensock mousefollow javascript animation example
In this example TweenMax is used to create a mouse-follow effect in 6 lines of code. It also shows you how awesome it is at overwriting tweens.

This is exactly the same code as above, except we use listen for the mousemove event rather than a click.


<section id="canvas">
    <div id="box"></div>
</section>

var $box = $('#box');

function moveBox(e) {
	TweenLite.to($box, 0.7, { css: { left: e.pageX, top: e.pageY }});
}

$(window).on('mousemove', moveBox);
View demo

Animating custom properties

Greensock custom property javascript animation example

Here I’m animating the custom property ‘currVal’ of a normal JavaScript object which then updates the number . Notice how easing still works.


<form id="number-input" action="">
    <input type="number" value="2500" />
    <button id="animate">Animate</button>
</form>

var	$text	= $('p.number'),
	$input	= $('input[type=number]'),
	endVal	= 0,
	currVal	= 0,
	obj	= {};

obj.getTextVal = function() {
	return parseInt(currVal, 10);
};

obj.setTextVal = function(val) {
	currVal = parseInt(val, 10);
	$text.text(currVal);
};

obj.setTextVal(0);

var animate = function(e) {
	e.preventDefault();
	currVal = 0; // Reset this every time
	endVal = $input.val();

	TweenLite.to(obj, 7, {setTextVal: endVal, ease: Power2.easeInOut});
};

$('#number-input').on('submit', animate);
View demo

Controlling the animation playhead

Example of controlling progress of a Greensock javascript animation timeline

Here I’m using the mouse position to control the progress (or playhead) of the entire animation.


<section id="canvas">
    <span>W</span>
    <span>o</span>
    <span>o</span>
    <span>o</span>
    <span>o</span>
    <span>t</span>
    <span>!</span>
    <span>!</span>
</section>

var $letters	= $('.text span'),
	tl	= new TimelineMax({paused: true});

tl.staggerFrom($letters, 1.5, {
	css: {alpha: 0, top: "-300", scale: 0, rotation: -20}, 
	ease: Elastic.easeOut
}, 0.2);

$(window).on('mousemove', function(e) {
	var prog = e.pageX / window.innerWidth;
	tl.progress(prog);
});
View demo

More examples

Start learning Greensock now

I’d suggest you get started with Jump Start: GSAP to quickly wrap your head around the Greensock Animation Platform. Once you’re done with that check out these resources:

I’d love to see your creations so share them in the comments.

  • Austinfacts

    This is really cool. I am entirely new to development in general. I started with basic HTML and CSS, and gradually moved up to the Javascript and jQuery I am using now. I just started looking into animation libraries a few days ago. There are so many from which to choose that it has become a little overwhelming. Would this be a good starting point? I have been looking at all kinds of lists entitled “Best Javascript Animation Libraries” and others similar to that. I was thinking about starting by using Raphael or processing. What would you recommend as a starting point (not necessarily coming from those two. And, you are allowed to say this one)?

  • Ivan Moreno

    Great article and insights Jens! GreenSock is the way to go if you want to have smooth animations with a higher penetration rate. Completely agree with you about the syntax benefits. Nice work. Cheers.

  • Pingback: HTML5 Game Engines and Frameworks - PDG Technologies::Outsourcing company Smartphone Web Windows 8 Development

  • Pingback: Tutorial: Create Apple navigation using Greensock

  • http://www.ihatetomatoes.net/ Petr Tichy

    Hi there,
    some of you might be interested my tutorial. It replicates the Apple elastic product navigation. Check it out and keep learning.

    Tutorial: Create Apple elastic navigation using Greensock
    http://www.ihatetomatoes.net/tutorial-apple-navigation-using-greensock/

    Cheers
    P.