views:

161

answers:

2

Hello everyone,

I am using Silverlight 3.0 + .Net 3.5 + VSTS 2008 + C# to develop silverlight application based on ASP.Net. I am very confused about what are the function of "TranslateTransform" and "RenderTransformOrigin" in the following code snippet?

BTW: I roughly understand RenderTransformOrigin means move an UI element in x-axis and y-axis by some offset, is that correct? Move the whole UI element?

        <Grid Margin="-1,0,100,0" x:Name="controlsContainer" Height="35" RenderTransformOrigin="0.5,0.5" VerticalAlignment="Bottom">
            <Grid.RenderTransform>
                <TransformGroup>
                    <ScaleTransform/>
                    <SkewTransform/>
                    <RotateTransform/>
                    <TranslateTransform Y="0"/>
                </TransformGroup>
            </Grid.RenderTransform>
            <Rectangle Margin="0,0,0,0" Height="35" VerticalAlignment="Top" Fill="#97000000" Stroke="#00000000" RenderTransformOrigin="0.5,0.5"/>
            <VideoPlayer:mediaControl Height="35" Margin="1,0,0,0" HorizontalAlignment="Stretch" VerticalAlignment="Top" x:Name="mediaControls" Visibility="Visible"/>
        </Grid>

thanks in advance, George

+1  A: 

The RenderTransformOrigin is most relevant with the RotateTransform - it determines the center of rotation.

The TranslateTransform will move the grid a certain number of pixels in the X/Y axis. In this case, it will not do anything because the Y is 0 and the X is a default 0. In this case, my guess is that an animation or animation in a visual state will modify the TranslateTransform.Y and what you're seeing is the initial value of Y=0.

Michael S. Scherotter
Thanks! 1. for my code, element controlsContainer has RenderTransformOrigin value of (0.5, 0.5), does it mean this element will be moved 50% right and 50% down? what is the function of TranslateTransform Y="0" here? 2. for the Rectangle element, RenderTransformOrigin has value (0.5, 0.5), does it mean this element will be moved 50% right and 50% down?
George2
the RenderTransformOrigin does not affect the TranslateTransform - it specifies the origin for rotation. 0.5,0.5 means that a 45 degree rotatation will rotate from the center of the object.
Michael S. Scherotter
Do you mean RenderTransformOrigin does not affect the TranslateTransform? I made some learning and disagree that RenderTransformOrigin makes no impacts on TranslateTransform, here a reference tutorial and please refer to the following section "3. Resizable element and RenderTransformOrigin.", any comments about how it functions when TranslateTransform and RenderTransformOrigin are used together? http://vbcity.com/blogs/canoz/archive/2010/05/02/beginning-silverlight-translatetransform.aspx
George2
The RenderTransformOrigin also determines the scale origin for the ScaleTransform.
Michael S. Scherotter
1. I am also learning from here, especially the following section "3. Resizable element and RenderTransformOrigin.", my confusion is how TranslateTransform and RenderTransformOrigin are used together? Any comments?From your reply, 1. Do you mean rendertransformorigin is always useless for TranslateTransform even if for resizable element, and it is X and Y property of TranslateTransform which impacts the result? 2. But for resizeble element, even if we set X and Y property, the element may be moved to offset other than X and Y value because of the elements' resizable property?
George2
+1  A: 

Your understanding of RenderTransformOrigin is not quite correct.

RenderTransformOrigin accepts a point that defines the origin or center point around which RenderTransforms such as RotateTransform is applied.

The interesting thing with the point defined by RenderTransformOrigin is that it is not in absolute coordinates. What does that mean? Well lets say you have an element that is 100 x 100 pixels in size if you wanted to apply your transformations around the center point, for example rotate 45 degrees around the center, you would not use the absolute coordinates of 50, 50 but rather 0.5, 0.5 which basically says the origin of the element is 0.5 or half way across and 0.5 down.

While typical values for the origin would be between 0 and 1, where 0 in the X axis is the left of the element and 1 the right, and 0 in the Y axis is the top of the element and 1 is the bottom of the element, you can also set the origin to a point outside the element. Like 1.5, 0.5 which would set the origin hlaf way down the element and half the element width outside to the right of the element.

TranslateTransform, does a translation or a move of the element, if you translate an element 45,60 what this means is move the element 45 units to the right and 60 down.

Here are a few examples you can play with, that might help make what I said clearer.

    <!-- Rectangle rotated around the left top corner -->
    <Rectangle Width="100" Height="100" Fill="Red" RenderTransformOrigin="0,0">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="45" />
        </Rectangle.RenderTransform>
    </Rectangle>

    <!-- Rectangle rotated around the center point -->
    <Rectangle Width="100" Height="100" Fill="Blue" RenderTransformOrigin="0.5,0.5">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="45" />
        </Rectangle.RenderTransform>
    </Rectangle>

    <!-- Rectangle rotated around the bottom right corner -->
    <Rectangle Width="100" Height="100" Fill="Yellow" RenderTransformOrigin="1,1">
        <Rectangle.RenderTransform>
            <RotateTransform Angle="45" />
        </Rectangle.RenderTransform>
    </Rectangle>

    <!-- Rectangle translated 50 units to the right 70 units up, the origin does not matter here-->
    <Rectangle Width="100" Height="100" Fill="Green" RenderTransformOrigin="0,0">
        <Rectangle.RenderTransform>
            <TranslateTransform X="50" Y="-70" />
        </Rectangle.RenderTransform>
    </Rectangle>
Chris Taylor
Thank! 1. for my code, element controlsContainer has RenderTransformOrigin value of (0.5, 0.5), does it mean this element will be moved 50% right and 50% down? what is the function of TranslateTransform Y="0" here?2. for the Rectangle element, RenderTransformOrigin has value (0.5, 0.5), does it mean this element will be moved 50% right and 50% down?
George2
@George2, no the TranslateTransform is not affected by the RenderTransformOrigin. See my comment on the last example "the origin does not matter here"
Chris Taylor
1. So, in my sample code, I can remove RenderTransformOrigin for element controlsContainer and also remove RenderTransformOrigin for the Rectangle element without any impact? 2. What does TranslateTransform Y="0" mean?
George2
@George2, yes if you remove the RenderTransformOrigin from your current example it should make no difference. <TranslateTransform Y="0" /> means move 0 units in the Y direction, effectively this will do nothing.
Chris Taylor
Hi Chris, I made some learning and disagree that RenderTransformOrigin makes no differences, here a reference tutorial and please refer to the following section "3. Resizable element and RenderTransformOrigin.", any comments about how it functions when TranslateTransform and RenderTransformOrigin are used together?http://vbcity.com/blogs/canoz/archive/2010/05/02/beginning-silverlight-translatetransform.aspx
George2
@George2, I think that post is missleading. I urge you to use the samples I provides and play around with it yourself, you will see that RenderTransformOrigin has no impact on the TranslateTransform. Now the ScaleTransform (Resizing) that is a different thing. Here is a post where a MS employee says "One thing is that RenderTransformOrigin doesn't affect TranslateTransform at all."http://forums.silverlight.net/forums/t/12543.aspx
Chris Taylor
So, you mean the document I am referring to is wrong? :-)
George2
@George2, no not wrong it just does not seem very clear what is being refered to. It talks about resizable element and rendertransformorigin, but the sample does not demonstrate this. What I expect the point is, is that the RenderTransformOrigin line 0.5, 0.5 will be the center of an element at any size, but that will only impact rotations and the like and NOT the translation. Like I said, if you try out the samples you will see that the RenderTransformOrigin does not affect the translation.
Chris Taylor
1. Do you mean rendertransformorigin is always useless for TranslateTransform even if for resizable element, and it is X and Y property of TranslateTransform which impacts the result? 2. But for resizeble element, even if we set X and Y property, the element may be moved to offset other than X and Y value because of the elements' resizable property?
George2
Thanks question answered!
George2