Skip to content

Commit 1ceaf70

Browse files
authored
perf(module:graph): do not run change detection on animation frame (#7132)
1 parent 4484674 commit 1ceaf70

2 files changed

Lines changed: 17 additions & 10 deletions

File tree

components/graph/core/minimap.ts

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6+
import { NgZone } from '@angular/core';
7+
68
import { drag } from 'd3-drag';
79
import { pointer, select } from 'd3-selection';
810
import { ZoomBehavior, zoomIdentity, ZoomTransform } from 'd3-zoom';
911

12+
import { reqAnimFrame } from 'ng-zorro-antd/core/polyfill';
1013
import { NzSafeAny } from 'ng-zorro-antd/core/types';
1114

1215
import { NzZoomTransform } from '../interface';
@@ -28,6 +31,7 @@ export class Minimap {
2831
private unlisteners: VoidFunction[] = [];
2932

3033
constructor(
34+
private ngZone: NgZone,
3135
private svg: SVGSVGElement,
3236
private zoomG: SVGGElement,
3337
private mainZoom: ZoomBehavior<NzSafeAny, NzSafeAny>,
@@ -174,7 +178,7 @@ export class Minimap {
174178
if (this.translate != null && this.zoom != null) {
175179
// Update the viewpoint rectangle shape since the aspect ratio of the
176180
// map has changed.
177-
requestAnimationFrame(() => this.zoom());
181+
this.ngZone.runOutsideAngular(() => reqAnimFrame(() => this.zoom()));
178182
}
179183

180184
// Serialize the main svg to a string which will be used as the rendering
@@ -196,12 +200,15 @@ export class Minimap {
196200
context!.clearRect(0, 0, this.canvasBuffer.width, this.canvasBuffer.height);
197201

198202
context!.drawImage(image, minimapOffset.x, minimapOffset.y, this.minimapSize.width, this.minimapSize.height);
199-
requestAnimationFrame(() => {
200-
// Hide the old canvas and show the new buffer canvas.
201-
select(this.canvasBuffer).style('display', 'block');
202-
select(this.canvas).style('display', 'none');
203-
// Swap the two canvases.
204-
[this.canvas, this.canvasBuffer] = [this.canvasBuffer, this.canvas];
203+
204+
this.ngZone.runOutsideAngular(() => {
205+
reqAnimFrame(() => {
206+
// Hide the old canvas and show the new buffer canvas.
207+
select(this.canvasBuffer).style('display', 'block');
208+
select(this.canvas).style('display', 'none');
209+
// Swap the two canvases.
210+
[this.canvas, this.canvasBuffer] = [this.canvasBuffer, this.canvas];
211+
});
205212
});
206213
};
207214

components/graph/graph-minimap.component.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* found in the LICENSE file at https://github.com/NG-ZORRO/ng-zorro-antd/blob/master/LICENSE
44
*/
55

6-
import { ChangeDetectionStrategy, Component, ElementRef, OnDestroy } from '@angular/core';
6+
import { ChangeDetectionStrategy, Component, ElementRef, NgZone, OnDestroy } from '@angular/core';
77

88
import { ZoomBehavior } from 'd3-zoom';
99

@@ -42,7 +42,7 @@ import { NzZoomTransform } from './interface';
4242
})
4343
export class NzGraphMinimapComponent implements OnDestroy {
4444
minimap?: Minimap;
45-
constructor(private elementRef: ElementRef<HTMLElement>) {}
45+
constructor(private elementRef: ElementRef<HTMLElement>, private ngZone: NgZone) {}
4646

4747
ngOnDestroy(): void {
4848
this.minimap?.destroy();
@@ -51,7 +51,7 @@ export class NzGraphMinimapComponent implements OnDestroy {
5151
init(containerEle: ElementRef, zoomBehavior: ZoomBehavior<NzSafeAny, NzSafeAny>): void {
5252
const svgEle = containerEle.nativeElement.querySelector('svg');
5353
const zoomEle = containerEle.nativeElement.querySelector('svg > g');
54-
this.minimap = new Minimap(svgEle, zoomEle, zoomBehavior, this.elementRef.nativeElement, 150, 0);
54+
this.minimap = new Minimap(this.ngZone, svgEle, zoomEle, zoomBehavior, this.elementRef.nativeElement, 150, 0);
5555
}
5656

5757
zoom(transform: NzZoomTransform): void {

0 commit comments

Comments
 (0)