( ctx: CanvasRenderingContext2D, text: string, maxWidth: number, )
| 24 | const TEXTURE_STROKE_WIDTH = 8; |
| 25 | |
| 26 | function fitText( |
| 27 | ctx: CanvasRenderingContext2D, |
| 28 | text: string, |
| 29 | maxWidth: number, |
| 30 | ): string { |
| 31 | if (ctx.measureText(text).width <= maxWidth) return text; |
| 32 | |
| 33 | let lo = 0; |
| 34 | let hi = text.length; |
| 35 | while (lo < hi) { |
| 36 | const mid = Math.ceil((lo + hi) / 2); |
| 37 | const candidate = `${text.slice(0, mid)}...`; |
| 38 | if (ctx.measureText(candidate).width <= maxWidth) lo = mid; |
| 39 | else hi = mid - 1; |
| 40 | } |
| 41 | |
| 42 | return `${text.slice(0, Math.max(1, lo))}...`; |
| 43 | } |
| 44 | |
| 45 | function createLabelTexture(name: string, color: string): LabelTexture | null { |
| 46 | const canvas = document.createElement("canvas"); |
no outgoing calls
no test coverage detected