Function
textComputer
(
text: string,
style: string,
width: number,
ctx: CanvasRenderingContext2D
)
Source from the content-addressed store, hash-verified
| 58 | |
| 59 | // 文本换行处理 |
| 60 | const textComputer = ( |
| 61 | text: string, |
| 62 | style: string, |
| 63 | width: number, |
| 64 | ctx: CanvasRenderingContext2D |
| 65 | ): string[] => { |
| 66 | if (text.replace(/\s/g, "").length === 0) { |
| 67 | return [] |
| 68 | } |
| 69 | ctx.font = style |
| 70 | let temp = 0 |
| 71 | const row: string[] = [] |
| 72 | for (let i = 0; i <= text.length; i++) { |
| 73 | if (ctx.measureText(text.substring(temp, i)).width >= width) { |
| 74 | row.push(text.substring(temp, i - 1)) |
| 75 | temp = i - 1 |
| 76 | } else if (i === text.length) { |
| 77 | row.push(text.substring(temp, i)) |
| 78 | } |
| 79 | } |
| 80 | return row |
| 81 | } |
| 82 | |
| 83 | // 绘制文本 |
| 84 | const textWriter = ( |
Tested by
no test coverage detected