Xamarin Forms pinch and pan together - xamarin
I have implemented both pan and pinch individually, and it works fine. I'm now trying to use pinch and pan together and I'm seeing some issues. Here's my code:
XAML:
<AbsoluteLayout x:Name="PinchZoomContainer">
<controls:NavBar x:Name="NavBar" ShowPrevNext="true" ShowMenu="false" IsModal="true" />
<controls:PanContainer x:Name="PinchToZoomContainer">
<Image x:Name="ImageMain" />
</controls:PanContainer>
</AbsoluteLayout>
Pinch/Pan Gesture Add's:
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
GestureRecognizers.Add(panGesture);
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += OnPinchUpdated;
GestureRecognizers.Add(pinchGesture);
Pan Method:
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
startX = e.TotalX;
startY = e.TotalY;
Content.AnchorX = 0;
Content.AnchorY = 0;
break;
case GestureStatus.Running:
// Translate and ensure we don't pan beyond the wrapped user interface element bounds.
Content.TranslationX = Math.Max(Math.Min(0, x + e.TotalX), -Math.Abs(Content.Width - App.ScreenWidth));
Content.TranslationY = Math.Max(Math.Min(0, y + e.TotalY), -Math.Abs(Content.Height - App.ScreenHeight));
break;
case GestureStatus.Completed:
// Store the translation applied during the pan
x = Content.TranslationX;
y = Content.TranslationY;
break;
}
}
Pinch Method:
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started)
{
// Store the current scale factor applied to the wrapped user interface element,
// and zero the components for the center point of the translate transform.
startScale = Content.Scale;
//ImageMain.AnchorX = 0;
//ImageMain.AnchorY = 0;
}
if (e.Status == GestureStatus.Running)
{
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max(1, currentScale);
currentScale = Math.Min(currentScale, 2.5);
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the X pixel coordinate.
double renderedX = Content.X + xOffset;
double deltaX = renderedX / Width;
double deltaWidth = Width / (Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the Y pixel coordinate.
double renderedY = Content.Y + yOffset;
double deltaY = renderedY / Height;
double deltaHeight = Height / (Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
// Calculate the transformed element pixel coordinates.
double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);
// Apply translation based on the change in origin.
Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);
Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);
// Apply scale factor
Content.Scale = currentScale;
}
if (e.Status == GestureStatus.Completed)
{
// Store the translation delta's of the wrapped user interface element.
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
}
}
If I turn off either gesture and only use the other then the functionality works perfectly. The issue arises when I add the pan AND pinch gestures. What seems to be happening is this:
1) The pan actually seems to be working as expected
2) When you pan on the image initially, let's say, move the image to Y-center and X-center, and then you try to zoom, the image gets set back to it's initial state. Then, when you pan, it moves you back to where you were before you tried to zoom (which is why I say the pan is working fine).
From what I'm understanding from my debugging is that when you zoom it's not taking into consideration the position you are currently at. So when you pan first, and then zoom, it doesn't zoom on the position you're at but the beginning point of the image. Then when you try to pan from there, the pan method still remembers where you were, and it moves you back to where you were before you tried to zoom.
Hoping some insight on this. Obviously, there's an issue with my pinch method. I just think (obviously can't figure out) I need to add logic into it that takes into consideration where you're currently at.
The main reason might be that everybody seems to copy and use this code (coming from the dev.xamarin site) with its very convoluted and very unnecessary co-ordinate calculations :-). Unnecessary because we could simply ask the view to do the heavy lifting for us, using the AnchorX and AnchorY properties which serve exactly this purpose.
We can have a double tap operation to zoom in and to revert to the original scale. Note that because Xamarin fails to provide coordinate values with its Tap events (a very unwise decision, actually), we can only zoom from the center now:
private void OnTapped(object sender, EventArgs e)
{
if (Scale > MIN_SCALE)
{
this.ScaleTo(MIN_SCALE, 250, Easing.CubicInOut);
this.TranslateTo(0, 0, 250, Easing.CubicInOut);
}
else
{
AnchorX = AnchorY = 0.5;
this.ScaleTo(MAX_SCALE, 250, Easing.CubicInOut);
}
}
The pinch handler is similarly simple, no need to calculate any translations at all. All we have to do is to set the anchors to the pinch starting point and the framework will do the rest, the scaling will occur around this point. Note that we even have an extra feature here, springy bounce-back on overshoot at both ends of the zoom scale.
private void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
switch (e.Status)
{
case GestureStatus.Started:
StartScale = Scale;
AnchorX = e.ScaleOrigin.X;
AnchorY = e.ScaleOrigin.Y;
break;
case GestureStatus.Running:
double current = Scale + (e.Scale - 1) * StartScale;
Scale = Clamp(current, MIN_SCALE * (1 - OVERSHOOT), MAX_SCALE * (1 + OVERSHOOT));
break;
case GestureStatus.Completed:
if (Scale > MAX_SCALE)
this.ScaleTo(MAX_SCALE, 250, Easing.SpringOut);
else if (Scale < MIN_SCALE)
this.ScaleTo(MIN_SCALE, 250, Easing.SpringOut);
break;
}
}
And the panning handler, even simpler. On start, we calculate the starting point from the anchor and during panning, we keep changing the anchor. This anchor being relative to the view area, we can easily clamp it between 0 and 1 and this stops the panning at the extremes without any translation calculation at all.
private void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
switch (e.StatusType)
{
case GestureStatus.Started:
StartX = (1 - AnchorX) * Width;
StartY = (1 - AnchorY) * Height;
break;
case GestureStatus.Running:
AnchorX = Clamp(1 - (StartX + e.TotalX) / Width, 0, 1);
AnchorY = Clamp(1 - (StartY + e.TotalY) / Height, 0, 1);
break;
}
}
The constants and variables used are just these:
private const double MIN_SCALE = 1;
private const double MAX_SCALE = 8;
private const double OVERSHOOT = 0.15;
private double StartX, StartY;
private double StartScale;
Went with a completely different method of handling this. For anyone who is having issues, this works 100%.
OnPanUpdated
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
var s = (ContentView)sender;
// do not allow pan if the image is in its intial size
if (currentScale == 1)
return;
switch (e.StatusType)
{
case GestureStatus.Running:
double xTrans = xOffset + e.TotalX, yTrans = yOffset + e.TotalY;
// do not allow verical scorlling unless the image size is bigger than the screen
s.Content.TranslateTo(xTrans, yTrans, 0, Easing.Linear);
break;
case GestureStatus.Completed:
// Store the translation applied during the pan
xOffset = s.Content.TranslationX;
yOffset = s.Content.TranslationY;
// center the image if the width of the image is smaller than the screen width
if (originalWidth * currentScale < ScreenWidth && ScreenWidth > ScreenHeight)
xOffset = (ScreenWidth - originalWidth * currentScale) / 2 - s.Content.X;
else
xOffset = System.Math.Max(System.Math.Min(0, xOffset), -System.Math.Abs(originalWidth * currentScale - ScreenWidth));
// center the image if the height of the image is smaller than the screen height
if (originalHeight * currentScale < ScreenHeight && ScreenHeight > ScreenWidth)
yOffset = (ScreenHeight - originalHeight * currentScale) / 2 - s.Content.Y;
else
//yOffset = System.Math.Max(System.Math.Min((originalHeight - (ScreenHeight)) / 2, yOffset), -System.Math.Abs((originalHeight * currentScale - ScreenHeight - (originalHeight - ScreenHeight) / 2)) + (NavBar.Height + App.StatusBarHeight));
yOffset = System.Math.Max(System.Math.Min((originalHeight - (ScreenHeight)) / 2, yOffset), -System.Math.Abs((originalHeight * currentScale - ScreenHeight - (originalHeight - ScreenHeight) / 2)));
// bounce the image back to inside the bounds
s.Content.TranslateTo(xOffset, yOffset, 500, Easing.BounceOut);
break;
}
}
OnPinchUpdated
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
var s = (ContentView)sender;
if (e.Status == GestureStatus.Started)
{
// Store the current scale factor applied to the wrapped user interface element,
// and zero the components for the center point of the translate transform.
startScale = s.Content.Scale;
s.Content.AnchorX = 0;
s.Content.AnchorY = 0;
}
if (e.Status == GestureStatus.Running)
{
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = System.Math.Max(1, currentScale);
currentScale = System.Math.Min(currentScale, 5);
//scaleLabel.Text = "Scale: " + currentScale.ToString ();
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the X pixel coordinate.
double renderedX = s.Content.X + xOffset;
double deltaX = renderedX / App.ScreenWidth;
double deltaWidth = App.ScreenWidth / (s.Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the Y pixel coordinate.
double renderedY = s.Content.Y + yOffset;
double deltaY = renderedY / App.ScreenHeight;
double deltaHeight = App.ScreenHeight / (s.Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
// Calculate the transformed element pixel coordinates.
double targetX = xOffset - (originX * s.Content.Width) * (currentScale - startScale);
double targetY = yOffset - (originY * s.Content.Height) * (currentScale - startScale);
// Apply translation based on the change in origin.
var transX = targetX.Clamp(-s.Content.Width * (currentScale - 1), 0);
var transY = targetY.Clamp(-s.Content.Height * (currentScale - 1), 0);
s.Content.TranslateTo(transX, transY, 0, Easing.Linear);
// Apply scale factor.
s.Content.Scale = currentScale;
}
if (e.Status == GestureStatus.Completed)
{
// Store the translation applied during the pan
xOffset = s.Content.TranslationX;
yOffset = s.Content.TranslationY;
// center the image if the width of the image is smaller than the screen width
if (originalWidth * currentScale < ScreenWidth && ScreenWidth > ScreenHeight)
xOffset = (ScreenWidth - originalWidth * currentScale) / 2 - s.Content.X;
else
xOffset = System.Math.Max(System.Math.Min(0, xOffset), -System.Math.Abs(originalWidth * currentScale - ScreenWidth));
// center the image if the height of the image is smaller than the screen height
if (originalHeight * currentScale < ScreenHeight && ScreenHeight > ScreenWidth)
yOffset = (ScreenHeight - originalHeight * currentScale) / 2 - s.Content.Y;
else
yOffset = System.Math.Max(System.Math.Min((originalHeight - ScreenHeight) / 2, yOffset), -System.Math.Abs(originalHeight * currentScale - ScreenHeight - (originalHeight - ScreenHeight) / 2));
// bounce the image back to inside the bounds
s.Content.TranslateTo(xOffset, yOffset, 500, Easing.BounceOut);
}
}
OnSizeAllocated (most of this you probably dont need, but some you do. consider ScreenWidth, ScreenHeight, yOffset, xOffset, currentScale)
protected override void OnSizeAllocated(double width, double height)
{
base.OnSizeAllocated(width, height); //must be called
if (width != -1 && (ScreenWidth != width || ScreenHeight != height))
{
ResetLayout(width, height);
originalWidth = initialLoad ?
ImageWidth >= 960 ?
App.ScreenWidth > 320
? 768
: 320
: ImageWidth / 3
: imageContainer.Content.Width / imageContainer.Content.Scale;
var normalizedHeight = ImageWidth >= 960 ?
App.ScreenWidth > 320 ? ImageHeight / (ImageWidth / 768)
: ImageHeight / (ImageWidth / 320)
: ImageHeight / 3;
originalHeight = initialLoad ?
normalizedHeight : (imageContainer.Content.Height / imageContainer.Content.Scale);
ScreenWidth = width;
ScreenHeight = height;
xOffset = imageContainer.TranslationX;
yOffset = imageContainer.TranslationY;
currentScale = imageContainer.Scale;
if (initialLoad)
initialLoad = false;
}
}
Layout (XAML in C#)
ImageMain = new Image
{
HorizontalOptions = LayoutOptions.CenterAndExpand,
VerticalOptions = LayoutOptions.CenterAndExpand,
Aspect = Aspect.AspectFill,
Source = ImageMainSource
};
imageContainer = new ContentView
{
Content = ImageMain,
BackgroundColor = Xamarin.Forms.Color.Black,
WidthRequest = App.ScreenWidth - 250
};
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
imageContainer.GestureRecognizers.Add(panGesture);
var pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += OnPinchUpdated;
imageContainer.GestureRecognizers.Add(pinchGesture);
double smallImageHeight = ImageHeight / (ImageWidth / 320);
absoluteLayout = new AbsoluteLayout
{
HeightRequest = App.ScreenHeight,
BackgroundColor = Xamarin.Forms.Color.Black,
};
AbsoluteLayout.SetLayoutFlags(imageContainer, AbsoluteLayoutFlags.All);
AbsoluteLayout.SetLayoutBounds(imageContainer, new Rectangle(0f, 0f, AbsoluteLayout.AutoSize, AbsoluteLayout.AutoSize));
absoluteLayout.Children.Add(imageContainer, new Rectangle(0, 0, 1, 1), AbsoluteLayoutFlags.All);
Content = absoluteLayout;
I've been working on a Image viewer with pan&zoom...
I reached another variation.
I'll share with you.
First, we need a Pan/Zoom class controller:
using System;
using Xamarin.Forms;
namespace Project.Util
{
public class PanZoom
{
bool pitching = false;
bool panning = false;
bool collectFirst = false;
double xOffset = 0;
double yOffset = 0;
//scale processing...
double scaleMin;
double scaleMax;
double scale;
double _xScaleOrigin;
double _yScaleOrigin;
double panTotalX;
double panTotalY;
ContentPage contentPage;
View Content;
public void Setup(ContentPage cp, View content)
{
contentPage = cp;
Content = content;
PinchGestureRecognizer pinchGesture = new PinchGestureRecognizer();
pinchGesture.PinchUpdated += PinchUpdated;
contentPage.Content.GestureRecognizers.Add(pinchGesture);
var panGesture = new PanGestureRecognizer();
panGesture.PanUpdated += OnPanUpdated;
contentPage.Content.GestureRecognizers.Add(panGesture);
contentPage.SizeChanged += (sender, e) => { layoutElements(); };
}
public void layoutElements()
{
if (contentPage.Width <= 0 || contentPage.Height <= 0 || Content.WidthRequest <= 0 || Content.HeightRequest <= 0)
return;
xOffset = 0;
yOffset = 0;
double pageW = contentPage.Width;
double pageH = contentPage.Height;
double w_s = pageW / Content.WidthRequest;
double h_s = pageH / Content.HeightRequest;
if (w_s < h_s)
scaleMin = w_s;
else
scaleMin = h_s;
scaleMax = scaleMin * 3.0;
scale = scaleMin;
double w = Content.WidthRequest * scale;
double h = Content.HeightRequest * scale;
double x = pageW / 2.0 - w / 2.0 + xOffset;
double y = pageH / 2.0 - h / 2.0 + yOffset;
AbsoluteLayout.SetLayoutBounds(Content, new Rectangle(x, y, w, h));
}
void fixPosition(
ref double x, ref double y, ref double w, ref double h,
bool setoffset
)
{
double pageW = contentPage.Width;
double pageH = contentPage.Height;
if (w <= pageW)
{
double new_x = pageW / 2.0 - w / 2.0;
if (setoffset)
xOffset = new_x - (pageW / 2.0 - w / 2.0);
x = new_x;
} else
{
if (x > 0)
{
double new_x = 0;
if (setoffset)
xOffset = new_x - (pageW / 2.0 - w / 2.0);
x = new_x;
}
if (x < (pageW - w))
{
double new_x = (pageW - w);
if (setoffset)
xOffset = new_x - (pageW / 2.0 - w / 2.0);
x = new_x;
}
}
if (h <= pageH)
{
double new_y = pageH / 2.0 - h / 2.0;
if (setoffset)
yOffset = new_y - (pageH / 2.0 - h / 2.0);
y = new_y;
}
else
{
if (y > 0)
{
double new_y = 0;
if (setoffset)
yOffset = new_y - (pageH / 2.0 - h / 2.0);
y = new_y;
}
if (y < (pageH - h))
{
double new_y = (pageH - h);
if (setoffset)
yOffset = new_y - (pageH / 2.0 - h / 2.0);
y = new_y;
}
}
}
private void PinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (sender != contentPage.Content)
return;
switch (e.Status)
{
case GestureStatus.Started:
{
pitching = true;
collectFirst = true;
double pageW = contentPage.Width;
double pageH = contentPage.Height;
_xScaleOrigin = e.ScaleOrigin.X * pageW;
_yScaleOrigin = e.ScaleOrigin.Y * pageH;
}
break;
case GestureStatus.Running:
if (pitching)
{
double targetScale = scale * e.Scale;
targetScale = Math.Min(Math.Max(scaleMin, targetScale), scaleMax);
double scaleDelta = targetScale / scale;
double pageW = contentPage.Width;
double pageH = contentPage.Height;
double w_old = Content.WidthRequest * scale;
double h_old = Content.HeightRequest * scale;
double x_old = pageW / 2.0 - w_old / 2.0 + xOffset;
double y_old = pageH / 2.0 - h_old / 2.0 + yOffset;
scale = targetScale;
//new w and h
double w = Content.WidthRequest * scale;
double h = Content.HeightRequest * scale;
//transform x old and y old
// to get new scaled position over a pivot
double _x = (x_old - _xScaleOrigin) * scaleDelta + _xScaleOrigin;
double _y = (y_old - _yScaleOrigin) * scaleDelta + _yScaleOrigin;
//fix offset to be equal to _x and _y
double x = pageW / 2.0 - w / 2.0 + xOffset;
double y = pageH / 2.0 - h / 2.0 + yOffset;
xOffset += _x - x;
yOffset += _y - y;
x = pageW / 2.0 - w / 2.0 + xOffset;
y = pageH / 2.0 - h / 2.0 + yOffset;
fixPosition(ref x, ref y, ref w, ref h, true);
AbsoluteLayout.SetLayoutBounds(Content, new Rectangle(x, y, w, h));
}
break;
case GestureStatus.Completed:
pitching = false;
break;
}
}
public void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (sender != contentPage.Content)
return;
switch (e.StatusType)
{
case GestureStatus.Started:
{
panning = true;
panTotalX = e.TotalX;
panTotalY = e.TotalY;
collectFirst = true;
}
break;
case GestureStatus.Running:
if (panning)
{
if (collectFirst)
{
collectFirst = false;
panTotalX = e.TotalX;
panTotalY = e.TotalY;
}
double pageW = contentPage.Width;
double pageH = contentPage.Height;
double deltaX = e.TotalX - panTotalX;
double deltaY = e.TotalY - panTotalY;
panTotalX = e.TotalX;
panTotalY = e.TotalY;
xOffset += deltaX;
yOffset += deltaY;
double w = Content.WidthRequest * scale;
double h = Content.HeightRequest * scale;
double x = pageW / 2.0 - w / 2.0 + xOffset;
double y = pageH / 2.0 - h / 2.0 + yOffset;
fixPosition(ref x, ref y, ref w, ref h, true);
AbsoluteLayout.SetLayoutBounds(Content, new Rectangle(x, y, w, h));
}
break;
case GestureStatus.Completed:
panning = false;
break;
}
}
}
}
In the content page:
using System;
using FFImageLoading.Forms;
using Xamarin.Forms;
using Project.Util;
namespace Project.ContentPages
{
public class ContentPage_ImageViewer : ContentPage
{
AbsoluteLayout al = null;
CachedImage image = null;
PanZoom panZoom;
public ContentPage_ImageViewer(string imageURL)
{
MasterDetailPage mdp = Application.Current.MainPage as MasterDetailPage;
mdp.IsGestureEnabled = false;
NavigationPage.SetHasBackButton(this, true);
Title = "";
image = new CachedImage()
{
HorizontalOptions = LayoutOptions.FillAndExpand,
VerticalOptions = LayoutOptions.FillAndExpand,
Aspect = Aspect.Fill,
LoadingPlaceholder = "placeholder_320x322.png",
ErrorPlaceholder = "placeholder_320x322.png",
Source = imageURL,
RetryCount = 3,
DownsampleToViewSize = false,
IsVisible = false,
FadeAnimationEnabled = false
};
image.Success += delegate (object sender, CachedImageEvents.SuccessEventArgs e)
{
Device.BeginInvokeOnMainThread(() =>
{
image.WidthRequest = e.ImageInformation.OriginalWidth;
image.HeightRequest = e.ImageInformation.OriginalHeight;
image.IsVisible = true;
for(int i = al.Children.Count-1; i >= 0; i--)
{
if (al.Children[i] is ActivityIndicator)
al.Children.RemoveAt(i);
}
panZoom.layoutElements();
});
};
ActivityIndicator ai = new ActivityIndicator()
{
IsRunning = true,
Scale = (Device.RuntimePlatform == Device.Android) ? 0.25 : 1.0,
VerticalOptions = LayoutOptions.Fill,
HorizontalOptions = LayoutOptions.Fill,
Color = Color.White
};
Content = (al = new AbsoluteLayout()
{
VerticalOptions = LayoutOptions.Fill,
HorizontalOptions = LayoutOptions.Fill,
BackgroundColor = Color.Black,
Children =
{
image,
ai
}
});
AbsoluteLayout.SetLayoutFlags(image, AbsoluteLayoutFlags.None);
AbsoluteLayout.SetLayoutBounds(ai, new Rectangle(0, 0, 1, 1));
AbsoluteLayout.SetLayoutFlags(ai, AbsoluteLayoutFlags.All);
panZoom = new PanZoom();
panZoom.Setup(this, image);
}
}
}
For me it worked like below, just did some changes in the code given in the question,
void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)
{
if (e.Status == GestureStatus.Started)
{
// Store the current scale factor applied to the wrapped user interface element,
// and zero the components for the center point of the translate transform.
startScale = Content.Scale;
Content.AnchorX = 0;
Content.AnchorY = 0;
}
if (e.Status == GestureStatus.Running)
{
// Calculate the scale factor to be applied.
currentScale += (e.Scale - 1) * startScale;
currentScale = Math.Max(1, currentScale);
// The ScaleOrigin is in relative coordinates to the wrapped user
interface element,
// so get the X pixel coordinate.
double renderedX = Content.X + xOffset;
double deltaX = renderedX / Width;
double deltaWidth = Width / (Content.Width * startScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
// The ScaleOrigin is in relative coordinates to the wrapped user
interface element,
// so get the Y pixel coordinate.
double renderedY = Content.Y + yOffset;
double deltaY = renderedY / Height;
double deltaHeight = Height / (Content.Height * startScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
// Calculate the transformed element pixel coordinates.
double targetX = xOffset - (originX * Content.Width) * (currentScale -
startScale);
double targetY = yOffset - (originY * Content.Height) * (currentScale -
startScale);
// Apply translation based on the change in origin.
Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);
Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);
// Apply scale factor.
Content.Scale = currentScale;
width = Content.Width * currentScale;
height = Content.Height * currentScale;
}
if (e.Status == GestureStatus.Completed)
{
// Store the translation delta's of the wrapped user interface element.
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
x = Content.TranslationX;
y = Content.TranslationY;
}
}
Pan Code
void OnPanUpdated(object sender, PanUpdatedEventArgs e)
{
if (!width.Equals(Content.Width) && !height.Equals(Content.Height))
{
switch (e.StatusType)
{
case GestureStatus.Started:
startX = Content.TranslationX;
startY = Content.TranslationY;
break;
case GestureStatus.Running:
if (!width.Equals(0))
{
Content.TranslationX = Math.Max(Math.Min(0, x + e.TotalX), -Math.Abs(Content.Width - width));// App.ScreenWidth));
}
if (!height.Equals(0))
{
Content.TranslationY = Math.Max(Math.Min(0, y + e.TotalY), -Math.Abs(Content.Height - height)); //App.ScreenHeight));
}
break;
case GestureStatus.Completed:
// Store the translation applied during the pan
x = Content.TranslationX;
y = Content.TranslationY;
xOffset = Content.TranslationX;
yOffset = Content.TranslationY;
break;
}
}
}
Related
Tried to make 3 moving parts of a screensaver in Processing, got one super fast part. Any ideas?
I created a screensaver that bounces around the screen. I tried to fit original the code into OOP (which I'm terrible at) so that I could have multiple versions of it bouncing around. Instead, I'm getting one instance travelling at an unusually fast speed. How can I get instance1, instance2 and instance3 to be seperate instead of combining into one? Notes: In the class screenSavers, C stands for constructor parameter to differentiate from the original variables at the top. CS stands for class-specific to avoid confusion with the original variables at the top. (yes, it's confusing. I don't know why I did that) GIANT WALL OF CODE ALERT import java.lang.Math; //defining dimensions and initial coords for the laptop frame float frameW = 120; float frameH = frameW * 3.0/4.0; float frameX = random(frameW * 2, width); float frameY = random(frameW * 2, height); //defining variables are used in the movement of the laptop float xSpeed = random(frameW / 50,frameH / 15); float ySpeed = random(2,5); boolean moveLeft = false; boolean moveUp = false; //defining the colours used in the program, as well as the alpha and stroke widths int alpha = 255; color baseColor = color(random(100, 255), random(100, 255), random(100, 255), alpha); color accentColor = color(random(100, 255), random(100, 255), random(100, 255), alpha); color baseColorAlpha; color accentColorAlpha; color bgColor = 0; color outline = 255; int outerCircleWeight = 5; int laptopStrokeWeight = 1; //initial states of little ellipses on the screen float startingPoint = 7.5; float leftDotRaise = 0; boolean leftDown = false; float centerDotRaise = 0; boolean centerDown = false; float rightDotRaise = 0; boolean rightDown = false; //variable for the radian at which the circles outside the laptop are at float circleOrbit; //variable for roundness of any initially circular shapes (ellipses, orbits, etc) float specialRadii = 200; //square-ification button int buttonX = 200; int buttonY = 200; boolean buttonState = true; int buttonTextSize = 30; String offMessage = "Revert"; String onMessage = "Squarify"; String buttonMessage = onMessage; public class screenSavers { float frameWCS; float xSpeedCS; float ySpeedCS; boolean moveLeftCS; boolean moveUpCS; int alphaCS; color baseColorCS; color accentColorCS; color bgColorCS; color outlineCS; int outerCircleWeightCS; int laptopStrokeWeightCS; float startingPointCS; float leftDotRaiseCS; boolean leftDownCS; float centerDotRaiseCS; boolean centerDownCS; float rightDotRaiseCS; boolean rightDownCS; float specialRadiiCS; int buttonXCS; int buttonYCS; boolean buttonStateCS; int buttonTextSizeCS; String offMessageCS; String onMessageCS; String buttonMessageCS; float circleOrbitCS; public screenSavers (float frameWC, float xSpeedC, float ySpeedC, boolean moveLeftC, boolean moveUpC, int alphaC, color bgColorC, color outlineC, int outerCircleWeightC, int laptopStrokeWeightC, float startingPointC, float leftDotRaiseC, boolean leftDownC, float centerDotRaiseC, boolean centerDownC, float rightDotRaiseC, boolean rightDownC, float specialRadiiC, int buttonXC, int buttonYC, boolean buttonStateC, int buttonTextSizeC, String offMessageC, String onMessageC, String buttonMessageC, float circleOrbitC){ frameWCS = frameWC; xSpeedCS = xSpeedC; ySpeedCS = ySpeedC; moveLeftCS = moveLeftC; moveUpCS = moveUpC; alphaCS = alphaC; bgColorCS = bgColorC; outlineCS = outlineC; outerCircleWeightCS = outerCircleWeightC; laptopStrokeWeightCS = laptopStrokeWeightC; startingPointCS = startingPointC; leftDotRaiseCS = leftDotRaiseC; leftDownCS = leftDownC; centerDotRaiseCS = centerDotRaiseC; centerDownCS = centerDownC; rightDotRaiseCS = rightDotRaiseC; rightDownCS = rightDownC; specialRadiiCS = specialRadiiC; buttonXCS = buttonXC; buttonYCS = buttonYC; buttonStateCS = buttonStateC; buttonTextSizeCS = buttonTextSizeC; offMessageCS = offMessageC; onMessageCS = onMessageC; buttonMessageCS = buttonMessageC; circleOrbitCS = circleOrbitC; } public void main(String[] args){ } public void updateAlpha(){ /* updates alpha versions of color variables that account for the alpha of the color, we have to put this so that it updates every frame, otherwise the transparency will only update with every bounce. This way the color updates with each bounce but the transparency constantly updates every frame (or 60 times per second) */ baseColorAlpha = color(red(baseColor),green(baseColor),blue(baseColor), alpha); accentColorAlpha = color(red(accentColor),green(accentColor),blue(accentColor), alpha); /* transparency of objects changes with X position of the mouse, 1st quarter of the screen = 25% brightness, 2nd quarter = 50%, 3rd quarter = 75%, 4th quarter = 100% */ if (mouseX < width / 3){ alpha = 75; } else if (mouseX > width / 3 && mouseX < 2 * width / 3) { alpha = 150; } else if (mouseX > width * 2 / 3 && mouseX < width){ alpha = 255; } } void resetBackground(){ //reset background to prevent copies of the laptop from generating background(bgColor); //set outline color stroke(outline); fill(baseColorAlpha); } void updateRadii (){ //changing radii of shapes based on button if (buttonStateCS == true){ specialRadiiCS = 2 * frameW; } else { specialRadiiCS = 0; } } void generateComputer(){ //making the frame rect(frameX, frameY, frameW, frameH); //making the screen fill(accentColorAlpha); rect(frameX, frameY, frameW - frameW / 5, frameH - frameH * 4.0/15.0); //switching back to the base color for the moving ellipses on the screen fill(baseColorAlpha); //creating the laptop keyboard fill(accentColorAlpha); beginShape(); vertex(frameX - frameW / 2, frameY + frameH / 2); vertex(frameX - (frameW / 2 + 15), frameY + frameH / 2 + 15); vertex(frameX + frameW / 2 + 15, frameY + frameH / 2 + 15); vertex(frameX + frameW / 2, frameY + frameH / 2); endShape(); } void dots(){ rect(frameX - frameW / 5,frameY + leftDotRaise + startingPoint,frameH / 5,frameH / 5,specialRadiiCS); //moving the dots if (leftDotRaise >= frameH * (11.0 / 75.0)){ leftDown = false; } else if (leftDotRaise <= frameH * (-11.0 / 75.0)){ leftDown = true; } if (leftDown == true){ leftDotRaise += frameH / 75; } else if (leftDown == false){ leftDotRaise -= frameH / 75; } rect(frameX,frameY + centerDotRaise, frameH / 5, frameH / 5,specialRadiiCS); if (centerDotRaise >= frameH * (11.0 / 75.0)){ centerDown = false; } else if (centerDotRaise <= frameH * (-11.0 / 75.0)){ centerDown = true; } if (centerDown == true){ centerDotRaise += frameH / 75; } else if (centerDown == false){ centerDotRaise -= frameH / 75; } rect(frameX + frameW / 5,frameY + rightDotRaise - startingPoint,frameH / 5,frameH / 5,specialRadiiCS); if (rightDotRaise >= frameH * (11.0 / 75.0)){ rightDown = false; } else if (rightDotRaise <= frameH * (-11.0 / 75.0)){ rightDown = true; } if (rightDown == true){ rightDotRaise += frameH / 75; } else if (rightDown == false){ rightDotRaise -= frameH / 75; } startingPoint = 0; } void generateOrbitals(){ //creating and animated the outer rotating circles pushMatrix(); translate(frameX, frameY); rotate(circleOrbitCS); translate(frameW * 1.5, frameH / -10); circleOrbitCS += 0.01; rect(0, 0, frameW / 2, frameW / 2, specialRadiiCS); popMatrix(); pushMatrix(); translate(frameX, frameY); rotate(circleOrbitCS); translate(frameW * -1.5, frameH / 10); circleOrbitCS += 0.01; rect(0, 0, frameW / 2, frameW / 2, specialRadiiCS); popMatrix(); pushMatrix(); translate(frameX, frameY); rotate(circleOrbitCS); translate(frameH / 10, frameW * 1.5); circleOrbit += 0.01; rect(0, 0, frameW / 2, frameW / 2, specialRadiiCS); popMatrix(); pushMatrix(); translate(frameX, frameY); rotate(circleOrbit); translate(frameH / -10, frameW * -1.5); circleOrbit += 0.01; rect(0, 0, frameW / 2,frameW / 2, specialRadiiCS); popMatrix(); } void generateOuterCircle(){ noFill(); stroke(accentColor); strokeWeight(outerCircleWeight); rect(frameX, frameY, frameW * 4, frameW * 4,specialRadii); strokeWeight(laptopStrokeWeight); } void bounce(){ if (frameX + 2 * frameW > width) { moveLeft = true; baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); } else if (frameX - 2 * frameW < 0) { moveLeft = false; baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); } if (frameY + 8.0/3.0 * frameH > height) { moveUp = true; baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); } else if (frameY - 8.0/3.0 * frameH < 0) { moveUp = false; baseColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); accentColor = color(random(50, 255), random(50, 255), random(50, 255),alpha); } if (moveLeft == true) { xSpeed = abs(xSpeed) * -1; } else { xSpeed = abs(xSpeed); } if (moveUp == true) { ySpeed = abs(ySpeed) * -1; } else { ySpeed = abs(ySpeed); } } void updateCoords(){ //update coords println("Screensaver Icon is currently at (" + frameX + ", " + frameY + ')'); } void generateSquareButton(){ //square-ification button fill(accentColor); rect(buttonXCS,buttonYCS, 200, 200, specialRadiiCS); fill(255); textSize(buttonTextSizeCS); text(buttonMessageCS,buttonXCS,buttonYCS+5); } void alignShapes(){ //letting the program know that we are referencing X and Y coords of a rectangle or text box by its center rectMode(CENTER); textAlign(CENTER); } void testForButton(){ //check if the mouse was pressed at the button's coordinates if (mouseX > buttonXCS - 100 && mouseY > buttonYCS - 100 && mouseX < buttonXCS + 50 && mouseY < buttonYCS + 50){ //if the button is on, turn it off if (buttonStateCS == true){ buttonStateCS = false; buttonMessageCS = offMessageCS; } else //if the button is off, turn it on if (buttonStateCS == false){ buttonStateCS = true; buttonMessageCS = onMessageCS; } } } void move(){ frameX += xSpeed; frameY += ySpeed; } void display(){ updateAlpha(); resetBackground(); updateRadii(); generateComputer(); dots(); generateOrbitals(); generateOuterCircle(); bounce(); move(); updateCoords(); generateSquareButton(); } } //creating the first instance of the screensaver as an object screenSavers instance1 = new screenSavers(frameW,xSpeed,ySpeed,moveLeft,moveUp,alpha,bgColor,outline,outerCircleWeight,laptopStrokeWeight,startingPoint,leftDotRaise,leftDown,centerDotRaise, centerDown, rightDotRaise, rightDown, specialRadii, buttonX, buttonY, buttonState, buttonTextSize, offMessage, onMessage, buttonMessage,circleOrbit); //creating the second instance of the screensaver as an object screenSavers instance2 = new screenSavers(frameW,xSpeed,ySpeed,moveLeft,moveUp,alpha,bgColor,outline,outerCircleWeight,laptopStrokeWeight,startingPoint,leftDotRaise,leftDown,centerDotRaise, centerDown, rightDotRaise, rightDown, specialRadii, buttonX, buttonY, buttonState, buttonTextSize, offMessage, onMessage, buttonMessage,circleOrbit); //creating the third instance of the screensaver as an object screenSavers instance3 = new screenSavers(frameW,xSpeed,ySpeed,moveLeft,moveUp,alpha,bgColor,outline,outerCircleWeight,laptopStrokeWeight,startingPoint,leftDotRaise,leftDown,centerDotRaise, centerDown, rightDotRaise, rightDown, specialRadii, buttonX, buttonY, buttonState, buttonTextSize, offMessage, onMessage, buttonMessage,circleOrbit); //setup loop, runs ONCE void setup() { /*note: to see the console update the X and Y, comment out fullScreen(); and un-comment size(1000,1000); */ //enters fullscreen, regardless of device resolution fullScreen(); //uncomment this for seeing coords updated //size(1000,1000); //makes the framerate go from 24 to a smoother 60 frames. If I don't add this your eyes will hurt frameRate(60); instance1.alignShapes(); instance2.alignShapes(); instance3.alignShapes(); } void draw() { instance1.display(); instance2.display(); instance3.display(); } ////////////////////////////////////////////////////////////////// //do something when mouse is pressed void mousePressed(){ instance1.testForButton(); instance2.testForButton(); instance3.testForButton(); }```
Right now you initialize all 3 instances with the exact same properties. They are only "combined" because you made them the same. You should identify what you want to be different between them, and pass in different properties. Other notes: Why does screenSavers have a main() function? Delete it Please rename it to ScreenSaver. Do not distinguish using C vs CS. Just name them the same. But use the this keyword to distinguish the class vars. example: this.frameW = frameW; Use proper indentation with if statements.
Found it a few days later by the way, I was looking for the this keyword
Best strategy to mimic <img> object-fit / object-position property with konva images
Assume we have a 1000x1000 image drawn onto canvas, and set konva image size to something like 500x700. Is it possible to mimic html <img /> tags object-fit property in konva so we can tell image object to fit image in cover / contain mode. Similarly it would be useful to mimic object-position. Thus far the only way I can think of doing this is to clip image with a rectangle with some custom logic added to handle use cases above. I was wondering if there is a better way?
You can emulate similar CSS behavior with cropX, cropY, cropWidth and cropHeight properties of Konva.Image. You can use this function for calculations: function getCrop(image, size, clipPosition = 'center-middle') { const width = size.width; const height = size.height; const aspectRatio = width / height; let newWidth; let newHeight; const imageRatio = image.width / image.height; if (aspectRatio >= imageRatio) { newWidth = image.width; newHeight = image.width / aspectRatio; } else { newWidth = image.height * aspectRatio; newHeight = image.height; } let x = 0; let y = 0; if (clipPosition === 'left-top') { x = 0; y = 0; } else if (clipPosition === 'left-middle') { x = 0; y = (image.height - newHeight) / 2; } else if (clipPosition === 'left-bottom') { x = 0; y = (image.height - newHeight); } else if (clipPosition === 'center-top') { x = (image.width - newWidth) / 2; y = 0; } else if (clipPosition === 'center-middle') { x = (image.width - newWidth) / 2; y = (image.height - newHeight) / 2; } else if (clipPosition === 'center-bottom') { x = (image.width - newWidth) / 2; y = (image.height - newHeight); } else if (clipPosition === 'right-top') { x = (image.width - newWidth); y = 0; } else if (clipPosition === 'right-middle') { x = (image.width - newWidth); y = (image.height - newHeight) / 2; } else if (clipPosition === 'right-bottom') { x = (image.width - newWidth); y = (image.height - newHeight); } else if (clipPosition === 'scale') { x = 0; y = 0; newWidth = width; newHeight = height; } else { console.error( new Error('Unknown clip position property - ' + clipPosition) ); } return { cropX: x, cropY: y, cropWidth: newWidth, cropHeight: newHeight } } // usage: const crop = getCrop(img.image(), { width: img.width(), height: img.height()}, pos); img.setAttrs(crop); img.getLayer().batchDraw(); Demo: https://konvajs.org/docs/sandbox/Scale_Image_To_Fit.html
Processing 3.5.3 Rotating points around center point Image shrinking in size and dissapears
My problem is as in the title. I am trying to write a simple game in processing with a car that you can drive on a 2D plane. I wanted to create a rotation of the car since it seems crucial so I did it as described here:Rotating points in 2D But my implementation seems to fail a bit. You see, when I hit left of right arrow the car actually rotates but shrinks in size as it is rotating and after few turns it completely dissapears. Can you show me what am I missing here? Thanks in advance! Code of my functions: class Point { float x, y; Point(float xx, float yy) { x = xx; y = yy; } Point() { x = y = 0.0; } void Rotate(Point center, float angle) { float s = sin(angle); float c = cos(angle); y = center.y + ((y-center.y) * c + (x-center.x) * s); x = center.x + ((x-center.x) * c - (y-center.y) * s); } } class Car { Point LT; Point RT; Point LB; Point RB; Point center; float r; float acceleration; Car() { LT = new Point(10, 10); RT = new Point (30, 10); LB = new Point(10, 50); RB = new Point(30, 50); r = sqrt(pow(15-30, 2) + pow(25-10, 2)); } Car(Point lt, Point rt, Point lb, Point rb) { LT = lt; RT = rt; LB = lb; RB = rb; center = new Point(abs((LT.x - RT.x)/2), abs((LT.y - LB.y)/2)); r = sqrt(pow(center.x -LT.x, 2) + pow(center.y - LT.y, 2)); } Car(Point Center, float w, float h) { center = Center; LT = new Point(center.x - w/2, center.y - h/2); RT = new Point (center.x + w/2, center.y - h/2); LB = new Point(center.x - w/2, center.y + h/2); RB = new Point(center.x + w/2, center.y + h/2); r = sqrt(pow(center.x -LT.x, 2) + pow(center.y - LT.y, 2)); } void Show() { fill(45, 128, 156); beginShape(); vertex(LT.x, LT.y); vertex(RT.x, RT.y); vertex(RB.x, RB.y); vertex(LB.x, LB.y); endShape(); } void Update() { } void Turn(float angle) { LT.Rotate(center, angle); RT.Rotate(center, angle); RB.Rotate(center, angle); LB.Rotate(center, angle); } void Accelerate(float accel) { } } In main I only use car.Show() and I turn by -0.1 per left cliock and 0.1 per right click EDIT If you want to see whole code visit my github repo
Unfortunately I can't explain more at the moment, but here's a simpler option using one of the formulas you've pointed to: Car car = new Car(); void setup(){ size(300,300); // this helps draw rectangles from centre (as opposed to corner (default)) rectMode(CENTER); car.position.set(150,150); } void draw(){ background(255); if(keyPressed){ if(keyCode == UP){ car.speed = 1; } } car.draw(); } void keyPressed(){ if(keyCode == LEFT){ car.steer -= radians(10); } if(keyCode == RIGHT){ car.steer += radians(10); } } void keyReleased(){ if(keyCode == UP){ car.speed = 0; } } class Car{ PVector position = new PVector(); PVector velocity = new PVector(); float speed; float steer; void update(){ // use the same polar to cartesian coordinates formulate for quick'n'dirty steering velocity.set(cos(steer) * speed,sin(steer) * speed); // update position based on velocity position.add(velocity); } void draw(){ update(); // use a nested coordinate system to handle translation and rotation for us // order of operations is important pushMatrix(); translate(position.x,position.y); rotate(steer); rect(0,0,30,15); popMatrix(); } } Update The main issue with points shrinking is you're cumulatively transforming the points when you rotate them. After each transformation there is no history of what the x,y were. Instead you should return a new point that is transformed, thus "remembering" the old x,y position. Bellow is a tweaked version of your code, minus the two constructor variants. Hopefully the comments will help: Car car = new Car(); void setup(){ size(300,300); } void draw(){ if(keyCode == UP){ if(keyPressed){ car.Accelerate(1); }else{ car.Accelerate(0); } } car.Update(); background(255); car.Show(); } void keyPressed(){ if(keyCode == LEFT){ car.Turn(radians(-3)); } if(keyCode == RIGHT){ car.Turn(radians(+3)); } } class Point { float x, y; Point(float xx, float yy) { x = xx; y = yy; } Point() { x = y = 0.0; } Point Rotate(Point center, float angle) { float s = sin(angle); float c = cos(angle); // return a new point (a rotated copy), rather than overwriting this one return new Point(center.x + ((x-center.x) * c - (y-center.y) * s), center.y + ((y-center.y) * c + (x-center.x) * s)); } // translate by another point void AddToSelf(Point point){ this.x += point.x; this.y += point.y; } // pretty print info when using println() String toString(){ return "[Point x=" + x + " y="+ y +"]"; } } class Car { Point LT; Point RT; Point LB; Point RB; Point center; float r; float acceleration; // car angle: used to compute velocity and update vertices float angle; // car position: used to offset rendering position of the corners Point position; // car velocity: amount by which position translates Point velocity = new Point(); Car() { float x = 10; float y = 10; float w = 40; float h = 20; // setup corners with no translation LT = new Point(0 , 0 ); RT = new Point(0 + w, 0 ); LB = new Point(0 , 0 + h); RB = new Point(0 + w, 0 + h); // setup initial position position = new Point(x,y); center = new Point(w / 2, h / 2); r = sqrt(pow(15-30, 2) + pow(25-10, 2)); } //Car(Point lt, Point rt, Point lb, Point rb) //{ // LT = lt; // RT = rt; // LB = lb; // RB = rb; // center = new Point(abs((LT.x - RT.x)/2), abs((LT.y - LB.y)/2)); // r = sqrt(pow(center.x -LT.x, 2) + pow(center.y - LT.y, 2)); //} //Car(Point Center, float w, float h) //{ // center = Center; // LT = new Point(center.x - w/2, center.y - h/2); // RT = new Point (center.x + w/2, center.y - h/2); // LB = new Point(center.x - w/2, center.y + h/2); // RB = new Point(center.x + w/2, center.y + h/2); // r = sqrt(pow(center.x -LT.x, 2) + pow(center.y - LT.y, 2)); //} void Show() { fill(45, 128, 156); beginShape(); // render corners offset by the car position vertex(position.x + LT.x, position.y + LT.y); vertex(position.x + RT.x, position.y + RT.y); vertex(position.x + RB.x, position.y + RB.y); vertex(position.x + LB.x, position.y + LB.y); endShape(CLOSE); } void Update() { // update velocity based on car angle and acceleration velocity.x = cos(angle) * acceleration; velocity.y = sin(angle) * acceleration; // update position based on velocity position.AddToSelf(velocity); } void Turn(float angle) { this.angle += angle; // replace the old point with the transformed points // (rather than continuosly transforming the same point) LT = LT.Rotate(center, angle); RT = RT.Rotate(center, angle); RB = RB.Rotate(center, angle); LB = LB.Rotate(center, angle); } void Accelerate(float accel) { acceleration = accel; } }
THREE.js Secondary camera rotated with head rotation in VR
I'm using THREE.js and Aframe ( in Exokit ) together and I have a component for a "selfie camera". I have a weird issue that when i enter VR the camera rotation is taken over by the head rotation. I understand how the camera rotation works has changed in recent versions of THREE.js ( ArrayCamera ) but I assumed that only affected the main camera and not all cameras in the scene. Below is my hacky component that works fine in 2D mode but in VR it messes up. The worst thing about it is im fine with it being linked to the head, the camera itself is a child object of the main camera anyway so it appears in front of the users face when opened and is moved with the head rotation - but its off angle when in VR like its pointing down and to the left a bit. Here are some screenshots that hopefully demonstrate the issue: Edit: need 10 rep to post images so here are urls instead 2D Mode VR Mode Any help much appreciated!! AFRAME.registerComponent('selfie-camera', { schema:{ resolution:{type:'int',default:512}, fov:{type:'int',default:100}, aspect:{type:'number',default:1.5}, near:{type:'number',default:0.001}, far:{type:'number',default:1000} }, init() { this.el.addEventListener('loaded',()=>{ this.renderTarget = new THREE.WebGLRenderTarget(this.data.resolution*1.5, this.data.resolution,{ antialias: true }); this.el.getObject3D('mesh').material.map = this.renderTarget.texture; this.cameraContainer = new THREE.Object3D(); this.el.object3D.add( this.cameraContainer ); this.el.takePicture = this.takePicture.bind(this); this.el.setSide = this.setSide.bind(this); this.wider = 1.5; this.photoMultiplier = 2; this.canvas = document.createElement('canvas'); }); this.testQuat = new THREE.Quaternion(); this.el.open = this.open.bind(this); this.el.close = this.close.bind(this); }, open(){ this.camera = new THREE.PerspectiveCamera( this.data.fov, this.data.aspect, this.data.near, this.data.far ); this.cameraContainer.add(this.camera); new TWEEN.Tween(this.el.getAttribute('scale')) .to(new THREE.Vector3(1,1,1), 650) .easing(TWEEN.Easing.Exponential.Out).start(); }, close(){ new TWEEN.Tween(this.el.getAttribute('scale')) .to(new THREE.Vector3(0.0000001,0.0000001,0.0000001), 200) .onComplete(()=>{ this.cameraContainer.remove(this.camera); delete this.camera; }) .easing(TWEEN.Easing.Exponential.Out).start(); }, tick(){ if(this.camera){ this.camera.getWorldQuaternion(this.testQuat); console.log(this.camera.quaternion); } this.el.getObject3D('mesh').material.visible = false; if(this.isTakingPicture) { this.renderTarget.setSize(this.data.resolution * this.wider * this.photoMultiplier, this.data.resolution * this.photoMultiplier); } this.el.sceneEl.renderer.render( this.el.sceneEl.object3D, this.camera, this.renderTarget ); if(this.isTakingPicture){ this.isTakingPicture = false; this.pictureResolve(this.createImageFromTexture()); this.renderTarget.setSize(this.data.resolution * this.wider, this.data.resolution); } this.el.getObject3D('mesh').material.visible = true; }, setSide(isFront){ let _this = this; new TWEEN.Tween({y:this.cameraContainer.rotation.y}) .to({y:isFront?Math.PI:0}, 350) .onUpdate(function(){ _this.cameraContainer.rotation.y = this.y; }) .easing(TWEEN.Easing.Exponential.Out).start(); }, takePicture(){ return new Promise(resolve=>{ this.isTakingPicture = true; this.pictureResolve = resolve; }) }, createImageFromTexture() { let width = this.data.resolution*this.wider*this.photoMultiplier, height = this.data.resolution*this.photoMultiplier; let pixels = new Uint8Array(4 * width * height); this.el.sceneEl.renderer.readRenderTargetPixels(this.renderTarget, 0, 0, width, height, pixels); pixels = this.flipPixelsVertically(pixels, width, height); let imageData = new ImageData(new Uint8ClampedArray(pixels), width, height); this.canvas.width = width; this.canvas.height = height; let context = this.canvas.getContext('2d'); context.putImageData(imageData, 0, 0); return this.canvas.toDataURL('image/jpeg',100); }, flipPixelsVertically: function (pixels, width, height) { let flippedPixels = pixels.slice(0); for (let x = 0; x < width; ++x) { for (let y = 0; y < height; ++y) { flippedPixels[x * 4 + y * width * 4] = pixels[x * 4 + (height - y) * width * 4]; flippedPixels[x * 4 + 1 + y * width * 4] = pixels[x * 4 + 1 + (height - y) * width * 4]; flippedPixels[x * 4 + 2 + y * width * 4] = pixels[x * 4 + 2 + (height - y) * width * 4]; flippedPixels[x * 4 + 3 + y * width * 4] = pixels[x * 4 + 3 + (height - y) * width * 4]; } } return flippedPixels; } });
You have to disable VR before rendering: var renderer = this.el.sceneEl.renderer; var vrEnabled = renderer.vr.enabled; renderer.vr.enabled = false; renderer.render(this.el.sceneEl.object3D, this.camera, this.renderTarget); renderer.vr.enabled = vrEnabled;
xamarin forms image zoom and scroll
I am using this pinchGestureRecognizer in order to zoom on an image, but the problem is that after zooming I am not able to scroll the image vertically, even though I've wrapped my image in a scrollView with Orientation="Both" Here is the PinchToZoomContainer class: public class PinchToZoomContainer : ContentView { double currentScale = 1; double startScale = 1; double xOffset = 0; double yOffset = 0; public PinchToZoomContainer() { var pinchGesture = new PinchGestureRecognizer(); pinchGesture.PinchUpdated += OnPinchUpdated; GestureRecognizers.Add(pinchGesture); } void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e) { if (e.Status == GestureStatus.Started) { // Store the current scale factor applied to the wrapped user interface element, // and zero the components for the center point of the translate transform. startScale = Content.Scale; Content.AnchorX = 0; Content.AnchorY = 0; } if (e.Status == GestureStatus.Running) { // Calculate the scale factor to be applied. currentScale += (e.Scale - 1) * startScale; currentScale = Math.Max(1, currentScale); // The ScaleOrigin is in relative coordinates to the wrapped user interface element, // so get the X pixel coordinate. double renderedX = Content.X + xOffset; double deltaX = renderedX / Width; double deltaWidth = Width / (Content.Width * startScale); double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth; // The ScaleOrigin is in relative coordinates to the wrapped user interface element, // so get the Y pixel coordinate. double renderedY = Content.Y + yOffset; double deltaY = renderedY / Height; double deltaHeight = Height / (Content.Height * startScale); double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight; // Calculate the transformed element pixel coordinates. double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale); double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale); // Apply translation based on the change in origin. Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0); Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0); // Apply scale factor Content.Scale = currentScale; } if (e.Status == GestureStatus.Completed) { // Store the translation delta's of the wrapped user interface element. xOffset = Content.TranslationX; yOffset = Content.TranslationY; } } } This the Clamp method: public static class DoubleExtensions { public static double Clamp(this double self, double min, double max) { return Math.Min(max, Math.Max(self, min)); } } And this is the image in my XAML file: <StackLayout Grid.Row="0" Grid.Column="0" Grid.ColumnSpan="2" Grid.RowSpan="3"> <ScrollView x:Name="imageScroll" Orientation="Both" VerticalOptions="FillAndExpand"> <entry:PinchToZoomContainer> <entry:PinchToZoomContainer.Content> <Image x:Name="zoomImage" Source="{images:EmbeddedImage Vebko.Images.CB_Edite_Vb1_2.jpg}" Margin="0,0,0,10" HorizontalOptions="Center" VerticalOptions="Center"/> </entry:PinchToZoomContainer.Content> </entry:PinchToZoomContainer> </ScrollView> </StackLayout> Can anybody please help?
What i guess is that while you scale the Content, the PinchToZoomContainer : ContentView size remains the same that is why scrollview doesn't react = change control's size with height/width requests in code when its content gets scaled.