fix(graph): fix focusItem will fail when fitView

This commit is contained in:
yilin.qyl 2019-02-28 16:09:31 +08:00
parent 3e93a1ac51
commit 260142ef86
2 changed files with 6 additions and 8 deletions

View File

@ -37,7 +37,8 @@ class View {
}
focusPoint(point) {
const viewCenter = this._getViewCenter();
this.graph.translate(viewCenter.x - point.x, viewCenter.y - point.y);
const modelCenter = this.getPointByCanvas(viewCenter.x, viewCenter.y);
this.graph.translate(modelCenter.x - point.x, modelCenter.y - point.y);
}
getPointByClient(clientX, clientY) {
const canvas = this.graph.get('canvas');
@ -66,11 +67,9 @@ class View {
item = this.graph.findById[item];
}
if (item) {
const model = item.get('model');
const matrix = item.get('group').getMatrix();
const x = model.x * matrix[0] + matrix[6];
const y = model.y * matrix[4] + matrix[7];
this.focusPoint({ x, y });
// 用实际位置而不是model中的x,y,防止由于拖拽等的交互导致model的x,y并不是当前的x,y
this.focusPoint({ x: matrix[6], y: matrix[7] });
}
}
changeSize(width, height) {

View File

@ -75,11 +75,10 @@ describe('view', () => {
graph.clear();
graph.zoom(2, { x: 50, y: 50 });
const node = graph.addItem('node', { shape: 'circle', x: 50, y: 50, size: 60, color: '#666' });
graph.paint();
graph.focusItem(node);
const bbox = graph.get('canvas').getBBox();
expect(bbox.x).to.equal(139);
expect(bbox.y).to.equal(139);
expect(bbox.x).to.equal(89);
expect(bbox.y).to.equal(89);
expect(bbox.width).to.equal(122);
});
});