Search code examples
javascriptgojs

fixed size of graphObject in gojs diagram


I'm looking for a possibility to define graphObject fixed size, so that the scale will not affect it. (something like actualBounds- but not readOnly)


Solution

  • Perhaps you mean like the last example here: https://gojs.net/latest/intro/legends.html#StaticParts

    It shows how to make a fixed title that is unaffected by scale. It does this by repositioning and re-scaling it every time the viewport changes via a "ViewportBoundsChanged" listener.

      diagram.add(
        $(go.Part,
          {
            layerName: "Grid",  // must be in a Layer that is Layer.isTemporary,
                                // to avoid being recorded by the UndoManager
            _viewPosition: new go.Point(0,0)  // some position in the viewport,
                                               // not in document coordinates
          },
          $(go.TextBlock, "A Title", { font: "bold 24pt sans-serif", stroke: "green" })));
    
      // Whenever the Diagram.position or Diagram.scale change,
      // update the position of all simple Parts that have a _viewPosition property.
      diagram.addDiagramListener("ViewportBoundsChanged", function(e) {
        e.diagram.commit(function(dia) {
          // only iterates through simple Parts in the diagram, not Nodes or Links
          dia.parts.each(function(part) {
            // and only on those that have the "_viewPosition" property set to a Point
            if (part._viewPosition) {
              part.position = dia.transformViewToDoc(part._viewPosition);
              part.scale = 1/dia.scale;
            }
          })
        }, "fix Parts");
      });