1
0
This commit is contained in:
MarcEricMartel 2023-11-15 12:32:30 -05:00
parent 699fcdcb66
commit 1d245662b6
3 changed files with 23 additions and 24 deletions

View File

@ -7,6 +7,7 @@
</PropertyGroup> </PropertyGroup>
<ItemGroup> <ItemGroup>
<PackageReference Include="Aspire.StackExchange.Redis.OutputCaching" Version="8.0.0-preview.1.23557.2" />
<PackageReference Include="Foundry.Extensions.Canvas" Version="1.1.6" /> <PackageReference Include="Foundry.Extensions.Canvas" Version="1.1.6" />
<PackageReference Include="Newtonsoft.Json" Version="13.0.3" /> <PackageReference Include="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup> </ItemGroup>

View File

@ -8,20 +8,24 @@ using Newtonsoft.Json;
using System.Drawing; using System.Drawing;
using System.Threading; using System.Threading;
using Microsoft.AspNetCore.Mvc.ApplicationModels; using Microsoft.AspNetCore.Mvc.ApplicationModels;
using Microsoft.AspNetCore.Connections;
using Aspire.StackExchange.Redis;
using StackExchange.Redis;
using System.Runtime.CompilerServices;
namespace BlazorCanvas.Server.Components.Data; namespace BlazorCanvas.Server.Components.Data;
// https://www.codeproject.com/Articles/5269947/Drawing-with-the-HTML-Canvas-Element-in-Blazor-Ser // https://www.codeproject.com/Articles/5269947/Drawing-with-the-HTML-Canvas-Element-in-Blazor-Ser
public class CanvasService public class CanvasService {
{
private Canvas2DContext? _currentCanvasContext; private Canvas2DContext? _currentCanvasContext;
private IJSRuntime _jsRuntime; private IJSRuntime _jsRuntime;
private IConnectionMultiplexer _cache;
private CanvasCommand _lastCommand = new(); private CanvasCommand _lastCommand = new();
public CanvasService(IJSRuntime jsRuntime) public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) {
{
_jsRuntime = jsRuntime; _jsRuntime = jsRuntime;
_cache = cache;
} }
public string currentColor { get; set; } = "Black"; public string currentColor { get; set; } = "Black";
@ -32,26 +36,21 @@ public class CanvasService
// TODO: Dessiner à partir des commandes de Franz. // TODO: Dessiner à partir des commandes de Franz.
public async void Draw(IEnumerable<CanvasCommand> lscommand) public async void Draw(IEnumerable<CanvasCommand> lsCommand) {
{ if (_currentCanvasContext is null) {
if (_currentCanvasContext is null)
{
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); _currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080); await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080);
} }
await _currentCanvasContext.BeginBatchAsync(); await _currentCanvasContext.BeginBatchAsync();
foreach (CanvasCommand command in lscommand) foreach (CanvasCommand command in lsCommand) {
{
await _currentCanvasContext.SetFillStyleAsync(command.Color); await _currentCanvasContext.SetFillStyleAsync(command.Color);
await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize);
} }
await _currentCanvasContext.EndBatchAsync(); await _currentCanvasContext.EndBatchAsync();
} }
public async void Draw(CanvasCommand command) public async void Draw(CanvasCommand command) {
{ if (_currentCanvasContext is null) {
if (_currentCanvasContext is null)
{
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync(); _currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080); await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080);
} }
@ -59,23 +58,20 @@ public class CanvasService
await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize); await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize);
} }
public async void HandleMouse(MouseEventArgs eventArgs) public async void HandleMouse(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 });
string color = "White"; string color = "White";
CanvasCommand command = new(); CanvasCommand command = new();
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.PageX - offsets.Value<double>("offsetLeft"); mouseX = eventArgs.PageX - offsets.Value<double>("offsetLeft");
mouseY = eventArgs.PageY - offsets.Value<double>("offsetTop"); mouseY = eventArgs.PageY - offsets.Value<double>("offsetTop");
} }
@ -83,8 +79,7 @@ public class CanvasService
if (eventArgs.Buttons == 1) // Couleur si bouton gauche, blanc si bouton droit if (eventArgs.Buttons == 1) // Couleur si bouton gauche, blanc si bouton droit
color = currentColor; color = currentColor;
if (snap) if (snap) { // Magnétisme boboche.
{ // Magnétisme boboche.
mouseX -= mouseX % pointSize; mouseX -= mouseX % pointSize;
mouseY -= mouseY % pointSize; mouseY -= mouseY % pointSize;
} }

View File

@ -1,5 +1,6 @@
using BlazorCanvas.Server.Components; using BlazorCanvas.Server.Components;
using BlazorCanvas.Server.Components.Data; using BlazorCanvas.Server.Components.Data;
using Microsoft.Extensions.Caching.Distributed;
using Microsoft.Extensions.Hosting; using Microsoft.Extensions.Hosting;
var builder = WebApplication.CreateBuilder(args); var builder = WebApplication.CreateBuilder(args);
@ -9,6 +10,8 @@ builder.AddServiceDefaults();
// Add services to the container. // Add services to the container.
builder.Services.AddRazorComponents() builder.Services.AddRazorComponents()
.AddInteractiveServerComponents(); .AddInteractiveServerComponents();
builder.AddRedisOutputCache("cache");
//builder.AddKeyedRabbitMQ("mq");
builder.Services.AddScoped<CanvasService>(); builder.Services.AddScoped<CanvasService>();