I don’t understand what the problem is. Here’s my test code. Note that I do not assign Diagram.padding or Diagram.scrollMargin or Diagram.scrollMode. Even after zooming in or out and panning, each time I click on the HTML button the new node is placed at the center of the viewport.
<!DOCTYPE html>
<html>
<head>
<title>Minimal GoJS Sample</title>
<!-- Copyright 1998-2022 by Northwoods Software Corporation. -->
</head>
<body>
<div id="myDiagramDiv" style="border: solid 1px black; width:100%; height:600px"></div>
<button id="myTestButton">Test</button>
<script src="https://unpkg.com/gojs"></script>
<script id="code">
function init() {
const $ = go.GraphObject.make;
myDiagram =
$(go.Diagram, "myDiagramDiv",
{
"undoManager.isEnabled": true
});
myDiagram.nodeTemplate =
$(go.Node, "Auto",
{ locationSpot: go.Spot.Center },
$(go.Shape,
{ fill: "white" },
new go.Binding("fill", "color")),
$(go.TextBlock,
{ margin: 8, editable: true },
new go.Binding("text").makeTwoWay())
);
myDiagram.model = new go.GraphLinksModel(
[
{ key: 1, text: "Alpha", color: "lightblue" },
{ key: 2, text: "Beta", color: "orange" },
{ key: 3, text: "Gamma", color: "lightgreen" },
{ key: 4, text: "Delta", color: "pink" }
],
[
{ from: 1, to: 2 },
{ from: 1, to: 3 },
{ from: 2, to: 2 },
{ from: 3, to: 4 },
{ from: 4, to: 1 }
]);
document.getElementById("myTestButton").addEventListener("click", e => {
myDiagram.commit(diag => {
const data = { text: new Date().toLocaleString() };
diag.model.addNodeData(data);
const node = diag.findNodeForData(data);
if (node) node.location = diag.viewportBounds.center;
})
});
}
window.addEventListener('DOMContentLoaded', init);
</script>
</body>
</html>