(canvas, memHistory, historyIndex, peakEl)
| 103 | * @param {HTMLElement} peakEl - element to display peak value |
| 104 | */ |
| 105 | export function drawMemGraph(canvas, memHistory, historyIndex, peakEl) { |
| 106 | const g = prepareGraph(canvas); |
| 107 | if (g === null) { |
| 108 | return; |
| 109 | } |
| 110 | const { ctx, w, h, barW } = g; |
| 111 | |
| 112 | let peak = 1; |
| 113 | for (let i = 0; i < GRAPH_SAMPLES; i++) { |
| 114 | if (memHistory[i] > peak) { |
| 115 | peak = memHistory[i]; |
| 116 | } |
| 117 | } |
| 118 | peak = roundPeak(peak); |
| 119 | peakEl.textContent = `peak ${peak}MB`; |
| 120 | |
| 121 | // filled area graph |
| 122 | ctx.fillStyle = "rgba(198,120,221,0.3)"; |
| 123 | ctx.strokeStyle = "#c678dd"; |
| 124 | ctx.lineWidth = 1; |
| 125 | ctx.beginPath(); |
| 126 | ctx.moveTo(0, h); |
| 127 | for (let i = 0; i < GRAPH_SAMPLES; i++) { |
| 128 | const idx = (historyIndex + i) % GRAPH_SAMPLES; |
| 129 | const y = h - (memHistory[idx] / peak) * h; |
| 130 | const x = Math.round(i * barW); |
| 131 | ctx.lineTo(x, y); |
| 132 | } |
| 133 | ctx.lineTo(w, h); |
| 134 | ctx.closePath(); |
| 135 | ctx.fill(); |
| 136 | |
| 137 | // stroke the top edge |
| 138 | ctx.beginPath(); |
| 139 | for (let i = 0; i < GRAPH_SAMPLES; i++) { |
| 140 | const idx = (historyIndex + i) % GRAPH_SAMPLES; |
| 141 | const y = h - (memHistory[idx] / peak) * h; |
| 142 | const x = Math.round(i * barW); |
| 143 | if (i === 0) { |
| 144 | ctx.moveTo(x, y); |
| 145 | } else { |
| 146 | ctx.lineTo(x, y); |
| 147 | } |
| 148 | } |
| 149 | ctx.stroke(); |
| 150 | } |
no test coverage detected