Archive for January, 2010

Jan 11 2010

Get Fancy with Flex 4 - Part Two

Published by Corey under Flex 4, Uncategorized

As a quick follow-up to my last post, here is another nifty transition. It’s a radial version of the classic “Squish” transition, in that the old image starts to peel away from the center out, to reveal a new image.

Here’s a simple example where I exaggerate the effect a bit to show you how it works - a possible prototype for a dynamic color picker (or something, yeah I’m grasping):





I wanted to go a bit further and mention that you can use any of these transitions within your AIR applications as well, makes for very fun desktop toy..or at least for fun transitions between states of a fun desktop toy, whether it be a cross fade, twist, morph, radial wipe… just experiment!


 

Click here to install the ‘flowers’ applet for desktop use.

Click here for the sample and shader source code.

One response so far

Jan 06 2010

Getting Fancy with Flex 4

Published by Corey under Flex 4

For those familiar with the Pixel Bender feature that shipped with Flash 10 you know that it’s possible to author custom image filters or blend filters for static content. With Flex 4 and the new AnimateTransitionShader effect you can now leverage Pixel Bender filters as the “engine” behind animated custom state transitions.

The idea behind AnimateTransitionShader is that the SDK will take care of the gory details behind:

  1. Taking a before bitmap snapshot of the effect target before the state change.
  2. Taking an after snapshot of the effect target in the new state.
  3. Assigning the before and after images to the Pixel Bender shader and animating the ‘progress’ property of the shader, updating the screen accordingly with the results of the shader operation for each frame.

Let’s take a quick look at a simple example.

Consider the animation below where our applet has a label or caption that changes values every few seconds. In this example we transition “to” each state using a modified version of the Hexagonal Tiling shader written by Petri Leskinen.





A quick walk-through of the code below for inquiring minds:

  • A standard Flex 4 applet consisting of two states.
  • A label is declared whose text value has one of two values depending on the current state (”Before” or “After”).
  • The magic: We declare an instance of an AnimateTransitionShader effect to serve as the default transition effect. It’s worth noting that we target the parent of the label in this case (”ticker”) because we want the bounds of our ‘before’ and ‘after’ images in our raster transition to be identical - this is a requirement of the AnimateTransitionShader effect. The effect is also initialized with a reference to the ‘HexCells’ Pixel Bender kernel.
  • A timer changes the current state every 2.5s.

That’s really all there is to it. When the state change occurs the effect will grab the input images and feed them to the HexCells shader and then animate the ‘progress‘ attribute of the shader from 0 to 1 as the transition progresses. The result of each animation frame is rendered to the screen by the Flex SDK.

I won’t go into details here about the exact interface we require for the Pixel Bender shader, but I encourage you to examine the example source included as part of this post.

For what it’s worth, the Spark CrossFade and Wipe effects that ship as part of Flex 4 are further examples of Pixel Bender shaders that can be used for simple transitions.

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
<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009"
        xmlns:s="library://ns.adobe.com/flex/spark"
        xmlns:mx="library://ns.adobe.com/flex/mx" width="300" height="150"
        applicationComplete="onAppComplete()" backgroundColor="#000000" >
   
    <fx:Script>
        <![CDATA[
            private var timer:Timer;
            private function onAppComplete():void
            {
                timer = new Timer(2500);
                timer.addEventListener("timer", onTick);
                timer.start();
            }
           
            private function onTick(e:Event):void
            {
                currentState=currentState=='State1'?'State2':'State1';
            }
        ]]>
    </fx:Script>
   
    <s:states>
        <s:State name="State1"/>
        <s:State name="State2"/>
    </s:states>
   
    <s:transitions>
        <s:Transition>
            <s:AnimateTransitionShader target="{ticker}" duration="1100"
                shaderByteCode="@Embed(source='HexCells.pbj',
                mimeType='application/octet-stream')"
/>
        </s:Transition>
    </s:transitions>
   
    <fx:Style>
        @font-face {
            src: url("MyriadWebPro-Bold.ttf");
            fontFamily: "Myriad";
            embedAsCFF: true;
        }
    </fx:Style>

    <s:Group id="ticker" width="300" height="150">
        <s:Label fontFamily="Myriad" text.State1="Before" text.State2="After"
             color="#FFFFFF" textAlign="center" buttonMode="true"  verticalCenter="0"
             fontSize="44" width="100%"/>
    </s:Group>

</s:Application>

The sky is pretty much the limit here. In fact since the effect is just a standard Flex Effect you can also specify interpolation options. Another example (below) uses a simple “twist” shader that you can use to essentially flip between two states of an effect target. In this case I’ve added a custom Bounce easer to the transition.



The source for the twist example follows, the bulk of the sample is identical to the one prior, so I’ve excluded the common code (full source of the examples is included in the archive attached to this post below).

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
...
   <s:transitions>
        <s:Transition>
            <s:AnimateTransitionShader target="{ticker}" duration="1200"
               shaderByteCode="@Embed(source='Twist.pbj',
               mimeType='application/octet-stream')"
>
                <s:easer>
                    <local:MyBounce/>
                </s:easer>
            </s:AnimateTransitionShader>
        </s:Transition>
    </s:transitions>
   
    <s:Group id="ticker" width="100%" height="100%" >
       <!-- Solid fill for our effect target so captured bitmap contains
                bounds of our Group -->
       <s:Rect width="100%" height="100%">
            <s:fill>
                <s:SolidColor color="#FFFFFF"/>
            </s:fill>
        </s:Rect>
        <s:BitmapImage source="@Embed(source='CalcLarge.png')"
            source.State2="@Embed(source='CalcSmall.png')"
            verticalCenter="0" horizontalCenter="0"/>
    </s:Group>
    ...


Download the cookbook code for each of the samples here.

For more information about blend shaders check out this article by the Flex effects guru himself, Chet Haase: Shader Transitions in Flex 4.

And to browse the sources of several Pixel Bender shaders (which you’ll have to port for use as blend shaders) - check out the Pixel Bender Exchange.

12 responses so far