diff --git a/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs b/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs index 97d7db5..eecf177 100644 --- a/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs +++ b/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs @@ -5,6 +5,9 @@ using Microsoft.AspNetCore.Components; using Microsoft.JSInterop; using Newtonsoft.Json.Linq; using Newtonsoft.Json; +using System.Drawing; +using System.Threading; +using Microsoft.AspNetCore.Mvc.ApplicationModels; namespace BlazorCanvas.Data; @@ -25,7 +28,26 @@ public class CanvasService { // TODO: Dessiner à partir des commandes de Franz. - public async void OnMouseMove(MouseEventArgs eventArgs) { + public async void Draw(IEnumerable lscommand) + { + if (_currentCanvasContext is null) + _currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); + await _currentCanvasContext.BeginBatchAsync(); + foreach (CanvasCommand command in lscommand) { + await _currentCanvasContext.SetFillStyleAsync(command.Color); + await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); + } + await _currentCanvasContext.EndBatchAsync(); + } + + public async void Draw(CanvasCommand command) { + if (_currentCanvasContext is null) + _currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); + await _currentCanvasContext.SetFillStyleAsync(command.Color); + await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); + } + + public async void HandleMouse(MouseEventArgs eventArgs) { double mouseX = 0, mouseY = 0; if (eventArgs.Buttons == 0 || eventArgs.Buttons > 2) @@ -42,8 +64,6 @@ public class CanvasService { mouseX = eventArgs.PageX - offsets.Value("offsetLeft"); mouseY = eventArgs.PageY - offsets.Value("offsetTop"); } - if (_currentCanvasContext is null) - _currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); if (eventArgs.Buttons == 1) // Couleur si bouton gauche, blanc si bouton droit color = currentColor; @@ -63,8 +83,7 @@ public class CanvasService { // TODO: Shipper les commandes à Franz. - await _currentCanvasContext.SetFillStyleAsync(color); - await _currentCanvasContext.FillRectAsync(mouseX, mouseY, pointSize, pointSize); + Draw(command); _lastCommand = command; } diff --git a/BlazorCanvas/BlazorCanvas/Pages/Index.razor b/BlazorCanvas/BlazorCanvas/Pages/Index.razor index 7b45283..74b820b 100644 --- a/BlazorCanvas/BlazorCanvas/Pages/Index.razor +++ b/BlazorCanvas/BlazorCanvas/Pages/Index.razor @@ -38,7 +38,7 @@ -
+