-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathframe.js
More file actions
451 lines (424 loc) · 14.2 KB
/
Copy pathframe.js
File metadata and controls
451 lines (424 loc) · 14.2 KB
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
446
447
448
449
450
451
import {
calcSeriesSpeedsAtEachInterval,
calcSeriesAverage,
getValueOfSeriesItem,
getTimeOfSeriesItem,
createSeriesItemInverted,
SERIES_TIME_UNIT,
} from './speed-series.js'
function isZeroLikeTime(time, epsilon = 1e-6) {
return !Number.isFinite(time) || Math.abs(time) <= epsilon
}
function clampPixelAverageWindow(pixelAverageWindow, maxWindow) {
const maxAllowed = Math.max(1, Math.floor(maxWindow || 1))
const nextWindow = Math.floor(pixelAverageWindow)
if (!Number.isFinite(nextWindow)) return 1
return Math.min(Math.max(1, nextWindow), maxAllowed)
}
function applyRollingAverageToSpeedSeries(avgWithSpeeds, pixelAverageWindow) {
const windowSize = clampPixelAverageWindow(pixelAverageWindow, avgWithSpeeds.length)
if (windowSize <= 1 || avgWithSpeeds.length <= 1) {
return avgWithSpeeds
}
const finiteWindow = []
const smoothed = []
let finiteSum = 0
let finiteCount = 0
for (let i = 0, c = avgWithSpeeds.length; i < c; i++) {
const step = avgWithSpeeds[i]
const speed = step[2]
const finiteSpeed = Number.isFinite(speed) ? speed : undefined
finiteWindow.push(finiteSpeed)
if (finiteSpeed !== undefined) {
finiteSum += finiteSpeed
finiteCount += 1
}
if (finiteWindow.length > windowSize) {
const removedSpeed = finiteWindow.shift()
if (removedSpeed !== undefined) {
finiteSum -= removedSpeed
finiteCount -= 1
}
}
const smoothedSpeed = finiteCount ? finiteSum / finiteCount : speed
smoothed.push([step[0], step[1], smoothedSpeed])
}
return smoothed
}
function resolveGraphMaxSpeed(localMaxAvgSpeed, previousMaxSpeed, renderOptions) {
const localMax = Number.isFinite(localMaxAvgSpeed) && localMaxAvgSpeed > 0
? localMaxAvgSpeed
: 1
const decay = Number.isFinite(renderOptions?.maxSpeedDecay) && renderOptions.maxSpeedDecay > 0 && renderOptions.maxSpeedDecay < 1
? renderOptions.maxSpeedDecay
: 0.96
const headroom = Number.isFinite(renderOptions?.maxSpeedHeadroom) && renderOptions.maxSpeedHeadroom >= 1
? renderOptions.maxSpeedHeadroom
: 1.08
const previousMax = Number.isFinite(previousMaxSpeed) && previousMaxSpeed > 0
? previousMaxSpeed
: 0
// previousMaxSpeed already includes headroom from the prior frame, so remove it
// before applying decay to avoid compounding headroom over time.
const previousBase = previousMax ? previousMax / headroom : 0
const decayedMax = previousBase ? previousBase * decay : 0
return Math.max(localMax, decayedMax) * headroom
}
function defaultDrawProgressBar(args) {
args.createDefaultPath()
args.canvasCtx.fill()
args.canvasCtx.stroke()
}
function defaultDrawGrid(args) {
args.createDefaultPath()
args.canvasCtx.stroke()
}
function defaultDrawSpeedOverlay(args) {
args.createDefaultPath()
args.canvasCtx.fill()
}
function defaultDrawSpeedLineLabel(args) {
args.createDefaultLinePath()
args.canvasCtx.stroke()
args.createDefaultLabelBackgroundPath()
args.canvasCtx.fill()
args.fillDefaultLabelText()
}
function defaultDrawBorder(args) {
args.createDefaultPath()
args.canvasCtx.stroke()
}
function calcAverageSpeedsForResolution(maxValue, stepList, { w: canvasWidth, h: canvasHeight }, renderOptions) {
if (!stepList.length) return
if (!(Number.isFinite(maxValue) && maxValue > 0)) return
const lastStep = stepList[stepList.length - 1]
const [, lastValue] = lastStep
const lastX = lastValue / maxValue * (canvasWidth - 1)
if (lastX) {
const pixelsPerValue = lastValue ? lastX / lastValue : 0
const avgResolution = lastValue / lastX
const avgWithSpeeds = calcSeriesSpeedsAtEachInterval(
calcSeriesAverage(
stepList,
avgResolution,
avgResolution,
getValueOfSeriesItem,
getTimeOfSeriesItem,
createSeriesItemInverted,
).avg,
SERIES_TIME_UNIT.INTERVAL,
)
const pixelAverageWindow = clampPixelAverageWindow(renderOptions?.pixelAverageWindow, canvasWidth)
const avgWithSpeedsSmoothed = applyRollingAverageToSpeedSeries(avgWithSpeeds, pixelAverageWindow)
const ignoreTrailingSpeedSample = renderOptions?.ignoreTrailingSpeedSample === true
const renderPointCount = ignoreTrailingSpeedSample && avgWithSpeedsSmoothed.length > 1
? avgWithSpeedsSmoothed.length - 1
: avgWithSpeedsSmoothed.length
let hasZeroTime = false
const zeroLikeTimeEpsilon = Number.isFinite(renderOptions?.zeroLikeTimeEpsilon) && renderOptions.zeroLikeTimeEpsilon > 0
? renderOptions.zeroLikeTimeEpsilon
: 1e-6
let localMaxAvgSpeed = 0
for (let i = 0; i < renderPointCount; i++) {
const [time,,speed] = avgWithSpeedsSmoothed[i]
if (isZeroLikeTime(time, zeroLikeTimeEpsilon) || !Number.isFinite(speed)) {
hasZeroTime = true
continue
}
localMaxAvgSpeed = Math.max(localMaxAvgSpeed, speed)
}
const maxAvgSpeed = localMaxAvgSpeed || 1
const height = canvasHeight - 1
let x = 0
let y = height
for (let i = 0, c = avgWithSpeedsSmoothed.length; i < c; i++) {
const [time, value, speed] = avgWithSpeedsSmoothed[i]
const valuePx = pixelsPerValue ? value * pixelsPerValue : 0
x += valuePx
let speedRatio = isZeroLikeTime(time, zeroLikeTimeEpsilon) ? 1
: Math.min(1, speed / maxAvgSpeed)
y = height - (height * speedRatio) + 0
avgWithSpeedsSmoothed[i].push(x, y)
}
return {
lastX,
lastValue,
maxValue,
pixelsPerValue,
avgResolution,
avgWithSpeeds: avgWithSpeedsSmoothed,
renderPointCount,
ignoreTrailingSpeedSample,
localMaxAvgSpeed,
maxAvgSpeed,
hasZeroTime,
pixelAverageWindow,
canvasWidth,
canvasHeight,
}
}
}
function renderTransferGraphFrame(args) {
const maxValue = Number.isFinite(args.maxValue) ? args.maxValue : 0
const series = args.series
const canvasCtx = args.canvasCtx
const size = args.size
const runningMaxSpeed = args.runningMaxSpeed
const graphOptions = args.graphOptions || {}
const renderOptions = args.renderOptions || {}
const manageMaxSpeed = args.manageMaxSpeed !== false
const recalculateMaxFromZero = args.recalculateMaxFromZero === true
let nextRunningMax = Number.isFinite(runningMaxSpeed) ? runningMaxSpeed : 0
let lastRenderedSpeed
if (recalculateMaxFromZero) {
nextRunningMax = 0
}
if (manageMaxSpeed && series.length > 1) {
const avgResult = calcAverageSpeedsForResolution(maxValue, series, size, {
pixelAverageWindow: graphOptions.pixelAverageWindow,
ignoreTrailingSpeedSample: graphOptions.ignoreTrailingSpeedSample !== false,
})
if (avgResult && avgResult.localMaxAvgSpeed > 0) {
nextRunningMax = resolveGraphMaxSpeed(avgResult.localMaxAvgSpeed, nextRunningMax, {
maxSpeedDecay: graphOptions.maxSpeedDecay,
maxSpeedHeadroom: graphOptions.maxSpeedHeadroom,
})
}
if (avgResult && avgResult.renderPointCount > 0) {
lastRenderedSpeed = avgResult.avgWithSpeeds[avgResult.renderPointCount - 1][2]
}
}
let finalRenderOptions = renderOptions
if (
typeof renderOptions.speedLabelFormatter === 'function' &&
!renderOptions.speedLabel &&
typeof lastRenderedSpeed === 'number' &&
Number.isFinite(lastRenderedSpeed)
) {
finalRenderOptions = Object.assign({}, renderOptions, {
speedLabel: renderOptions.speedLabelFormatter(lastRenderedSpeed)
})
}
renderStepToCanvas(
maxValue,
series,
canvasCtx,
size,
manageMaxSpeed ? (nextRunningMax || undefined) : undefined,
finalRenderOptions
)
return { runningMaxSpeed: nextRunningMax, lastRenderedSpeed }
}
function renderStepToCanvas(maxValue, stepList, canvasCtx, { w: canvasWidth, h: canvasHeight }, globalMaxSpeed, renderOptions) {
const {
colorBackground = '#a1e992',
colorBackgroundStroke = '#8dd07a',
colorOverlay = '#06b027',
gridCols = 10,
gridRows = 5,
gridColor = 'rgba(0,0,0,0.125)',
borderColor = 'rgba(0,0,0,0.25)',
speedLabel = '',
speedLabelColor = 'rgba(0,0,0,0.75)',
speedLabelBackgroundColor = 'rgba(255,255,255,0)',
speedGuideColor = 'rgba(0,0,0,0.7)',
ignoreTrailingSpeedSample = true,
drawProgressBar = defaultDrawProgressBar,
drawGrid = defaultDrawGrid,
drawSpeedOverlay = defaultDrawSpeedOverlay,
drawSpeedLineLabel = defaultDrawSpeedLineLabel,
drawBorder = defaultDrawBorder,
} = renderOptions || {}
if (!stepList.length) return
if (!(Number.isFinite(maxValue) && maxValue > 0)) return
const lastStep = stepList[stepList.length - 1]
const [, lastValue] = lastStep
const backgroundValue = Number.isFinite(renderOptions?.backgroundValue)
? Math.min(maxValue, Math.max(0, renderOptions.backgroundValue))
: lastValue
const lastX = backgroundValue / maxValue * (canvasWidth - 1)
let currentSpeedY = undefined
canvasCtx.save()
canvasCtx.clearRect(0, 0, canvasWidth, canvasHeight)
canvasCtx.fillStyle = colorBackground
canvasCtx.strokeStyle = colorBackgroundStroke
canvasCtx.lineWidth = 1
const renderProgressBar = drawProgressBar instanceof Function ? drawProgressBar : defaultDrawProgressBar
renderProgressBar({
canvasCtx,
canvasWidth,
canvasHeight,
lastX,
backgroundValue,
createDefaultPath: () => {
canvasCtx.beginPath()
canvasCtx.rect(0.5, 0.5, lastX, canvasHeight - 1)
},
})
// Grid lines drawn over the full canvas width (including the unfilled area)
canvasCtx.save()
canvasCtx.strokeStyle = gridColor
canvasCtx.lineWidth = 1
const renderGrid = drawGrid instanceof Function ? drawGrid : defaultDrawGrid
renderGrid({
canvasCtx,
canvasWidth,
canvasHeight,
gridCols,
gridRows,
createDefaultPath: () => {
canvasCtx.beginPath()
for (let i = 1; i < gridCols; i++) {
const gx = Math.round(canvasWidth * i / gridCols) + 0.5
canvasCtx.moveTo(gx, 0.5)
canvasCtx.lineTo(gx, canvasHeight - 0.5)
}
for (let i = 1; i < gridRows; i++) {
const gy = Math.round(canvasHeight * i / gridRows) + 0.5
canvasCtx.moveTo(0.5, gy)
canvasCtx.lineTo(canvasWidth - 0.5, gy)
}
},
})
canvasCtx.restore()
if (lastX) {
const avgResult = calcAverageSpeedsForResolution(
maxValue,
stepList,
{ w: canvasWidth, h: canvasHeight },
Object.assign({}, renderOptions, { ignoreTrailingSpeedSample }),
)
const {
avgWithSpeeds,
renderPointCount,
pixelsPerValue,
localMaxAvgSpeed,
} = avgResult
const resolvedMaxSpeed = (
typeof globalMaxSpeed === 'number' && globalMaxSpeed > 0
? globalMaxSpeed
: localMaxAvgSpeed
) || 1
const maxAvgSpeed = resolvedMaxSpeed
canvasCtx.save()
const endX = Math.min(canvasWidth - 0.5, Math.max(0.5, lastX + 0.5))
canvasCtx.fillStyle = colorOverlay
const renderSpeedOverlay = drawSpeedOverlay instanceof Function ? drawSpeedOverlay : defaultDrawSpeedOverlay
renderSpeedOverlay({
canvasCtx,
canvasWidth,
canvasHeight,
lastX,
endX,
avgWithSpeeds,
renderPointCount,
pixelsPerValue,
maxAvgSpeed,
createDefaultPath: () => {
canvasCtx.beginPath()
let x = 0.5
let y = canvasHeight - 0.5
const height = canvasHeight - 1
canvasCtx.moveTo(x, y)
for (let i = 0, c = renderPointCount; i < c; i++) {
const [time, value, speed] = avgWithSpeeds[i]
const valuePx = pixelsPerValue ? value * pixelsPerValue : 0
x += valuePx
let speedRatio = isZeroLikeTime(time, 1e-6) ? 1
: Math.min(1, speed / maxAvgSpeed)
y = height - (height * speedRatio) + 0.5
canvasCtx.lineTo(x, y)
}
// Keep the last measured speed until the filled progress width ends.
if (x < endX) {
canvasCtx.lineTo(endX, y)
x = endX
}
currentSpeedY = y
canvasCtx.lineTo(x, canvasHeight - 0.5)
canvasCtx.closePath()
},
})
// canvasCtx.strokeStyle = '#e00000'
// canvasCtx.stroke()
canvasCtx.restore()
}
// Speed label aligned to the right edge with a guide line at the current speed height.
if (speedLabel && Number.isFinite(currentSpeedY)) {
canvasCtx.save()
canvasCtx.font = 'bold 11px sans-serif'
const labelPaddingX = 4
const labelPaddingY = 2
const labelMetrics = canvasCtx.measureText(speedLabel)
const labelWidth = Math.ceil(labelMetrics.width)
const guideY = Math.max(10.5, Math.min(canvasHeight - 3.5, currentSpeedY))
const textX = canvasWidth - 4.5
const labelBottomY = Math.max(12.5, Math.min(canvasHeight - 2.5, guideY - 2))
const labelTopY = labelBottomY - 11 - labelPaddingY * 2
const labelLeftX = textX - labelWidth - labelPaddingX * 2
const renderSpeedLineLabel = drawSpeedLineLabel instanceof Function ? drawSpeedLineLabel : defaultDrawSpeedLineLabel
renderSpeedLineLabel({
canvasCtx,
canvasWidth,
canvasHeight,
guideY,
textX,
labelBottomY,
labelTopY,
labelLeftX,
labelWidth,
labelPaddingX,
labelPaddingY,
speedLabel,
speedLabelColor,
speedLabelBackgroundColor,
speedGuideColor,
createDefaultLinePath: ({ strokeStyle } = {}) => {
canvasCtx.beginPath()
canvasCtx.moveTo(0.5, guideY)
canvasCtx.lineTo(canvasWidth - 3.5, guideY)
canvasCtx.strokeStyle = strokeStyle ?? speedGuideColor
},
createDefaultLabelBackgroundPath: ({ fillStyle } = {}) => {
canvasCtx.beginPath()
canvasCtx.rect(
labelLeftX,
labelTopY,
labelWidth + labelPaddingX * 2,
11 + labelPaddingY * 2,
)
canvasCtx.fillStyle = fillStyle ?? speedLabelBackgroundColor
},
fillDefaultLabelText: ({ fillStyle, textAlign, textBaseline } = {}) => {
canvasCtx.fillStyle = fillStyle ?? speedLabelColor
canvasCtx.textAlign = textAlign ?? 'right'
canvasCtx.textBaseline = textBaseline ?? 'bottom'
canvasCtx.fillText(speedLabel, textX, labelBottomY)
},
})
canvasCtx.restore()
}
// Border around the entire canvas
canvasCtx.save()
canvasCtx.strokeStyle = borderColor
canvasCtx.lineWidth = 1
const renderBorder = typeof drawBorder === 'function' ? drawBorder : defaultDrawBorder
renderBorder({
canvasCtx,
canvasWidth,
canvasHeight,
borderColor,
createDefaultPath: () => {
canvasCtx.beginPath()
canvasCtx.rect(0.5, 0.5, canvasWidth - 1, canvasHeight - 1)
},
})
canvasCtx.restore()
canvasCtx.restore()
}
export {
calcAverageSpeedsForResolution,
resolveGraphMaxSpeed,
renderTransferGraphFrame,
renderStepToCanvas,
}