30 Days of Mootools 1.2 Tutorials - Day 11 - Using Fx.Morph, Fx Options and Fx Events

- Troy

Mootools 1.2 Fx.Morph, Fx Options, and Fx Events

If you haven’t already, be sure and check out the rest of the Mootools 1.2 tutorials in our 30 days series.

Today, we are going continue exploring the Fx section of the library. First, we will learn how to use Fx.Morph (which essentially lets you tween multiple style properties), then we are going check out some of the Fx options that apply to both Fx.Tween and Fx.Morph, finally we will see how to use Fx events like “onComplete” and “onStart.” With these options and events, we will gain finer control over animated transitions.

Fx.Morph

Creating a new Fx.Morph

Initiating a new morph looks a lot like creating a new tween, except you have to account for multiple style properties.

//first, lets throw our element into a var
var morphElement = $('morph_element');
 
//now, we create our morph
var morphObject = new Fx.Morph(morphElement);
 
//now we can set the style properties just like Fx.Tween
//except now we can set multiple style properties
morphObject.set({
    'width': 100,
    'height': 100,
    'background-color': '#eeeeee'
});
 
//we can also start our morph like we would start a tween
//except we can now input multiple style properties
morphObject.start({
    'width': 300,
    'height': 300,
    'background-color': '#d3715c'
});

And that is all there is to creating, setting and starting a morph.

To set this up proper, we should create our variables, separate out our functions and create a few events to control the whole thing:

var morphSet = function(){
	//now we can set the style properties just like Fx.Tween
	//except now we can set multiple style properties
	this.set({
		'width': 100,
		'height': 100,
		'background-color': '#eeeeee'
	});
}
 
var morphStart = function(){
	//we can also start out morph like we would start a tween
	//except we can now input multiple style properties
	this.start({
		'width': 300,
		'height': 300,
		'background-color': '#d3715c'
	});
}
 
 
var morphReset = function(){
	//set the values back to start
	this.set({
		'width': 0,
		'height': 0,
		'background-color': '#ffffff'
	});
}
 
window.addEvent('domready', function() {
    //first, lets throw our element into a var
	var morphElement = $('morph_element');
 
	//now, we create our morph
	var morphObject = new Fx.Morph(morphElement);
 
	//here we add the click event to the buttons
	//and we bind morphObject and the function
	//allowing us to use "this" above
	$('morph_set').addEvent('click', morphSet.bind(morphObject));  
	$('morph_start').addEvent('click', morphStart.bind(morphObject));
	$('morph_reset').addEvent('click', morphReset.bind(morphObject));
});
<div id="morph_element"></div>
<button id="morph_set">Set</button>
<button id="morph_start">Start</button>
<button id="morph_reset">reset</button>



The Fx Options

The following options are accepted by both Fx.Tween and Fx.Morph. They are simple to implement and give you a ton of control over your effects. To set these options, use the following syntax:

//set up your tween or morph
//then just set up your options between the { }
var morphObject = new Fx.Morph(morphElement, {
    //first state the name of the option
    //place a :
    //then define your option
    duration: 'long',
    transition: 'sine:in'
});

fps (frames per second)

This option determines the frames per second of the animation. The default is 50, and it will accept numbers and variables that contain numbers

//set up your tween or morph
//then just set up your options between the { }
var morphObject = new Fx.Morph(morphElement, {
    fps: 60
});
 
//or
var framesPerSecond = 60;
 
var tweenObject = new Fx.Tween(tweenElement, {
    fps: framesPerSecond
});

unit

This option sets the number unit. For example, do you want ‘100′ to mean px, % or ems?

//set up your tween or morph
//then just set up your options between the { }
var morphObject = new Fx.Morph(morphElement, {
    unit: '%'
});

link

Link provides a way for you to manage multiple calls to start an effect. For example, if you have a mouseover effect, do you want it to start over each time the user hovers? Or, if a person mouses over the object twice, should it ignore the second call to start or should it chain them up and start a second time once it finishes the first round? Link has three settings:

  • ‘ignore’ (default) - ignore just ignores any calls to start until its done with the effect
  • ‘cancel’ - will cancel the current effect if another call is made, giving the newest call precedence
  • ‘chain’ - chain lets you “chain” the effects together and will stack the calls and execute them until it goes through all the chained calls
//set up your tween or morph
//then just set up your options between the { }
var morphObject = new Fx.Morph(morphElement, {
    link: 'chain'
});

duration

Duration lets you define how long the animation will take. Duration is not the same thing as speed, so if you want an object to move 100px in a duration of 1 second, it will go slower than an object moving 1000px in 1 second. You can input a number (measured in milliseconds), a variable containing a number, or one of three shortcuts:

  • ’short’ = 250ms
  • ‘normal’ = 500ms (default)
  • ‘long’ = 1000ms
//set up your tween or morph
//then just set up your options between the { }
var morphObject = new Fx.Morph(morphElement, {
    duration: 'long'
});
 
//or
var morphObject = new Fx.Morph(morphElement, {
    duration: 1000
});

transition

The last option, transition, gives you the ability to determine the transition type. For example, should it be a smooth transition or should it start out slowly then speed up toward the end. Check out these examples of the transitions available with the Mootools core library:

var tweenObject = new Fx.Tween(tweenElement, {
    transition: 'quad:in'
});

Note: the first transition bar fires a red highlight effect on start and an orange highlight effect on complete. Check out how to use Fx events below.

‘quad:in’
‘quad:out’
‘quad:in:out’
‘cubic:in’
‘cubic:out’
‘cubic:in:out’
‘quart:in’
‘quart:out’
‘quart:in:out’
‘quint:in’
‘quint:out’
‘quint:in:out’
‘expo:in’
‘expo:out’
‘expo:in:out’
‘circ:in’
‘circ:out’
‘circ:in:out’
’sine:in’
’sine:out’
’sine:in:out’
‘back:in’
‘back:out’
‘back:in:out’
‘bounce:in’
‘bounce:out’
‘bounce:in:out’
‘elastic:in’
‘elastic:out’
‘elastic:in:out’

The 30 transition types above break down into 10 sets:

  • Quad
  • Cubic
  • Quart
  • Quint
  • Expo
  • Circ
  • Sine
  • Back
  • Bounce
  • Elastic

Each set has three options:

  • Ease In
  • Ease Out
  • Ease In Out

Fx Events

The Fx event give you the opportunity to fire some code at different points throughout the animation effect. This can be very useful for creating user feedback and gives you yet another level of control over your tweens and morphs:

  • onStart - will fire when the Fx starts
  • onCancel - will fire when the Fx is cancelled
  • onComplete - will fire when the Fx is complete
  • onChainComplete - will fire when chained Fx completes

When building a tween or a morph, you can set set up one of these events just like you would an option, except instead of a value, you give it a function:

//first we pass the new Fx.Tween to a variable
//then we define the element to tween
quadIn = new Fx.Tween(quadIn, {
        //here are some options
	link: 'cancel',
	transition: ‘quad:in,
 
       //here are some events
	onStart: function(passes_tween_element){
                //these event will pass the tween object
                //so here we are applying the "highlight" effect
                //when the animation starts
	        passes_tween_element.highlight('#C54641');
	},
 
        //notice how the commas are consistent
        //between every option and event
        //and NO COMMA AFTER THE LAST option or event  
        onComplete: function(passes_tween_element){
                //and here we apply the highlight effect on complete
	        passes_tween_element.highlight('#C54641');
	}
});

Example

To generate the transition code above, we can reuse our functions in a way we haven’t seen in this series before. All of the transition elements above use two functions, one to tween out on mouse enter and one to tween back on mouse leave:

 
//this is the function we use on mouse enter, tweens width to 700px
var enterFunction = function() {
	this.start('width', '700px');
}
 
//this is the function we use on mouse leave, tweens width back to 300px
var leaveFunction = function() {
	this.start('width', '300px');
}
 
window.addEvent('domready', function() {
    //here we create throw some elements into vars
    var quadIn = $('quadin');
    var quadOut = $('quadout');
    var quadInOut = $('quadinout');
 
    //then we create three tween elements, one for each var above
    quadIn = new Fx.Tween(quadIn, {
	link: 'cancel',
	transition: Fx.Transitions.Quad.easeIn,
	onStart: function(passes_tween_element){
		passes_tween_element.highlight('#C54641');
	},
	onComplete: function(passes_tween_element){
		passes_tween_element.highlight('#E67F0E');
	}	
    });
 
   quadOut = new Fx.Tween(quadOut, {
	link: 'cancel',
	transition: 'quad:out'
    });
 
    quadInOut = new Fx.Tween(quadInOut, {
	link: 'cancel',
	transition: 'quad:in:out'
    });
 
    //now we add the mouse enter and mouse leave events
    //notice the use of .addEvents
    //this works just like .addEvent
    //except you can add multiple events using the pattern below 
    $('quadin').addEvents({
        //first, you say what kind of event, place event type inside 'single quotes'
        //then place a :
        //finally you state your function
        //in this case, its a function that binds to the tween
        'mouseenter': enterFunction.bind(quadIn),
        'mouseleave': leaveFunction.bind(quadIn)
    });
 
    $('quadout').addEvents({
        //notice how we reuse the same functions here
        'mouseenter': enterFunction.bind(quadOut),
        'mouseleave': leaveFunction.bind(quadOut)
    });
 
    $('quadinout').addEvents({
        //we also use those same functions here
        //but each time they apply to an event on a different element
        //and bind to a different tween
        'mouseenter': enterFunction.bind(quadInOut),
        'mouseleave': leaveFunction.bind(quadInOut)
    });

To Learn More…

You can gain even finer grained control over your effects with the tools in the Fx library. Be sure to read over the Fx section in the docs, as well as tween, morph and transitions

Download a zip of everything you need to get started

Including the live examples on this page, the Mootools 1.2 core, an external javascript file, an external css file and a simple html file.

Tomorrow’s Mootools 1.2 Tutorial

Day 12 - Drag and Drop with Drag.Move

Questions, Comments or Suggestions

If you have any questions about this tutorial, suggestions for future tutorials or any thoughts on the subjects covered here, feel free to drop us a comment. Hope you find this useful.

Bookmark and Share

Comments (13) Trackbacks (11)
  1. ghazal


    Scrapbooking my eleventh page.
    Keep up the good work.
    Cheers
    Ghazal
  2. Mike B


    Thanks for the tutorial. I noticed a typo in your last example, on the onComplete you're passing a parameter called boo, should be passes_tween_element.
  3. Louis


    Great, thank you!

    You may want to speak a little about shortcuts (morph(), etc), and best practices (i.e. reuse instances).
  4. PeterC


    Hi Troy, eyy nice here, i was specting this, the fx transitions and stuff, this help me a lot, and for sure, other pple too.

    bb
    keep the good work!
    :D
  5. Troy


    @2 - Thanks Mike, I have corrected it above.

    @3 - Hey Louis, i'll be sure to either add some coverage of the shortcuts here or touch on them in the future. As far as best practices, we would love to hear your thoughts on the subject and see any resources you think are valuable. Have you seen anything out there that specifically deals with Mootools in this area, or have you mostly just seen this info in JavaScript tutorials? Thanks again.
  6. Emanuel


    Thanks for the hard work you are putting in this course. It is exciting to find such a high quality and dedication, as part of a community.

    Q - How can I set the tween/morph starting from the center of the element (or any other specific x/y point)?
  7. jake williamson


    really enjoying the tutorials, making the moo even mighter.

    do you know if you can use the unit parameter to get an element to scale from say 100px high to 100% high?

    i'm having a go, but mootools is percentage scaling all the elements!

    so if i start with a div 100px by 100px and then tell it to go to 100px by 100% it scales the width too (in percentage).

    i'm guessing theres no way round this?!
  8. Web designer in Perth


    I just have to say, you've saved my remaining hair (which I have been pulling out all day trying to debug some code for a new website I'm designing.

    I found your tut' and within two minutes my problems were solved. Many, many thanks! Looking forward to reading the others now.
  9. gijs


    Great tut, i finally get a little grip on mootools.

    Q: o downloaded the above package, but i get 1 error.
    $("morph_set") is null

    Don't know what it is...

    Kind regards,

    Gijs
  10. Adriaan


    Can't you include duration and a delay? I want to use an effect, but it should only start after 1 second, how do I do that?
  11. Mike Hankey


    Curse you...curse you I say. Now I'm hooked on MooTools!
    Thanks for the great series,
    Mike
  12. Tobias


    heya, does anyone know if it's possible to alter the onComplete function? If i instantiate my object and then decide to change the onComplete.

    var morphElement = $('morph_element');
    var morphObject = new Fx.Morph(morphElement);
    morphObject.set({
    onComplete: function(){
    // some code here
    }
    });

    Otherwise, supertutorial!
    / Tobias
  13. pathanck


    Starting from "Fx Events" subsection you use the same variable for the Tween object and Tween element. This might be a mistake, or is not explained very clearly.


Leave a Reply