在项目中使用了jointjs绘制高交互流程图,需要根据鼠标滚轮滚动实现放大缩小功能,缩放中心在页面鼠标处。最终核心实现代码如下:
handleCellMouseWheel = (cellView, e, x, y, delta) => {
e.preventDefault();
const oldScale = this.paper.scale().sx;
const newScale = oldScale + delta * .1;
scaleToPoint(newScale, x, y);
};
scaleToPoint = (nextScale, x, y) => {
if (nextScale >= MIN_SCALE && nextScale <= MAX_SCALE) {
const currentScale = this.paper.scale().sx;
const beta = currentScale / nextScale;
const ax = x - (x * beta);
const ay = y - (y * beta);
const translate = this.paper.translate();
const nextTx = translate.tx - ax * nextScale;
const nextTy = translate.ty - ay * nextScale;
this.paper.translate(nextTx, nextTy);
const ctm = this.paper.matrix();
ctm.a = nextScale;
ctm.d = nextScale;
this.paper.matrix(ctm);
}
};