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.

<?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 name="Box_clipContent_test"
        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.

10 thoughts on “Clipping a Flex container’s content using the clipContent property

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

  3. There is workaround for this bug, prior to Flex 4.0. Simply don’t use negative x or y coordinates
    in child components of Container. Clipping works when negative coordinates are applied to child
    itself.

  4. If there is a container1 and inside that there is a container2 and inside that there is one more container3 then if clipContent of container2 is true and container1 is false then container3 moves out of container2 as well as container1(despite clipContent of container1 being 0).
    Is this a bug?

Comments are closed.