{"id":786,"date":"2016-06-02T12:10:54","date_gmt":"2016-06-02T17:10:54","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=786"},"modified":"2017-02-03T15:03:41","modified_gmt":"2017-02-03T20:03:41","slug":"reproducing-a-clever-animation-product-part-3","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/06\/02\/reproducing-a-clever-animation-product-part-3\/","title":{"rendered":"Reproducing A Clever Animation Product, Part 3"},"content":{"rendered":"<p>Today I implemented a few more basic animation features and gained more insight into how the <a href=\"http:\/\/greensock.com\/\">Greensock animation product<\/a> probably works.<\/p>\n<p>The first thing I did was add the capability to pause and continue the animation.  That was trivial.<\/p>\n<p>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?<\/p>\n<p>Not right.<\/p>\n<p>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&#8217;re moving forward through the animation this isn&#8217;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&#8217;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).<\/p>\n<p>This actually works well&#8211;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.<\/p>\n<p>Enter the slider bar. <\/p>\n<p>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&#8217;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&#8217;s supposed to be in.<\/p>\n<p>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&#8217;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.<\/p>\n<p><iframe loading=\"lazy\" width=\"330px\" height=\"410px\" src=\"https:\/\/www.rpchurchill.com\/demo\/animation\/animation_demo_20160602.html\" frameborder=\"0\" allowfullscreen><\/iframe><\/p>\n<p>And, here is the code in its increasingly entropic glory.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n<!DOCTYPE html>\r\n<html>\r\n  <head>\r\n    <title>Fast Animation Script Demo<\/title>\r\n    <link type=\"text\/css\" rel=\"stylesheet\" href=\"..\/stylesheet.css\"\/>\r\n\r\n    <style>\r\n      .demo {\r\n        height: 200px;\r\n        width: 300px;\r\n        margin-left: auto;\r\n        margin-right: auto;\r\n        background-color: #222222;\r\n        display: block;\r\n        overflow: hidden;\r\n        position: relative;\r\n      }\r\n      .demoPanel {\r\n        position: absolute;\r\n        background-color: rgba(0,0,0,0);\r\n        color: #EEEEEE;\r\n        font-size: 1.2em;\r\n      }\r\n      .animation_button {\r\n        margin-left: auto;\r\n        margin-right: auto;\r\n        display: block;\r\n        height: 35px;\r\n        width: 300px;\r\n        margin-top: 3px;\r\n        margin-bottom: 3px;\r\n      }\r\n      #animationSlider {\r\n        width: 300px;\r\n        margin-left: auto;\r\n        margin-right: auto;\r\n        margin-top: 6px;\r\n        margin-bottom: 6px;\r\n        display: block;        \r\n      }\r\n    <\/style>\r\n  <\/head>\r\n  <body>\r\n    <div class=\"demo\">\r\n      <div id=\"handle001\" class=\"demoPanel\"><\/div>\r\n      <div id=\"handle002\" class=\"demoPanel\"><\/div>\r\n    <\/div>\r\n    <input type=\"range\" id=\"animationSlider\" min=\"0\" onchange=\"doSomeStuffSetStep(this.value)\"\/>\r\n    <button class=\"animation_button\" onclick=\"rerunClick()\" >Rerun the Animation<\/button>\r\n    <button class=\"animation_button\" onclick=\"backwardsClick()\" >Run the Animation Backwards<\/button>\r\n    <button class=\"animation_button\" onclick=\"pauseClick()\" >Pause the Animation<\/button>\r\n    <button class=\"animation_button\" onclick=\"continueClick()\" >Continue the Animation<\/button>\r\n    <p id=\"elapsedElement\"><\/p>\r\n    <script>\r\n      \/\/specify element\r\n      \/\/specify initial properties\r\n      \/\/specify final properties\r\n      \/\/specify duration\r\n      \/\/specify start time\r\n      \/\/build list of commands that run for every animation call\r\n      \r\n      var bcActivitiesList = new Array;\r\n      for (var i=0; i<1800; i++) {  \/\/>\r\n        bcActivitiesList[i] = new Array;\r\n        bcActivitiesList[i][0] = 0;  \/\/number of commands each animation step\r\n      }  \/\/30 seconds of activities\r\n      var bcFinalActivity = -1;  \/\/index of last animation activity\r\n      \r\n      var bcInitializations = new Array;\r\n      for (var i=0; i<=1; i++) {\r\n        bcInitializations[i] = new Array;\r\n        bcInitializations[i][0] = 0;\r\n      }\r\n\r\n      function bcInitElement(pElement,pLeft,pTop,pColor,pBackgroundColor,pWidth,pOpacity,pDisplay,innerHTML) {\r\n        pElement.style.left = pLeft;\r\n        pElement.style.top = pTop;\r\n        pElement.style.color = pColor;\r\n        pElement.style.backgroundColor = pBackgroundColor;\r\n        pElement.style.width = pWidth;\r\n        pElement.style.opacity = pOpacity;\r\n        pElement.style.display = pDisplay;\r\n        pElement.innerHTML = innerHTML;\r\n      }\r\n      function bcDefineLeftTween(sElement,pStart,pEnd,tBegin,tDuration,mMethod) {\r\n        var s = sElement+\".style.left='\"+pStart+\"px';\"\r\n        bcInitializations[0][0] = bcInitializations[0].push(s);\r\n        var alpha = tBegin * 60.0;\r\n        var steps = tDuration * 60;\r\n        var omega = alpha + steps;\r\n        var increment = (pEnd - pStart) \/ steps;\r\n        var left = pStart;\r\n        if (alpha > 0) {\r\n          bcActivitiesList[alpha-1][0] = bcActivitiesList[alpha-1].push(s);\r\n        }\r\n        for (var i=alpha; i<=omega; i++) { \/\/>\r\n          s = sElement+\".style.left='\"+Math.round(left)+\"px';\"\r\n          bcActivitiesList[i][0] = bcActivitiesList[i].push(s);\r\n          if (mMethod == \"linear\") {\r\n            left += increment;\r\n          }\r\n        }  \r\n        bcInitializations[1][0] = bcInitializations[1].push(s);\r\n        if (omega > bcFinalActivity) {\r\n          bcFinalActivity = omega;\r\n        }\r\n      }\r\n      function bcDefineTopTween(sElement,pStart,pEnd,tBegin,tDuration,mMethod) {\r\n        var s = sElement+\".style.top='\"+pStart+\"px';\"\r\n        bcInitializations[0][0] = bcInitializations[0].push(s);\r\n        var alpha = tBegin * 60.0;\r\n        var steps = tDuration * 60;\r\n        var omega = alpha + steps;\r\n        var increment = (pEnd - pStart) \/ steps;\r\n        var top = pStart;\r\n        if (alpha > 0) {\r\n          bcActivitiesList[alpha-1][0] = bcActivitiesList[alpha-1].push(s);\r\n        }\r\n        for (var i=alpha; i<=omega; i++) { \/\/>\r\n          s = sElement+\".style.top='\"+Math.round(top)+\"px';\"\r\n          bcActivitiesList[i][0] = bcActivitiesList[i].push(s);\r\n          if (mMethod == \"linear\") {\r\n            top += increment;\r\n          }\r\n        }  \r\n        bcInitializations[1][0] = bcInitializations[1].push(s);\r\n        if (omega > bcFinalActivity) {\r\n          bcFinalActivity = omega;\r\n        }\r\n      }\r\n      function bcDefineOpacityTween(sElement,pStart,pEnd,tBegin,tDuration,mMethod) {\r\n        var s = sElement+\".style.opacity='\"+pStart+\"';\"\r\n        bcInitializations[0][0] = bcInitializations[0].push(s);\r\n        var alpha = tBegin * 60.0;\r\n        var steps = tDuration * 60;\r\n        var omega = alpha + steps;\r\n        var increment = (pEnd - pStart) \/ steps;\r\n        var opacity = pStart;\r\n        if (alpha > 0) {\r\n          bcActivitiesList[alpha-1][0] = bcActivitiesList[alpha-1].push(s);\r\n        }\r\n        for (var i=alpha; i<=omega; i++) { \/\/>\r\n          s = sElement+\".style.opacity='\"+opacity+\"';\"\r\n          bcActivitiesList[i][0] = bcActivitiesList[i].push(s);\r\n          if (mMethod == \"linear\") {\r\n            opacity += increment;\r\n          }\r\n        }  \r\n        bcInitializations[1][0] = bcInitializations[1].push(s);\r\n        if (omega > bcFinalActivity) {\r\n          bcFinalActivity = omega;\r\n        }\r\n      }\r\n      \r\n      function bcInitializeElements(beginOrEnd) {\r\n        if (beginOrEnd == \"begin\") {\r\n          var index = 0;\r\n        } else if (beginOrEnd == \"end\") {\r\n          var index = 1;\r\n        }\r\n        var i = 1;\r\n        while (i <= bcInitializations[index][0]) {  \/\/>\r\n          eval(bcInitializations[index][i]);\r\n          i++;\r\n        }\r\n      }\r\n      var bcAnimationSlider = document.getElementById(\"animationSlider\");\r\n      function bcUpdateSlider(value) {\r\n        bcAnimationSlider.value = value;\r\n      }\r\n      function bcInitializeSlider(value) {\r\n        bcAnimationSlider.max = bcFinalActivity;\r\n        bcAnimationSlider.value = value;\r\n      }\r\n      \r\n      var elapsedHandle = document.getElementById(\"elapsedElement\");\r\n      function doSomeStuff(step) {\r\n        var startTime = Date.now();\r\n        var i = 1;\r\n        while (i <= bcActivitiesList[step][0]) {  \/\/>\r\n          eval(bcActivitiesList[step][i]);\r\n          i++;\r\n        }\r\n        var endTime = Date.now();\r\n        var elapsed = endTime - startTime;\r\n        if (elapsed > 16) {\r\n          elapsedHandle.innerHTML = elapsed;\r\n        } else {\r\n          elapsedHandle.innerHTML = \"\";\r\n        }\r\n      } \r\n\r\n      var animationStep = 0;\r\n      \r\n      function doSomeStuffSetStep(step) {\r\n        var numstep = Number(step);\r\n        animationStep = numstep;\r\n        doSomeStuff(numstep);\r\n      }\r\n      \r\n      var handle001 = document.getElementById(\"handle001\");\r\n      bcInitElement(handle001,\"201px\",\"50px\",\"red\",\"rgba(0,0,0,0)\",\"260px\",1,\"block\",\"This line slides in from the side\");\r\n      bcDefineLeftTween(\"handle001\",301,25,0.0,0.75,\"linear\");\r\n      bcDefineOpacityTween(\"handle001\",0.0,1.0,0.0,0.75,\"linear\")\r\n      \r\n      var handle002 = document.getElementById(\"handle002\");\r\n      bcInitElement(handle002,\"13px\",\"-25px\",\"blue\",\"rgba(0,0,0,0)\",\"280px\",1,\"block\",\"This line drops down from the top\");\r\n      bcDefineTopTween(\"handle002\",-25,125,0.5,0.75,\"linear\");\r\n      bcDefineOpacityTween(\"handle002\",0.0,1.0,0.5,0.75,\"linear\")\r\n      \r\n      var maxAnimationSteps = bcFinalActivity;\r\n\r\n      bcInitializeElements(\"begin\");\r\n      bcInitializeSlider(0);\r\n      var animateIncrement = 1;\r\n      var requestId;\r\n      function animateSomeStuff() {\r\n        if ((animationStep >= 0) && (animationStep <= maxAnimationSteps)) {\r\n          doSomeStuff(animationStep);\r\n          bcUpdateSlider(animationStep);\r\n        }\r\n        if ((animationStep < 0) || (animationStep > maxAnimationSteps)) {\r\n          cancelAnimationFrame(requestID);\r\n        } else {\r\n          animationStep += animateIncrement;\r\n        } \r\n        requestId = window.requestAnimationFrame(animateSomeStuff);      \r\n      }\r\n      animateSomeStuff();\r\n      \r\n      function rerunClick() {\r\n        animationStep = 0;\r\n        bcInitializeElements(\"begin\");\r\n        bcUpdateSlider(animationStep);\r\n        animateIncrement = 1;\r\n        animateSomeStuff();\r\n      }\r\n      function backwardsClick() {\r\n        animationStep = bcFinalActivity;\r\n        bcInitializeElements(\"end\");\r\n        bcUpdateSlider(animationStep);\r\n        animateIncrement = -1;\r\n        animateSomeStuff();\r\n      }\r\n      function pauseClick() {\r\n        cancelAnimationFrame(requestId);\r\n      }\r\n      function continueClick() {\r\n        animateSomeStuff();\r\n      }\r\n    <\/script>\r\n  <\/body>\r\n<\/html><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>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 &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/06\/02\/reproducing-a-clever-animation-product-part-3\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[14],"tags":[87,101,49],"_links":{"self":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/786"}],"collection":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/comments?post=786"}],"version-history":[{"count":4,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/786\/revisions"}],"predecessor-version":[{"id":1465,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/786\/revisions\/1465"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=786"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=786"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=786"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}