From 874d415903a7e85d001cfcce2268a69164234414 Mon Sep 17 00:00:00 2001 From: antv Date: Mon, 4 Nov 2024 10:56:06 +0800 Subject: [PATCH] fix(graph): fix resize logic --- packages/g6/src/runtime/graph.ts | 23 +++++++++++-------- .../behavior/update-label/demo/changeImg.js | 2 -- 2 files changed, 14 insertions(+), 11 deletions(-) diff --git a/packages/g6/src/runtime/graph.ts b/packages/g6/src/runtime/graph.ts index 36df5904ae0..928dd699693 100644 --- a/packages/g6/src/runtime/graph.ts +++ b/packages/g6/src/runtime/graph.ts @@ -155,16 +155,18 @@ export class Graph extends EventEmitter { } /** - * 获取当前画布容器的尺寸 + * 设置当前画布容器的尺寸 * - * Get the size of the current canvas container + * Set the size of the current canvas container * @param width - 画布宽度 | canvas width * @param height - 画布高度 | canvas height * @apiCategory canvas */ public setSize(width: number, height: number): void { - Object.assign(this.options, { width, height }); - this.context.canvas?.resize(width, height); + if (width) this.options.width = width; + if (height) this.options.height = height; + + this.resize(width, height); } /** @@ -1242,11 +1244,14 @@ export class Graph extends EventEmitter { */ public resize(width: number, height: number): void; public resize(width?: number, height?: number): void { - const size: Vector2 = !width || !height ? sizeOf(this.context.canvas!.getContainer()!) : [width, height]; - if (isEqual(size, this.getSize())) return; - emit(this, new GraphLifeCycleEvent(GraphEvent.BEFORE_SIZE_CHANGE, { size })); - this.context.canvas.resize(...size); - emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_SIZE_CHANGE, { size })); + const containerSize = sizeOf(this.context.canvas!.getContainer()!); + const canvasSize = this.context.canvas!.getSize(); + const specificSize: Vector2 = [width || containerSize[0], height || containerSize[1]]; + + if (isEqual(specificSize, canvasSize)) return; + emit(this, new GraphLifeCycleEvent(GraphEvent.BEFORE_SIZE_CHANGE, { size: specificSize })); + this.context.canvas.resize(...specificSize); + emit(this, new GraphLifeCycleEvent(GraphEvent.AFTER_SIZE_CHANGE, { size: specificSize })); } /** diff --git a/packages/site/examples/behavior/update-label/demo/changeImg.js b/packages/site/examples/behavior/update-label/demo/changeImg.js index 4a669cbfe8f..6a105c5aec8 100644 --- a/packages/site/examples/behavior/update-label/demo/changeImg.js +++ b/packages/site/examples/behavior/update-label/demo/changeImg.js @@ -99,5 +99,3 @@ graph.on('node:pointerleave', (event) => { }, }); }); - -window.graph = graph; \ No newline at end of file