In the interest of making these writings fit with my schedule today and tomorrow I’m going to take a brief detour and describe the next change before completing the one that’s in progress. This will also cover something I’ll be making use of tomorrow. Looking at the current transform definitions in the addTransformToList
calls:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
var handle001 = document.getElementById("handle001"); bcInitElement(handle001,"301px","50px","red","rgba(0,0,0,0)","260px",0,"block","This line slides in, out, in"); addTransformToList("handle001:1",0.75,{left:-284, opacity:1.0},"linear"); addTransformToList("handle001",0.50,{left:284},"linear"); addTransformToList("handle001:2",0.75,{left:-284},"linear"); addTransformToList("handle001",0.00,{display:"none"},""); var handle002 = document.getElementById("handle002"); bcInitElement(handle002,"17px","-25px","blue","rgba(0,0,0,0)","280px",0,"block","This line drops down from the top"); addTransformToList("handle002",0.75,{top:150, opacity:1.0},"linear","handle001:1-=0.25"); addTransformToList("handle002",0.00,{display:"none"},"","handle001:2"); var handle003 = document.getElementById("handle003"); bcInitElement(handle003,"120px","60px","yellow","rgba(0,0,0,0)","80px",0,"block","This text grows in scale"); //handle003.style.transform = "rotate(45deg) scale(0.5,0.5)"; //handle003.style.transform = "rotate(45deg)"; handle003.style.transform = "scale(0.5,0.5)"; addTransformToList("handle003",0.05,{opacity:1.0},"linear","handle001:2-=0.05"); addTransformToList("handle003",1.50,{scale:"(2.0,2.0)"},"linear","+=0.50"); |
..you can see a term for the type of transform as the fourth parameter in each call. In most cases the value is “linear,” which is supposed to mean that the transformation takes place in equal increments across its duration. The value doesn’t matter for transforms that are instantaneous, like those where the display property changes from, say, “block” to “none.” In other cases, which I haven’t developed in this series of posts but which I have implemented in parts, the transition may be nonlinear. It may move at a constant speed over most of the duration of the transform but them slow down towards the end, thus “easing” into place. The tool I’m emulating does this by default. In other cases the element may vibrate around a point, with the initial direction of movement being one way or another (left vs. right, up vs. down…). In other cases the vertical velocity may be modified by a form of “gravity” which slows the upward speed and then increases the downward speed. Other types are possible.
The Greensock demo I referenced yesterday doesn’t do anything special in this respect, so the function parameter can usually be omitted. “Linear” or nothing can be assumed, unless something special is needed, and then a string or object literal can be included that provides the necessary information. In the meantime I’m going to move the parameter to be the fifth (and currently last) parameter in the function call, so the updated code will look like this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
var handle001 = document.getElementById("handle001"); bcInitElement(handle001,"301px","50px","red","rgba(0,0,0,0)","260px",0,"block","This line slides in, out, in"); addTransformToList("handle001:1",0.75,{left:-284, opacity:1.0}); addTransformToList("handle001",0.50,{left:284}); addTransformToList("handle001:2",0.75,{left:-284}); addTransformToList("handle001",0.00,{display:"none"}); var handle002 = document.getElementById("handle002"); bcInitElement(handle002,"17px","-25px","blue","rgba(0,0,0,0)","280px",0,"block","This line drops down from the top"); addTransformToList("handle002",0.75,{top:150, opacity:1.0},"handle001:1-=0.25"); addTransformToList("handle002",0.00,{display:"none"},"handle001:2"); var handle003 = document.getElementById("handle003"); bcInitElement(handle003,"120px","60px","yellow","rgba(0,0,0,0)","80px",0,"block","This text grows in scale"); handle003.style.transform = "scale(0.5,0.5)"; addTransformToList("handle003",0.05,{opacity:1.0},"handle001:2-=0.05"); addTransformToList("handle003",1.50,{scale:"(2.0,2.0)"},"+=0.50"); |
It turns out that if a given parameter in a function call is not provided in JavaScript that the interpreter will define the item’s type as “undefined” in a way that can be tested for as follows:
1 2 3 4 5 6 7 8 9 10 |
//original declaration function addTransformToList(sElement,tDuration,trans,mMethod,tAdjustment) { ... //updated declaration function addTransformToList(sElement,tDuration,trans,tAdjustment,mMethod) { var tAdjustment = typeof tAdjustment !== "undefined" tAdjustment : ""; var mMethod = typeof mMethod !== "undefined" mMethod : "linear"; ... |
Here I’ve swapped the order of the method and adjustment parameters. I also now test the type of both parameters. If the parameter is present I interpret and use it as appropriate and if the parameter is not present I give it a default value that covers most of the cases. Here are how the resultant function calls might look:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
//standard call, no offsets in time, no special transitions addTransformToList("handle001",0.75,{left:-284, opacity:1.0}); //adds offset in time from end of previous transition // ...or the one that ends the latest as defined so far addTransformToList("handle002",0.50,{top:150},"+=0.50"); //adds offset in time from the end of a named transition, defines an effect addTransformToList("handle003",1.00,{scale:"(2.0,2.0)"},"handle002",{easeIn:(0.90,0.20)}); //no transition in time, but placeholder required //defines a special transition effect addTransformToList("handle004",0.60,{left:100},,"{vibrate:("right",50,0.5)}); |
At root some pretty complicated animation behaviors can be defined with minimal expression in user code. The code needed to support this in the background isn’t trivial, but the idea is for me to do as much work as possible so the user of the tool doesn’t have to. I do it once (in theory) and a bunch of people then get to save all their time.