Search code examples
javascriptjointjs

Getting outer elements in jointJS to expand the paper


I want to expand the paper in case of the user is moving an element to the bottom or right border:

var paper = new joint.dia.Paper({
    el: $('#canvas'),
    width: 801,
    height: 496,
    model: graph
});

rect.on('change:position', function(evt, pos) {
    if (pos.y > 400) { height = pos.y + 96; }
    else { height = 496; }

    if (pos.x > 680) { width = pos.x + 121; }
    else { width = 801; }

    paper.setDimensions(width, height);
});

This is working for just a single element. But if there is already another element, this doesn't work.

So I need to find the element(s) with the highest x or y value. That means I search for the element which is the most right or bottom of the paper. Then I would put this into another if-clause.

I thought of using paper.findViewsInArea(rect), but I don't really know how to get the result I need.


Solution

  • You don't have to use setDimensions(). Just do something like

    rect.on('change:position', function(evt, pos, y) {
        paper.fitToContent({
            minWidth: 801,
            minHeight: 496
        });
    });