(
ctx: CanvasRenderingContext2D,
text: string,
maxWidth: number,
)
| 90 | const TEXTURE_STROKE_WIDTH = 8; |
| 91 | |
| 92 | function fitText( |
| 93 | ctx: CanvasRenderingContext2D, |
| 94 | text: string, |
| 95 | maxWidth: number, |
| 96 | ): string { |
| 97 | if (ctx.measureText(text).width <= maxWidth) return text; |
| 98 | |
| 99 | let lo = 0; |
| 100 | let hi = text.length; |
| 101 | while (lo < hi) { |
| 102 | const mid = Math.ceil((lo + hi) / 2); |
| 103 | const candidate = `${text.slice(0, mid)}...`; |
| 104 | if (ctx.measureText(candidate).width <= maxWidth) lo = mid; |
| 105 | else hi = mid - 1; |
| 106 | } |
| 107 | |
| 108 | return `${text.slice(0, Math.max(1, lo))}...`; |
| 109 | } |
| 110 | |
| 111 | function createLabelTexture( |
| 112 | name: string, |
no outgoing calls
no test coverage detected