RobotApp/RobotApp.Client/Pages/Components/Mapping/MapView.razor
Đăng Nguyễn 3b44ea6d8d update ruller
2025-10-02 11:02:44 +07:00

483 lines
18 KiB
Plaintext

@inject IJSRuntime JS
@using Excubo.Blazor.Canvas.Contexts
<div class="view">
<div class="toolbar">
<MudTooltip Text="Start Localization" role="button" Placement="Placement.Bottom" Color="Color.Info">
<button type="button" class="btn btn-secondary action-button" disabled="@(false)">
<i class="mdi mdi-play icon-button"></i>
</button>
</MudTooltip>
<MudTooltip Text="Stop Localization" role="button" Placement="Placement.Bottom" Color="Color.Info">
<button type="button" class="btn btn-secondary action-button" disabled="@(true)">
<i class="mdi mdi-pause icon-button"></i>
</button>
</MudTooltip>
<MudTooltip Text="Reset View" role="button" Placement="Placement.Bottom" Color="Color.Info">
<button type="button" class="btn btn-secondary action-button" @onclick="ResetView">
<i class="mdi mdi-restore icon-button"></i>
</button>
</MudTooltip>
<div class="ms-auto d-flex align-items-center">
<div class="zoom-info-container">
<small class="zoom-info">
<span class="info-item">
<i class="mdi mdi-magnify"></i>
Zoom: @($"{ZoomScale:F2}x")
</span>
<span class="info-separator">|</span>
<span class="info-item">
<i class="mdi mdi-crosshairs-gps"></i>
Mouse: (@($"{MouseX:F0}"), @($"{MouseY:F0}"))
</span>
<span class="info-separator">|</span>
<span class="info-item">
<i class="mdi mdi-map-marker"></i>
World: (@($"{WorldMouseX:F2}m"), @($"{WorldMouseY:F2}m"))
</span>
<span class="info-separator">|</span>
<span class="info-item">
<i class="mdi mdi-map-marker"></i>
Translate: (@($"{CanvasTranslateX:F2}"), @($"{CanvasTranslateY:F2}"))
</span>
</small>
</div>
</div>
</div>
<div @ref="ViewContainerRef" class="d-flex position-relative w-100 flex-grow-1 overflow-hidden">
<canvas @ref="CanvasRef"
@onwheel="HandleWheel"
@onwheel:preventDefault="true"
@onmousemove="HandleMouseMove"
@onmouseleave="HandleMouseLeave"
style="display: block; cursor: crosshair; transform: scale(1, -1)"></canvas>
</div>
</div>
@code {
private ElementReference CanvasRef;
private ElementReference ViewContainerRef;
private double ZoomScale = 1.0;
private const double MIN_ZOOM = 0.1;
private const double MAX_ZOOM = 5.0;
private const double BASE_PIXELS_PER_METER = 50.0;
private bool IsMouseInCanvas = false;
private double MouseX;
private double MouseY;
private double OriginX = 0;
private double OriginY = 0;
private double WorldMouseX;
private double WorldMouseY;
private double CanvasWidth;
private double CanvasHeight;
private double CanvasTranslateX = 0;
private double CanvasTranslateY = 0;
private const double RulerHeight = 20;
protected override async Task OnAfterRenderAsync(bool first_render)
{
await base.OnAfterRenderAsync(first_render);
if (!first_render) return;
var parentSize = await JS.InvokeAsync<DomRect>("getElementSize", ViewContainerRef);
CanvasWidth = parentSize.Width;
CanvasHeight = parentSize.Height;
await JS.InvokeVoidAsync("setCanvasSize", CanvasRef, CanvasWidth, CanvasHeight);
CanvasTranslateX = CanvasWidth / 2;
CanvasTranslateY = CanvasHeight / 2;
await DrawCanvas();
}
private async Task DrawCanvas()
{
await using var ctx = await JS.GetContext2DAsync(CanvasRef);
await ctx.ClearRectAsync(0, 0, CanvasWidth, CanvasHeight);
// Draw rulers first (outside transform)
await DrawRulers(ctx);
await ctx.SaveAsync();
await ctx.TranslateAsync(CanvasTranslateX, CanvasTranslateY);
await ctx.ScaleAsync(ZoomScale, ZoomScale);
await DrawGrid(ctx);
await DrawAxes(ctx);
await ctx.RestoreAsync();
}
private async Task DrawRulers(Context2D ctx)
{
double visibleWorldLeft = CanvasToWorldX(0);
double visibleWorldRight = CanvasToWorldX(CanvasWidth);
double visibleWorldTop = CanvasToWorldY(0);
double visibleWorldBottom = CanvasToWorldY(CanvasHeight);
double scaleInterval = GetRulerScaleInterval();
await DrawXRuler(ctx, RulerHeight, visibleWorldLeft, visibleWorldRight, scaleInterval);
await DrawYRuler(ctx, RulerHeight, visibleWorldTop, visibleWorldBottom, scaleInterval);
}
private double GetRulerScaleInterval()
{
double pixelsPerMeter = BASE_PIXELS_PER_METER * ZoomScale;
if (pixelsPerMeter >= 400) return 0.1;
else if (pixelsPerMeter >= 200) return 0.2;
else if (pixelsPerMeter >= 100) return 0.5;
else if (pixelsPerMeter >= 50) return 1.0;
else if (pixelsPerMeter >= 25) return 2.0;
else if (pixelsPerMeter >= 12) return 5.0;
else if (pixelsPerMeter >= 6) return 10.0;
else return 20.0;
}
private async Task DrawXRuler(Context2D ctx, double rulerHeight, double visibleWorldLeft, double visibleWorldRight, double scaleInterval)
{
await ctx.FillStyleAsync("rgba(240, 240, 240, 0.9)");
await ctx.FillRectAsync(0, 0, CanvasWidth, rulerHeight);
await ctx.StrokeStyleAsync("rgba(100, 100, 100, 0.8)");
await ctx.LineWidthAsync(1);
await ctx.BeginPathAsync();
await ctx.MoveToAsync(0, rulerHeight);
await ctx.LineToAsync(CanvasWidth, rulerHeight);
await ctx.StrokeAsync();
double startWorld = Math.Floor(visibleWorldLeft / scaleInterval) * scaleInterval;
double endWorld = Math.Ceiling(visibleWorldRight / scaleInterval) * scaleInterval;
startWorld -= scaleInterval;
endWorld += scaleInterval;
for (double worldX = startWorld; worldX <= endWorld; worldX += scaleInterval)
{
double canvasX = WorldToCanvasX(worldX);
if (canvasX < -50 || canvasX > CanvasWidth + 50) continue;
bool isMajorTick = IsNearMultiple(worldX, scaleInterval * 2) || Math.Abs(worldX) < 0.001;
double tickHeight = isMajorTick ? rulerHeight * 0.4 : rulerHeight * 0.2;
await ctx.StrokeStyleAsync("rgba(60, 60, 60, 0.8)");
await ctx.LineWidthAsync(1);
await ctx.BeginPathAsync();
await ctx.MoveToAsync(canvasX, rulerHeight);
await ctx.LineToAsync(canvasX, rulerHeight - tickHeight);
await ctx.StrokeAsync();
if (isMajorTick && canvasX >= -20 && canvasX <= CanvasWidth + 20)
{
await ctx.SaveAsync();
await ctx.TranslateAsync(canvasX, rulerHeight - tickHeight - 8);
await ctx.ScaleAsync(1, -1);
await ctx.FillStyleAsync("blue");
await ctx.FontAsync("bold 10px Arial");
await ctx.TextAlignAsync(TextAlign.Center);
string labelText = FormatRulerLabel(worldX, scaleInterval);
await ctx.FillTextAsync(labelText, 0, 0);
await ctx.RestoreAsync();
}
}
}
private async Task DrawYRuler(Context2D ctx, double rulerWidth, double visibleWorldTop, double visibleWorldBottom, double scaleInterval)
{
await ctx.FillStyleAsync("rgba(240, 240, 240, 0.9)");
await ctx.FillRectAsync(0, 0, rulerWidth, CanvasHeight);
await ctx.StrokeStyleAsync("rgba(100, 100, 100, 0.8)");
await ctx.LineWidthAsync(1);
await ctx.BeginPathAsync();
await ctx.MoveToAsync(rulerWidth, 0);
await ctx.LineToAsync(rulerWidth, CanvasHeight);
await ctx.StrokeAsync();
double startWorld = Math.Floor(visibleWorldTop / scaleInterval) * scaleInterval;
double endWorld = Math.Ceiling(visibleWorldBottom / scaleInterval) * scaleInterval;
startWorld -= scaleInterval;
endWorld += scaleInterval;
for (double worldY = startWorld; worldY <= endWorld; worldY += scaleInterval)
{
double canvasY = WorldToCanvasY(worldY);
if (canvasY < -50 || canvasY > CanvasHeight + 50) continue;
bool isMajorTick = IsNearMultiple(worldY, scaleInterval * 2) || Math.Abs(worldY) < 0.001;
double tickWidth = isMajorTick ? rulerWidth * 0.4 : rulerWidth * 0.2;
await ctx.StrokeStyleAsync("rgba(60, 60, 60, 0.8)");
await ctx.LineWidthAsync(1);
await ctx.BeginPathAsync();
await ctx.MoveToAsync(rulerWidth, canvasY);
await ctx.LineToAsync(rulerWidth - tickWidth, canvasY);
await ctx.StrokeAsync();
if (isMajorTick && canvasY >= -20 && canvasY <= CanvasHeight + 20)
{
await ctx.SaveAsync();
await ctx.TranslateAsync(rulerWidth - tickWidth - 2, canvasY);
await ctx.ScaleAsync(1, -1);
await ctx.RotateAsync(-Math.PI / 2);
await ctx.FillStyleAsync("blue");
await ctx.FontAsync("bold 10px Arial");
await ctx.TextAlignAsync(TextAlign.Center);
string labelText = FormatRulerLabel(worldY, scaleInterval);
await ctx.FillTextAsync(labelText, 0, 0);
await ctx.RestoreAsync();
}
}
}
private bool IsNearMultiple(double value, double multiple)
{
if (multiple == 0) return false;
double remainder = Math.Abs(value % multiple);
double epsilon = multiple * 0.001;
return remainder < epsilon || remainder > multiple - epsilon;
}
private string FormatRulerLabel(double worldValue, double scaleInterval)
{
if (scaleInterval < 1.0)
{
return $"{worldValue:F1}m";
}
else if (scaleInterval < 10.0)
{
return $"{worldValue:F0}m";
}
else
{
return $"{worldValue:F0}m";
}
}
private async Task DrawAxes(Context2D ctx)
{
double originCanvasX = OriginX * BASE_PIXELS_PER_METER;
double originCanvasY = OriginY * BASE_PIXELS_PER_METER;
await ctx.FillStyleAsync("red");
await ctx.BeginPathAsync();
await ctx.ArcAsync(originCanvasX, originCanvasY, 10 / ZoomScale, 0, Math.PI * 2);
await ctx.FillAsync(FillRule.NonZero);
await ctx.LineWidthAsync(2 / ZoomScale);
await ctx.StrokeStyleAsync("blue");
await ctx.BeginPathAsync();
await ctx.MoveToAsync(-CanvasWidth / ZoomScale, originCanvasY);
await ctx.LineToAsync(CanvasWidth / ZoomScale, originCanvasY);
await ctx.StrokeAsync();
await ctx.StrokeStyleAsync("red");
await ctx.BeginPathAsync();
await ctx.MoveToAsync(originCanvasX, -CanvasHeight / ZoomScale);
await ctx.LineToAsync(originCanvasX, CanvasHeight / ZoomScale);
await ctx.StrokeAsync();
double gridSpacingMeters = GetGridSpacingMeters();
double arrowLength = gridSpacingMeters * BASE_PIXELS_PER_METER;
double arrowHeadSize = 16 / ZoomScale;
await ctx.FillStyleAsync("blue");
await ctx.StrokeStyleAsync("blue");
await ctx.LineWidthAsync(4 / ZoomScale);
await ctx.BeginPathAsync();
await ctx.MoveToAsync(originCanvasX, originCanvasY);
await ctx.LineToAsync(originCanvasX + arrowLength, originCanvasY);
await ctx.StrokeAsync();
await ctx.BeginPathAsync();
await ctx.MoveToAsync(originCanvasX + arrowLength, originCanvasY);
await ctx.LineToAsync(originCanvasX + arrowLength - arrowHeadSize, originCanvasY - arrowHeadSize / 2);
await ctx.LineToAsync(originCanvasX + arrowLength - arrowHeadSize, originCanvasY + arrowHeadSize / 2);
await ctx.ClosePathAsync();
await ctx.FillAsync(FillRule.NonZero);
await ctx.FillStyleAsync("red");
await ctx.StrokeStyleAsync("red");
await ctx.LineWidthAsync(4 / ZoomScale);
await ctx.BeginPathAsync();
await ctx.MoveToAsync(originCanvasX, originCanvasY);
await ctx.LineToAsync(originCanvasX, originCanvasY + arrowLength);
await ctx.StrokeAsync();
await ctx.BeginPathAsync();
await ctx.MoveToAsync(originCanvasX, originCanvasY + arrowLength);
await ctx.LineToAsync(originCanvasX - arrowHeadSize / 2, originCanvasY + arrowLength - arrowHeadSize);
await ctx.LineToAsync(originCanvasX + arrowHeadSize / 2, originCanvasY + arrowLength - arrowHeadSize);
await ctx.ClosePathAsync();
await ctx.FillAsync(FillRule.NonZero);
}
private async Task DrawGrid(Context2D ctx)
{
await ctx.StrokeStyleAsync("rgba(200, 200, 200, 0.4)");
await ctx.LineWidthAsync(1 / ZoomScale);
await ctx.SetLineDashAsync(new double[] { 5 / ZoomScale, 5 / ZoomScale });
double gridSpacingMeters = GetGridSpacingMeters();
double gridSpacingPixels = gridSpacingMeters * BASE_PIXELS_PER_METER;
double visibleLeft = -CanvasTranslateX / ZoomScale;
double visibleRight = (CanvasWidth - CanvasTranslateX) / ZoomScale;
double visibleTop = -CanvasTranslateY / ZoomScale;
double visibleBottom = (CanvasHeight - CanvasTranslateY) / ZoomScale;
double startX = Math.Floor(visibleLeft / gridSpacingPixels) * gridSpacingPixels;
double startY = Math.Floor(visibleTop / gridSpacingPixels) * gridSpacingPixels;
for (double x = startX; x <= visibleRight; x += gridSpacingPixels)
{
await ctx.BeginPathAsync();
await ctx.MoveToAsync(x, visibleTop);
await ctx.LineToAsync(x, visibleBottom);
await ctx.StrokeAsync();
}
for (double y = startY; y <= visibleBottom; y += gridSpacingPixels)
{
await ctx.BeginPathAsync();
await ctx.MoveToAsync(visibleLeft, y);
await ctx.LineToAsync(visibleRight, y);
await ctx.StrokeAsync();
}
await ctx.SetLineDashAsync(new double[] { });
}
private double GetGridSpacingMeters()
{
double PixelsPerMeter = BASE_PIXELS_PER_METER * ZoomScale;
if (PixelsPerMeter >= 300) return 0.2;
else if (PixelsPerMeter >= 150) return 0.5;
else if (PixelsPerMeter >= 75) return 1.0;
else if (PixelsPerMeter >= 40) return 2.0;
else if (PixelsPerMeter >= 20) return 5.0;
else if (PixelsPerMeter >= 10) return 10.0;
else return 20.0;
}
private double CanvasToWorldX(double canvasX)
{
return (canvasX - CanvasTranslateX) / ZoomScale / BASE_PIXELS_PER_METER - OriginX;
}
private double CanvasToWorldY(double canvasY)
{
return (canvasY - CanvasTranslateY) / ZoomScale / BASE_PIXELS_PER_METER - OriginY;
}
private double WorldToCanvasX(double worldX)
{
return (worldX + OriginX) * BASE_PIXELS_PER_METER * ZoomScale + CanvasTranslateX;
}
private double WorldToCanvasY(double worldY)
{
return (worldY + OriginY) * BASE_PIXELS_PER_METER * ZoomScale + CanvasTranslateY;
}
private async Task HandleMouseMove(MouseEventArgs e)
{
MouseX = e.OffsetX;
MouseY = e.OffsetY;
IsMouseInCanvas = true;
WorldMouseX = CanvasToWorldX(MouseX);
WorldMouseY = CanvasToWorldY(MouseY);
StateHasChanged();
if (e.Buttons == 4)
{
CanvasTranslateX += e.MovementX;
CanvasTranslateY -= e.MovementY;
await DrawCanvas();
}
}
private async Task HandleMouseLeave(MouseEventArgs e)
{
IsMouseInCanvas = false;
MouseX = 0;
MouseY = 0;
StateHasChanged();
await DrawCanvas();
}
private async Task HandleWheel(WheelEventArgs e)
{
if (e.Buttons == 4) return;
const double zoomFactor = 0.1;
double oldZoom = ZoomScale;
if (e.DeltaY < 0) ZoomScale = Math.Min(MAX_ZOOM, ZoomScale * (1 + zoomFactor));
else ZoomScale = Math.Max(MIN_ZOOM, ZoomScale * (1 - zoomFactor));
if (Math.Abs(ZoomScale - oldZoom) < 0.001) return;
MouseX = e.OffsetX;
MouseY = e.OffsetY;
double zoomPointWorldX = (MouseX - CanvasTranslateX) / oldZoom / BASE_PIXELS_PER_METER - OriginX;
double zoomPointWorldY = (MouseY - CanvasTranslateY) / oldZoom / BASE_PIXELS_PER_METER - OriginY;
double newZoomPointCanvasX = (zoomPointWorldX + OriginX) * BASE_PIXELS_PER_METER * ZoomScale;
double newZoomPointCanvasY = (zoomPointWorldY + OriginY) * BASE_PIXELS_PER_METER * ZoomScale;
CanvasTranslateX = MouseX - newZoomPointCanvasX;
CanvasTranslateY = MouseY - newZoomPointCanvasY;
WorldMouseX = CanvasToWorldX(MouseX);
WorldMouseY = CanvasToWorldY(MouseY);
StateHasChanged();
await DrawCanvas();
}
private async Task ResetView()
{
CanvasTranslateX = CanvasWidth / 2;
CanvasTranslateY = CanvasHeight / 2;
ZoomScale = 1.0;
StateHasChanged();
await DrawCanvas();
}
public class DomRect
{
public double Width { get; set; }
public double Height { get; set; }
}
}