* Resize a canvas backing store to match its CSS layout width, clear it, * and return a draw context. Returns `null` if the canvas has no layout * width yet (e.g. panel hidden, first frame). * @param {HTMLCanvasElement} canvas * @returns {{ ctx: CanvasRenderingContext2D, w: number, h: number, ba
(canvas)
| 9 | * @returns {{ ctx: CanvasRenderingContext2D, w: number, h: number, barW: number } | null} |
| 10 | */ |
| 11 | function prepareGraph(canvas) { |
| 12 | const w = canvas.clientWidth; |
| 13 | if (w === 0) { |
| 14 | return null; |
| 15 | } |
| 16 | if (canvas.width !== w) { |
| 17 | canvas.width = w; |
| 18 | } |
| 19 | const ctx = canvas.getContext("2d"); |
| 20 | const h = GRAPH_HEIGHT; |
| 21 | ctx.clearRect(0, 0, w, h); |
| 22 | return { ctx, w, h, barW: w / GRAPH_SAMPLES }; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Round a peak value up to the nearest multiple of 4, with a floor of 4. |
no test coverage detected