Skip to content

Commit

Permalink
Standardizing on precision
Browse files Browse the repository at this point in the history
  • Loading branch information
brett-estabrook committed Nov 1, 2021
1 parent 0819d5d commit f6fd13a
Showing 1 changed file with 21 additions and 23 deletions.
44 changes: 21 additions & 23 deletions Sources/Microcharts.Forms/ChartView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ private void OnPaintCanvas(object sender, SKPaintSurfaceEventArgs e)
}


double zoomCurScale = 1;
double zoomStartScale = 1;
float zoomCurScale = 1;
float zoomStartScale = 1;

SKPoint zoomStartOrigin = new SKPoint(0, 0);
SKPoint zoomStartOffset = new SKPoint(0, 0);
Expand All @@ -115,45 +115,43 @@ void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)

if (e.Status == GestureStatus.Running)
{
float eScale = (float)e.Scale;
SKPoint eScaleOrigin = new SKPoint((float)e.ScaleOrigin.X, (float)e.ScaleOrigin.Y);
SKPoint canvasSize = new SKPoint(CanvasSize.Width, CanvasSize.Height);

// e.Scale is the delta to be applied for the current frame
// Calculate the scale factor to be applied.
zoomCurScale += (e.Scale - 1) * zoomStartScale;
zoomCurScale += (eScale - 1) * zoomStartScale;
zoomCurScale = Math.Max(1, zoomCurScale);
zoomCurScale = Math.Min(5, zoomCurScale);


SKPoint zoomPanDelta = new SKPoint((float)((e.ScaleOrigin.X - zoomStartOrigin.X) * CanvasSize.Width), (float)((e.ScaleOrigin.Y - zoomStartOrigin.Y) * CanvasSize.Height));
SKPoint zoomPanDelta = new SKPoint((eScaleOrigin.X - zoomStartOrigin.X) * CanvasSize.Width, (eScaleOrigin.Y - zoomStartOrigin.Y) * CanvasSize.Height);

// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the X pixel coordinate.
double renderedX = X + zoomStartOffset.X;
double deltaX = renderedX / CanvasSize.Width;
double deltaWidth = CanvasSize.Width / (CanvasSize.Width * zoomStartScale);
double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;
float renderedX = (float)X + zoomStartOffset.X;
float deltaX = renderedX / CanvasSize.Width;
float deltaWidth = CanvasSize.Width / (CanvasSize.Width * zoomStartScale);
float originX = (eScaleOrigin.X - deltaX) * deltaWidth;

//Console.WriteLine("ScaleOrigin: {0}, {1}", e.ScaleOrigin.X, e.ScaleOrigin.Y);

// The ScaleOrigin is in relative coordinates to the wrapped user interface element,
// so get the Y pixel coordinate.
double renderedY = Y + zoomStartOffset.Y;
double deltaY = renderedY / CanvasSize.Height;
double deltaHeight = CanvasSize.Height / (CanvasSize.Height * zoomStartScale);
double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;
float renderedY = (float)Y + zoomStartOffset.Y;
float deltaY = renderedY / CanvasSize.Height;
float deltaHeight = CanvasSize.Height / (CanvasSize.Height * zoomStartScale);
float originY = (eScaleOrigin.Y - deltaY) * deltaHeight;

// Calculate the transformed element pixel coordinates.
double targetX = zoomStartOffset.X - (originX * CanvasSize.Width) * (zoomCurScale - zoomStartScale);
double targetY = zoomStartOffset.Y - (originY * CanvasSize.Height) * (zoomCurScale - zoomStartScale);

// Apply translation based on the change in origin.
zoomTranslation.X = (float)targetX;
zoomTranslation.Y = (float)targetY;

zoomTranslation.X = zoomStartOffset.X - (originX * CanvasSize.Width) * (zoomCurScale - zoomStartScale);
zoomTranslation.Y = zoomStartOffset.Y - (originY * CanvasSize.Height) * (zoomCurScale - zoomStartScale);

// Calculate final translation with pan, and clamp the whole thing
SKPoint final = zoomTranslation + zoomPanDelta;
final.X = Math.Min(Math.Max(final.X, -CanvasSize.Width * (float)(zoomCurScale - 1)), 0);
final.Y = Math.Min(Math.Max(final.Y, -CanvasSize.Height * (float)(zoomCurScale - 1)), 0);
final.X = Math.Min(Math.Max(final.X, -CanvasSize.Width * (zoomCurScale - 1)), 0);
final.Y = Math.Min(Math.Max(final.Y, -CanvasSize.Height * (zoomCurScale - 1)), 0);


axisChart.XForm.Scale = (float)zoomCurScale;
axisChart.XForm.Offset = final;
InvalidateSurface();
Expand Down

0 comments on commit f6fd13a

Please sign in to comment.