partridgejiang / Kekule.js

A Javascript cheminformatics toolkit.
http://partridgejiang.github.io/Kekule.js
MIT License
248 stars 61 forks source link

autofit/autozoom for composer #177

Open cleblond opened 4 years ago

cleblond commented 4 years ago

What's the best way autofit or autozoom large structures/mechanisms in composer?

partridgejiang commented 4 years ago

The following code may help to shrink the large structures to fit the client of composer:

var editor = composer.getEditor();
var objBox = editor.getObjectsContainerBox(editor.getChemSpace().getChildren());
var visualBox = chemEditor.getVisibleClientScreenBox();
if (objBox && visualBox)
{
  var sx = (visualBox.x2 - visualBox.x1) / (objBox.x2 - objBox.x1);
  var sy = (visualBox.y2 - visualBox.y1) / (objBox.y2 - objBox.y1);
  var ratio = Math.min(sx, sy);
  if (ratio < 1)
  {
    chemEditor.setZoom(chemEditor.getCurrZoom() * ratio);
    chemEditor.scrollClientToObject(chemEditor.getChemSpace().getChildren());
  }
}