{"id":863,"date":"2016-07-07T15:01:42","date_gmt":"2016-07-07T20:01:42","guid":{"rendered":"http:\/\/rpchurchill.com\/?p=863"},"modified":"2016-07-11T20:57:04","modified_gmt":"2016-07-12T01:57:04","slug":"reproducing-a-clever-animation-product-part-21","status":"publish","type":"post","link":"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/07\/07\/reproducing-a-clever-animation-product-part-21\/","title":{"rendered":"Reproducing A Clever Animation Product, Part 21"},"content":{"rendered":"<p>The last subject I wanted to explore was animation effects, which is to say defining tweens that proceed from beginning to end in a fashion that is other than linear, which is the default I&#8217;ve been using to this point.  The tweens are defined using the <code>addTransformToList<\/code> function, as shown in multiple instances below.<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n      var handle001 = document.getElementById(\"handle001\");\r\n      bcInitElement(handle001,\"301px\",\"50px\",\"red\",\"rgba(0,0,0,0)\",\"260px\",0,\"block\",\"This line slides in, out, in\");\r\n      addTransformToList(\"handle001:1\",0.75,{left:-284, opacity:1.0});\r\n      addTransformToList(\"handle001\",0.50,{left:284});\r\n      addTransformToList(\"handle001:2\",0.75,{left:-284});\r\n      addTransformToList(\"handle001\",0.00,{display:\"none\"});\r\n      \r\n      var handle002 = document.getElementById(\"handle002\");\r\n      bcInitElement(handle002,\"17px\",\"-25px\",\"blue\",\"rgba(0,0,0,0)\",\"280px\",0,\"block\",\"This line drops down from the top\");\r\n      addTransformToList(\"handle002\",0.75,{top:150, opacity:1.0},\"linear\",\"handle001:1-=0.25\");\r\n      addTransformToList(\"handle002\",0.00,{display:\"none\"},\"\",\"handle001:2\");\r\n\r\n      var handle003 = document.getElementById(\"handle003\");\r\n      bcInitElement(handle003,\"120px\",\"60px\",\"yellow\",\"rgba(0,0,0,0)\",\"80px\",0,\"block\",\"This text grows in scale\");\r\n      \/\/handle003.style.transform = \"rotate(45deg) scale(0.5,0.5)\";\r\n      \/\/handle003.style.transform = \"rotate(45deg)\";\r\n      handle003.style.transform = \"scale(0.5,0.5)\";\r\n      addTransformToList(\"handle003\",0.05,{opacity:1.0},\"linear\",\"handle001:2-=0.05\");\r\n      addTransformToList(\"handle003\",1.50,{scale:\"(2.0,2.0)\"},\"linear\",\"+=0.50\");\r\n      addTransformToList(\"handle003\",0.00,{display:\"none\"});\r\n\r\n      var handle004 = document.getElementById(\"handle004\");\r\n      bcInitElement(handle004,\"80px\",\"70px\",\"orange\",\"rgba(0,0,0,0)\",\"140px\",0,\"block\",\"This text rotates into place\");\r\n      addTransformToList(\"handle004\",1.00,{opacity:1.0});\r\n      addTransformToList(\"handle004\",1.00,{rotate:\"-360\"},\"linear\",\"-=1.00\");\r\n<\/pre>\n<p>If, instead of defining a linear tween, we wanted to define one that behaved differently, we&#8217;d have to provide an alternate string or object literal.  The information would have to include as many parameters as required to govern the desired effect.  Here&#8217;s one I did for an animation on one of my intro pages; it simply specifies a string and the details are hard-coded in the tween function (in this case a <code>bcDefineLeftTween<\/code> function).<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\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        } else if (mMethod == \"vibrate_right\") {\r\n          var multiplier = i - alpha - (Math.PI*0.5);\r\n          multiplier *= 0.3;\r\n          multiplier = Math.cos(multiplier);\r\n          multiplier *= 100.0;\r\n          multiplier *= (omega-i) \/ (omega-alpha);\r\n          \/\/var multiplier = Math.cos(i - alpha - Math.PI*0.5)* 100 * (omega-i) \/ (omega-alpha);\r\n          left = pStart + multiplier;\r\n        }\r\n      }  \r\n<\/pre>\n<p>The idea is that the element would start in a given spot, get kicked to the right by some specified amplitude, vibrate back to the original position in a sine wave pattern, with a vibration having a specified period, and with a specified decay (during which the vibration gets smaller and smaller).  I used parameters to govern the amplitude (the 100.0, in pixels), period (the 0.3, modifying the cumulative angle), and decay (the (omega-i) \/ (omega-alpha) term) so I might define an object literal of the form:<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n  {effect: \"vibrate_right\", freq: 0.3, amplitude: 100.0, decay: \"linear\"}      \r\n<\/pre>\n<p>If I wanted the vibrate starting to the left I would just change the sign of the last term in the first line of the relevant block like this:<\/p>\n<pre class=\"toolbar-overlay:false wrap:false height-set:true lang:default decode:true \">\r\n          var multiplier = i - alpha + (Math.PI*0.5);\r\n<\/pre>\n<p>This would make the tween start in the original location but as the quantity i &#8211; alpha began to increase the cosine value would first go negative instead of positive.<\/p>\n<p>The tween definitions here aren&#8217;t bad but we&#8217;d certainly want to implement something more modular.  We wouldn&#8217;t want to have to embed all that specialized code in each function that defines a tween, we&#8217;d probably want to use a functional parameter that gets its parameters set in one place, then is called in the tween function using a standard-looking interface that takes only the alpha, omega, and i values as inputs.  Any other method would involve too much duplication of code.<\/p>\n<p>If you are wondering, this is implemented in the Greensock product in a very powerful way.  They&#8217;ve created a page which lets you see how it all works in detail, <a href=\"http:\/\/greensock.com\/ease-visualizer\">here<\/a>.  As I&#8217;ve stated before, the folks who&#8217;ve created this product have done such a nice job with it, and their licensing terms are so easy, that it would be crazy to try to reverse-engineer or duplicate the whole thing.<\/p>\n<p>One type of effect I do want to create involves simulating gravity, so that when you give an element an initial velocity moving upward, a downward velocity increment is added during every animation step.  Combined with a modest leftward or rightward movement, an object can be made to &#8220;fly&#8221; in a natural-looking arc.  A bunch of objects animated together in this way could be made to look like a fountain of sorts.  See <a href=\"http:\/\/greensock.com\/\">here<\/a> (the effect starts about halfway through the title animation) for the inspiration for this exercise, which will begin next week.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>The last subject I wanted to explore was animation effects, which is to say defining tweens that proceed from beginning to end in a fashion that is other than linear, which is the default I&#8217;ve been using to this point. &hellip; <a href=\"https:\/\/rpchurchill.com\/wordpress\/posts\/2016\/07\/07\/reproducing-a-clever-animation-product-part-21\/\">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\/863"}],"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=863"}],"version-history":[{"count":3,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/863\/revisions"}],"predecessor-version":[{"id":866,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/posts\/863\/revisions\/866"}],"wp:attachment":[{"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/media?parent=863"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/categories?post=863"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/rpchurchill.com\/wordpress\/wp-json\/wp\/v2\/tags?post=863"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}