1
0

Cleanup - kosherification

This commit is contained in:
MarcEricMartel 2023-11-12 08:25:03 -05:00
parent 47bf4a351e
commit d91c39d65c

View File

@ -9,10 +9,8 @@ using Newtonsoft.Json;
namespace BlazorCanvas.Data; namespace BlazorCanvas.Data;
public class CanvasService { public class CanvasService {
public ElementReference divCanvas; private Canvas2DContext? _currentCanvasContext;
public BECanvasComponent myCanvas = new(); private IJSRuntime _jsRuntime;
Canvas2DContext? currentCanvasContext;
IJSRuntime _jsRuntime;
public CanvasService(IJSRuntime jsRuntime) { public CanvasService(IJSRuntime jsRuntime) {
_jsRuntime = jsRuntime; _jsRuntime = jsRuntime;
@ -20,32 +18,31 @@ public class CanvasService {
public string currentcolor { get; set; } = "Black"; public string currentcolor { get; set; } = "Black";
public int pointsize { get; set; } = 1; public int pointsize { get; set; } = 1;
public ElementReference divCanvas { get; set; }
public BECanvasComponent myCanvas { get; set; } = new();
public async void OnMouseMove(MouseEventArgs eventArgs) public async void OnMouseMove(MouseEventArgs eventArgs) {
{
double mouseX = 0, mouseY = 0; double mouseX = 0, mouseY = 0;
if (eventArgs.Buttons == 0 || eventArgs.Buttons > 2) 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. return; // Rien faire si aucun bouton est appuyé ou si les deux boutons/ d'autres boutons sont appuyés.
if (divCanvas.Id?.Length > 0) if (divCanvas.Id?.Length > 0) {
{
string data = await _jsRuntime.InvokeAsync<string>("getDivCanvasOffsets", string data = await _jsRuntime.InvokeAsync<string>("getDivCanvasOffsets",
new object[] { divCanvas }); new object[] { divCanvas });
JObject? offsets = (JObject?)JsonConvert.DeserializeObject(data); 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.
{ // Translation entre le canvas et la souris.
mouseX = eventArgs.ClientX - offsets.Value<double>("offsetLeft"); mouseX = eventArgs.ClientX - offsets.Value<double>("offsetLeft");
mouseY = eventArgs.ClientY - offsets.Value<double>("offsetTop"); mouseY = eventArgs.ClientY - offsets.Value<double>("offsetTop");
} }
if (currentCanvasContext is null) if (_currentCanvasContext is null)
currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); _currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
await currentCanvasContext.SetFillStyleAsync(eventArgs.Buttons == 1 ? await _currentCanvasContext.SetFillStyleAsync(eventArgs.Buttons == 1 ?
currentcolor : currentcolor :
"White"); // Couleur si bouton gauche, blanc si bouton droit "White"); // Couleur si bouton gauche, blanc si bouton droit
await currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize); await _currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize);
} }
} }
} }