Setting the origin X and Y coordinate for a linear gradient stroke in Flex Gumbo

The following example shows how you can set the origin X and Y coordinate for a linear gradient stroke in Flex Gumbo by setting the x and y properties on the LinearGradientStroke object.

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/2009/01/04/setting-the-origin-x-and-y-coordinate-for-a-linear-gradient-stroke-in-flex-gumbo/ -->
<Application name="LinearGradientStroke_x_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="x:">
                <HSlider id="xSlider"
                        minimum="-100"
                        maximum="100"
                        value="0"
                        snapInterval="1"
                        tickInterval="25"
                        liveDragging="true" />
            </FormItem>
            <FormItem label="y:">
                <HSlider id="ySlider"
                        minimum="-100"
                        maximum="100"
                        value="0"
                        snapInterval="1"
                        tickInterval="25"
                        liveDragging="true" />
            </FormItem>
            <FormItem label="rotation:">
                <HSlider id="rotationSlider"
                        minimum="-360"
                        maximum="360"
                        value="0"
                        snapInterval="1"
                        tickInterval="45"
                        liveDragging="true" />
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <Graphic>
        <Rect id="rect" width="300" height="200">
            <stroke>
                <LinearGradientStroke id="linearGrad"
                        x="{xSlider.value}"
                        y="{ySlider.value}"
                        rotation="{rotationSlider.value}"
                        weight="5">
                    <GradientEntry color="red" />
                    <GradientEntry color="white" />
                    <GradientEntry color="blue" />
                </LinearGradientStroke>
            </stroke>
        </Rect>
    </Graphic>

</Application>

View source is enabled in the following example.

You can also set the LinearGradientStroke object’s x, y, and rotation properties using ActionScript, as seen in the following example:

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2009/01/04/setting-the-origin-x-and-y-coordinate-for-a-linear-gradient-stroke-in-flex-gumbo/ -->
<Application name="LinearGradientStroke_x_test"
        xmlns="http://ns.adobe.com/mxml/2009"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

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

            private function xSlider_change(evt:SliderEvent):void {
                linearGrad.x = evt.value;
            }

            private function ySlider_change(evt:SliderEvent):void {
                linearGrad.y = evt.value;
            }

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

    <ApplicationControlBar dock="true">
        <Form styleName="plain">
            <FormItem label="x:">
                <HSlider id="xSlider"
                        minimum="-100"
                        maximum="100"
                        value="0"
                        snapInterval="1"
                        tickInterval="25"
                        liveDragging="true"
                        change="xSlider_change(event);" />
            </FormItem>
            <FormItem label="y:">
                <HSlider id="ySlider"
                        minimum="-100"
                        maximum="100"
                        value="0"
                        snapInterval="1"
                        tickInterval="25"
                        liveDragging="true"
                        change="ySlider_change(event);" />
            </FormItem>
            <FormItem label="rotation:">
                <HSlider id="rotationSlider"
                        minimum="-360"
                        maximum="360"
                        value="0"
                        snapInterval="1"
                        tickInterval="45"
                        liveDragging="true"
                        change="rotationSlider_change(event);" />
            </FormItem>
        </Form>
    </ApplicationControlBar>

    <Graphic>
        <Rect id="rect" width="300" height="200">
            <stroke>
                <LinearGradientStroke id="linearGrad"
                        x="{xSlider.value}"
                        y="{ySlider.value}"
                        rotation="{rotationSlider.value}"
                        weight="5">
                    <GradientEntry color="red" />
                    <GradientEntry color="white" />
                    <GradientEntry color="blue" />
                </LinearGradientStroke>
            </stroke>
        </Rect>
    </Graphic>

</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.