How to center paper scaling JointJS

I implemented paper scaling and it works great.

I associated it with scrolling the mouse wheel, but I still encounter one problem: in the API, the scale function is defined as scale paper.scale(sx, sy, [ox, oy]), and I decided that ox and oy can center the scaling in a certain position. In my case, this position should be a pointer. But although I transmit the coordinates ( offsetXand offsetYmouse events), it has absolutely no effect.

Can someone give me an example of how to use oxand oy?

+4
source share
1 answer

Remember to first convert the mouse coordinates to the coordinate system of the viewport.

function offsetToLocalPoint(offsetX, offsetY, paper) {
    var svgPoint = paper.svg.createSVGPoint();
    svgPoint.x = offsetX;
    svgPoint.y = offsetY;
    var offsetTransformed = svgPoint.matrixTransform(paper.viewport.getCTM().inverse());
    return offsetTransformed
}

reset translate , paper.scale() origin [ox, oy] of scale. mousewheel .

function onMouseWheel(e) {

    e.preventDefault();
    e = e.originalEvent;

    var delta = Math.max(-1, Math.min(1, e.wheelDelta)) / SPEED;
    var newScale = V(paper.viewport).scale().sx + delta;

    if (newScale > MIN_SCALE && newScale < MAX_SCALE) {
        paper.setOrigin(0, 0); // reset the previous 'translate'            
        var p = offsetToLocalPoint(e.offsetX, e.offsetY);
        paper.scale(newScale, newScale, p.x, p.y);
    }
}

- .

+4

All Articles