Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Canvas redering list images #141

Open
GiorgioMauro opened this issue Oct 4, 2024 · 0 comments
Open

Canvas redering list images #141

GiorgioMauro opened this issue Oct 4, 2024 · 0 comments

Comments

@GiorgioMauro
Copy link

I need some help with my Blazor application. I’m working with a canvas and images, and I have a list of images that I load from a folder. My issue is that I want to change the displayed image using arrow buttons, but currently, only the first image loaded during initialization is being rendered. Could you help me improve my code? I’ll paste the UI Blazor code and a few methods below.

razor.cs

            <div @ref="canvasDiv" @onmousedown="OnMouseDown" @onmousemove="OnMouseMove" @onmouseup="OnMouseUp" @onwheel="OnMouseWheel">
                <BECanvas @ref="myCanvas" Height="950" Width="850"></BECanvas><br /><br /><br /><br />
            </div>
            <img src="Batch/@Job/@Bsn/@Phase/@_imgName" @ref="imageRef" style="display:none;" /> 

razor.cs

private ElementReference imageRef;

async Task RedrawCanvas()
{
await currentCanvasContext.ClearRectAsync(0, 0, 800, 800);

   await currentCanvasContext.SaveAsync();

   await currentCanvasContext.TranslateAsync(translateX, translateY);
   await currentCanvasContext.ScaleAsync(zoomFactor, zoomFactor);

   Console.WriteLine(imageRef.Id);
   Console.WriteLine(_imageId);
   Console.WriteLine(imageRef.Context);

   await currentCanvasContext.DrawImageAsync(imageRef, 0, 0, 800, 800);

   var rectanglesCopy = new List<Rectangle>(rectangles);

   foreach (var rect in rectanglesCopy)
   {
       // Modifica il colore del rettangolo selezionato
       if (rect.IsSelected)
       {
           await currentCanvasContext.SetStrokeStyleAsync("black");
       }
       else
       {
           await currentCanvasContext.SetStrokeStyleAsync(rect.Color);
       }

       // Ridisegna il rettangolo tenendo conto della scala e della traduzione
       await currentCanvasContext.StrokeRectAsync(rect.X, rect.Y, rect.Width, rect.Height);
   }

   await currentCanvasContext.RestoreAsync();

}

method to change image

    private async void NextImage()
    {
        if (_currentImages != _totalImages)
        {
            _currentImages += 1;
            _imgName = _imgListTest.ElementAt(_currentImages);

            await RedrawCanvas();
            StateHasChanged();
        }
    }

I believe the issue is that the imageref.Id has a single ID, and when I attempt to change the image, it doesn’t update. Could someone assist me with this? (I could provide the other methods if required)

Thank y'all

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant