Setting the focal point on a Rect object in Flex Gumbo

The following example shows how you can set the focal point on a Flex Gumbo Rect object by setting the focalPointRatio property to a value between -1 and +1.

Full code after the jump.

To use the following code, you must have Flash Player 10 and a Flex Gumbo SDK installed in your Flex Builder 3. For more information on downloading and installing the Gumbo SDK into Flex Builder 3, see “Using the beta Gumbo SDK in Flex Builder 3”.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/25/setting-the-focal-point-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_fill_RadialGradient_focalPointRatio_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="focalPointRatio:">
                <HSlider id="slider1"
                        minimum="-1.0"
                        maximum="1.0"
                        value="0.0"
                        snapInterval="0.1"
                        tickInterval="0.1"
                        liveDragging="true" />
            </FormItem>
            <FormItem label="rotation:">
                <HSlider id="slider2"
                        minimum="-360"
                        maximum="360"
                        value="0"
                        snapInterval="1"
                        tickInterval="45"
                        liveDragging="true" />
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <Graphic>
        <Rect id="rect" width="300" height="200">
            <fill>
                <RadialGradient id="radialGradient"
                        focalPointRatio="{slider1.value}"
                        rotation="{slider2.value}">
                    <entries>
                        <GradientEntry color="red" />
                        <GradientEntry color="white" />
                        <GradientEntry color="blue" />
                    </entries>
                </RadialGradient>
            </fill>
        </Rect>
    </Graphic>

</Application>

View source is enabled in the following example.

You can also set the focalPointRatio property using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/25/setting-the-focal-point-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_fill_RadialGradient_focalPointRatio_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <Script>
        <![CDATA[
            import mx.events.SliderEvent;

            private function slider1_change(evt:SliderEvent):void {
                radialGradient.focalPointRatio = evt.value;
            }

            private function slider2_change(evt:SliderEvent):void {
                radialGradient.rotation = evt.value;
            }
        ]]>
    </Script>

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="focalPointRatio:">
                <HSlider id="slider1"
                        minimum="-1.0"
                        maximum="1.0"
                        value="0.0"
                        snapInterval="0.1"
                        tickInterval="0.1"
                        liveDragging="true"
                        change="slider1_change(event);" />
            </FormItem>
            <FormItem label="rotation:">
                <HSlider id="slider2"
                        minimum="-360"
                        maximum="360"
                        value="0"
                        snapInterval="1"
                        tickInterval="45"
                        liveDragging="true"
                        change="slider2_change(event);" />
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <Graphic>
        <Rect id="rect" width="300" height="200">
            <fill>
                <RadialGradient id="radialGradient">
                    <entries>
                        <GradientEntry color="red" />
                        <GradientEntry color="white" />
                        <GradientEntry color="blue" />
                    </entries>
                </RadialGradient>
            </fill>
        </Rect>
    </Graphic>

</Application>

Due to popular demand, here is the “same” example in a more ActionScript friendly format:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/12/25/setting-the-focal-point-on-a-rect-object-in-flex-gumbo/ -->
<Application name="Rect_fill_RadialGradient_focalPointRatio_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white"
        initialize="init();">

    <Script>
        <![CDATA[
            import mx.containers.ApplicationControlBar;
            import mx.containers.Form;
            import mx.containers.FormItem;
            import mx.controls.HSlider;
            import mx.events.SliderEvent;
            import mx.graphics.GradientEntry;
            import mx.graphics.Graphic;
            import mx.graphics.RadialGradient;
            import mx.graphics.Rect;

            private var slider1:HSlider;
            private var slider2:HSlider;
            private var rect:Rect;
            private var radialGradient:RadialGradient;

            private function init():void {
                slider1 = new HSlider();
                slider1.minimum = -1.0;
                slider1.maximum = 1.0;
                slider1.value = 0.0;
                slider1.snapInterval = 0.1;
                slider1.tickInterval = 0.1;
                slider1.liveDragging = true;
                slider1.addEventListener(SliderEvent.CHANGE, slider1_change);

                slider2 = new HSlider();
                slider2.minimum = -360;
                slider2.maximum = 360;
                slider2.value = 0;
                slider2.snapInterval = 1;
                slider2.tickInterval = 45;
                slider2.liveDragging = true;
                slider2.addEventListener(SliderEvent.CHANGE, slider2_change);

                var formItem1:FormItem = new FormItem();
                formItem1.label = "focalPointRatio:";
                formItem1.addChild(slider1);

                var formItem2:FormItem = new FormItem();
                formItem2.label = "rotation:";
                formItem2.addChild(slider2);

                var form:Form = new Form();
                form.styleName = "plain";
                form.addChild(formItem1);
                form.addChild(formItem2);

                var appControlBar:ApplicationControlBar = new ApplicationControlBar();
                appControlBar.dock = true;
                appControlBar.addChild(form);
                addChildAt(appControlBar, 0);

                var entryArr:Array = [];
                entryArr.push(new GradientEntry(0xFF0000)); // red
                entryArr.push(new GradientEntry(0xFFFFFF)); // white
                entryArr.push(new GradientEntry(0x0000FF)); // blue

                radialGradient = new RadialGradient();
                radialGradient.entries = entryArr;

                rect = new Rect();
                rect.fill = radialGradient;
                rect.width = 300;
                rect.height = 200;

                var graphic:Graphic = new Graphic();
                graphic.addElement(rect);
                addChild(graphic);
            }

            private function slider1_change(evt:SliderEvent):void {
                radialGradient.focalPointRatio = evt.value;
            }

            private function slider2_change(evt:SliderEvent):void {
                radialGradient.rotation = evt.value;
            }
        ]]>
    </Script>

</Application>

This entry is based on a beta version of the Flex Gumbo SDK and therefore is very likely to change as development of the Flex SDK continues. The API can (and will) change causing examples to possibly not compile in newer versions of the Flex Gumbo SDK.