-
Notifications
You must be signed in to change notification settings - Fork 4.1k
feat(module:qrcode): support nzType && nzBoostLevel, optimization nzPadding #9535
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 6 | ||
| order: 7 | ||
| title: | ||
| zh-CN: 自定义颜色 | ||
| en-US: Custom Color | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| --- | ||
| order: 5 | ||
| order: 6 | ||
| title: | ||
| zh-CN: 带衬垫 | ||
| en-US: With padding | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| order: 5 | ||
| version: 21.0.0 | ||
| title: | ||
| zh-CN: 自定义渲染类型 | ||
| en-US: Custom Render Type | ||
| --- | ||
|
|
||
| ## zh-CN | ||
|
|
||
| 通过设置 `nzType` 自定义渲染结果,提供 `canvas` 和 `svg` 两个选项。 | ||
|
|
||
| ## en-US | ||
|
|
||
| Customize the rendering results by `nzType`, provide options `canvas` and `svg`. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| import { Component } from '@angular/core'; | ||
|
|
||
| import { NzQRCodeModule } from 'ng-zorro-antd/qr-code'; | ||
|
|
||
| @Component({ | ||
| selector: 'nz-demo-qr-code-type', | ||
| imports: [NzQRCodeModule], | ||
| template: ` | ||
| <nz-qrcode nzValue="https://ng.ant.design/"></nz-qrcode> | ||
| <nz-qrcode nzValue="https://ng.ant.design/" nzType="svg"></nz-qrcode> | ||
| `, | ||
| styles: [ | ||
| ` | ||
| nz-qrcode { | ||
| margin-right: 12px; | ||
| } | ||
| ` | ||
| ] | ||
| }) | ||
| export class NzDemoQrCodeTypeComponent {} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,194 @@ | ||
| /** | ||
| * Use of this source code is governed by an MIT-style license that can be | ||
| * found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE | ||
| */ | ||
|
|
||
| import { AfterViewInit, ChangeDetectionStrategy, Component, effect, ElementRef, input, viewChild } from '@angular/core'; | ||
|
|
||
| import { CrossOrigin, Excavation, Modules } from './typing'; | ||
| import { DEFAULT_BACKGROUND_COLOR, DEFAULT_FRONT_COLOR, excavateModules, generatePath, isSupportPath2d } from './utils'; | ||
|
|
||
| @Component({ | ||
| changeDetection: ChangeDetectionStrategy.OnPush, | ||
| selector: 'nz-qrcode-canvas', | ||
| exportAs: 'nzQRCodeCanvas', | ||
| template: ` | ||
| <canvas role="img" #canvas></canvas> | ||
|
||
| @if (icon()) { | ||
| <img style="display:none;" #image alt="QR-Code" [attr.src]="this.icon()" crossorigin="anonymous" /> | ||
| } | ||
| `, | ||
| styles: ` | ||
| :host { | ||
| display: block; | ||
| line-height: 0; | ||
| } | ||
| ` | ||
| }) | ||
| export class NzQrcodeCanvasComponent implements AfterViewInit { | ||
| canvas = viewChild.required<ElementRef<HTMLCanvasElement>>('canvas'); | ||
| image = viewChild<ElementRef<HTMLImageElement>>('image'); | ||
| readonly icon = input<string>(''); | ||
| readonly margin = input<number>(0); | ||
| readonly cells = input<Modules>([]); | ||
| readonly numCells = input<number>(0); | ||
| readonly calculatedImageSettings = input<{ | ||
| x: number; | ||
| y: number; | ||
| h: number; | ||
| w: number; | ||
| excavation: Excavation | null; | ||
| opacity: number; | ||
| crossOrigin: CrossOrigin; | ||
| } | null>(null); | ||
| readonly size = input<number>(160); | ||
| readonly color = input<string>(DEFAULT_FRONT_COLOR); | ||
| readonly bgColor = input<string>(DEFAULT_BACKGROUND_COLOR); | ||
|
|
||
| constructor() { | ||
| effect(() => { | ||
Laffery marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| this.icon(); | ||
| this.margin(); | ||
| this.cells(); | ||
| this.numCells(); | ||
| this.calculatedImageSettings(); | ||
| this.size(); | ||
| this.color(); | ||
| this.bgColor(); | ||
| if (!this.canvas()?.nativeElement) { | ||
| return; | ||
| } | ||
|
|
||
| this.render(); | ||
| }); | ||
| } | ||
|
|
||
| ngAfterViewInit(): void { | ||
| this.render(); | ||
OriginRing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| private render(): void { | ||
| const canvas = this.canvas(); | ||
| if (!canvas) { | ||
| return; | ||
| } | ||
|
|
||
| const ctx = canvas.nativeElement.getContext('2d') as CanvasRenderingContext2D; | ||
| if (!ctx) { | ||
| return; | ||
| } | ||
|
|
||
| this.setupCanvas(ctx); | ||
| this.drawQRCode(ctx); | ||
| this.handleImageLoading(ctx); | ||
| } | ||
|
|
||
| private setupCanvas(ctx: CanvasRenderingContext2D): void { | ||
| const canvas = this.canvas(); | ||
| if (!canvas) { | ||
| return; | ||
| } | ||
|
|
||
| const pixelRatio = window.devicePixelRatio || 1; | ||
| canvas.nativeElement.height = canvas.nativeElement.width = this.size() * pixelRatio; | ||
| canvas.nativeElement.style.width = canvas.nativeElement.style.height = `${this.size()}px`; | ||
|
|
||
| const scale = (this.size() / this.numCells()) * pixelRatio; | ||
OriginRing marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| ctx.scale(scale, scale); | ||
|
|
||
| ctx.fillStyle = this.bgColor(); | ||
| ctx.fillRect(0, 0, this.numCells(), this.numCells()); | ||
| ctx.fillStyle = this.color(); | ||
| } | ||
|
|
||
| private drawQRCode(ctx: CanvasRenderingContext2D): void { | ||
| const cellsToDraw = this.getCellsToDraw(); | ||
| const haveImageToRender = this.haveImageToRender(); | ||
|
|
||
| if (!haveImageToRender) { | ||
| this.renderQRCode(ctx, cellsToDraw); | ||
| } | ||
| } | ||
|
|
||
| private getCellsToDraw(): Modules { | ||
| let cellsToDraw = this.cells(); | ||
| const imageSettings = this.calculatedImageSettings(); | ||
| if (this.haveImageToRender() && imageSettings && imageSettings.excavation) { | ||
| cellsToDraw = excavateModules(this.cells(), imageSettings.excavation); | ||
| } | ||
| return cellsToDraw; | ||
| } | ||
|
|
||
| private haveImageToRender(): boolean { | ||
| return this.calculatedImageSettings() != null && !!this.image(); | ||
| } | ||
|
|
||
| private renderQRCode(ctx: CanvasRenderingContext2D, cells: Modules): void { | ||
| if (isSupportPath2d) { | ||
| ctx.fill(new Path2D(generatePath(cells, this.margin()))); | ||
| } else { | ||
| cells.forEach((row, rdx) => { | ||
| row.forEach((cell, cdx) => { | ||
| if (cell) { | ||
| ctx.fillRect(cdx + this.margin(), rdx + this.margin(), 1, 1); | ||
| } | ||
| }); | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| private handleImageLoading(ctx: CanvasRenderingContext2D): void { | ||
| if (!this.haveImageToRender()) { | ||
| return; | ||
| } | ||
|
|
||
| const image = this.image(); | ||
| if (!image) { | ||
| return; | ||
| } | ||
|
|
||
| const onLoad = (): void => { | ||
| this.cleanupImageListeners(onLoad, onError); | ||
| this.onImageLoadSuccess(ctx); | ||
| }; | ||
|
|
||
| const onError = (): void => { | ||
| this.cleanupImageListeners(onLoad, onError); | ||
| this.onImageLoadError(ctx); | ||
| }; | ||
|
|
||
| image.nativeElement.addEventListener('load', onLoad); | ||
| image.nativeElement.addEventListener('error', onError); | ||
| } | ||
|
|
||
| private onImageLoadSuccess(ctx: CanvasRenderingContext2D): void { | ||
| const cellsToDraw = this.getCellsToDraw(); | ||
| this.renderQRCode(ctx, cellsToDraw); | ||
|
|
||
| const imageSettings = this.calculatedImageSettings(); | ||
| const image = this.image(); | ||
| if (imageSettings && image) { | ||
| ctx.globalAlpha = imageSettings.opacity; | ||
| ctx.drawImage( | ||
| image.nativeElement, | ||
| imageSettings.x + this.margin(), | ||
| imageSettings.y + this.margin(), | ||
| imageSettings.w, | ||
| imageSettings.h | ||
| ); | ||
| } | ||
| } | ||
|
|
||
| private onImageLoadError(ctx: CanvasRenderingContext2D): void { | ||
| const cellsToDraw = this.getCellsToDraw(); | ||
| this.renderQRCode(ctx, cellsToDraw); | ||
| } | ||
|
|
||
| private cleanupImageListeners(onLoad: () => void, onError: () => void): void { | ||
| const image = this.image(); | ||
| if (image?.nativeElement) { | ||
| image.nativeElement.removeEventListener('load', onLoad); | ||
| image.nativeElement.removeEventListener('error', onError); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.