-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathPaint.razor
135 lines (107 loc) · 3.89 KB
/
Paint.razor
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
@page "/paint/{sessionId}"
@using DrawTogether.UI.Client.Components;
@using DrawTogether.UI.Shared
@using Microsoft.AspNetCore.SignalR.Client
@using System.Reactive.Subjects
@using System.Reactive.Linq
@inject NavigationManager navigationManager;
@implements IDisposable;
<h3>Paint!</h3>
<ColorPicker ColorValueChanged="UpdateColor"></ColorPicker>
<CursorPicker CursorSizeChanged="UpdateCursorSize"></CursorPicker>
<section id="paint" style="background-color: cornsilk; width: 900px; height: 500px;"
@onmousemove="MoveCursor" onmousedown="" onmouseup="">
<svg width="900" height="500" >
<Circle radius="@CursorSize" cursorId="user" cX="@cX" cY="@cY" fillColor="@Color" borderColor="@Color" />
@foreach (var c in ClientStrokes)
{
<polyline points="@string.Join(' ', c.points.Select(x => $"{x.X}, {x.Y}"))"
stroke="@(c.color)" stroke-width="@(c.cursorSize)"/>
//<Circle radius="@c.cursorSize" cX="@c.cX" cY="@c.cY" fillColor="@c.color" borderColor="@c.color" />
}
</svg>
</section>
<p>@mousePointerMessage</p>
<p>@blazorHubDebugMessage</p>
<button id="reset" @onclick="ResetScreen">Clear Screen</button>
@code {
[Parameter]
public string SessionId { get; set; }
List<StrokeData> ClientStrokes = new List<StrokeData>();
private string _hubUrl;
private HubConnection _hubConnection;
protected override async Task OnInitializedAsync()
{
// Create the chat client
var baseUrl = navigationManager.BaseUri;
_hubUrl = navigationManager.BaseUri.TrimEnd('/') + DrawHubConstants.HubUri;
blazorHubDebugMessage = $"Attempting to connect to {_hubUrl}";
_hubConnection = new HubConnectionBuilder()
.WithUrl(_hubUrl)
.Build();
_hubConnection.On<StrokeData[]>("DrawStrokes", DrawStrokes);
await _hubConnection.StartAsync();
OutboundStrokes = _strokeSubject
//.Throttle(TimeSpan.FromMilliseconds(20))
.Buffer(TimeSpan.FromMilliseconds(75), 20)
.Where(x => x.Count > 0)
.Subscribe(OnNext);
await _hubConnection.SendAsync("JoinSession", SessionId);
await base.OnInitializedAsync();
}
private void OnNext(IList<Point> obj)
{
var strokeData = new StrokeData()
{
color = Color,
cursorSize = CursorSize,
points = obj.ToArray(),
};
_hubConnection.SendAsync("AddStrokes", SessionId, new[]{ strokeData });
}
private void DrawStrokes(StrokeData[] strokes)
{
ClientStrokes.AddRange(strokes);
// Inform blazor the UI needs updating
StateHasChanged();
}
double cX = 110.0d;
double cY = 110.0d;
private string mousePointerMessage = "foo";
private string blazorHubDebugMessage = "bar";
private string Color = "black";
private Subject<Point> _strokeSubject = new Subject<Point>();
private IDisposable OutboundStrokes { get; set; }
private int CursorSize = 8;
private void MoveCursor(MouseEventArgs e)
{
//mousePointerMessage = $"Mouse coordinates: {e.ScreenX}:{e.ScreenY}";
cX = e.OffsetX;
cY = e.OffsetY;
//mousePointerMessage = $"Moving circle to {cX},{cY} [button pressed? {e.Buttons}]";
if (e.Buttons == 1)
{
var x = e.OffsetX;
var y = e.OffsetY;
mousePointerMessage = $"Writing point {x},{y} [button pressed? {e.Buttons}]";
_strokeSubject.OnNext(new Point(){ X = x, Y = y });
}
}
private void UpdateColor(string color)
{
Color = color;
}
private void UpdateCursorSize(int cursorSize)
{
CursorSize = cursorSize;
}
private void ResetScreen()
{
//strokes.Clear();
}
public void Dispose()
{
_hubConnection.DisposeAsync();
OutboundStrokes.Dispose();
}
}