How to dynamically change the size of the Canvas? - apache-flex

If the size of canvas set in the MXML markup, some part of the components that go beyond the boundaries of canvas not displayed. If the canvas size to change dynamically(canvas.width, canvas.height), canvas boundaries are virtually absent.
How to dynamically change the size of the Canvas?

Do you really need a canvas? If you need sizes changed dinamically try to put your content inside a VBox for example ( of any relative container ) and use minWidth, minHeight to set it up, otherwise, Canvas is a component so is whould have width and height properties.
e.g.
<Canvas id="myCanvas" width="400" height="300">
<!-- content here -- >
</Canvas>

override the canvas's updateDisplayList function.
override protected function updateDisplayList(w:Number,h:Number):void {
super.updateDisplayList(w,h);
this.setActualSize((child1.width+child2.width), Math.max(child1.height,child2.height));
}
Alternatives are to override the canvas measuredHeight/measuredWidth getters. Or override the "measure" function.
If scrolling is what you want, you have horizontalSCrollPolicy/verticalScrollPolicy properties of the canvas.
Lots of different solutions depending on your exact problem.

Related

Increase the height of Flex Container dynamically and introduce scrollbar on the browser

I am trying to increase the height of container with increase in the number of contents inside the container.
Like in my case i m using tileList inside tabNavigator , when I put contents inside the tileList, the height of tileList does not increase
beyond vertical height of the viewport. It puts scrollbar on the container. I want to increase the height of an flex container with increase in the contents and introduce
scrollbar on the browser with increase in contents in the flex container.
Could anybody please suggest me how I could achieve this.
Thanks in advance.
You need to write a javascript function which increases the height of embed object and you need to call this javascript method from flex while adding a new item to tile list.
This link explains how to access javascript from flex.
Have considered using javascript and resize the element directly in the source HTML?
The JS code could look something like that:
function changeSize(id) {
var flex = document.getElementById(id);
flex.setAttribute("width", "800");
flex.setAttribute("height", "600");
}

Flex: Why does setting scaleX/Y in mxml effect the components size but setting it in actionscript does not?

I'm playing around with the scaleX/Y in the canvas tag and have noticed some strange behaviour. When I set scale in in mxml the width and height of the canvas are adjusted accordingly. For example if I have a canvas like this:
<mx:Canvas width="1000" height="1000" scaleX="0.1" scaleY="0.1" />
The canvas now appears on screen to have a width and height of 100 and if inside my creationComplete callback I check the width and height property they are indeed 100.
But if I do exactly the same thing except I set the scaleX/Y property from actionscript the canvas on screen appears to have a width and height of 100 as expected, but when I check the width and height property of the canvas they are still at the previous values of 1000.
Could anyone help me understand what is going on and also tell me if there is any method that will refresh the width and height values so that they are correct?
Thanks,
Chris
At what point in the Canvas' lifecycle are you adjusting the scaleX/Y properties in Actionscript? And then, at what point are you re-checking them?
It may be helpful to examine this white paper on Flex component lifecycles (PDF) to pick the right timing for actionscript-based visual component tweaking.

In Flex, how can I get the dimensions of a childless canvas component at runtime?

One of my components looks like this:
<mx:Canvas id="grid" width="100%" height="100%"></mx:Canvas>
On creationComplete, I load some spirte that I want to scale and position based on the dimensions of the canvas to create a custom grid layout, but when I access the dimensions of 'grid' I get 0 and 0. Is there any way to get the dimensions without assigning absolute values?
I can't check this at the moment but can you access the measuredHeight and measuredWidth?
My understanding is that unless the Canvas contains one or more DisplayObject children, it will always report its width and height properties as 0, regardless of the percentage sizings you may have applied to it.
You could always add an empty dummy DisplayObject to the Canvas, but that wouldn't be very elegant. Depending on how you've planned to implement your custom grid, it's possible you'll have to rethink the design...
Is it possibly created but not yet displayed (e.g. visible)? Since final size and shape is derived, it doesn't happen until the thing actually needs to be drawn. Width and height are documented to be the values actually in use - there are even events for when they change.
Worst case, try trapping it out in the canvas Resize event.
have you tried binding the width and height of the Canvas to its parent? If it is 100%, than it will have the same size as its parent and you could either bind or size your Sprite (UIComponent) based on the parent container of the Canvas.

Multi line text in Flex doesnt recalculate when runtime css is changed

For loading time considerations I am using a runtime css file in my Flex Application.
I am having a problem with a multi line text control :
<mx:Text id="txtDescription" selectable="false"
styleName="imageRolloverButtonTextDark" width="100%" textAlign="center"
text="{_rolloverText}"/>
When my CSS stylesheet has loaded the text style correctly changes, but the height is not recalculated. It appears to be just a single line field.
FYI: The control is not actually visible, and triggered by a rollover. So I dont really care if the stylesheet hasnt loaded and they get standard system text. I jsut want it to be the correct height when it has been loaded.
Per the Adobe documentation for Text
Sizing a Text control
Flex sizes the
Text control as follows:
If you specify a pixel value for both
the height and width properties, any
text that exceeds the size of the
control is clipped at the border.
If you specify an explicit pixel
width, but no height, Flex wraps the
text to fit the width and calculates
the height to fit the required number
of lines.
If you specify a percentage-based
width and no height, Flex does not
wrap the text, and the height equals
the number of lines as determined by
the number of Return characters.
If you specify only a height and no
width, the height value does not
affect the width calculation, and Flex
sizes the control to fit the width of
the maximum line.
As a general rule, if you have long
text, you should specify a pixel-based
width property. If the text might
change and you want to ensure that the
Text control always takes up the same
space in your application, set
explicit height and width properties
that fit the largest expected text.
So the trick I've used to deal with this is to have the Text get its width via a binding expression from whatever container limits it's width, typically the immediate parent.
e.g.
<mx:Canvas id="box" width="100%" backgroundColor="Red">
<mx:Text width="{box.width}" text="{someReallyLongString}" />
</mx:Canvas>
For any one else who has this problem, The solution I found was to create a custom component extending the mx.controls.Text
and then override the styleChange() Method, and explicitly call the invalidateDisplayList() method for the text field once the style has been applied.
It should be called automatically when the styleis changed but no...for some reason in flex 3.5 it is not.
public class TextObject extends Text { //...
override public function styleChanged(styleProp:String):void { invalidateDisplayList(); }
}
Hope that save some one all the time I lost on it.
Could you use a fixed pixel width instead of 100%? I've had issues with 100% being wrongly calculated on dynamic text controls before.

How do I visually "break out" of a Container in Flex?

Here's my problem - I have some code like this:
<mx:Canvas width="300" height="300">
<mx:Button x="800" />
</mx:Canvas>
So the problem is that the Button inside the canvas has an x property way in excess of the Canvas's width - since it's a child of the Canvas, the Canvas masks it and creates some scrollbars for me to scroll over to the button.
What I'd like is to display the button - 800 pixels to the left of the Canvas without the scrollbars while still leaving the button as a child of the Canvas. How do I do that?
I figured it out - apparently the Container has a property called clipContent - here's the description from Adobe:
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. For additional control over the appearance of scrollbars, see horizontalScrollPolicy and verticalScrollPolicy.
The default value is true.
So basically - to show the button outside of the bounds of the container I need to do the following:
<mx:Canvas width="300" height="300" clipContent="false" >
<mx:Button x="800" />
</mx:Canvas>
That was easier than I thought it was going to be. :)
Here's the official doc...
You should be able to use the includeInLayout property also, which would allow you to apply it to each child component independently.

Resources