Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Image can not rotate properly in LayoutTransformControl #18245

Open
yll690 opened this issue Feb 19, 2025 · 7 comments
Open

Image can not rotate properly in LayoutTransformControl #18245

yll690 opened this issue Feb 19, 2025 · 7 comments
Labels

Comments

@yll690
Copy link
Contributor

yll690 commented Feb 19, 2025

Describe the bug

Put an Image control in LayoutTransformControl and set LayoutTransform to rotate 90 degree, the Image will show at a weird position.
Image

To Reproduce

Create a new window and input these xaml.

  <Border BorderThickness="1" Width="100" Height="100" BorderBrush="Black">
    <LayoutTransformControl>
        <Image>
          <DrawingImage>
            <DrawingImage.Drawing>
              <GeometryDrawing Brush="Red" Geometry="M0,0 h10 v100 h-10 z" />
            </DrawingImage.Drawing>
          </DrawingImage>
        </Image>
      <LayoutTransformControl.LayoutTransform>
        <RotateTransform Angle="90" />
      </LayoutTransformControl.LayoutTransform>
    </LayoutTransformControl>
  </Border>

Expected behavior

The Image should be place in the vertical and horizontal center of the Border.

Avalonia version

11.2.3

OS

Windows

Additional context

Adjusting the Stretch and HorizontalAlignment affects Image's position.
Putting a Panel or other control between LayoutTransformControl and Image can solve the problem.

  <Border BorderThickness="1" Width="100" Height="100" BorderBrush="Black">
    <LayoutTransformControl>
      <Panel>
        <Image>
          <DrawingImage>
            <DrawingImage.Drawing>
              <GeometryDrawing Brush="Red" Geometry="M0,0 h10 v100 h-10 z" />
            </DrawingImage.Drawing>
          </DrawingImage>
        </Image>
      </Panel>
      <LayoutTransformControl.LayoutTransform>
        <RotateTransform Angle="90" />
      </LayoutTransformControl.LayoutTransform>
    </LayoutTransformControl>
  </Border>

Image

@yll690 yll690 added the bug label Feb 19, 2025
@maxkatz6
Copy link
Member

Was Transform Origin previously set?
Looks like it was transformed relatively to the top-left point (and for 45deg case it was moved bottom back into the boundaries).
Setting transform origin to center might solve the issue.

@yll690
Copy link
Contributor Author

yll690 commented Feb 19, 2025

Hi, It's a simple demo, I didn't set other properties.
Setting RenderTransformOrigin does have effect. But it is also abnormal. I cann't find the pattern in several attempts.

Image

Image

Image

@yll690
Copy link
Contributor Author

yll690 commented Feb 19, 2025

If I set HorizontalAlignment="Left" it will become this:
Image

@timunie
Copy link
Contributor

timunie commented Feb 19, 2025

LayoutTransform has an Origin property which you may want to set to "50%, 50%" (I think this should be acceptable syntax, havent tested it yet)

@yll690
Copy link
Contributor Author

yll690 commented Feb 20, 2025

Unfortunately there is no Origin property in LayoutTransformControl and setting LayoutTransformControl's RenderTransformOrigin does not help. Although RotateTransform has CenterX and CenterY properties, they don't accept relative point. Setting them also makes no difference.
Anyhow, I think layout transform shouldn't calculate origin, which is a main difference between layout transform and render transform.

@timunie
Copy link
Contributor

timunie commented Feb 20, 2025

<StackPanel>
	<Slider x:Name="Rotate" Minimum="-360" Maximum="360"></Slider>
	<Border BorderThickness="1"  Width="100" Height="100" BorderBrush="Gray">
		<LayoutTransformControl HorizontalAlignment="Center">
			<Image>
				<DrawingImage>
					<DrawingImage.Drawing>
						<GeometryDrawing Brush="Red" Geometry="M0,0 h10 v100 h-10 z" />
					</DrawingImage.Drawing>
				</DrawingImage>
			</Image>
			<LayoutTransformControl.LayoutTransform>
				<RotateTransform Angle="{Binding #Rotate.Value}" />
			</LayoutTransformControl.LayoutTransform>
		</LayoutTransformControl>
	</Border>
</StackPanel>

Result:

Image

looks okay to me.

@timunie
Copy link
Contributor

timunie commented Feb 20, 2025

Ah I see ONLY 90° is wrong, so should be a Math issue.

Tip

You may clone Avalonia and add your sample to src -> Samples -> Sandbox. From there you could debug the Math calculations. Could be a divide by zero or something.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants