|
| 1 | +/** |
| 2 | + * Use of this source code is governed by an MIT-style license that can be |
| 3 | + * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE |
| 4 | + */ |
| 5 | + |
| 6 | +import { AfterViewInit, ChangeDetectionStrategy, Component, effect, ElementRef, input, ViewChild } from '@angular/core'; |
| 7 | + |
| 8 | +import { NzButtonModule } from 'ng-zorro-antd/button'; |
| 9 | +import { NzIconModule } from 'ng-zorro-antd/icon'; |
| 10 | +import { NzSpinModule } from 'ng-zorro-antd/spin'; |
| 11 | + |
| 12 | +import { CrossOrigin, Excavation, Modules } from './typing'; |
| 13 | +import { DEFAULT_BACKGROUND_COLOR, DEFAULT_FRONT_COLOR, excavateModules, generatePath, isSupportPath2d } from './utils'; |
| 14 | + |
| 15 | +@Component({ |
| 16 | + changeDetection: ChangeDetectionStrategy.OnPush, |
| 17 | + selector: 'nz-qrcode-canvas', |
| 18 | + exportAs: 'nzQRCodeCanvas', |
| 19 | + template: ` |
| 20 | + <canvas role="img" #canvas></canvas> |
| 21 | + @if (icon()) { |
| 22 | + <img style="display:none;" #image alt="QR-Code" [attr.src]="this.icon()" crossorigin="anonymous" /> |
| 23 | + } |
| 24 | + `, |
| 25 | + styles: ` |
| 26 | + :host { |
| 27 | + display: block; |
| 28 | + line-height: 0; |
| 29 | + } |
| 30 | + `, |
| 31 | + imports: [NzSpinModule, NzButtonModule, NzIconModule] |
| 32 | +}) |
| 33 | +export class NzQrcodeCanvasComponent implements AfterViewInit { |
| 34 | + @ViewChild('canvas', { static: false }) canvas!: ElementRef<HTMLCanvasElement>; |
| 35 | + @ViewChild('image', { static: false }) image!: ElementRef<HTMLImageElement>; |
| 36 | + readonly icon = input<string>(''); |
| 37 | + readonly margin = input<number>(0); |
| 38 | + readonly cells = input<Modules>([]); |
| 39 | + readonly numCells = input<number>(0); |
| 40 | + readonly calculatedImageSettings = input<{ |
| 41 | + x: number; |
| 42 | + y: number; |
| 43 | + h: number; |
| 44 | + w: number; |
| 45 | + excavation: Excavation | null; |
| 46 | + opacity: number; |
| 47 | + crossOrigin: CrossOrigin; |
| 48 | + } | null>(null); |
| 49 | + readonly size = input<number>(160); |
| 50 | + readonly color = input<string>(DEFAULT_FRONT_COLOR); |
| 51 | + readonly bgColor = input<string>(DEFAULT_BACKGROUND_COLOR); |
| 52 | + |
| 53 | + constructor() { |
| 54 | + effect(() => { |
| 55 | + this.icon(); |
| 56 | + this.margin(); |
| 57 | + this.cells(); |
| 58 | + this.numCells(); |
| 59 | + this.calculatedImageSettings(); |
| 60 | + this.size(); |
| 61 | + this.color(); |
| 62 | + this.bgColor(); |
| 63 | + if (!this.canvas?.nativeElement) { |
| 64 | + return; |
| 65 | + } |
| 66 | + |
| 67 | + this.render(); |
| 68 | + }); |
| 69 | + } |
| 70 | + |
| 71 | + ngAfterViewInit(): void { |
| 72 | + this.render(); |
| 73 | + } |
| 74 | + |
| 75 | + private render(): void { |
| 76 | + const ctx = this.canvas.nativeElement.getContext('2d') as CanvasRenderingContext2D; |
| 77 | + if (!ctx) { |
| 78 | + return; |
| 79 | + } |
| 80 | + |
| 81 | + this.setupCanvas(ctx); |
| 82 | + this.drawQRCode(ctx); |
| 83 | + this.handleImageLoading(ctx); |
| 84 | + } |
| 85 | + |
| 86 | + private setupCanvas(ctx: CanvasRenderingContext2D): void { |
| 87 | + const pixelRatio = window.devicePixelRatio || 1; |
| 88 | + this.canvas.nativeElement.height = this.canvas.nativeElement.width = this.size() * pixelRatio; |
| 89 | + this.canvas.nativeElement.style.width = this.canvas.nativeElement.style.height = `${this.size()}px`; |
| 90 | + |
| 91 | + const scale = (this.size() / this.numCells()) * pixelRatio; |
| 92 | + ctx.scale(scale, scale); |
| 93 | + |
| 94 | + ctx.fillStyle = this.bgColor(); |
| 95 | + ctx.fillRect(0, 0, this.numCells(), this.numCells()); |
| 96 | + ctx.fillStyle = this.color(); |
| 97 | + } |
| 98 | + |
| 99 | + private drawQRCode(ctx: CanvasRenderingContext2D): void { |
| 100 | + const cellsToDraw = this.getCellsToDraw(); |
| 101 | + const haveImageToRender = this.haveImageToRender(); |
| 102 | + |
| 103 | + if (!haveImageToRender) { |
| 104 | + this.renderQRCode(ctx, cellsToDraw); |
| 105 | + } |
| 106 | + } |
| 107 | + |
| 108 | + private getCellsToDraw(): Modules { |
| 109 | + let cellsToDraw = this.cells(); |
| 110 | + const imageSettings = this.calculatedImageSettings(); |
| 111 | + if (this.haveImageToRender() && imageSettings && imageSettings.excavation) { |
| 112 | + cellsToDraw = excavateModules(this.cells(), imageSettings.excavation); |
| 113 | + } |
| 114 | + return cellsToDraw; |
| 115 | + } |
| 116 | + |
| 117 | + private haveImageToRender(): boolean { |
| 118 | + return this.calculatedImageSettings != null && !!this.image; |
| 119 | + } |
| 120 | + |
| 121 | + private renderQRCode(ctx: CanvasRenderingContext2D, cells: Modules): void { |
| 122 | + if (isSupportPath2d) { |
| 123 | + ctx.fill(new Path2D(generatePath(cells, this.margin()))); |
| 124 | + } else { |
| 125 | + cells.forEach((row, rdx) => { |
| 126 | + row.forEach((cell, cdx) => { |
| 127 | + if (cell) { |
| 128 | + ctx.fillRect(cdx + this.margin(), rdx + this.margin(), 1, 1); |
| 129 | + } |
| 130 | + }); |
| 131 | + }); |
| 132 | + } |
| 133 | + } |
| 134 | + |
| 135 | + private handleImageLoading(ctx: CanvasRenderingContext2D): void { |
| 136 | + if (!this.haveImageToRender()) { |
| 137 | + return; |
| 138 | + } |
| 139 | + |
| 140 | + const onLoad = (): void => { |
| 141 | + this.cleanupImageListeners(onLoad, onError); |
| 142 | + this.onImageLoadSuccess(ctx); |
| 143 | + }; |
| 144 | + |
| 145 | + const onError = (): void => { |
| 146 | + this.cleanupImageListeners(onLoad, onError); |
| 147 | + this.onImageLoadError(ctx); |
| 148 | + }; |
| 149 | + |
| 150 | + this.image.nativeElement.addEventListener('load', onLoad); |
| 151 | + this.image.nativeElement.addEventListener('error', onError); |
| 152 | + } |
| 153 | + |
| 154 | + private onImageLoadSuccess(ctx: CanvasRenderingContext2D): void { |
| 155 | + const cellsToDraw = this.getCellsToDraw(); |
| 156 | + this.renderQRCode(ctx, cellsToDraw); |
| 157 | + |
| 158 | + const imageSettings = this.calculatedImageSettings(); |
| 159 | + if (imageSettings) { |
| 160 | + ctx.globalAlpha = imageSettings.opacity; |
| 161 | + ctx.drawImage( |
| 162 | + this.image.nativeElement, |
| 163 | + imageSettings.x + this.margin(), |
| 164 | + imageSettings.y + this.margin(), |
| 165 | + imageSettings.w, |
| 166 | + imageSettings.h |
| 167 | + ); |
| 168 | + } |
| 169 | + } |
| 170 | + |
| 171 | + private onImageLoadError(ctx: CanvasRenderingContext2D): void { |
| 172 | + const cellsToDraw = this.getCellsToDraw(); |
| 173 | + this.renderQRCode(ctx, cellsToDraw); |
| 174 | + } |
| 175 | + |
| 176 | + private cleanupImageListeners(onLoad: () => void, onError: () => void): void { |
| 177 | + if (this.image?.nativeElement) { |
| 178 | + this.image.nativeElement.removeEventListener('load', onLoad); |
| 179 | + this.image.nativeElement.removeEventListener('error', onError); |
| 180 | + } |
| 181 | + } |
| 182 | +} |
0 commit comments