void SetCanvasOrigin(Point clientPosition) { if (Canvas == null) { return; } // get the minimap client area, in pixels, without borders and padding var clientArea = new Rect(0, 0, Width, Height); clientArea.Inflate(-TotalPadding, -TotalPadding); // clamp the mouse within the client area clientPosition = clientArea.Clamp(new Vector(clientPosition)).ToPoint(); // get the mouse position as a percentage of the client area size var x = (clientPosition.X - TotalPadding) / clientArea.Width; var y = (clientPosition.Y - TotalPadding) / clientArea.Height; // get the visible area on the canvas, in canvas coordinates var viewport = Canvas.Viewport; var canvasBounds = Canvas.ComputeCanvasBounds(false); // limit it to the rectangle in which the center of the viewport may be placed whilst rendering only the occupied portions of the canvas visible var restrictedBounds = canvasBounds; if (restrictedBounds.Width > viewport.Width) { restrictedBounds.Inflate(-viewport.Width / 2, 0); } else { restrictedBounds.X += restrictedBounds.Width / 2; restrictedBounds.Width = 0; } if (restrictedBounds.Height > viewport.Height) { restrictedBounds.Inflate(0, -viewport.Height / 2); } else { restrictedBounds.Y += restrictedBounds.Height / 2; restrictedBounds.Height = 0; } // center the canvas on the mouse position in canvas coordinates, limited to the above rectangle Canvas.Origin = restrictedBounds.Clamp(new Vector(canvasBounds.Left + canvasBounds.Width * x, canvasBounds.Top + canvasBounds.Height * y)); }