There are a few rectangles inside a canvas. How can I get the coordinates of the rectangles wrt canvas using the Name of rectangles in C# ?
You can do this using TransformToVisual. There's a good example here: Visual.TransformToVisual MSDN
From the page for Posterity:
<StackPanel Name="myStackPanel" Margin="8">
<TextBlock Name="myTextBlock" Margin="4" Text="Hello, world" />
</StackPanel>
// Return the general transform for the specified visual object.
GeneralTransform generalTransform1 = myStackPanel.TransformToVisual(myTextBlock);
// Retrieve the point value relative to the child.
Point currentPoint = generalTransform1.Transform(new Point(0, 0));
Related
I'm trying to add a 3D effect on a plane in a React site. But I haven't been able to create a plane that fills the camera, like in all the Three.js shader or post-processing examples.
I have tried using an Orthographic Camera + plane, based on this answer, like so:
<Canvas>
<OrthographicCamera left={-1} right={1} top={1} bottom={-1} near={0} far={1} />
<planeGeometry args={[2, 2]} />
</OrthographicCamera>
</Canvas>
But this results in a small square in the middle of the canvas.
I also tried using a ScreenQuad, like so:
<Canvas style={{width: '100vw', height: '100vh'}}>
<ScreenQuad />
</Canvas>
But this results in a triangle in the middle of the canvas.
Here is a repo reproducing both examples: https://github.com/Cecile-Lebleu/gatsby-r3f-bug
I can only cover the Canvas by blowing up the size of the plane, but it's not a good solution: the effect looks giant on small screens and still cropped on larger screens.
What's going on? How can I make a simple plane that covers the camera regardless of Canvas size?
In case anyone ever runs into this, the solution thanks to #0xca0a:
Set the camera to default: <OrthographicCamera makeDefault>
And scale the mesh to fit the viewport.
const viewport = useThree(state => state.viewport)
return (
<mesh scale={[viewport.width, viewport.height, 1]}>
<planeGeometry />
</mesh>
Add in useAspect for responsive images:
function Scene() {
const size = useAspect(1800, 1000)
return (
<mesh scale={size}>
)}
The first two args are the width and height of the image, it then calculates a viewport big enough to cover, and it's responsive.
Or just use drei/image.
I need to use an AbsoluteLayout for other controls on the page not listed here. How do I layout a common senerio where I have a SearchBar at the top, then a ListView which fills the rest of the screen.
I tried this, but the ListView goes incorrectly under the SearchBar
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<SearchBar></SearchBar>
<ListView
AbsoluteLayout.LayoutFlags="All"
AbsoluteLayout.LayoutBounds="0,0,1,1">
</ListView>
</AbsoluteLayout>
Part of the problem is AbsoluteLayoutFlags you have set on the listview.
when you set it to all, you are telling the layout to start at 0,0 and go all the way to 1,1. Which is why the listview is appearing on the searchbar.
<AbsoluteLayout VerticalOptions="FillAndExpand" HorizontalOptions="FillAndExpand">
<SearchBar AbsoluteLayout.LayoutBounds="0,0,1,40"
AbsoluteLayout.LayoutFlags="WidthProportional,PositionProportional"/>
<ListView
AbsoluteLayout.LayoutFlags="XProportional,SizeProportional"
AbsoluteLayout.LayoutBounds="0,40,1,1">
<ListView.ItemsSource>
<x:Array Type="{x:Type x:String}">
<x:String>mono</x:String>
<x:String>monodroid</x:String>
<x:String>monotouch</x:String>
<x:String>monorail</x:String>
<x:String>monodevelop</x:String>
<x:String>monotone</x:String>
<x:String>monopoly</x:String>
<x:String>monomodal</x:String>
<x:String>mononucleosis</x:String>
</x:Array>
</ListView.ItemsSource>
</ListView>
</AbsoluteLayout>
Xamarin Documentation
I am sure you were referencing the documentation. I linked it here and quoting part of it. Hopefully it helps.
Proportional values define a relationship between a layout and a view.
This relationship defines a child view's position or scale value as a
proportion of the corresponding value of the parent layout. These
values are expressed as doubles with values between 0 and 1.
Proportional values are used to position and size views within the
layout. So, when a view's width is set as a proportion, the resultant
width value is the proportion multiplied by the AbsoluteLayout's
width. For example, with an AbsoluteLayout of width 500 and a view set
to have a proportional width of .5, the rendered width of the view
will be 250 (500 x .5).
I'm adding a drop shadow to a Xamarin.UWP project (but the question is not really Xamarin-specific but UWP in general):
bool IsShadowSupported => ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 3); // SDK >= 14393
if (IsShadowSupported) {
var compositor = ElementCompositionPreview.GetElementVisual(Control).Compositor;
dropShadow = compositor.CreateDropShadow();
if (Control is Windows.UI.Xaml.Controls.TextBlock textBlock)
dropShadow.Mask = textBlock.GetAlphaMask();
shadowVisual = compositor.CreateSpriteVisual();
shadowVisual.Shadow = dropShadow;
ElementCompositionPreview.SetElementChildVisual(Control, shadowVisual);
...
dropShadow.Offset = new Vector3((float)Shadow.GetDistanceX(Element), (float)Shadow.GetDistanceY(Element), -5f);
}
It runs and the shadow appears—but above the text, not beneath it. At first I thought this would be determined by the Z coordinate of the offset, but no negative, positive or zero value there changes anything. The shadow looks like this:
Which is not a bad effect on its own but it's not what was requested: white text and a dark grey shadow beneath it.
The problem is that the SetElementChildVisual sets the visual as the last child of the given element, which will make the shadow appear above the TextBlock. Unfortunately not even the parent of the TextBlock is enough, you should instead have a adjacent element that will host the shadow:
<Grid x:Name="ShadowHost" />
<TextBlock x:Name="Hello" Text="Hello" />
Now use ShadowHost instead of Control in your code except for the GetAlphaMask call where you should use the TextBlock instead.
Of course this is quite some work to make shadows work, which is why you can try to use the Windows Community Toolkit's DropShadowPanel instead - see documentation for more info.
I have InkCanvas inside a zoomable ScrollViewer:
<ScrollViewer x:Name="ScrollViewer" ZoomMode="Enabled">
<Border Height="5000" Width="5000" BorderBrush="Black" BorderThickness="1">
<InkCanvas x:Name="inkCanvas" />
</Border>
</ScrollViewer>
I want to position ruler to the top left corner.
Ruler is positioned on the InkCanvas and state of ScrollViewer is defined by by HorizontalOffset, VerticalOffset and ZoomFactor
I've found this code (sample)
void OnBringIntoView(e)
{
// Set Ruler Origin to Scrollviewer Viewport origin.
// The purpose of this behavior is to allow the user to "grab" the
// ruler and bring it into view no matter where the scrollviewer viewport
// happens to be. Note that this is accomplished by a simple translation
// that adjusts to the zoom factor. The additional ZoomFactor term is to
// make ensure the scale of the InkPresenterRuler is invariant to Zoom.
Matrix3x2 viewportTransform =
Matrix3x2.CreateScale(ScrollViewer.ZoomFactor) *
Matrix3x2.CreateTranslation(
ScrollViewer.HorizontalOffset,
ScrollViewer.VerticalOffset) *
Matrix3x2.CreateScale(1.0f / ScrollViewer.ZoomFactor);
ruler.Transform = viewportTransform;
}
in short:
viewport = Scale(zoom) * Translate(offset) * Scale(1/zoom)
This works, but I'm a bit lost.
What does the first scale do and what does the second? Why can't I use juct TranslateTransform?
Why can't I use juct TranslateTransform?
Because the InkPresenterRuler.Transform property is designed to be Matrix3x2.
What does the first scale do and what does the second?
Scale(zoom)*Scale(1/zoom) aiming at letting the ruler be invariant to Zoom. No matter what your current zoom level is, the ruler will return to it's original size.
Translate(offset) changes the translation values(offsetX and offsetY) of the transform Matrix.
For details of the transformation theory you can refer to:
Remarks of Matrix Transform
Affine transformations section of Transformation matrix Wiki.
I need to get width/height of displayed image in imegView and compare it to orginal image size which is in imageView.getImage().getWidth()/getHeight() and listen changes if user resize it form application GUI.
I get this size from imageView.fitWidthProperty() and similar for height, but I get size of imageView not image within it:
I get size of blue which is imageView but I need size of displayed image (green). How can I get it as property to listen when user resize window app (green also change size but it seems to have max and min size so it stop resize with imageView when it is max or min).
I use this property to compute % of size of orginal image in bottom right below blue rectangle.
Is it possible?
E: Below is fxml of this tab:
<BorderPane fx:id="root" fx:controller="..."
xmlns:fx="..." xmlns="..." stylesheets="#...css">
<center>
<StackPane>
<ImageView fx:id="..."/>
<fx:include source="...fxml" fx:id="..."/>
</StackPane>
</center>
<bottom>
<VBox fx:id="..." minHeight="110" styleClass="small-padding" spacing="5">
<HBox alignment="CENTER_RIGHT" spacing="5">
<fx:include source="...fxml" resources="..."
fx:id="..."/>
</HBox>
<TextArea fx:id="..." promptText="%..." styleClass="-fx-description-text-area"/>
</VBox>
</bottom>
When the ImageView is set to preserve the aspect ratio, one has to calculate the real dimensions by hand. At least I've not found another way.
double aspectRatio = image.getWidth() / image.getHeight();
double realWidth = Math.min(imageView.getFitWidth(), imageView.getFitHeight() * aspectRatio);
double realHeight = Math.min(imageView.getFitHeight(), imageView.getFitWidth() / aspectRatio);
To explain this a bit: getFitHeight/Width is your blue rectangle. When preserving the aspect ratio of the source image, at least one side of the blue rectangle must have the same length as the corresponding side of the green rectangle. Also, the green rectangle will always be inside of the blue one, hence the call to Math.min().
~>1)
The fact that the Image is not resizing to cover all the ImageView has to do with preserveRatio method.If you want to be covered setPreserveRatio(false);
with a combination of setSmooth( true );
~>2)
The green border is the size of the original image.
~>3)
Before adding the Image to the ImageView you can do:
Image image = new Image("FILE:...");
image.getWidth( );
image.getHeight( );
And that is the original size of the image.
As for the size of the image when it is displayed inside the ImageView:
imageView.getFitWidth();
imageView.getFitHeight();
For the size of ImageView inside the Scene (the blue border in your question):
imageView.getWidth();
imageView.getHeight();
ImageView class has properties for the above.
I am not aware of any property that you could use directly to get at the numbers you want but you might propose this as an improvement for a future version of JavaFX.
What you can do now is write some code which tracks all proerties of the ImageView which have an influence on the size of the displayed image and then compute the displayed size yourself. This does not sound too complicated to me.