Blog

Still having fun with Flash

I would like to share some little piece of code that I wrote recently in AS3. It’s been quite a while since last time I did anything with Flash technology and I must admit that I had loads of fun. Maybe too much. I still can’t believe that this technology is dying in favour of HTML considering only the technical parts. I really support what the Web and all it’s technology represents and how much it empowers people who care to learn it. I really get inspired by the folks at Mozilla and how hard they work to keep the web an open competitive place for everyone.

But dealing with browser glitches and wimps, render engines optimisations, thousands of mobile phones, hunting for bugs on hundreds of CSS rules and so on is just a freaking nightmare! Specially when you are on the frontline on release day. It’s totally doable, I get paid really well to do it, but it is not fun. There’s also that dread that every single day there’s a newer technology supposedly much better the one you’ve been learning on the past 24h. There are so many frameworks and libraries and plugins to handle every device/browser/os. Or maybe it’s your client/project/boss that demands some specific technology. Flash and Java have the same problem but it’s much much smaller. Instead of choosing between 5 Flash libraries for animating something on the screen, you have to choose between 15 in HTML. By the way, those are fictional numbers just so that you can feel the dread. Or is it really?

In my opinion, Flash will not go away anytime soon. It changed too many things, it runs in too many environments, it works for many projects, there are too many experts and tools and books and blogs and knowledge. You can’t kill something this big that easy. And it’s also a lot of fun!

Enough sentimental blabber! I implemented a small super efficient image scroller using a few 2D rendering tricks of the platform. Using Stage3D would probably give even better results but it’s out of my reach for now. This little program works really well for navigating huge panoramic images smooooothly.  Check it out for yourself. The PanoViewer Demo is a release build so make sure to use a Release Flash Player plugin. You can get a really amazing effect if your monitor has very low response time and delay. My monitor failed miserably on these tests so the effect beauty is really diminished.

There are two projects: PanoViewer and Panomax.

PanoViewer

It’s the Flash application that handles the logic of loading assets, rendering buttons and initializing the Panomax instance. It’s important to notice the SWF metadata tag with the frameRate = “1” parameter. This specifies the frame rate of the application, in frames per second. The default value is 24. Lowering it’s value gives the runtime a much longer Elastic Racetrack , roughly 1/2 second for each phase, which also means lower loads on the system. This is a key concept to understand and we’ll exploit it.

The CachingImageLoader is a very simple external image loader with very basic caching functionality and tries to deal with Security Sandbox details. The CTAButton – Call To Action – is a custom button class with different style and it makes sure the text label always fits its boundaries.

Panomax

Here’s where the magic happens. The loaded panoramic image (Bitmap) and other parameters are passed to the Panomax instance. It calculates all boundaries, configure the interactions and starts a Timer. On the TimerEvent handler function that all matrix transformations are calculated and where a part of the panoramic image is painted on the canvas. If it was not obvious yet, painting a small portion of an image is much faster than moving a gigantic image around. We just need 1 Matrix object and 1 call to BitmapData.draw(…). Things start to get very interesting when we add the horizontal looping (function onTimerXLoop). To help us understand how it works I made this schematic:

Panomax schematics

You have to start from the middle, where gTx = 0. If you go reading up, gTx increases and the image is moving to the left. If you go reading down, the image is moving to the right. Let’s not forget that Matrix transformations (tx, ty) signs means that negative is to the left and positive is to the right. Here’s an awesome Matrix transformation tutorial if you are not familiar. Since the image loops horizontally, the are some states in which it needs to be rendered twice: one on the far left and the other on the far right. For that we use 2 Matrix objects and 2 BitmapData.draw(…) calls which is the heaviest state for the system.

There are some clever tricks that we can do with the transformations of the matrixes in order to use simple formulas. For instance, once the left edge of image A moves out on the right edge of the canvas, it looks exactly like image B with its right edge on the right edge of the canvas which also looks exactly like image A with it’s right edge on the right edge of the canvas. So we can swap/render B with A! Clearly, from my code, I couldn’t figure out the simple formulas so I ended up with complicated ones.

Now, for the last trick, we need to analyse just 1 line of code: e.updateAfterEvent(); // for immediate rendering! To understand this you really have to dominate the concept of the Elastic Racetrack that I mentioned earlier. With the app frame rate reduced to 1, our async code (the TimerEvent handler function) has more time to run and most probably won’t interfere with the other phases of the racetrack. So the Matrix calculations are done, the images are rendered on the canvas but the application frame is not updated yet because the rendering phase is still many milliseconds away. Thus we force the rendering with plenty of time to do so. With enough time for both racetrack phases, what we get is butter smooth scrolling performance!

This is it! I hope you liked it this tutorial. The code is on Github so go ahead and study and fork it!

Panorama Image Credits

  1. SonyCenter 360 panorama: Homepage, Download, Author: François Reincke

    Full resolution: 3,500 × 1,096 pixels, file size: 922 KB, MIME type: image/jpeg, License: CC-BY-SA 3.0.

  2. Trafalgar Square: Homepage, DownloadAuthor: David Iliff alias DiliffFull resolution: 9.932 × 2.075 pixels, File size: 5.67 MB, MIME type: image/jpeg, Camera location: 51° 30′ 29.39″ N, 0° 7′ 41.31″ W, License: CC-BY-SA 3.0.
  3. Lac de Joux: Homepage, Download,Author: Lausanne De Suisse alias 100zaxFull resolution: 6.000 × 697 pixels, File size: 759 KB, MIME type: image/jpeg, Camera location: ??? N, ??? W,License: CC-BY-SA 3.0.

Flattr this!

Finally my private place on the web!

Hello all!

This is yet another adventure start for me. My first domain! My first private spot on the internet where I can make it the way I want and fill it with stuff that I want (and is not illegal off course). I don’t think I will create a WikiLeaks or a new YouTube but I might add another cool place on the internet. Only time will tell. I must say I love to begin new projects but, like any ordinary folk, they tend not to be completed. This one is different because it doesn’t have a time limit, a dead line nor never will be completed. This domain will come along with me for the rest of my life, improving, growing and evolving as my self.

The main goal of this domain is actually very simple: the start point off my second life which is my online life. It needs to be nurtured, managed and honed because I consider it a valuable asset and part of me. I want to unify all my online presence into one single unique image that can translate into what I really am in real life. I am sure that I can get a benefit out of it. But there will be a lot of work to be done, a lot of knowledge to earn and some challenges to overcome. The good thing is that none of it scares me.

Another thing before I forget: I am more and more involved into helping saving the world. I’m not yet an eco-boring or whatever but I can clearly see what kind of shit we are diving heads first and that raised my awareness and conciousness. The decisions that we make now will have great impact tomorrow so I proudly chose to host this domain in HostPapa which is a Certified Green Web Hosting. Little by little we’ll make our time on this planet a little bit longer and more pleasant.

Hold up everybody! We can do this!

Oh yeah! This domain is going to be full of clichés, impact phrases, crazy talking and who knows what else can come out!

I talk and write like a dreamer.

Nice to meet you and thanks again for stopping by.

Flattr this!