|
| 1 | +export class DotTextCanvas { |
| 2 | + canvas: HTMLCanvasElement |
| 3 | + ctx: CanvasRenderingContext2D |
| 4 | + points: Map<string, Array<number[]>> = new Map() |
| 5 | + originText: string |
| 6 | + fontSize: number |
| 7 | + color: string |
| 8 | + fontWeight: number |
| 9 | + constructor(text: string, fontSize: number, color: string, fontWeight: number) { |
| 10 | + this.originText = text |
| 11 | + this.fontSize = fontSize |
| 12 | + this.color = color |
| 13 | + this.fontWeight = fontWeight |
| 14 | + this.createTextPoint(text) |
| 15 | + const [canvas, ctx] = this.executor() |
| 16 | + this.canvas = canvas |
| 17 | + this.ctx = ctx |
| 18 | + } |
| 19 | + |
| 20 | + createTextPoint(text: string) { |
| 21 | + const canvas = document.createElement('canvas'); const ctx = canvas.getContext('2d')!; const size = 16 |
| 22 | + canvas.width = canvas.height = size |
| 23 | + ctx.font = `${size}px SimSun` |
| 24 | + ctx.fillText(text, 0, 14) |
| 25 | + const canvasData = ctx.getImageData(0, 0, size, size).data |
| 26 | + const textPointSet = [] |
| 27 | + for (let i = 0; i < size; i++) { |
| 28 | + const temp: number[] = [] |
| 29 | + textPointSet.push(temp) |
| 30 | + for (let j = 0; j < size; j++) { |
| 31 | + const index = i * size * 4 + j * 4 |
| 32 | + temp.push(canvasData[index + 3] ? 1 : 0) |
| 33 | + } |
| 34 | + } |
| 35 | + this.points.set(text, textPointSet) |
| 36 | + return textPointSet |
| 37 | + } |
| 38 | + |
| 39 | + executor(): [HTMLCanvasElement, CanvasRenderingContext2D] { |
| 40 | + this.originText.split('').forEach(text => this.getText(text)) |
| 41 | + return this.getCanvas(this.combineText()) |
| 42 | + } |
| 43 | + |
| 44 | + getText(text: string) { |
| 45 | + return this.points.has(text) |
| 46 | + ? this.points.get(text) |
| 47 | + : this.createTextPoint(text) |
| 48 | + } |
| 49 | + |
| 50 | + combineText() { |
| 51 | + const result: Array<number[]> = [[]] |
| 52 | + const len = this.originText.length |
| 53 | + |
| 54 | + for (let i = 0; i < len; i++) { |
| 55 | + (this.points.get(this.originText[i]) || []).forEach((item, index) => { |
| 56 | + result[index] = (result[index] || []).concat(item) |
| 57 | + }) |
| 58 | + } |
| 59 | + return result |
| 60 | + } |
| 61 | + |
| 62 | + getCanvas(textPointSet: Array<number[]>): [HTMLCanvasElement, CanvasRenderingContext2D] { |
| 63 | + const canvas: HTMLCanvasElement = document.createElement('canvas') |
| 64 | + const ctx: CanvasRenderingContext2D = canvas.getContext('2d')! |
| 65 | + const h = textPointSet.length |
| 66 | + const w = textPointSet[0].length |
| 67 | + const oneTempLength = this.fontSize / h |
| 68 | + canvas.height = this.fontSize |
| 69 | + canvas.width = this.fontSize * this.originText.length |
| 70 | + for (let i = 0; i < h; i++) { |
| 71 | + for (let j = 0; j < w; j++) { |
| 72 | + if (textPointSet[i][j]) { |
| 73 | + ctx.beginPath() |
| 74 | + ctx.arc(oneTempLength * (j + 0.5), oneTempLength * (i + 0.5), oneTempLength * this.fontWeight / h, 0, Math.PI * 2) |
| 75 | + ctx.fillStyle = this.color |
| 76 | + ctx.fill() |
| 77 | + } |
| 78 | + } |
| 79 | + } |
| 80 | + return [canvas, ctx] |
| 81 | + } |
| 82 | + |
| 83 | + clearCanvas() { |
| 84 | + this.ctx.clearRect(0, 0, this.canvas.width, this.canvas.height) |
| 85 | + } |
| 86 | +} |
0 commit comments