Site Navigation
vegUISiteKit - The Effect Framework
written by vegu on 16 Mar, 2007 10:37:30
The effect framework library of the VSK is the foundation library that adds certain methods to the VSK_Node object that enable the development of cool effects that you can use on your html nodes.
Effects themselves are processed by a timer that runs at a fairly short interval. I decided to have a global timer that would handle all effects on all nodes instead of creating a new timer object for every effect, this should solve problems with timer limitations on certain systems.
You can specify the interval of that timer by setting the VSK_FX_INTERVAL global variable somewhere in your code. By default it is set to 25 ms which means the effects are processed about 40 times a second. Depending on the amount of effects you are including at the same time it might be wise to up the number to 50 (20 times per second) which still allows for fairly smooth animation.
Example:The methods added by the effect library to the VSK_Node object are:
fx_start():
Starts the global effect timer, it is called automatically whenever a new effect is initialized on any node.
fx_stop():
Stops the global effect timer, it is called automatically whenever there are no effects initialized on any node.
fx_init():
Initialize an effect on the node.
fx_halt():
Halts an effect - identified by effect id - that is currently active on the node.
Most of these functions work automatically and you should hardly ever need to call them by hand.
Initializing an effect
The effect framework itself does not provide any effects, it just serves as a framework that the various effect libraries can then utilize to get the job done.
Whenever you include an effect library (for example the fade effect) the VSK_Node object will be extended by certain functions. In the case of the fade library it gets two new methods: fx_fade_in and fx_fade_out.
You can then simply call the appropriate method with it's various arguments - which obviously vary depending on the effect :)
Example:
var n = v(myNode);
n.fx_fade_out(1000);
In this example the argument submitted to the fx_fade_out method is simply the time of the effect, meaning our node will take about a second to fade out.
You will be able to find a more in depth tutorial on the usage of the various effect libraries on this side.
Creating your own effects
To create your own effect simply add a method to the VSK_Node using prototype. The method itself will create an event object that holds a function to process the event and can also hold various properties of the event. The event object itself is not predefined and will be initialized as a generic javascript object.
The example below is taken straight from the fade effect library and is the fx_fade_in effect.
Example:VSK_Node.prototype.fx_fade_in = function(n, limit) {
if(!this.b)
return;
var N = this.b;
if(!limit)
var limit = 100;
var E = {
main : function() {
var interval = 100 / (n / VSK_FX_INTERVAL);
if(v(N).t() < limit)
v(N).fade(v(N).t()+interval);
else
v(N).fx_halt('fade_in');
}
};
this.fx_init('fade_in',E,['fade_out']);
return E;
};
Because we return the created effect object it means that we can add properties to it. Such as the onhalt event that gets fired when the effect is done and about to be removed from the node.
Example:n.fx_fade_in(1000).onhalt = function() { alert('Faded completly in!'); };
Sequences
The effect framework also provides a sequence object that you can use to create effect sequences that can be looped infinitely
To use an effect sequence you first must create a new sequence objec. The argument passed to the constructor function is the node that the effects in the sequence will affect.
Example:var n = v(myNode);
var sequence = new VSK_FX_Sequence(n);
Then you define the body property of the sequence object to point to the sequence function. This function will be called over and over as soon as the sequence's
loop() method is called.
Example:
sequence.body = function(n) {
n.fx_fade_out(1000).onhalt = function() {
n.fx_fade_in(1000).sequence = sequence;
};
};
sequence.loop();
Related Posts
Your Comment
Comments
No comments yet.