Cleanup - kosherification
This commit is contained in:
parent
47bf4a351e
commit
d91c39d65c
@ -9,10 +9,8 @@ using Newtonsoft.Json;
|
||||
namespace BlazorCanvas.Data;
|
||||
|
||||
public class CanvasService {
|
||||
public ElementReference divCanvas;
|
||||
public BECanvasComponent myCanvas = new();
|
||||
Canvas2DContext? currentCanvasContext;
|
||||
IJSRuntime _jsRuntime;
|
||||
private Canvas2DContext? _currentCanvasContext;
|
||||
private IJSRuntime _jsRuntime;
|
||||
|
||||
public CanvasService(IJSRuntime jsRuntime) {
|
||||
_jsRuntime = jsRuntime;
|
||||
@ -20,32 +18,31 @@ public class CanvasService {
|
||||
|
||||
public string currentcolor { get; set; } = "Black";
|
||||
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;
|
||||
|
||||
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)
|
||||
{
|
||||
if (divCanvas.Id?.Length > 0) {
|
||||
string data = await _jsRuntime.InvokeAsync<string>("getDivCanvasOffsets",
|
||||
new object[] { divCanvas });
|
||||
JObject? offsets = (JObject?)JsonConvert.DeserializeObject(data);
|
||||
|
||||
if (offsets is not null && offsets.HasValues)
|
||||
{ // Translation entre le canvas et la souris.
|
||||
if (offsets is not null && offsets.HasValues) { // Translation entre le canvas et la souris.
|
||||
mouseX = eventArgs.ClientX - offsets.Value<double>("offsetLeft");
|
||||
mouseY = eventArgs.ClientY - offsets.Value<double>("offsetTop");
|
||||
}
|
||||
if (currentCanvasContext is null)
|
||||
currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
|
||||
if (_currentCanvasContext is null)
|
||||
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
|
||||
|
||||
await currentCanvasContext.SetFillStyleAsync(eventArgs.Buttons == 1 ?
|
||||
await _currentCanvasContext.SetFillStyleAsync(eventArgs.Buttons == 1 ?
|
||||
currentcolor :
|
||||
"White"); // Couleur si bouton gauche, blanc si bouton droit
|
||||
await currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize);
|
||||
await _currentCanvasContext.FillRectAsync(mouseX, mouseY, pointsize, pointsize);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user