-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathPathLayer.tsx
445 lines (408 loc) · 12.1 KB
/
PathLayer.tsx
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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
import { useCallback } from 'react';
import { inRange, last } from 'lodash';
import { CAPTION_SIZE } from './TimeCaptions';
import { useDraw, usePicking } from '../hooks/useCanvas';
import {
type DataPoint,
DEFAULT_PATH_END,
type DrawingFunction,
type OperationalPoint,
type PathData,
type PickingDrawingFunction,
type Point,
type SpaceTimeChartContextType,
} from '../lib/types';
import {
drawAliasedDisc,
drawAliasedLine,
drawPathExtremity,
getCrispLineCoordinate,
} from '../utils/canvas';
import { indexToColor, hexToRgb } from '../utils/colors';
import { getSpaceBreakpoints } from '../utils/scales';
function getDirection({ points }: PathData, reverse?: boolean): 'up' | 'down' {
if (points.length < 2) return 'down';
if (!reverse) {
for (let i = 1, l = Math.min(3, points.length); i < l; i++) {
const diff = points[i].position - points[i - 1].position;
if (diff > 0) return 'up';
if (diff < 0) return 'down';
}
} else {
for (let i = points.length - 2, l = Math.max(points.length - 4, points.length); i > l; i--) {
const diff = points[i].position - points[i + 1].position;
if (diff > 0) return 'up';
if (diff < 0) return 'down';
}
}
return 'down';
}
const DEFAULT_PICKING_TOLERANCE = 5;
const PAUSE_THICKNESS = 7;
const PAUSE_OPACITY = 0.2;
type PathStyle = {
width: number;
endWidth: number;
dashArray?: number[];
opacity?: number;
lineCap?: CanvasLineCap;
};
export type PathLevel = 1 | 2 | 3 | 4;
const STYLES: Record<PathLevel, PathStyle> = {
1: {
width: 1.5,
endWidth: 1.5,
},
2: {
width: 1,
endWidth: 1,
},
3: {
width: 1,
endWidth: 1,
dashArray: [5, 5],
lineCap: 'square',
},
4: {
width: 1.5,
endWidth: 1,
dashArray: [0, 4],
lineCap: 'round',
},
} as const;
export const DEFAULT_LEVEL: PathLevel = 2;
export type PathLayerProps = {
path: PathData;
// Style:
color: string;
pickingTolerance?: number;
level?: PathLevel;
};
/**
* This component handles drawing a Path inside a SpaceTimeChart. It renders:
* - The path itself
* - The pauses
* - The "picking" shape (to handle interactions)
*/
export const PathLayer = ({
path,
color,
level = DEFAULT_LEVEL,
pickingTolerance = DEFAULT_PICKING_TOLERANCE,
}: PathLayerProps) => {
/**
* This function returns the list of points to join to draw the path. It will be both used to
* render the visible path, and the segments on the picking layer.
*/
const getPathSegments = useCallback(
({
getTimePixel,
getSpacePixel,
spaceScaleTree,
timeAxis,
spaceAxis,
}: SpaceTimeChartContextType): Point[] => {
const res: Point[] = [];
path.points.forEach(({ position, time }, i, a) => {
if (!i) {
res.push({
[timeAxis]: getTimePixel(time),
[spaceAxis]: getSpacePixel(position),
} as Point);
} else {
const { position: prevPosition, time: prevTime } = a[i - 1];
const spaceBreakPoints = getSpaceBreakpoints(prevPosition, position, spaceScaleTree);
spaceBreakPoints.forEach((breakPosition) => {
const breakTime =
prevTime +
((breakPosition - prevPosition) / (position - prevPosition)) * (time - prevTime);
res.push({
[timeAxis]: getTimePixel(breakTime),
[spaceAxis]: getSpacePixel(breakPosition),
} as Point);
});
res.push({
[timeAxis]: getTimePixel(time),
[spaceAxis]: getSpacePixel(position),
} as Point);
}
});
return res;
},
[path]
);
/**
* This function returns the list of important points, where the mouse can snap.
*/
const getSnapPoints = useCallback(
({
getTimePixel,
getSpacePixel,
timeAxis,
spaceAxis,
operationalPoints,
}: SpaceTimeChartContextType): Point[] => {
const res: Point[] = [];
const stopPositions = new Set(operationalPoints.map((p) => p.position));
path.points.forEach(({ position, time }) => {
if (stopPositions.has(position))
res.push({
[timeAxis]: getTimePixel(time),
[spaceAxis]: getSpacePixel(position),
} as Point);
});
return res;
},
[path]
);
/**
* This function draws the stops of the path on the operational points.
*/
const drawPauses = useCallback<DrawingFunction>(
(ctx, { getTimePixel, getSpacePixel, operationalPoints, swapAxis }) => {
const stopPositions = new Set(operationalPoints.map((p) => p.position));
path.points.forEach(({ position, time }, i, a) => {
if (i) {
const { position: prevPosition, time: prevTime } = a[i - 1];
if (prevPosition === position && stopPositions.has(position)) {
const spacePixel = getCrispLineCoordinate(getSpacePixel(position), ctx.lineWidth);
ctx.beginPath();
if (!swapAxis) {
ctx.moveTo(getTimePixel(prevTime), spacePixel);
ctx.lineTo(getTimePixel(time), spacePixel);
} else {
ctx.moveTo(spacePixel, getTimePixel(prevTime));
ctx.lineTo(spacePixel, getTimePixel(time));
}
ctx.stroke();
}
}
});
},
[path]
);
/**
* This function draws the label of the path.
*/
const drawLabel = useCallback(
(
ctx: CanvasRenderingContext2D,
{
width,
height,
swapAxis,
theme: {
background,
pathsStyles: { fontSize, fontFamily },
},
}: SpaceTimeChartContextType,
label: string,
labelColor: string,
points: Point[],
pathLength: number
) => {
if (!label) return;
const firstPointOnScreenIndex = points.findIndex(({ x, y }) =>
!swapAxis
? inRange(x, 0, width) && inRange(y, 0, height - CAPTION_SIZE)
: inRange(x, CAPTION_SIZE, width) && inRange(y, 0, height)
);
if (firstPointOnScreenIndex < 0) return;
const prev = points[firstPointOnScreenIndex - 1];
const curr = points[firstPointOnScreenIndex];
const next = points[firstPointOnScreenIndex + 1];
let position: Point = curr;
let angle = 0;
if (firstPointOnScreenIndex === 0) {
if (next) angle = Math.atan2(next.y - curr.y, next.x - curr.x);
} else {
const slope = (curr.y - prev.y) / (curr.x - prev.x);
const yOnYAxisIntersect = curr.y - curr.x * slope;
const xOnXAxisIntersect = curr.x - curr.y / slope;
if (yOnYAxisIntersect >= 0) {
position = {
x: 0,
y: yOnYAxisIntersect,
};
} else {
position = {
x: xOnXAxisIntersect + 10 / slope,
y: 10,
};
}
angle = Math.atan2(curr.y - prev.y, curr.x - prev.x);
}
// Finally, draw label:
ctx.save();
ctx.translate(position.x, position.y);
ctx.rotate(angle);
ctx.font = `${fontSize}px ${fontFamily}`;
ctx.textAlign = 'start';
const padding = 2;
const measure = ctx.measureText(label);
const w = measure.width + 2 * padding;
const actualBoundingBoxHeight =
measure.actualBoundingBoxAscent + measure.actualBoundingBoxDescent;
const h = actualBoundingBoxHeight + 2 * padding;
const dx = w < pathLength ? 5 : (pathLength - w) / 2; // Progressively center the label if the path is shorter than the label
const dy = angle >= 0 ? -5 : 15;
ctx.globalAlpha = 0.75;
ctx.fillStyle = background;
ctx.fillRect(dx - padding, dy - h + padding, w, h);
ctx.fillStyle = labelColor;
ctx.globalAlpha = 1;
ctx.fillText(label, dx, dy);
ctx.restore();
},
[]
);
/**
* This function draws the extremities of the path.
*/
const drawExtremities = useCallback<DrawingFunction>(
(ctx, { getTimePixel, getSpacePixel, swapAxis }) => {
if (!path.points.length) return;
const pathDirection = getDirection(path);
const from = path.points[0];
const fromEnd = path.fromEnd || DEFAULT_PATH_END;
const to = last(path.points) as DataPoint;
const toEnd = path.toEnd || DEFAULT_PATH_END;
drawPathExtremity(
ctx,
getTimePixel(from.time),
getSpacePixel(from.position),
swapAxis,
'from',
pathDirection,
fromEnd
);
drawPathExtremity(
ctx,
getTimePixel(to.time),
getSpacePixel(to.position),
swapAxis,
'to',
pathDirection,
toEnd
);
},
[path]
);
const computePathLength = useCallback(
(operationalPoints: OperationalPoint[], segments: Point[]) => {
let totalLength = 0;
// Compute length of pauses
const stopPositions = new Set(operationalPoints.map((p) => p.position));
path.points.forEach(({ position, time }, i, pointsArray) => {
if (i > 0) {
const { position: prevPosition, time: prevTime } = pointsArray[i - 1];
if (prevPosition === position && stopPositions.has(position)) {
totalLength += time - prevTime;
}
}
});
// Compute length of pathSegments
segments.forEach(({ x, y }, i, segmentArray) => {
if (i > 0) {
const { x: prevX, y: prevY } = segmentArray[i - 1];
totalLength += Math.sqrt(Math.pow(prevX - x, 2) + Math.pow(prevY - y, 2));
}
});
return totalLength;
},
[path]
);
const drawAll = useCallback<DrawingFunction>(
(ctx, stcContext) => {
// Draw stops:
ctx.strokeStyle = color;
ctx.lineWidth = PAUSE_THICKNESS;
ctx.globalAlpha = PAUSE_OPACITY;
ctx.lineCap = 'round';
drawPauses(ctx, stcContext);
const style = STYLES[level];
// Draw main path:
ctx.strokeStyle = color;
ctx.lineWidth = style.width;
ctx.setLineDash(style.dashArray || []);
ctx.globalAlpha = style.opacity || 1;
ctx.lineCap = style.lineCap || 'square';
ctx.beginPath();
const segments = getPathSegments(stcContext);
segments.forEach(({ x, y }, i) => {
if (!i) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
});
ctx.stroke();
// Draw extremities:
ctx.setLineDash([]);
ctx.lineWidth = style.endWidth;
drawExtremities(ctx, stcContext);
// Draw label:
if (!stcContext.hidePathsLabels) {
const pathLength = computePathLength(stcContext.operationalPoints, segments);
drawLabel(ctx, stcContext, path.label, color, segments, pathLength);
}
},
[
color,
drawPauses,
level,
getPathSegments,
drawExtremities,
computePathLength,
drawLabel,
path.label,
]
);
useDraw('paths', drawAll);
const drawPicking = useCallback<PickingDrawingFunction>(
(imageData, stcContext) => {
const { registerPickingElement } = stcContext;
// Draw segments:
getPathSegments(stcContext).forEach((point, i, a) => {
if (i) {
const previousPoint = a[i - 1];
const index = registerPickingElement({
type: 'segment',
pathId: path.id,
from: previousPoint,
to: point,
});
const lineColor = hexToRgb(indexToColor(index));
drawAliasedLine(
imageData,
previousPoint,
point,
lineColor,
STYLES[level].width + pickingTolerance,
true
);
}
});
// Draw snap points:
getSnapPoints(stcContext).forEach((point) => {
const index = registerPickingElement({
type: 'point',
pathId: path.id,
point,
});
const lineColor = hexToRgb(indexToColor(index));
drawAliasedDisc(
imageData,
point,
(STYLES[level].width + pickingTolerance) * 2,
lineColor,
false
);
});
},
[getPathSegments, getSnapPoints, level, path.id, pickingTolerance]
);
usePicking('paths', drawPicking);
return null;
};
export default PathLayer;