Skip to content

Commit

Permalink
fix(graph): fix resize logic
Browse files Browse the repository at this point in the history
  • Loading branch information
antv committed Nov 4, 2024
1 parent 0ef8e8f commit 874d415
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 11 deletions.
23 changes: 14 additions & 9 deletions packages/g6/src/runtime/graph.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,16 +155,18 @@ export class Graph extends EventEmitter {
}

/**
* <zh/> 获取当前画布容器的尺寸
* <zh/> 设置当前画布容器的尺寸
*
* <en/> Get the size of the current canvas container
* <en/> Set the size of the current canvas container
* @param width - <zh/> 画布宽度 | <en/> canvas width
* @param height - <zh/> 画布高度 | <en/> 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);
}

/**
Expand Down Expand Up @@ -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 }));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,5 +99,3 @@ graph.on('node:pointerleave', (event) => {
},
});
});

window.graph = graph;

0 comments on commit 874d415

Please sign in to comment.