|
|
@@ -1,23 +1,30 @@
|
|
1
|
1
|
import { Point, Stroke } from "../../model/canvas-state";
|
|
2
|
2
|
import { ClientStateService } from "../state/state.client-service";
|
|
3
|
|
-
|
|
|
3
|
+import { getBrushWidthAt, drawVariableWidthSegment, getCatmullRomPath } from "./cat-mul-rom.util";
|
|
4
|
4
|
|
|
5
|
5
|
|
|
6
|
6
|
export class ClientDrawService {
|
|
7
|
|
- private readonly canvasContext: CanvasRenderingContext2D
|
|
|
7
|
+ private readonly cctx: CanvasRenderingContext2D
|
|
8
|
8
|
|
|
9
|
9
|
private currentColor
|
|
10
|
10
|
|
|
11
|
|
- private currentWidth = 12;
|
|
|
11
|
+ private currentDensity
|
|
|
12
|
+
|
|
|
13
|
+ private currentWidth
|
|
12
|
14
|
|
|
13
|
|
- private currentStroke?: number
|
|
|
15
|
+ private currentStrokeId?: number
|
|
14
|
16
|
|
|
15
|
17
|
constructor(
|
|
16
|
18
|
private readonly stateService: ClientStateService,
|
|
17
|
19
|
private readonly canvas: HTMLCanvasElement = document.getElementById("canvas") as HTMLCanvasElement,
|
|
18
|
20
|
private readonly colorPicker = document.getElementById('colorPicker')!,
|
|
|
21
|
+
|
|
|
22
|
+ private readonly densitySlider = document.getElementById('densitySlider')!,
|
|
|
23
|
+ private readonly densityValue = document.getElementById('densityValue')!,
|
|
|
24
|
+
|
|
19
|
25
|
private readonly widthSlider = document.getElementById('widthSlider')!,
|
|
20
|
|
- private readonly widthValue = document.getElementById('widthValue')!,
|
|
|
26
|
+ private readonly widthValue = document.getElementById('widthValue')!,
|
|
|
27
|
+
|
|
21
|
28
|
private readonly menuButton = document.getElementById('menuButton')!,
|
|
22
|
29
|
private readonly drawer = document.getElementById('drawer')!,
|
|
23
|
30
|
private readonly closeButton = document.getElementById('closeButton')!,
|
|
|
@@ -25,33 +32,33 @@ export class ClientDrawService {
|
|
25
|
32
|
this.resizeCanvas()
|
|
26
|
33
|
this.setupCanvasEvents()
|
|
27
|
34
|
window.addEventListener("resize", () => this.resizeCanvas());
|
|
28
|
|
- this.canvasContext = canvas.getContext("2d")!
|
|
29
|
|
- this.currentColor = colorPicker?.getAttribute('value') ?? "#eaafff"
|
|
|
35
|
+ this.cctx = canvas.getContext("2d")!
|
|
|
36
|
+ this.currentColor = colorPicker!.getAttribute('value')!
|
|
|
37
|
+ this.currentDensity = Number(densitySlider!.getAttribute('value'))!
|
|
|
38
|
+ this.currentWidth = Number(widthSlider!.getAttribute('value'))!
|
|
30
|
39
|
}
|
|
31
|
40
|
|
|
32
|
41
|
draw() {
|
|
33
|
|
- const strokes: Stroke[] = this.stateService.getStrokes()
|
|
34
|
42
|
|
|
35
|
|
- this.canvasContext.globalAlpha = 0.05;
|
|
36
|
|
- this.canvasContext.fillStyle = "white";
|
|
37
|
|
- this.canvasContext.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
38
|
|
- this.canvasContext.globalAlpha = 1;
|
|
|
43
|
+ console.log("drawing")
|
|
|
44
|
+ const strokes: Stroke[] = this.stateService.getStrokes()
|
|
39
|
45
|
|
|
|
46
|
+ this.cctx.clearRect(0, 0, this.canvas.width, this.canvas.height);
|
|
|
47
|
+
|
|
|
48
|
+ this.cctx.globalAlpha = 1;
|
|
40
|
49
|
strokes.forEach(stroke => {
|
|
41
|
|
- this.canvasContext.fillStyle = stroke.color;
|
|
42
|
|
- this.drawCurve(stroke.points, stroke.width)
|
|
|
50
|
+ this.cctx.fillStyle = stroke.color;
|
|
|
51
|
+ this.drawCurve(stroke.points, stroke.density, stroke.width)
|
|
43
|
52
|
})
|
|
44
|
|
-
|
|
45
|
|
- requestAnimationFrame(() => this.draw())
|
|
46
|
53
|
}
|
|
47
|
54
|
|
|
48
|
55
|
|
|
49
|
56
|
private addPointToStroke = (e: any) => {
|
|
50
|
|
- if (this.currentStroke === undefined) {
|
|
|
57
|
+ if (this.currentStrokeId === undefined) {
|
|
51
|
58
|
return
|
|
52
|
59
|
}
|
|
53
|
60
|
const rect = this.canvas.getBoundingClientRect();
|
|
54
|
|
- this.stateService.addPoint(this.currentStroke, {
|
|
|
61
|
+ this.stateService.addPoint(this.currentStrokeId, {
|
|
55
|
62
|
x: e.clientX - rect.left,
|
|
56
|
63
|
y: e.clientY - rect.top,
|
|
57
|
64
|
})
|
|
|
@@ -59,10 +66,10 @@ export class ClientDrawService {
|
|
59
|
66
|
|
|
60
|
67
|
private beginStroke = (e: any) => {
|
|
61
|
68
|
const rect = this.canvas.getBoundingClientRect();
|
|
62
|
|
-
|
|
63
|
69
|
this.stateService.beginStroke({
|
|
64
|
70
|
color: this.currentColor,
|
|
65
|
71
|
width: this.currentWidth,
|
|
|
72
|
+ density: this.currentDensity,
|
|
66
|
73
|
points: [{
|
|
67
|
74
|
x: e.clientX - rect.left,
|
|
68
|
75
|
y: e.clientY - rect.top
|
|
|
@@ -70,7 +77,7 @@ export class ClientDrawService {
|
|
70
|
77
|
,
|
|
71
|
78
|
}).then((id: number) => {
|
|
72
|
79
|
console.log("begin stroke", id)
|
|
73
|
|
- this.currentStroke = id;
|
|
|
80
|
+ this.currentStrokeId = id;
|
|
74
|
81
|
this.canvas.addEventListener("mousemove", this.addPointToStroke)
|
|
75
|
82
|
})
|
|
76
|
83
|
}
|
|
|
@@ -99,6 +106,12 @@ export class ClientDrawService {
|
|
99
|
106
|
this.currentWidth = Number(target.value)
|
|
100
|
107
|
});
|
|
101
|
108
|
|
|
|
109
|
+ this.densitySlider?.addEventListener('input', (e) => {
|
|
|
110
|
+ const target = e.target as HTMLTextAreaElement
|
|
|
111
|
+ this.densityValue!.innerHTML = target.value
|
|
|
112
|
+ this.currentDensity = Number(target.value)
|
|
|
113
|
+ });
|
|
|
114
|
+
|
|
102
|
115
|
this.menuButton.addEventListener('click', () => {
|
|
103
|
116
|
this.drawer.classList.toggle('open');
|
|
104
|
117
|
});
|
|
|
@@ -125,138 +138,18 @@ export class ClientDrawService {
|
|
125
|
138
|
this.canvas.height = size;
|
|
126
|
139
|
}
|
|
127
|
140
|
|
|
128
|
|
- private drawCurve(points: Point[], density: number) {
|
|
|
141
|
+ private drawCurve(points: Point[], density: number, width: number) {
|
|
129
|
142
|
if (points.length < 2) return;
|
|
130
|
143
|
|
|
|
144
|
+ points = getCatmullRomPath(points, density)
|
|
|
145
|
+
|
|
131
|
146
|
for (let i = 0; i < points.length - 1; i++) {
|
|
132
|
|
- const width1 = getBrushWidthAt(i, points.length);
|
|
133
|
|
- const width2 = getBrushWidthAt(i + 1, points.length);
|
|
|
147
|
+ const width1 = getBrushWidthAt(i, points.length, width);
|
|
|
148
|
+ const width2 = getBrushWidthAt(i + 1, points.length, width);
|
|
134
|
149
|
const p1 = points[i];
|
|
135
|
150
|
const p2 = points[i + 1];
|
|
136
|
|
- drawVariableWidthSegment(this.canvasContext, p1, p2, width1, width2);
|
|
|
151
|
+ drawVariableWidthSegment(this.cctx, p1, p2, width1, width2);
|
|
137
|
152
|
|
|
138
|
153
|
}
|
|
139
|
154
|
}
|
|
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();
|
|
262
|
155
|
}
|