## Making Images

GoJS has two functions for creating images: Diagram.makeImageData, which outputs a Base64 image data string, and Diagram.makeImage, which is a convenience function that calls Diagram.makeImageData and returns a new HTMLImageElement with the image data as its source. Both functions have the same single argument, a JavaScript Object that contains several definable properties, enumerated in the documentation.

This page is almost identical to the page on Making SVG, which shows how to render SVG elements instead of PNG images.

      diagram.initialContentAlignment = go.Spot.Center;

      // define a simple Node template (but use the default Link template)
      diagram.nodeTemplate =
        $(go.Node, "Auto",
          $(go.Shape, "RoundedRectangle",
            // Shape.fill is bound to Node.data.color
            new go.Binding("fill", "color")),
          $(go.TextBlock,
            { margin: 3 },  // some room around the text
            // TextBlock.text is bound to Node.data.key
            new go.Binding("text", "key"))
        );

      // create the model data that will be represented by Nodes and Links
      var nodeDataArray = [
        { key: "Alpha", color: "lightblue" },
        { key: "Beta", color: "orange" },
        { key: "Gamma", color: "lightgreen" },
        { key: "Delta", color: "pink" }
      ];
      var linkDataArray = [
        { from: "Alpha", to: "Beta" },
        { from: "Alpha", to: "Gamma" },
        { from: "Beta", to: "Beta" },
        { from: "Gamma", to: "Delta" },
        { from: "Delta", to: "Alpha" }
      ];
      diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);
      window.myDiagram = diagram;

     window.goCode2 = function(pre) {
      if (diagramclass === undefined) diagramclass = go.Diagram;
      if (typeof pre === "string") pre = document.getElementById(pre);
      var f = eval("(function () {window.img = " + pre.textContent + "})");
      f();
      img.className = "images";
      pre.parentElement.insertBefore(img, pre.nextSibling)
    }

     window.goCode3 = function(pre) {
      if (diagramclass === undefined) diagramclass = go.Diagram;
      if (typeof pre === "string") pre = document.getElementById(pre);
      var f = eval("(function () {" + pre.textContent + "})");
      f();
    }

      window.addImage = function(img) {
        obj = document.getElementById("severalImages");
        img.className = "images";
        obj.appendChild(img)
      }

Calling makeImage with no arguments or with an empty properties object results in an image that is the same size as the Diagram's viewport.

    myDiagram.makeImage();

Calling makeImage with an object that has the "scale" property set to 1 results in an image that includes the whole diagram, not just the area visible in the viewport. However, the empty areas around the document bounds are trimmed away.

    myDiagram.makeImage({
      scale: 1
    });

Setting the scale property will create a scaled image that is precisely large enough to contain the Diagram. The following image is created with a scale of 2.

    myDiagram.makeImage({
      scale: 2
    });

The following image is created by setting the size option of makeImage. Note that the canvas is scaled uniformly and any extra space is placed on the bottom or right side of the image.

    myDiagram.makeImage({
      size: new go.Size(100,100)
    });

The following image is also created by setting the size option of makeImage, but only the width is set. The height will be whatever size is needed to uniformly contain the Diagram.

    myDiagram.makeImage({
      size: new go.Size(100,NaN)
    });

The parts option allows us to specify an Iterable collection of Parts to draw. This is useful if you only want to make an image of part of the diagram, such as a selection of nodes.

      myPartsList = new go.List();
      myPartsList.add(myDiagram.findNodeForKey("Beta"));
      myPartsList.add(myDiagram.findNodeForKey("Delta"));

    myDiagram.makeImage({
      parts: myPartsList
    });

Or simply drawing only the links:

    myDiagram.makeImage({
      parts: myDiagram.links
    });

Setting both scale and size creates an image that is scaled specifically and cropped to the given size, as in the following image.

    myDiagram.makeImage({
      scale: 1.5,
      size: new go.Size(100,100)
    });

We may want a very large, scaled image that has a limit on its size, and we can use the maxSize property to constrain one or both dimensions. The following image has a very large scale applied but is limited in size horizontally, so some horizontal cropping will occur.

The default value for maxSize is `go.Size(2000, 2000)`, and specifying `go.Size(600, NaN)` is equivalent to specifying `go.Size(600, 2000)`. If we wanted no cropping on the height we could instead write `go.Size(600, Infinity)`.

    myDiagram.makeImage({
      scale: 9,
      maxSize: new go.Size(600, NaN)
    });

Setting both position and size creates a diagram image that is positioned specifically and cropped to the given size. When a position is set but no scale is set, the scale defaults to 1.

    myDiagram.makeImage({
      position: new go.Point(20,20),
      size: new go.Size(50,50)
    });

Setting the background to a CSS color string will replace the transparent Diagram background with the given color.

    myDiagram.makeImage({
      size: new go.Size(NaN,250),
      background: "rgba(0, 255, 0, 0.5)" // semi-transparent green background
    });

In the following code we use the document bounds to split the Diagram into four equal parts, making four images out of each part. In this way we can prepare images for pagination, making a gallery, or printing purposes. The four images created are shown below.

        var d = myDiagram.documentBounds;
        var halfWidth = d.width / 2;
        var halfHeight = d.height / 2;
        img = myDiagram.makeImage({
                position: new go.Point(d.x, d.y),
                size: new go.Size(halfWidth,halfHeight)
              });
        addImage(img); // Adds the image to a DIV below

        img = myDiagram.makeImage({
                position: new go.Point(d.x + halfWidth, d.y),
                size: new go.Size(halfWidth,halfHeight)
              });
        addImage(img);

        img = myDiagram.makeImage({
                position: new go.Point(d.x, d.y+ halfHeight),
                size: new go.Size(halfWidth,halfHeight)
              });
        addImage(img);

        img = myDiagram.makeImage({
                position: new go.Point(d.x + halfWidth, d.y + halfHeight),
                size: new go.Size(halfWidth,halfHeight)
              });
        addImage(img);

### Image Type

We can set the type and details properties of the argument object in order to retrieve different kinds of images. The only widely supported type is "image/jpeg". The details for a jpeg determine its quality by using values from 0 to 1 inclusive. Jpegs are not commonly used for Diagrams because their lossy compression can render text unreadable.

The following image is an outputted jpeg. Note how the transparent background is turned black, because the jpeg format does not support alpha transparency, and the default state of the HTML5 canvas is that of fully transparent black pixels, rgba(0,0,0,0).

    myDiagram.makeImage({
      scale: 1,
      type: "image/jpeg"
    });

The following image is a jpeg created with an AntiqueWhite background specified.

    myDiagram.makeImage({
      scale: 1,
      background: "AntiqueWhite",
      type: "image/jpeg"
    });

The following image is a jpeg created (with an AntiqueWhite background) and the details option, at very low quality.

    myDiagram.makeImage({
      scale: 1,
      background: "AntiqueWhite",
      type: "image/jpeg",
      details: 0.05
    });