【问题标题】:C# How to zoom to cursor location using SkiaSharp control?C# 如何使用 SkiaSharp 控件缩放到光标位置?
【发布时间】:2020-10-15 09:39:17
【问题描述】:

我有一个 2D 地图,我想根据光标 X 和 Y 坐标进行放大。目前我有一些工作代码,但如果我在初始缩放后将光标移动到新位置,下一次缩放会稍微关闭。我一直试图弄清楚这一点,但我无法理解数学。它可能很简单,我只是无法想象正确的方法。

示例代码。

    float ZoomMax = 7f;
    float ZoomMin = 1f;

    private float[] MapPan = new float[] { 0, 0 };
    private float MapScale = 1f;

    protected override void OnMouseWheel(MouseEventArgs e)
    {
        var coordinates = panelMap.PointToClient(Cursor.Position);

        if (e.Delta > 0)
        {
            if (MapScale < ZoomMax)
            {
                MapScale += 0.2f;
                ZoomToMouse(coordinates.X, coordinates.Y);
            }
            else
            {
                MapScale = ZoomMax;
            }
        }
        else if (e.Delta < 0)
        {
            if (MapScale > ZoomMin)
            {
                MapScale -= 0.2f;
                ZoomToMouse(coordinates.X, coordinates.Y);
            }
            else
            {
                MapPan[0] = 0;
                MapPan[1] = 0;
                MapScale = ZoomMin;
            }
        }
    }


    private void ZoomToMouse(int x, int y)
    {
        float xScaled = x * MapScale;
        float xScaled = y * MapScale;

        float X = x - xScaled;
        float Y = y - yScaled;

        MapPan[0] = X / MapScale;
        MapPan[1] = Y / MapScale;
    }

    private void map_PaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
    {
        SKCanvas skCanvas = e.Surface.Canvas;

        skCanvas.Scale(MapScale);
        skCanvas.Translate(MapPan[0], MapPan[1]);

        using(SKPaint skPaint = new SKPaint())
        {
            skCanvas.DrawText("Hello", 0, 0, skPaint);
        }          
    }

【问题讨论】:

    标签: c# zooming pan skiasharp


    【解决方案1】:

    如果有人遇到类似问题,这就是我想出的。

        private float ZoomMax = 7f;
        private float ZoomMin = 1f;
    
        private PointF MapPan = new PointF(0, 0);
        private float MapScale = 1f;
    
        protected override void OnMouseWheel(MouseEventArgs e)
        {
            if (FindControlAtCursor(this) != map) { return; }
    
            var coordinates = map.PointToClient(Cursor.Position);
           
            float ScrollDelta = e.Delta * 0.002f;
    
            float prevScale = MapScale;
            MapScale = Clamp(MapScale + ScrollDelta,ZoomMin,ZoomMax);
    
            ZoomToMouse(coordinates, prevScale);
        }
    
        public static Control FindControlAtCursor(Form form)
        {
            Point pos = Cursor.Position;
            if (form.Bounds.Contains(pos))
                return FindControlAtPoint(form, form.PointToClient(pos));
            return null;
        }
    
        public static float Clamp(float value, float min, float max)
        {
            return (value < min) ? min : (value > max) ? max : value;
        }
    
        private void ZoomToMouse(PointF Mouse, float PreviousScale)
        {
            PointF TranslatedMouse = new PointF((Mouse.X / PreviousScale) - MapPan.X, (Mouse.Y / PreviousScale) - MapPan.Y);
    
            PointF ScaledMouse = new PointF(TranslatedMouse.X * MapScale, TranslatedMouse.Y * MapScale);
            PointF NewPosition = new PointF((Mouse.X - ScaledMouse.X) / MapScale, (Mouse.Y - ScaledMouse.Y) / MapScale);
    
            float currentWidth = map.Width * MapScale;
            float currentHeight = map.Height * MapScale;
    
            float diffX = (currentWidth - map.Width) / MapScale;
            float diffY = (currentHeight - map.Height) / MapScale;
    
            MapPan.X = Clamp(NewPosition.X, -diffX, 0);
            MapPan.Y = Clamp(NewPosition.Y, -diffY, 0);
        }
    
        private void map_PaintSurface(object sender, SKPaintGLSurfaceEventArgs e)
        {
            SKCanvas skCanvas = e.Surface.Canvas;
    
            skCanvas.Scale(MapScale);
            skCanvas.Translate(MapPan.X, MapPan.Y);
    
            using (SKPaint skPaint = new SKPaint())
            {
                skCanvas.DrawText("Hello", 0, 0, skPaint);
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      • 2015-05-29
      • 1970-01-01
      • 1970-01-01
      • 2014-09-05
      • 1970-01-01
      相关资源
      最近更新 更多