02
Feb
08

Clipping a Flex container’s content using the clipContent property

The following example shows how you can clip a container’s content in Flex by setting the clipContent property.
According to the Flex 3 documentation for the Container class’s clipContent property:

Whether to apply a clip mask if the positions and/or sizes of this container’s children extend outside the borders of this container. If false, the children of this container remain visible when they are moved or sized outside the borders of this container. If true, the children of this container are clipped.
If clipContent is false, then scrolling is disabled for this container and scrollbars will not appear. If clipContent is true, then scrollbars will usually appear when the container’s children extend outside the border of the container.
The default value is true.

Full code after the jump.

View MXML

<?xml version="1.0" encoding="utf-8"?>
<!-- http://blog.flexexamples.com/2008/02/02/clipping-a-flex-containers-content-using-the-clipcontent-property/ -->
<mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
        layout="vertical"
        verticalAlign="middle"
        backgroundColor="white">

    <mx:ApplicationControlBar dock="true">
        <mx:CheckBox id="checkBox"
                label="clipContent:"
                labelPlacement="left"
                selected="false" />
    </mx:ApplicationControlBar>

    <mx:Box id="box"
            clipContent="{checkBox.selected}"
            width="320"
            height="240">
        <mx:Image id="img"
                source="http://www.helpexamples.com/flash/images/image1.jpg" />
    </mx:Box>

    <mx:Label text="{img.width} x {img.height}" />

</mx:Application>

View source is enabled in the following example.

As you can see, the Box container is set to 320 pixels wide by 240 pixels high. The image being loaded is 400 pixels high by 267 pixels wide. When the clipContent property is set to false, the image exceeds the bounds of the Box container causing it to overlap the Label control. If you set the clipContent property to true, the image doesn’t exceed the bounds of the Box container and horizontal and vertical scroll bars appear, allowing you to scroll to see the rest of the content.


4 Responses to “Clipping a Flex container's content using the clipContent property”


  1. 1 Sean Feb 4th, 2008 at 7:10 pm

    Flex has a bug that it does not clip properly the -x and -y coords ( often not clipping at all @ negative values .)

    I posted a bug on the Flex Bug system but it has yet to be resolved.

    I hope they fix it soon.

    Sean - HeliHobby.com

  2. 2 peterd Feb 4th, 2008 at 11:21 pm

    Sean,

    Do you know the bug numbers?

    Peter

  3. 3 FlexFlip May 14th, 2008 at 4:52 am

    Is it possible to clip with rounded corners?

  4. 4 Kasey Drexler Sep 9th, 2008 at 6:33 pm

    You can clip with rounded corners. Just set a canvas with rounded corners up with a mask of what ever you are trying to clip with rounded corners. For instance:

    image.mask = canvas_with_rounded_corners;

    -Peace

Leave a Reply

This blog is terrible at eating HTML tags. If you plan on posting code/XML, please escape your "<" characters as "&lt;" and your ">" characters as "&gt;".