Skip to content

Commit

Permalink
refactor(plugins): adjust minimap to avoid flash in some scene (#6316)
Browse files Browse the repository at this point in the history
Co-authored-by: antv <antv@antfin.com>
  • Loading branch information
Aarebecca and antv authored Sep 12, 2024
1 parent 9221e92 commit f2697cd
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 11 deletions.
2 changes: 1 addition & 1 deletion packages/g6/__tests__/demos/plugin-minimap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ export const pluginMinimap: TestCase = async (context) => {
const graph = new Graph({
...context,
data: { nodes: Array.from({ length: 20 }).map((_, i) => ({ id: `node${i}` })) },
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element'],
behaviors: ['drag-canvas', 'zoom-canvas', 'drag-element', 'hover-activate'],
plugins: [
{
key: 'minimap',
Expand Down
41 changes: 31 additions & 10 deletions packages/g6/src/plugins/minimap/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Canvas, DisplayObject, IRenderer, Landmark } from '@antv/g';
import { Renderer } from '@antv/g-canvas';
import { throttle } from '@antv/util';
import { debounce, throttle } from '@antv/util';
import { GraphEvent } from '../../constants';
import type { RuntimeContext } from '../../runtime/types';
import { GraphData } from '../../spec';
Expand Down Expand Up @@ -93,6 +93,13 @@ export interface MinimapOptions extends BasePluginOptions {
* <en/> Renderer, default to use Canvas renderer
*/
renderer?: IRenderer;
/**
* <zh/> 延迟更新时间(毫秒),用于性能优化
*
* <en/> Delay update time(ms), used for performance optimization
* @defaultValue 128
*/
delay?: number;
}

/**
Expand All @@ -114,15 +121,35 @@ export class Minimap extends BasePlugin<MinimapOptions> {
border: '1px solid #ddd',
background: '#fff',
},
delay: 128,
};

private canvas!: Canvas;

constructor(context: RuntimeContext, options: MinimapOptions) {
super(context, Object.assign({}, Minimap.defaultOptions, options));
this.setOnRender();
this.bindEvents();
}

public update(options: Partial<MinimapOptions>): void {
this.unbindEvents();
super.update(options);
if ('delay' in options) this.setOnRender();
this.bindEvents();
}

private setOnRender() {
this.onRender = debounce(
() => {
this.renderMinimap();
this.renderMask();
},
this.options.delay,
true,
);
}

private bindEvents() {
const { graph } = this.context;
graph.on(GraphEvent.AFTER_DRAW, this.onDraw);
Expand All @@ -142,14 +169,7 @@ export class Minimap extends BasePlugin<MinimapOptions> {
this.onRender();
};

private onRender = throttle(
() => {
this.renderMinimap();
this.renderMask();
},
32,
{ leading: true },
) as () => void;
private onRender!: () => void;

private shapes = new Map<ID, DisplayObject>();

Expand Down Expand Up @@ -296,7 +316,8 @@ export class Minimap extends BasePlugin<MinimapOptions> {
} = this.options;

if (this.canvas) {
this.canvas.resize(width, height);
const { width: w, height: h } = this.canvas.getConfig();
if (width !== w || height !== h) this.canvas.resize(width, height);
if (renderer) this.canvas.setRenderer(renderer);
} else {
const dom = document.createElement('div');
Expand Down

0 comments on commit f2697cd

Please sign in to comment.