()
| 51 | } |
| 52 | |
| 53 | export default function makerQRCodeImg() { |
| 54 | const qrcodepostmaker = document.createElement("canvas") |
| 55 | qrcodepostmaker.id = "qrcodepostmaker" |
| 56 | qrcodepostmaker.style.display = "none" |
| 57 | document.body.appendChild(qrcodepostmaker) |
| 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 = ( |
| 85 | textArr: string[], |
| 86 | Style: string, |
| 87 | align: CanvasTextAlign, |
| 88 | color: string, |
| 89 | borderColor: string, |
| 90 | fontSize: number, |
| 91 | lineHeight: number, |
| 92 | pos: Position, |
| 93 | ctx: CanvasRenderingContext2D |
| 94 | ): Position => { |
| 95 | ctx.font = Style |
| 96 | ctx.textAlign = align |
| 97 | ctx.fillStyle = color |
| 98 | ctx.strokeStyle = borderColor |
| 99 | for (const text of textArr) { |
| 100 | pos.y += fontSize * lineHeight |
| 101 | ctx.fillText(text, pos.x, pos.y) |
| 102 | } |
| 103 | return pos |
| 104 | } |
| 105 | |
| 106 | // 生成海报 |
| 107 | const makerQRPost = (): void => { |
| 108 | const canvas = document.querySelector<HTMLCanvasElement>("#qrcodepostmaker") |
| 109 | const ctx = canvas.getContext("2d") |
| 110 | if (!ctx) return |
nothing calls this directly
no test coverage detected