diff --git a/BlazorCanvas/BlazorCanvas/Pages/Index.razor b/BlazorCanvas/BlazorCanvas/Pages/Index.razor index 0209b6a..b664552 100644 --- a/BlazorCanvas/BlazorCanvas/Pages/Index.razor +++ b/BlazorCanvas/BlazorCanvas/Pages/Index.razor @@ -8,37 +8,41 @@ @inject IJSRuntime jsRuntime
-

Canvas Communautaire!

- - - - +
+

Canvas Communautaire!

+ + + + +
-
+
+ @code { ElementReference divCanvas; @@ -48,24 +52,30 @@ private string currentcolor { get; set; } = "Black"; private int pointsize { get; set; } = 1; - async void OnMouseMove(MouseEventArgs eventArgs) { + async void OnMouseMove(MouseEventArgs eventArgs) + { double mouseX = 0, mouseY = 0; - if (eventArgs.Buttons == 0) - return; - if (divCanvas.Id?.Length > 0) { + if (eventArgs.Buttons == 0 || eventArgs.Buttons > 2) + return; // Rien faire si aucun bouton est appuyé ou si les deux boutons/ d'autres boutons sont appuyés. + + if (divCanvas.Id?.Length > 0) + { string data = await jsRuntime.InvokeAsync("getDivCanvasOffsets", new object[] { divCanvas }); JObject? offsets = (JObject?)JsonConvert.DeserializeObject(data); - if (offsets is not null && offsets.HasValues) { + if (offsets is not null && offsets.HasValues) + { // Translation entre le canvas et la souris. mouseX = eventArgs.ClientX - offsets.Value("offsetLeft"); mouseY = eventArgs.ClientY - offsets.Value("offsetTop"); } if (currentCanvasContext is null) currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); - await currentCanvasContext.SetFillStyleAsync(currentcolor); + await currentCanvasContext.SetFillStyleAsync(eventArgs.Buttons == 1 ? + currentcolor : + "White"); // Couleur si bouton gauche, blanc si bouton droit await currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize); } }