This commit is contained in:
Đăng Nguyễn
2025-10-03 11:37:39 +07:00
parent c5686e4ecf
commit 511614df72
6 changed files with 44 additions and 124 deletions

View File

@@ -164,23 +164,23 @@
{
const double zoomFactor = 0.15;
double oldZoom = ZoomScale;
ZoomScale = Math.Min(MAX_ZOOM, ZoomScale * (1 + zoomFactor));
if (Math.Abs(ZoomScale - oldZoom) < 0.001) return;
await ZoomAtCenter(oldZoom);
}
private async Task ZoomOut()
{
const double zoomFactor = 0.15;
const double zoomFactor = 0.15;
double oldZoom = ZoomScale;
ZoomScale = Math.Max(MIN_ZOOM, ZoomScale * (1 - zoomFactor));
if (Math.Abs(ZoomScale - oldZoom) < 0.001) return;
await ZoomAtCenter(oldZoom);
}
@@ -188,22 +188,22 @@
{
double centerX = CanvasWidth / 2;
double centerY = CanvasHeight / 2;
double centerWorldX = (centerX - CanvasTranslateX) / oldZoom / BASE_PIXELS_PER_METER - OriginX;
double centerWorldY = (centerY - CanvasTranslateY) / oldZoom / BASE_PIXELS_PER_METER - OriginY;
double newCenterCanvasX = (centerWorldX + OriginX) * BASE_PIXELS_PER_METER * ZoomScale;
double newCenterCanvasY = (centerWorldY + OriginY) * BASE_PIXELS_PER_METER * ZoomScale;
CanvasTranslateX = centerX - newCenterCanvasX;
CanvasTranslateY = centerY - newCenterCanvasY;
if (IsMouseInCanvas)
{
WorldMouseX = CanvasToWorldX(MouseX);
WorldMouseY = CanvasToWorldY(MouseY);
}
StateHasChanged();
await DrawCanvas();
}
@@ -268,23 +268,26 @@
private async Task HandleTouchMove(TouchEventArgs e)
{
if (e.Touches.Length == 1)
if (IsTouching)
{
await HandleSingleTouchMove(e.Touches[0]);
if (e.Touches.Length == 1)
{
await HandleSingleTouchMove(e.Touches[0]);
}
else if (e.Touches.Length == 2)
{
await HandlePinchZoom(e.Touches[0], e.Touches[1]);
}
StateHasChanged();
await DrawCanvas();
}
else if (e.Touches.Length == 2)
{
await HandlePinchZoom(e.Touches[0], e.Touches[1]);
}
StateHasChanged();
await DrawCanvas();
}
private void HandleTouchStart(TouchEventArgs e)
{
IsTouching = true;
if (e.Touches.Length == 1)
{
LastTouchPoint = new TouchPoint
@@ -305,7 +308,7 @@
X = e.Touches[1].ClientX,
Y = e.Touches[1].ClientY
};
LastTouchDistance = CalculateTouchDistance(LastTouchPoint, LastSecondTouchPoint);
}
}
@@ -321,26 +324,26 @@
private async Task HandleSingleTouchMove(Microsoft.AspNetCore.Components.Web.TouchPoint touch)
{
if (LastTouchPoint == null) return;
var currentPoint = new TouchPoint
{
X = touch.ClientX,
Y = touch.ClientY
};
double deltaX = currentPoint.X - LastTouchPoint.X;
double deltaY = currentPoint.Y - LastTouchPoint.Y;
CanvasTranslateX += deltaX;
CanvasTranslateY += deltaY;
LastTouchPoint = currentPoint;
var canvasRect = await JS.InvokeAsync<DomRect>("getElementBoundingRect", CanvasRef);
MouseX = currentPoint.X - canvasRect.X;
MouseY = currentPoint.Y - canvasRect.Y;
IsMouseInCanvas = true;
WorldMouseX = CanvasToWorldX(MouseX);
WorldMouseY = CanvasToWorldY(MouseY);
}
@@ -348,32 +351,32 @@
private async Task HandlePinchZoom(Microsoft.AspNetCore.Components.Web.TouchPoint touch1, Microsoft.AspNetCore.Components.Web.TouchPoint touch2)
{
if (LastTouchPoint == null || LastSecondTouchPoint == null) return;
var currentTouch1 = new TouchPoint { X = touch1.ClientX, Y = touch1.ClientY };
var currentTouch2 = new TouchPoint { X = touch2.ClientX, Y = touch2.ClientY };
double currentDistance = CalculateTouchDistance(currentTouch1, currentTouch2);
if (LastTouchDistance > 0)
{
double distanceRatio = currentDistance / LastTouchDistance;
double oldZoom = ZoomScale;
ZoomScale = Math.Max(MIN_ZOOM, Math.Min(MAX_ZOOM, ZoomScale * distanceRatio));
if (Math.Abs(ZoomScale - oldZoom) > 0.001)
{
double centerX = (currentTouch1.X + currentTouch2.X) / 2;
double centerY = (currentTouch1.Y + currentTouch2.Y) / 2;
var canvasRect = await JS.InvokeAsync<DomRect>("getElementBoundingRect", CanvasRef);
double canvasCenterX = centerX - canvasRect.X;
double canvasCenterY = centerY - canvasRect.Y;
ZoomAtPoint(oldZoom, canvasCenterX, canvasCenterY);
}
}
LastTouchDistance = currentDistance;
LastTouchPoint = currentTouch1;
LastSecondTouchPoint = currentTouch2;
@@ -390,10 +393,10 @@
{
double pointWorldX = (pointX - CanvasTranslateX) / oldZoom / BASE_PIXELS_PER_METER - OriginX;
double pointWorldY = (pointY - CanvasTranslateY) / oldZoom / BASE_PIXELS_PER_METER - OriginY;
double newPointCanvasX = (pointWorldX + OriginX) * BASE_PIXELS_PER_METER * ZoomScale;
double newPointCanvasY = (pointWorldY + OriginY) * BASE_PIXELS_PER_METER * ZoomScale;
CanvasTranslateX = pointX - newPointCanvasX;
CanvasTranslateY = pointY - newPointCanvasY;
}