-
Goal: Current method: protected override async Task OnInitializedAsync()
{
BlazorDiagram.Options.Zoom.Enabled = false;
BlazorDiagram.Wheel += BlazorDiagram_Wheel;
}
private void BlazorDiagram_Wheel(Blazor.Diagrams.Core.Events.WheelEventArgs obj)
{
var zoom = BlazorDiagram.Zoom;
if (obj.DeltaY < 0)
zoom += 0.05d;
else
zoom = (Math.Max(zoom - 0.05d, 0.01d));
BlazorDiagram.SetZoom(zoom);
} This code only handles "custom" zooming for now. Issue: |
Beta Was this translation helpful? Give feedback.
Answered by
rjtwins
Sep 26, 2023
Replies: 1 comment
-
After looking at the source code i've done this: private void BlazorDiagram_Wheel(Blazor.Diagrams.Core.Events.WheelEventArgs e)
{
if (e.ShiftKey)
return;
if (e.AltKey)
return;
if (BlazorDiagram.Container == null || e.DeltaY == 0)
return;
var scale = BlazorDiagram.Options.Zoom.ScaleFactor;
var oldZoom = BlazorDiagram.Zoom;
var deltaY = BlazorDiagram.Options.Zoom.Inverse ? e.DeltaY * -1 : e.DeltaY;
var newZoom = deltaY > 0 ? oldZoom * scale : oldZoom / scale;
newZoom = Math.Clamp(newZoom, BlazorDiagram.Options.Zoom.Minimum, BlazorDiagram.Options.Zoom.Maximum);
if (newZoom < 0 || newZoom == BlazorDiagram.Zoom)
return;
// Other algorithms (based only on the changes in the zoom) don't work for our case
// This solution is taken as is from react-diagrams (ZoomCanvasAction)
var clientWidth = BlazorDiagram.Container.Width;
var clientHeight = BlazorDiagram.Container.Height;
var widthDiff = clientWidth * newZoom - clientWidth * oldZoom;
var heightDiff = clientHeight * newZoom - clientHeight * oldZoom;
var clientX = e.ClientX - BlazorDiagram.Container.Left;
var clientY = e.ClientY - BlazorDiagram.Container.Top;
var xFactor = (clientX - BlazorDiagram.Pan.X) / oldZoom / clientWidth;
var yFactor = (clientY - BlazorDiagram.Pan.Y) / oldZoom / clientHeight;
var newPanX = BlazorDiagram.Pan.X - widthDiff * xFactor;
var newPanY = BlazorDiagram.Pan.Y - heightDiff * yFactor;
BlazorDiagram.Batch(() =>
{
BlazorDiagram.SetPan(newPanX, newPanY);
BlazorDiagram.SetZoom(newZoom);
});
} |
Beta Was this translation helpful? Give feedback.
0 replies
Answer selected by
rjtwins
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
After looking at the source code i've done this:
Where the pan will be updated where the guard clauses are now.