8

I noticed that jointjs do support zoom in/out as of http://www.jointjs.com/ But could not find any examples.

Please help me with the steps to zoom in/out the result graph created by jointjs within a <div>?

Nagama Inamdar
  • 2,851
  • 22
  • 39
  • 48
suho
  • 912
  • 6
  • 12

2 Answers2

10

Joint Paper has a method called scale().

I have this implementation for scaling (zooming) my graph:

var graphScale = 1;

var paperScale = function(sx, sy) {
     paper.scale(sx, sy);
};

var zoomOut = function() {
    graphScale -= 0.1;
    paperScale(graphScale, graphScale);
};

var zoomIn = function() {
    graphScale += 0.1;
    paperScale(graphScale, graphScale);
};

var resetZoom = function() {
    graphScale = 1;
    paperScale(graphScale, graphScale);
};

Maybe this is what you need?

Shashwat Black
  • 992
  • 5
  • 13
  • 24
Murasame
  • 183
  • 3
  • 13
6

paper.$el.on('mousewheel DOMMouseScroll', function onMouseWheel(e) {
  //function onMouseWheel(e){
  e.preventDefault();
  e = e.originalEvent;

  var delta = Math.max(-1, Math.min(1, (e.wheelDelta || -e.detail))) / 50;
  var offsetX = (e.offsetX || e.clientX - $(this).offset().left);

  var offsetY = (e.offsetY || e.clientY - $(this).offset().top);
  var p = offsetToLocalPoint(offsetX, offsetY);
  var newScale = V(paper.viewport).scale().sx + delta;
  console.log(' delta' + delta + ' ' + 'offsetX' + offsetX + 'offsety--' + offsetY + 'p' + p.x + 'newScale' + newScale)
  if (newScale > 0.4 && newScale < 2) {
    paper.setOrigin(0, 0);
    paper.scale(newScale, newScale, p.x, p.y);
  }
});

function offsetToLocalPoint(x, y) {
  var svgPoint = paper.svg.createSVGPoint();
  svgPoint.x = x;
  svgPoint.y = y;

  var pointTransformed = svgPoint.matrixTransform(paper.viewport.getCTM().inverse());
  return pointTransformed;
}
Alp
  • 29,274
  • 27
  • 120
  • 198
Atish Aryan
  • 77
  • 1
  • 2
  • Oh my goodness, I've been struggling with this for a while now (not too long, but long enough to get annoying), and that one line, setting the origin to (0, 0) before scaling, is all I needed. Thank you. There are some other flaws in the way it works, but you've saved me some grief. – Wall_Dough Aug 10 '17 at 13:31
  • 1
    What is V in ```V(paper.viewport)``` ? – blueren Apr 12 '18 at 09:23
  • @blueren : it is the Vectorizer library that is embedded within joint.js (you can find it easily). – moudug Dec 06 '18 at 10:13