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>
<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="Newtonsoft.Json" Version="13.0.3" />
</ItemGroup>

View File

@ -8,20 +8,24 @@ using Newtonsoft.Json;
using System.Drawing;
using System.Threading;
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;
// 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 IJSRuntime _jsRuntime;
private IConnectionMultiplexer _cache;
private CanvasCommand _lastCommand = new();
public CanvasService(IJSRuntime jsRuntime)
{
public CanvasService(IJSRuntime jsRuntime, IConnectionMultiplexer cache) {
_jsRuntime = jsRuntime;
_cache = cache;
}
public string currentColor { get; set; } = "Black";
@ -32,26 +36,21 @@ public class CanvasService
// TODO: Dessiner à partir des commandes de Franz.
public async void Draw(IEnumerable<CanvasCommand> lscommand)
{
if (_currentCanvasContext is null)
{
public async void Draw(IEnumerable<CanvasCommand> lsCommand) {
if (_currentCanvasContext is null) {
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
await _currentCanvasContext.ClearRectAsync(0, 0, 1920, 1080);
}
await _currentCanvasContext.BeginBatchAsync();
foreach (CanvasCommand command in lscommand)
{
foreach (CanvasCommand command in lsCommand) {
await _currentCanvasContext.SetFillStyleAsync(command.Color);
await _currentCanvasContext.FillRectAsync(command.X, command.Y, command.PointSize, command.PointSize);
}
await _currentCanvasContext.EndBatchAsync();
}
public async void Draw(CanvasCommand command)
{
if (_currentCanvasContext is null)
{
public async void Draw(CanvasCommand command) {
if (_currentCanvasContext is null) {
_currentCanvasContext = await myCanvas.CreateCanvas2DAsync();
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);
}
public async void HandleMouse(MouseEventArgs eventArgs)
{
public async void HandleMouse(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 });
string color = "White";
CanvasCommand command = new();
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.PageX - offsets.Value<double>("offsetLeft");
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
color = currentColor;
if (snap)
{ // Magnétisme boboche.
if (snap) { // Magnétisme boboche.
mouseX -= mouseX % pointSize;
mouseY -= mouseY % pointSize;
}

View File

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