Save and restore diagram position and zoom


I have a diagram which displays different models.
To switch between models, I basically call diagram.clear() and diagram.model = xyz
I need diagram.clear() as I have custom parts in the diagram which are not part of the model.

For each model, I am saving the position and scale in the "ViewportBoundsChanged" event listener and restoring it when the model is loaded again.

I don’t know if this recently changed, but in the latest goJS version the diagram.clear() event will also trigger the "ViewportBoundsChanged" event with a different position. This will overwrite the last correct model position.

Is there any other way I can use to save the diagram position/scale or some way to ignore the "ViewportBoundsChanged" in case it is triggered by diagram.clear()?

Before you call Diagram.clear and set Diagram.model, remember the Diagram properties that you want to save, namely Diagram.position and Diagram.scale.

Then call Diagram.clear, set Diagram.initialPosition and Diagram.initialScale, and finally set the new Diagram.model.

This assumes you haven’t set any other “initial…” properties on the Diagram.

1 Like