Today I implemented a few more basic animation features and gained more insight into how the Greensock animation product probably works.
The first thing I did was add the capability to pause and continue the animation. That was trivial.
I had previously implemented a list of commands that allows the script to return all animated elements to their original states so everything could be initialized when the user asked to rerun the animation. If I wanted to run the animation in reverse I had to create yet another structure that stored the end state of each element. I did this by adding a second dimension to the bcActivitiesList array. Major element zero holds the list of beginning states while major element one holds the list of ending states. I also had to change the increment step to a negative number when needed. Clicking the rerun button resets the animation step to zero and the increment to positive one. Clicking the run in reverse button resets the animation step to its highest value and sets the increment to negative one. The pause and continue buttons work whether the animation is moving forwards or backwards. So far, so good, right?
Not right.
The way this works not the only information the script has access to for any animation step is about elements that change in some way. If we’re moving forward through the animation this isn’t a big deal because the incremental changes naturally proceed from their initial state. If we walk through the animation in reverse, though, what might we know about the state of an element in the step before we’ve modified it for the first time? We might not know anything, except for the initial value we store in the initialization array. I therefore reasoned that the tween functions should each inset a copy of the initial state into the list of updates in the animation step before modifications begin (assuming they begin after step zero).
This actually works well–if the animation sweeps through every animation step in reverse. That happens when the user clicks on the run in reverse button but it might not happen if the animation step is set to an arbitrary value.
Enter the slider bar.
I added a slider bar with the idea of allowing the user to be able to set the animation to an arbitrary point. First, the slider is only available in browsers that support HTML5, but for my purposes that’s a minor issue. A bigger issue is the previously-mentioned lack of state information for every element during every time step. The slider moves with the animation and when the user moves it when the animation is not running on its own. Moving the slider by hand only returns a value when the user stops moving and releases the slider, which will leave the animation step at an arbitrary value. If no update was performed during that step for a given element then that element might not reflect the state it’s supposed to be in.
This indicates that, in order for the slider mechanism to work properly, the state of each element has to be entered into the activity list for every animation state. This isn’t actually a big deal if the number of animated elements is small, but I wonder how things would look as the number of animated elements grows. What are the limits to the complexity and number of elements we might be able to handle smoothly? (Think of the animated fountain of green balls shown in the Greensock demo linked above.) Those are questions for next week. In the meantime here is the demo in its current state. Play around with it and let me know if you see the same things I do.
And, here is the code in its increasingly entropic glory.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 |
<!DOCTYPE html> <html> <head> <title>Fast Animation Script Demo</title> <link type="text/css" rel="stylesheet" href="../stylesheet.css"/> <style> .demo { height: 200px; width: 300px; margin-left: auto; margin-right: auto; background-color: #222222; display: block; overflow: hidden; position: relative; } .demoPanel { position: absolute; background-color: rgba(0,0,0,0); color: #EEEEEE; font-size: 1.2em; } .animation_button { margin-left: auto; margin-right: auto; display: block; height: 35px; width: 300px; margin-top: 3px; margin-bottom: 3px; } #animationSlider { width: 300px; margin-left: auto; margin-right: auto; margin-top: 6px; margin-bottom: 6px; display: block; } </style> </head> <body> <div class="demo"> <div id="handle001" class="demoPanel"></div> <div id="handle002" class="demoPanel"></div> </div> <input type="range" id="animationSlider" min="0" onchange="doSomeStuffSetStep(this.value)"/> <button class="animation_button" onclick="rerunClick()" >Rerun the Animation</button> <button class="animation_button" onclick="backwardsClick()" >Run the Animation Backwards</button> <button class="animation_button" onclick="pauseClick()" >Pause the Animation</button> <button class="animation_button" onclick="continueClick()" >Continue the Animation</button> <p id="elapsedElement"></p> <script> //specify element //specify initial properties //specify final properties //specify duration //specify start time //build list of commands that run for every animation call var bcActivitiesList = new Array; for (var i=0; i<1800; i++) { //> bcActivitiesList[i] = new Array; bcActivitiesList[i][0] = 0; //number of commands each animation step } //30 seconds of activities var bcFinalActivity = -1; //index of last animation activity var bcInitializations = new Array; for (var i=0; i<=1; i++) { bcInitializations[i] = new Array; bcInitializations[i][0] = 0; } function bcInitElement(pElement,pLeft,pTop,pColor,pBackgroundColor,pWidth,pOpacity,pDisplay,innerHTML) { pElement.style.left = pLeft; pElement.style.top = pTop; pElement.style.color = pColor; pElement.style.backgroundColor = pBackgroundColor; pElement.style.width = pWidth; pElement.style.opacity = pOpacity; pElement.style.display = pDisplay; pElement.innerHTML = innerHTML; } function bcDefineLeftTween(sElement,pStart,pEnd,tBegin,tDuration,mMethod) { var s = sElement+".style.left='"+pStart+"px';" bcInitializations[0][0] = bcInitializations[0].push(s); var alpha = tBegin * 60.0; var steps = tDuration * 60; var omega = alpha + steps; var increment = (pEnd - pStart) / steps; var left = pStart; if (alpha > 0) { bcActivitiesList[alpha-1][0] = bcActivitiesList[alpha-1].push(s); } for (var i=alpha; i<=omega; i++) { //> s = sElement+".style.left='"+Math.round(left)+"px';" bcActivitiesList[i][0] = bcActivitiesList[i].push(s); if (mMethod == "linear") { left += increment; } } bcInitializations[1][0] = bcInitializations[1].push(s); if (omega > bcFinalActivity) { bcFinalActivity = omega; } } function bcDefineTopTween(sElement,pStart,pEnd,tBegin,tDuration,mMethod) { var s = sElement+".style.top='"+pStart+"px';" bcInitializations[0][0] = bcInitializations[0].push(s); var alpha = tBegin * 60.0; var steps = tDuration * 60; var omega = alpha + steps; var increment = (pEnd - pStart) / steps; var top = pStart; if (alpha > 0) { bcActivitiesList[alpha-1][0] = bcActivitiesList[alpha-1].push(s); } for (var i=alpha; i<=omega; i++) { //> s = sElement+".style.top='"+Math.round(top)+"px';" bcActivitiesList[i][0] = bcActivitiesList[i].push(s); if (mMethod == "linear") { top += increment; } } bcInitializations[1][0] = bcInitializations[1].push(s); if (omega > bcFinalActivity) { bcFinalActivity = omega; } } function bcDefineOpacityTween(sElement,pStart,pEnd,tBegin,tDuration,mMethod) { var s = sElement+".style.opacity='"+pStart+"';" bcInitializations[0][0] = bcInitializations[0].push(s); var alpha = tBegin * 60.0; var steps = tDuration * 60; var omega = alpha + steps; var increment = (pEnd - pStart) / steps; var opacity = pStart; if (alpha > 0) { bcActivitiesList[alpha-1][0] = bcActivitiesList[alpha-1].push(s); } for (var i=alpha; i<=omega; i++) { //> s = sElement+".style.opacity='"+opacity+"';" bcActivitiesList[i][0] = bcActivitiesList[i].push(s); if (mMethod == "linear") { opacity += increment; } } bcInitializations[1][0] = bcInitializations[1].push(s); if (omega > bcFinalActivity) { bcFinalActivity = omega; } } function bcInitializeElements(beginOrEnd) { if (beginOrEnd == "begin") { var index = 0; } else if (beginOrEnd == "end") { var index = 1; } var i = 1; while (i <= bcInitializations[index][0]) { //> eval(bcInitializations[index][i]); i++; } } var bcAnimationSlider = document.getElementById("animationSlider"); function bcUpdateSlider(value) { bcAnimationSlider.value = value; } function bcInitializeSlider(value) { bcAnimationSlider.max = bcFinalActivity; bcAnimationSlider.value = value; } var elapsedHandle = document.getElementById("elapsedElement"); function doSomeStuff(step) { var startTime = Date.now(); var i = 1; while (i <= bcActivitiesList[step][0]) { //> eval(bcActivitiesList[step][i]); i++; } var endTime = Date.now(); var elapsed = endTime - startTime; if (elapsed > 16) { elapsedHandle.innerHTML = elapsed; } else { elapsedHandle.innerHTML = ""; } } var animationStep = 0; function doSomeStuffSetStep(step) { var numstep = Number(step); animationStep = numstep; doSomeStuff(numstep); } var handle001 = document.getElementById("handle001"); bcInitElement(handle001,"201px","50px","red","rgba(0,0,0,0)","260px",1,"block","This line slides in from the side"); bcDefineLeftTween("handle001",301,25,0.0,0.75,"linear"); bcDefineOpacityTween("handle001",0.0,1.0,0.0,0.75,"linear") var handle002 = document.getElementById("handle002"); bcInitElement(handle002,"13px","-25px","blue","rgba(0,0,0,0)","280px",1,"block","This line drops down from the top"); bcDefineTopTween("handle002",-25,125,0.5,0.75,"linear"); bcDefineOpacityTween("handle002",0.0,1.0,0.5,0.75,"linear") var maxAnimationSteps = bcFinalActivity; bcInitializeElements("begin"); bcInitializeSlider(0); var animateIncrement = 1; var requestId; function animateSomeStuff() { if ((animationStep >= 0) && (animationStep <= maxAnimationSteps)) { doSomeStuff(animationStep); bcUpdateSlider(animationStep); } if ((animationStep < 0) || (animationStep > maxAnimationSteps)) { cancelAnimationFrame(requestID); } else { animationStep += animateIncrement; } requestId = window.requestAnimationFrame(animateSomeStuff); } animateSomeStuff(); function rerunClick() { animationStep = 0; bcInitializeElements("begin"); bcUpdateSlider(animationStep); animateIncrement = 1; animateSomeStuff(); } function backwardsClick() { animationStep = bcFinalActivity; bcInitializeElements("end"); bcUpdateSlider(animationStep); animateIncrement = -1; animateSomeStuff(); } function pauseClick() { cancelAnimationFrame(requestId); } function continueClick() { animateSomeStuff(); } </script> </body> </html> |