From 47bf4a351ecb2e07315fc9d6a873cb8e37444e9f Mon Sep 17 00:00:00 2001 From: MarcEricMartel Date: Sun, 12 Nov 2023 08:15:22 -0500 Subject: [PATCH] =?UTF-8?q?S=C3=A9paration=20backend/frontend?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../BlazorCanvas/Data/CanvasService.cs | 51 +++++++++ BlazorCanvas/BlazorCanvas/Pages/Index.razor | 105 ++++++------------ BlazorCanvas/BlazorCanvas/Program.cs | 1 + 3 files changed, 86 insertions(+), 71 deletions(-) create mode 100644 BlazorCanvas/BlazorCanvas/Data/CanvasService.cs diff --git a/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs b/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs new file mode 100644 index 0000000..2629b89 --- /dev/null +++ b/BlazorCanvas/BlazorCanvas/Data/CanvasService.cs @@ -0,0 +1,51 @@ +using Blazor.Extensions.Canvas.Canvas2D; +using Blazor.Extensions; +using Microsoft.AspNetCore.Components.Web; +using Microsoft.AspNetCore.Components; +using Microsoft.JSInterop; +using Newtonsoft.Json.Linq; +using Newtonsoft.Json; + +namespace BlazorCanvas.Data; + +public class CanvasService { + public ElementReference divCanvas; + public BECanvasComponent myCanvas = new(); + Canvas2DContext? currentCanvasContext; + IJSRuntime _jsRuntime; + + public CanvasService(IJSRuntime jsRuntime) { + _jsRuntime = jsRuntime; + } + + public string currentcolor { get; set; } = "Black"; + public int pointsize { get; set; } = 1; + + public async void OnMouseMove(MouseEventArgs eventArgs) + { + double mouseX = 0, mouseY = 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) + { // 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(eventArgs.Buttons == 1 ? + currentcolor : + "White"); // Couleur si bouton gauche, blanc si bouton droit + await currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize); + } + } +} diff --git a/BlazorCanvas/BlazorCanvas/Pages/Index.razor b/BlazorCanvas/BlazorCanvas/Pages/Index.razor index b664552..f3128d5 100644 --- a/BlazorCanvas/BlazorCanvas/Pages/Index.razor +++ b/BlazorCanvas/BlazorCanvas/Pages/Index.razor @@ -3,80 +3,43 @@ @using Blazor.Extensions.Canvas @using Blazor.Extensions.Canvas.Canvas2D @using Blazor.Extensions.Canvas.WebGL +@using BlazorCanvas.Data @using Newtonsoft.Json @using Newtonsoft.Json.Linq -@inject IJSRuntime jsRuntime +@inject CanvasService canvasService -
-
-

Canvas Communautaire!

- - - - -
+
+

Canvas Communautaire!

+ + + +
-
- +
+
-
- -@code { - ElementReference divCanvas; - BECanvasComponent myCanvas = new(); - Canvas2DContext? currentCanvasContext; - - private string currentcolor { get; set; } = "Black"; - private int pointsize { get; set; } = 1; - - async void OnMouseMove(MouseEventArgs eventArgs) - { - double mouseX = 0, mouseY = 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) - { // 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(eventArgs.Buttons == 1 ? - currentcolor : - "White"); // Couleur si bouton gauche, blanc si bouton droit - await currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize); - } - } -} \ No newline at end of file + (c) Cowboy Kim + \ No newline at end of file diff --git a/BlazorCanvas/BlazorCanvas/Program.cs b/BlazorCanvas/BlazorCanvas/Program.cs index a7052e2..4d2f05c 100644 --- a/BlazorCanvas/BlazorCanvas/Program.cs +++ b/BlazorCanvas/BlazorCanvas/Program.cs @@ -7,6 +7,7 @@ var builder = WebApplication.CreateBuilder(args); // Add services to the container. builder.Services.AddRazorPages(); builder.Services.AddServerSideBlazor(); +builder.Services.AddScoped(); builder.Services.AddSingleton(); var app = builder.Build();