{"id":208,"date":"2007-11-19T21:17:44","date_gmt":"2007-11-20T05:17:44","guid":{"rendered":"http:\/\/dougmccune.com\/blog\/2007\/11\/19\/flex-coverflow-performance-improvement-flex-carousel-component-and-vertical-coverflow\/"},"modified":"2008-11-02T17:01:32","modified_gmt":"2008-11-03T01:01:32","slug":"flex-coverflow-performance-improvement-flex-carousel-component-and-vertical-coverflow","status":"publish","type":"post","link":"https:\/\/dougmccune.com\/blog\/2007\/11\/19\/flex-coverflow-performance-improvement-flex-carousel-component-and-vertical-coverflow\/","title":{"rendered":"Flex CoverFlow performance improvement, Flex Carousel Component, and Vertical CoverFlow"},"content":{"rendered":"<div style=\"border:1px solid red; background-color:#efefef; padding:10px;\">\n<strong>UPDATE: Feb 26, 2008<\/strong><br \/>\nI have updated the CoverFlow library a bit and added support for adding children without explicitly setting the width and height of the children (ie using the normal Flex auto-sizing validation stuff). Grab the <a href=\"http:\/\/dougmccune.com\/flex\/coverflow\/dougmccune_coverflowlib.zip\">latest source code here<\/a>.\n<\/div>\n<p>I&#8217;ve made some much needed performance improvements to the Flex CoverFlow component I released a few days ago. I&#8217;ve also taken the concept of 3D Flex containers a little further and added a 3D Carousel container and a vertical version of CoverFlow. <a href=\"http:\/\/dougmccune.com\/flex\/coverflow\/flexcontrols\/srcview\/index.html\">Source code here<\/a>.<\/p>\n<p>Check out the performance now and stop whining (oh, and notice that this blog post has 3 of these embedded 3D components):<br \/>\n<div id=\"swf44431\">This movie requires Flash Player 9.<\/div>\n<script type=\"text\/javascript\">\n\tswfobject.embedSWF(\"http:\/\/dougmccune.com\/flex\/coverflow\/itunes\/CoverFlow_ITunes.swf\", \"swf44431\", \"450\", \"250\", \"9.0.0\", \"\", {}, {wmode: \"window\", menu: \"false\", quality: \"high\", bgcolor: \"#FFFFFF\", allowScriptAccess: \"always\"}, {});\n<\/script>\n<\/p>\n<p><strong>Performance improvements<\/strong><br \/>\nWhen I first released the component I took the shotgun approach to rendering, which meant I rendered the entire scene every frame and all the materials were animated. This is the lazy approach that ensures that the 3D scene always looks right, but it also kills performance. So you poor people out there with computers from the 1980s complained that the component made your browser shoot up to 110% cpu and all that. Instead of telling you to get faster computers I decided to make a few small modifications to speed things up.<\/p>\n<p>First, instead of rendering every frame we&#8217;re now rendering only while the animation is happening. This is a sweet improvement that Brock Brinkerhoff suggested in an email he sent me (thanks Brock!). Basically in our enter frame handler we check if Tweener is currently tweening the selected child. If so, we make PaperVision 3D render the scene. If not we just ignore and don&#8217;t waste CPU power. This means that once the movement has stopped (ie the selected child comes to rest in the center), the component no longer uses much CPU at all.<\/p>\n<p>The second improvement was to not use animated materials. I added a new material that I called <code>FlexMaterial<\/code> (and the associated reflected version, <code>ReflectionFlexMaterial<\/code>). This material is non animated, but listens for <code>FlexEvent.UPDATE_COMPLETE<\/code> events fired from the child (and all of the child&#8217;s children if it has any). Whenever an <code>updateComplete<\/code> event fires the material re-renders. This ensures that our material is always up-to-date. One note about this: if you have animations in your child components that do not dispatch <code>updateComplete<\/code> events you&#8217;ll have to manually dispatch <code>updateComplete<\/code> over and over. This is pretty easy, basically what I did for a child that I wanted animated was add the following code in MXML:<br \/>\n<code>enterFrame=\"event.currentTarget.dispatchEvent(new FlexEvent(FlexEvent.UPDATE_COMPLETE))\"<\/code><br \/>\nand that ensures that the child will always be updated.<\/p>\n<p>Oh, and while I was at it I made a modification to get around the z-ordering issue. This was a problem when you selected one of the planes that was not directly next to the selected one. When the selected plane moved back into place it would move through the other planes on its way. Now we push the selected plane straight back at a faster rate than it moves sideways, seems to have pretty much fixed the issue (at least good enough for my eyes).<\/p>\n<div style=\"float:right; padding-left:10px; padding-bottom:10px;\">\n<div id=\"swf44432\">This movie requires Flash Player 9.<\/div>\n<script type=\"text\/javascript\">\n\tswfobject.embedSWF(\"http:\/\/dougmccune.com\/flex\/coverflow\/vitunes\/VCoverFlow_ITunes.swf\", \"swf44432\", \"200\", \"400\", \"9.0.0\", \"\", {}, {wmode: \"window\", menu: \"false\", quality: \"high\", bgcolor: \"#FFFFFF\", allowScriptAccess: \"always\"}, {});\n<\/script>\n\n<\/div>\n<p><strong>Vertical CoverFlow<\/strong><br \/>\nI refactored the code a little bit to allow me to easily change the 3D layout. This let me crank out a vertical version of the CoverFlow component in no time, shown here on the right. This is almost all the same code, just tweaked to lay things out vertically instead of horizontally. I figure this can be used for a sidebar widget. I had to remove the sweet web 2.0 reflections cause I didn&#8217;t know where they would go. The cool part about the refactored code is that the base component handles all the core stuff (like creating the 3d scene and planes, etc) and then each extension can simply modify the layout method. Sweet.<br \/>\n<br style=\"clear:both;\" \/><br \/>\n<strong>Carousel Flex Component<\/strong><br \/>\nAnd while I was playing I couldn&#8217;t help taking some of <a href=\"http:\/\/theflashblog.com\/?p=293\">Lee Brimelow&#8217;s code<\/a> and making a 3D carousel Flex component. Thanks to Lee for doing the math for me. So here&#8217;s a Flex container to do that 3D carousel thingy. One sweet thing is that I took the same approach to preserve full interactivity of the child components. When the selected child is rotated to completely face the user the real child is swapped with the 3D plane and you can fully interact with it just like in a normal Flex app. <\/p>\n<p><div id=\"swf44433\">This movie requires Flash Player 9.<\/div>\n<script type=\"text\/javascript\">\n\tswfobject.embedSWF(\"http:\/\/dougmccune.com\/flex\/coverflow\/carouseltunes\/Carousel_ITunes.swf\", \"swf44433\", \"450\", \"240\", \"9.0.0\", \"\", {}, {wmode: \"window\", menu: \"false\", quality: \"high\", bgcolor: \"#FFFFFF\", allowScriptAccess: \"always\"}, {});\n<\/script>\n<\/p>\n<p><strong>Take it further<\/strong><br \/>\nSo if you take a look at the source code you&#8217;ll see that I&#8217;ve created a <code>BasePV3DContainer<\/code>, which extends ViewStack and creates 3D planes for each child in the container. The idea here is that you can create an extension of this class and implement the <code>layoutChildren()<\/code> method and make up your own 3D container component. I wouldn&#8217;t say the base class I&#8217;ve provided is perfect, there&#8217;s certainly some code that should get moved around or done differently, but you can figure it out (hell, it&#8217;s only 300 some lines of code with comments). If you make any other 3D container Flex components then either post a comment here or drop me an email: doug@dougmccune.com.<\/p>\n<p><a href=\"http:\/\/dougmccune.com\/flex\/coverflow\/flexcontrols\/srcview\/index.html\"><strong>Get the source<\/strong><\/a><br \/>\n(Note that this source package include the CoverFlow, Vertical CoverFlow, and Carousel components, as well as an example showing using the CoverFlow component with Flex controls. This does not include the iTunes examples in this post. I&#8217;m too lazy to clean that code up well enough to give out.)<\/p>\n","protected":false},"excerpt":{"rendered":"<p>UPDATE: Feb 26, 2008 I have updated the CoverFlow library a bit and added support for adding children without explicitly setting the width and height of the children (ie using the normal Flex auto-sizing validation stuff). Grab the latest source code here. I&#8217;ve made some much needed performance improvements to the Flex CoverFlow component I [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[2],"tags":[27],"class_list":["post-208","post","type-post","status-publish","format-standard","hentry","category-flex","tag-papervision-3d"],"aioseo_notices":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"_links":{"self":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/208","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/comments?post=208"}],"version-history":[{"count":3,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/208\/revisions"}],"predecessor-version":[{"id":354,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/posts\/208\/revisions\/354"}],"wp:attachment":[{"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/media?parent=208"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/categories?post=208"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dougmccune.com\/blog\/wp-json\/wp\/v2\/tags?post=208"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}