|
|
@@ -1,4 +1,4 @@
|
|
1
|
|
-import { Cursor } from "../../model/canvas-state";
|
|
|
1
|
+import { Point, Stroke } from "../../model/canvas-state";
|
|
2
|
2
|
import { ClientStateService } from "../state/state.client-service";
|
|
3
|
3
|
|
|
4
|
4
|
|
|
|
@@ -6,72 +6,117 @@ import { ClientStateService } from "../state/state.client-service";
|
|
6
|
6
|
export class ClientDrawService {
|
|
7
|
7
|
private readonly canvasContext: CanvasRenderingContext2D
|
|
8
|
8
|
|
|
9
|
|
- constructor(
|
|
|
9
|
+ private currentColor
|
|
|
10
|
+
|
|
|
11
|
+ private currentWidth = 12;
|
|
10
|
12
|
|
|
|
13
|
+ private currentStroke?: number
|
|
|
14
|
+
|
|
|
15
|
+ constructor(
|
|
11
|
16
|
private readonly stateService: ClientStateService,
|
|
12
|
|
- private readonly canvas: HTMLCanvasElement = document.getElementById("canvas") as HTMLCanvasElement
|
|
|
17
|
+ private readonly canvas: HTMLCanvasElement = document.getElementById("canvas") as HTMLCanvasElement,
|
|
|
18
|
+ private readonly colorPicker = document.getElementById('colorPicker')!,
|
|
|
19
|
+ private readonly widthSlider = document.getElementById('widthSlider')!,
|
|
|
20
|
+ private readonly widthValue = document.getElementById('widthValue')!,
|
|
|
21
|
+ private readonly menuButton = document.getElementById('menuButton')!,
|
|
|
22
|
+ private readonly drawer = document.getElementById('drawer')!,
|
|
|
23
|
+ private readonly closeButton = document.getElementById('closeButton')!,
|
|
13
|
24
|
) {
|
|
14
|
25
|
this.resizeCanvas()
|
|
15
|
26
|
this.setupCanvasEvents()
|
|
16
|
27
|
window.addEventListener("resize", () => this.resizeCanvas());
|
|
17
|
28
|
this.canvasContext = canvas.getContext("2d")!
|
|
|
29
|
+ this.currentColor = colorPicker?.getAttribute('value') ?? "#eaafff"
|
|
18
|
30
|
}
|
|
19
|
31
|
|
|
20
|
32
|
draw() {
|
|
21
|
|
- const cursors: Cursor[] = this.stateService.getCursors()
|
|
22
|
|
-
|
|
|
33
|
+ const strokes: Stroke[] = this.stateService.getStrokes()
|
|
|
34
|
+
|
|
23
|
35
|
this.canvasContext.globalAlpha = 0.05;
|
|
24
|
36
|
this.canvasContext.fillStyle = "white";
|
|
25
|
|
- this.canvasContext.fillRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
37
|
+ this.canvasContext.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
26
|
38
|
this.canvasContext.globalAlpha = 1;
|
|
27
|
39
|
|
|
28
|
|
- cursors
|
|
29
|
|
- .forEach(cursor => {
|
|
30
|
|
- this.canvasContext.beginPath();
|
|
31
|
|
- this.canvasContext.arc(cursor.x, cursor.y, 30, 0, Math.PI * 2);
|
|
32
|
|
- this.canvasContext.fillStyle = "#eaafff";
|
|
33
|
|
- this.canvasContext.fill();
|
|
34
|
|
- })
|
|
|
40
|
+ strokes.forEach(stroke => {
|
|
|
41
|
+ this.canvasContext.fillStyle = stroke.color;
|
|
|
42
|
+ this.drawCurve(stroke.points, stroke.width)
|
|
|
43
|
+ })
|
|
35
|
44
|
|
|
36
|
45
|
requestAnimationFrame(() => this.draw())
|
|
37
|
46
|
}
|
|
38
|
47
|
|
|
39
|
|
- private onMouseMove = (e: any) => {
|
|
40
|
|
- const cursor = this.stateService.get('cursorK')
|
|
41
|
|
- if (!cursor) {
|
|
|
48
|
+
|
|
|
49
|
+ private addPointToStroke = (e: any) => {
|
|
|
50
|
+ if (this.currentStroke === undefined) {
|
|
42
|
51
|
return
|
|
43
|
52
|
}
|
|
44
|
53
|
const rect = this.canvas.getBoundingClientRect();
|
|
45
|
|
- this.stateService.set('cursorK', {
|
|
|
54
|
+ this.stateService.addPoint(this.currentStroke, {
|
|
46
|
55
|
x: e.clientX - rect.left,
|
|
47
|
56
|
y: e.clientY - rect.top,
|
|
48
|
57
|
})
|
|
49
|
58
|
};
|
|
50
|
59
|
|
|
51
|
|
- private onMouseDown = (e: any) => {
|
|
|
60
|
+ private beginStroke = (e: any) => {
|
|
52
|
61
|
const rect = this.canvas.getBoundingClientRect();
|
|
53
|
62
|
|
|
54
|
|
- this.stateService.set('cursorK', {
|
|
55
|
|
- x: e.clientX - rect.left,
|
|
56
|
|
- y: e.clientY - rect.top,
|
|
57
|
|
- }).then(_ => {
|
|
58
|
|
- this.canvas.addEventListener("mousemove", this.onMouseMove)
|
|
59
|
|
- });
|
|
60
|
|
-
|
|
|
63
|
+ this.stateService.beginStroke({
|
|
|
64
|
+ color: this.currentColor,
|
|
|
65
|
+ width: this.currentWidth,
|
|
|
66
|
+ points: [{
|
|
|
67
|
+ x: e.clientX - rect.left,
|
|
|
68
|
+ y: e.clientY - rect.top
|
|
|
69
|
+ }]
|
|
|
70
|
+ ,
|
|
|
71
|
+ }).then((id: number) => {
|
|
|
72
|
+ console.log("begin stroke", id)
|
|
|
73
|
+ this.currentStroke = id;
|
|
|
74
|
+ this.canvas.addEventListener("mousemove", this.addPointToStroke)
|
|
|
75
|
+ })
|
|
61
|
76
|
}
|
|
62
|
77
|
|
|
63
|
78
|
private setupCanvasEvents() {
|
|
64
|
|
- this.canvas.addEventListener("mousedown", this.onMouseDown);
|
|
|
79
|
+ this.canvas.addEventListener("mousedown", this.beginStroke);
|
|
65
|
80
|
|
|
66
|
81
|
this.canvas.addEventListener("mouseup", () => {
|
|
67
|
|
- this.canvas.removeEventListener('mousemove', this.onMouseMove)
|
|
68
|
|
- this.stateService.set('cursorK', undefined)
|
|
|
82
|
+ this.canvas.removeEventListener('mousemove', this.addPointToStroke)
|
|
69
|
83
|
});
|
|
70
|
84
|
|
|
71
|
85
|
this.canvas.removeEventListener("mouseleave", () => {
|
|
72
|
|
- this.canvas.removeEventListener('mousemove', this.onMouseMove)
|
|
73
|
|
- this.stateService.set('cursorK', undefined)
|
|
|
86
|
+ this.canvas.removeEventListener('mousemove', this.addPointToStroke)
|
|
74
|
87
|
});
|
|
|
88
|
+
|
|
|
89
|
+ this.colorPicker?.addEventListener('input', (e) => {
|
|
|
90
|
+ const target = e.target as HTMLTextAreaElement
|
|
|
91
|
+ console.log(target.value)
|
|
|
92
|
+ this.currentColor = target.value
|
|
|
93
|
+
|
|
|
94
|
+ });
|
|
|
95
|
+
|
|
|
96
|
+ this.widthSlider?.addEventListener('input', (e) => {
|
|
|
97
|
+ const target = e.target as HTMLTextAreaElement
|
|
|
98
|
+ this.widthValue!.innerHTML = target.value
|
|
|
99
|
+ this.currentWidth = Number(target.value)
|
|
|
100
|
+ });
|
|
|
101
|
+
|
|
|
102
|
+ this.menuButton.addEventListener('click', () => {
|
|
|
103
|
+ this.drawer.classList.toggle('open');
|
|
|
104
|
+ });
|
|
|
105
|
+
|
|
|
106
|
+ this.closeButton.addEventListener('click', () => {
|
|
|
107
|
+ this.drawer.classList.toggle('open');
|
|
|
108
|
+
|
|
|
109
|
+ });
|
|
|
110
|
+
|
|
|
111
|
+ document.addEventListener('click', (e) => {
|
|
|
112
|
+ if(!e.target){
|
|
|
113
|
+ return
|
|
|
114
|
+ }
|
|
|
115
|
+ if (!this.drawer.contains(e.target as Node) && !this.menuButton.contains(e.target as Node)) {
|
|
|
116
|
+ this.drawer.classList.remove('open');
|
|
|
117
|
+ }
|
|
|
118
|
+ });
|
|
|
119
|
+
|
|
75
|
120
|
}
|
|
76
|
121
|
|
|
77
|
122
|
private resizeCanvas() {
|
|
|
@@ -79,4 +124,139 @@ export class ClientDrawService {
|
|
79
|
124
|
this.canvas.width = size;
|
|
80
|
125
|
this.canvas.height = size;
|
|
81
|
126
|
}
|
|
|
127
|
+
|
|
|
128
|
+ private drawCurve(points: Point[], density: number) {
|
|
|
129
|
+ if (points.length < 2) return;
|
|
|
130
|
+
|
|
|
131
|
+ for (let i = 0; i < points.length - 1; i++) {
|
|
|
132
|
+ const width1 = getBrushWidthAt(i, points.length);
|
|
|
133
|
+ const width2 = getBrushWidthAt(i + 1, points.length);
|
|
|
134
|
+ const p1 = points[i];
|
|
|
135
|
+ const p2 = points[i + 1];
|
|
|
136
|
+ drawVariableWidthSegment(this.canvasContext, p1, p2, width1, width2);
|
|
|
137
|
+
|
|
|
138
|
+ }
|
|
|
139
|
+ }
|
|
|
140
|
+}
|
|
|
141
|
+
|
|
|
142
|
+/**
|
|
|
143
|
+ * Returns a point on a Catmull-Rom spline segment at parameter t
|
|
|
144
|
+ */
|
|
|
145
|
+export function catmullRomPoint(
|
|
|
146
|
+ p0: Point,
|
|
|
147
|
+ p1: Point,
|
|
|
148
|
+ p2: Point,
|
|
|
149
|
+ p3: Point,
|
|
|
150
|
+ t: number
|
|
|
151
|
+): Point {
|
|
|
152
|
+ const t2 = t * t;
|
|
|
153
|
+ const t3 = t2 * t;
|
|
|
154
|
+
|
|
|
155
|
+ const x =
|
|
|
156
|
+ 0.5 *
|
|
|
157
|
+ (2 * p1.x +
|
|
|
158
|
+ (-p0.x + p2.x) * t +
|
|
|
159
|
+ (2 * p0.x - 5 * p1.x + 4 * p2.x - p3.x) * t2 +
|
|
|
160
|
+ (-p0.x + 3 * p1.x - 3 * p2.x + p3.x) * t3);
|
|
|
161
|
+
|
|
|
162
|
+ const y =
|
|
|
163
|
+ 0.5 *
|
|
|
164
|
+ (2 * p1.y +
|
|
|
165
|
+ (-p0.y + p2.y) * t +
|
|
|
166
|
+ (2 * p0.y - 5 * p1.y + 4 * p2.y - p3.y) * t2 +
|
|
|
167
|
+ (-p0.y + 3 * p1.y - 3 * p2.y + p3.y) * t3);
|
|
|
168
|
+
|
|
|
169
|
+ return { x, y };
|
|
|
170
|
+}
|
|
|
171
|
+
|
|
|
172
|
+/**
|
|
|
173
|
+ * Converts a list of raw input points into a smooth Catmull-Rom spline
|
|
|
174
|
+ */
|
|
|
175
|
+export function getCatmullRomPath(
|
|
|
176
|
+ points: Point[],
|
|
|
177
|
+ density: number,
|
|
|
178
|
+): Point[] {
|
|
|
179
|
+ const segmentsPerInterval = density;
|
|
|
180
|
+
|
|
|
181
|
+ if (points.length === 0) return [];
|
|
|
182
|
+ if (points.length === 1) return [{ ...points[0] }];
|
|
|
183
|
+
|
|
|
184
|
+ const smoothed: Point[] = [];
|
|
|
185
|
+ const n = points.length;
|
|
|
186
|
+
|
|
|
187
|
+ for (let i = 0; i < n - 1; i++) {
|
|
|
188
|
+ const p0 = points[Math.max(0, i - 1)];
|
|
|
189
|
+ const p1 = points[i];
|
|
|
190
|
+ const p2 = points[i + 1];
|
|
|
191
|
+ const p3 = points[Math.min(n - 1, i + 2)];
|
|
|
192
|
+
|
|
|
193
|
+ const step = 1 / segmentsPerInterval;
|
|
|
194
|
+
|
|
|
195
|
+ for (let s = 0; s <= segmentsPerInterval; s++) {
|
|
|
196
|
+ const t = s * step;
|
|
|
197
|
+ const pt = catmullRomPoint(p0, p1, p2, p3, t);
|
|
|
198
|
+ smoothed.push(pt);
|
|
|
199
|
+ }
|
|
|
200
|
+ }
|
|
|
201
|
+
|
|
|
202
|
+ // Remove near-duplicates at segment boundaries
|
|
|
203
|
+ const result = smoothed.filter((pt, idx, arr) => {
|
|
|
204
|
+ if (idx === 0) return true;
|
|
|
205
|
+ const prev = arr[idx - 1];
|
|
|
206
|
+ return Math.hypot(pt.x - prev.x, pt.y - prev.y) > 0.001;
|
|
|
207
|
+ });
|
|
|
208
|
+
|
|
|
209
|
+ return result;
|
|
|
210
|
+}
|
|
|
211
|
+
|
|
|
212
|
+function getBrushWidthAt(index: number, totalPoints: number, baseWidth: number = 12): number {
|
|
|
213
|
+ // Example: taper at start and end + speed-based variation
|
|
|
214
|
+ const t = index / (totalPoints - 1);
|
|
|
215
|
+ let width = baseWidth;
|
|
|
216
|
+
|
|
|
217
|
+ // Ease in / ease out
|
|
|
218
|
+ if (t < 0.1) width *= t * 10;
|
|
|
219
|
+ if (t > 0.9) width *= (1 - t) * 10;
|
|
|
220
|
+
|
|
|
221
|
+ return Math.max(1, width);
|
|
|
222
|
+}
|
|
|
223
|
+
|
|
|
224
|
+export function drawVariableWidthSegment(
|
|
|
225
|
+ ctx: CanvasRenderingContext2D,
|
|
|
226
|
+ p1: Point,
|
|
|
227
|
+ p2: Point,
|
|
|
228
|
+ width1: number,
|
|
|
229
|
+ width2: number
|
|
|
230
|
+): void {
|
|
|
231
|
+ const dx = p2.x - p1.x;
|
|
|
232
|
+ const dy = p2.y - p1.y;
|
|
|
233
|
+ const len = Math.hypot(dx, dy); // More efficient than sqrt(dx*dx + dy*dy)
|
|
|
234
|
+
|
|
|
235
|
+ if (len < 0.001) return; // Points are too close
|
|
|
236
|
+
|
|
|
237
|
+ // Normalized perpendicular vector (rotated 90 degrees)
|
|
|
238
|
+ const nx = -dy / len;
|
|
|
239
|
+ const ny = dx / len;
|
|
|
240
|
+
|
|
|
241
|
+ const halfW1 = width1 / 2;
|
|
|
242
|
+ const halfW2 = width2 / 2;
|
|
|
243
|
+
|
|
|
244
|
+ // Four corners of the quadrilateral
|
|
|
245
|
+ const x1 = p1.x + nx * halfW1;
|
|
|
246
|
+ const y1 = p1.y + ny * halfW1;
|
|
|
247
|
+ const x2 = p1.x - nx * halfW1;
|
|
|
248
|
+ const y2 = p1.y - ny * halfW1;
|
|
|
249
|
+ const x3 = p2.x - nx * halfW2;
|
|
|
250
|
+ const y3 = p2.y - ny * halfW2;
|
|
|
251
|
+ const x4 = p2.x + nx * halfW2;
|
|
|
252
|
+ const y4 = p2.y + ny * halfW2;
|
|
|
253
|
+
|
|
|
254
|
+ ctx.beginPath();
|
|
|
255
|
+ ctx.moveTo(x1, y1);
|
|
|
256
|
+ ctx.lineTo(x2, y2);
|
|
|
257
|
+ ctx.lineTo(x3, y3);
|
|
|
258
|
+ ctx.lineTo(x4, y4);
|
|
|
259
|
+ ctx.closePath();
|
|
|
260
|
+
|
|
|
261
|
+ ctx.fill();
|
|
82
|
262
|
}
|