# Tools

Tools handle all of the input events. There are many kinds of predefined Tool classes that implement all of the common operations that users do.

For flexibility and simplicity, all input events are canonicalized as InputEvents and redirected by the diagram to go to the Diagram.currentTool. By default the Diagram.currentTool is an instance of ToolManager held as the Diagram.toolManager. The ToolManager implements support for all mode-less tools. The ToolManager is responsible for finding another tool that is ready to run and then making it the new current tool. This causes the new tool to process all of the input events (mouse, keyboard, and touch) until the tool decides that it is finished, at which time the diagram's current tool reverts back to the Diagram.defaultTool, which is normally the ToolManager, again.

Although the terminology includes the word "mouse", often that refers to both mouse events and touch events.

See samples that make use of Tools in the samples index.

## Predefined Tools

Each Diagram has an instance of most of the tool classes, all managed by the diagram's ToolManager. If you want to change the interactive behavior, in many common cases you may be able to do so by setting properties on the Diagram, on your Parts, or on individual GraphObjects. But more generally you may need to modify one or more of the tools, which are accessible as properties of the Diagram.toolManager.

Some tools want to run when a mouse-down occurs. These tools include:

- ToolManager.actionTool, an ActionTool, for allowing "buttons" and other GraphObjects to grab events from the regular tools
- ToolManager.relinkingTool, a RelinkingTool, for reconnecting an existing Link
- ToolManager.linkReshapingTool, a LinkReshapingTool, for changing the route of a Link
- ToolManager.resizingTool, a ResizingTool, for changing the GraphObject.desiredSize of a Part or an object within a Part
- ToolManager.rotatingTool, a RotatingTool, for changing the GraphObject.angle of a Part or an object within a Part

Some tools want to run when a mouse-move occurs after a mouse-down. These tools include:

- ToolManager.linkingTool, a LinkingTool, for drawing a new Link
- ToolManager.draggingTool, a DraggingTool, for moving or copying selected Parts
- ToolManager.dragSelectingTool, a DragSelectingTool, for rubber-band selection of some Parts within a rectangular area
- ToolManager.panningTool, a PanningTool, for panning/scrolling the diagram

Some tools only want to run upon a mouse-up event after a mouse-down. These tools include:

- ToolManager.contextMenuTool, a ContextMenuTool, for showing a context menu for a GraphObject
- ToolManager.textEditingTool, a TextEditingTool, for in-place editing of TextBlocks in selected Parts
- ToolManager.clickCreatingTool, a ClickCreatingTool, for inserting a new Part when the user clicked
- ToolManager.clickSelectingTool, a ClickSelectingTool, for selecting or de-selecting a Part

To change the behavior of a tool, you may be able to set properties on the tool, on the Diagram, on a particular Part, or on a particular GraphObject.

- For example, to disable the rubber-band selection tool (DragSelectingTool), set `diagram.toolManager.dragSelectingTool.isEnabled = false;`.
- You can change the appearance of a selected Part (actually its selection Adornment) by setting Part.selectionAdornmentTemplate. (See Selection for more discussion.)
- You can enable users to draw new links interactively (LinkingTool) by setting GraphObject.fromLinkable and GraphObject.toLinkable on the port objects of your nodes.
- You can disable the movement of a Part (DraggingTool), including Nodes and Groups, by setting Part.movable to false.
- You can limit the movement of a Part by setting Part.minLocation and/or Part.maxLocation. For more general limitations, set Part.dragComputation to a function that computes the desired new location.
- You can disable resizing any part (ResizingTool) by setting Diagram.allowResize to false.
- Tooltips, implemented by the ToolManager, are discussed in ToolTips.
- Context menus, implemented by the ContextMenuTool, are discussed in Context Menus.

More detail is available in the section about Permissions.

Some commonly set properties include:

- Enable inserting parts via double-clicking by the ClickCreatingTool by setting ClickCreatingTool.archetypeNodeData to a node data object.
- Control what parts become selected by DragSelectingTool by setting DragSelectingTool.isPartialInclusion.
- Customize the link data that is copied when a new link is drawn by LinkingTool by setting LinkingTool.archetypeLinkData.
- Limit how parts are resized by the ResizingTool by setting ResizingTool.cellSize, ResizingTool.maxSize, or ResizingTool.minSize.
- Limit how parts are rotated by the RotatingTool by setting RotatingTool.snapAngleEpsilon or RotatingTool.snapAngleMultiple.

Remember that all of the individual tools are available via the Diagram.toolManager. For example, to enable the ClickCreatingTool:

      myDiagram.toolManager.clickCreatingTool.archetypeNodeData =
        { key: "Node", text: "some description", color: "green" };

You can also set tool properties when using the constructor or GraphObject.make to define your Diagram:

      const diagram =
        new go.Diagram("myDiagramDiv",
          {
            allowCopy: false,
            "grid.visible": true,
            "grid.gridCellSize": new go.Size(30, 20),
            "clickCreatingTool.archetypeNodeData":  // a node data JavaScript object
              { key: "Node", text: "some description", color: "green" },
            "dragSelectingTool.box":  // an unbound Part
              new go.Part({ layerName: "Tool" })
                .add(
                  new go.Shape({ name: "SHAPE", fill: null, stroke: "blue", strokeWidth: 3 })
                ),
            "draggingTool.isGridSnapEnabled": true,
            "linkReshapingTool.handleArchetype":  // a GraphObject that is copied for each handle
              new go.Shape({ width: 10, height: 10, fill: "yellow" }),
            "resizingTool.isGridSnapEnabled": true,
            "rotatingTool.snapAngleMultiple": 90,
            "rotatingTool.snapAngleEpsilon": 45
          }
        );

At this time the syntax for setting properties on predefined subobjects only works for the Diagram constructor.

## The Tool Lifecycle

While each prebuilt tool in GoJS is used for a different purpose, all Tools are guaranteed to share some functions and properties. All tools share a general "lifecycle" -- that is, the order in which these common functions are called. One can think of this cycle as "starting" when the ToolManager is alerted of some input event and begins searching through the pertinent list of tools (i.e., if the mouse-down event is registered, ToolManager starts searching its ToolManager.mouseDownTools list). Below is a diagram representing the general lifecycle of a tool.

      diagram.nodeTemplate =
        new go.Node("Auto", { locationSpot: go.Spot.Center })
          .bindTwoWay("location", "loc", go.Point.parse, go.Point.stringify)
          .add(
            new go.Shape("RoundedRectangle", { fill: "lightgreen", stroke: "lightgray" })
              .bind("fill", "color"),
            new go.TextBlock({
                margin: 8,
                wrap: go.Wrap.Fit,
                width: 170,
                font: "bold 10pt sans-serif",
                textAlign: "center"
              })
              .bind("text")
          );

      diagram.linkTemplate =
        new go.Link()
          .bind("curve")
          .add(
            new go.Shape(),
            new go.Shape({ toArrow: "OpenTriangle", fill: null }),   // the arrowhead
            new go.TextBlock({ margin: 1, segmentOffset: new go.Point(0,0) })
              .bind("text")
              .bind("segmentOffset")
              .bind("segmentOrientation")
          );

      diagram.model =
        new go.GraphLinksModel(
          {
            nodeDataArray:
            [
              { key: 1, text: "ToolManager receives mouse event and searches a tool list such as mouseDownTools", loc: "50 0" },
              { key: 2, text: "tool.canStart()", loc: "50 100", color: "lightyellow" },
              { key: 3, text: "toolManager.currentTool = the chosen tool", loc: "50 200" },
              { key: 4, text: "tool.doStart()", loc: "50 275", color: "lightyellow" },
              { key: 5, text: "tool.doActivate()", loc: "50 350", color: "lightyellow" },
              { key: 6, text: "isActive === true", loc: "50 425" },
              { key: 7, text: "tool.doMouseDown() or\ntool.doMouseMove() or\ntool.doMouseUp() or\ntool.doMouseWheel() or\ntool.doKeyDown() or\ntool.doKeyUp()",
                loc: "-200 500", color: "lightyellow" },
              { key: 8, text: "tool.doCancel()", loc: "250 500", color: "lightyellow" },
              { key: 9, text: "tool.stopTool()", loc: "50 575", color: "lightyellow" },
              { key: 10, text: "toolManager.currentTool = toolManager.defaultTool", loc: "50 650" },
              { key: 11, text: "tool.doDeactivate()", loc: "50 725", color: "lightyellow" },
              { key: 12, text: "tool.doStop()", loc: "50 800", color: "lightyellow" }
            ],
            linkDataArray:
            [
              { from: 1, to: 2, text: "on each tool call", segmentOffset: new go.Point(0,-50) },
              { from: 2, to: 3, text: "if it returns true", segmentOffset: new go.Point(0,-50) },
              { from: 3, to: 4 },
              { from: 4, to: 5 },
              { from: 5, to: 6 },
              { from: 6, to: 7, text: "Receives input", curve: go.Curve.Bezier, segmentOrientation: go.Orientation.Opposite, segmentOffset: new go.Point(0,10) },
              { from: 7, to: 6, text: "Input continues", curve: go.Curve.Bezier, segmentOrientation: go.Orientation.Along, segmentOffset: new go.Point(0,10) },
              { from: 7, to: 9, text: "Input stops tool", segmentOrientation: go.Orientation.Along, segmentOffset: new go.Point(0,10) },
              { from: 6, to: 8, text: "User cancels tool", segmentOrientation: go.Orientation.Along, segmentOffset: new go.Point(0,-10)},
              { from: 8, to: 9 },
              { from: 9, to: 10 },
              { from: 10, to: 11 },
              { from: 11, to: 12 },
            ]
          }
          );

For more information on how these specific functions work, see the Tool documentation.

## Tools and Adornments

Adornments are used for more than indicating that a Part is selected. Each Tool that is in the ToolManager.mouseDownTools list (in other words, any mode-less tool that is started with a mouse-down or finger-down event) gets the opportunity to add its own Adornments for its own purposes when a Part is selected.

### ResizingTool

When a Part is resizable, the ResizingTool adds an Adornment containing eight resize handles, four at the corners and four at the middles of the sides.

If you want to let the user resize the whole node, just set Part.resizable to true. In this case resizing will set the Node's GraphObject.desiredSize.

      diagram.add(
        new go.Node("Auto", { resizable: true })
          .add(
            new go.Shape("RoundedRectangle", { fill: "orange" }),
            new go.TextBlock("Hello!", { margin: 5 })
          ));
      diagram.commandHandler.selectAll();

If you want the user to resize a particular object within the node, you need to name that object and assign Part.resizeObjectName. Resizing will set the Part.resizeObject's GraphObject.desiredSize, in this case the Shape's desiredSize.

      diagram.add(
        new go.Node("Vertical", {
            resizable: true, resizeObjectName: "SHAPE",  // resize the Shape, not the Node
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE",
                fill: "orange",
                width: 50, height: 30
              }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

You can limit the minimum and maximum size for the resized object by setting GraphObject.maxSize and GraphObject.minSize. Note that these GraphObject properties are set on the Part.resizeObject, not on the Part itself.

      diagram.add(
        new go.Node("Vertical", {
            resizable: true, resizeObjectName: "SHAPE",
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE",
                fill: "orange",
                width: 50, height: 30,
                // limit size by setting or binding maxSize and/or minSize
                maxSize: new go.Size(100, 40), minSize: new go.Size(20, 20)
              }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

You can also cause resizing to be multiples of a given size by setting Part.resizeCellSize.

      diagram.add(
        new go.Node("Vertical", {
            resizable: true, resizeObjectName: "SHAPE",
            resizeCellSize: new go.Size(10, 10),  // new size will be multiples of resizeCellSize
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE",
                fill: "orange",
                width: 50, height: 30,
                maxSize: new go.Size(100, 40), minSize: new go.Size(20, 20)
              }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

When an object is resizable, it is commonplace to try to remember the new size by updating the model data, so that it can be saved and loaded later. This can be accomplished with a TwoWay Binding on the GraphObject.desiredSize property. But note that the binding needs to be on the actual GraphObject that is resized, not on the whole Node. In this case, because the Part.resizeObjectName is referring to a Shape, that means the binding needs to be on the Shape.

      diagram.add(
        new go.Node("Vertical", {
            resizable: true, resizeObjectName: "SHAPE",
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE",
                fill: "orange",
                width: 50, height: 30
              })
              // TwoWay Binding of the desiredSize
              .bindTwoWay("desiredSize", "size", go.Size.parse, go.Size.stringify),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

You can customize all eight standard resize handles by setting ResizingTool.archetypeHandle. For example, to change all of the handles to be slightly larger yellow circles:

      diagram.toolManager.resizingTool.handleArchetype =
        new go.Shape("Circle", {
          width: 10, height: 10, fill: "yellow", cursor: "pointer"
        });

      diagram.add(
        new go.Node("Vertical", {
            resizable: true, resizeObjectName: "SHAPE",
            selectionAdorned: false  // don't show selection Adornment, a rectangle
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE",
                fill: "gray",
                width: 50, height: 30,
                maxSize: new go.Size(100, 40), minSize: new go.Size(20, 20)
              }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

Note that the ResizingTool will automatically set each resize handle's GraphObject.cursor appropriately. Note also that because Part.selectionAdorned is false, there is no blue rectangle default selection adornment.

You can customize the resize handles by setting Part.resizeAdornmentTemplate. For example, to allow the user to only change the width of a Shape in a Node, the Adornment should have only two resize handles: one at the left and one at the right. The Adornment is implemented as a Spot Panel that surrounds a Placeholder, representing the adorned Shape, with two rectangular blue Shapes, each representing a handle. There is also a TextBlock placed above the adorned shape showing the shape's current width.

      diagram.add(
        new go.Node("Vertical", {
            resizable: true, resizeObjectName: "SHAPE",
            resizeAdornmentTemplate:  // specify what resize handles there are and how they look
              new go.Adornment("Spot")
                .add(
                  new go.Placeholder(),  // takes size and position of adorned object
                  new go.Shape("Circle", { // left resize handle
                      alignment: go.Spot.Left, cursor: "col-resize",
                      desiredSize: new go.Size(9, 9),
                      fill: "lightblue", stroke: "dodgerblue"
                    }),
                  new go.Shape("Circle", {  // right resize handle
                      alignment: go.Spot.Right, cursor: "col-resize",
                      desiredSize: new go.Size(9, 9),
                      fill: "lightblue", stroke: "dodgerblue"
                    }),
                  new go.TextBlock({ // show the width as text
                      alignment: go.Spot.Top, alignmentFocus: new go.Spot(0.5, 1, 0, -2),
                      stroke: "dodgerblue"
                    })
                    .bindObject("text", "adornedObject",
                                shp => shp.naturalBounds.width.toFixed(0))
                ),
            selectionAdorned: false  // don't show selection Adornment, a rectangle
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE",
                fill: "orange",
                width: 50, height: 30,
                maxSize: new go.Size(200, 40), minSize: new go.Size(20, 20)
              }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

There are examples custom resizing tools defined in the samples and extensions directories: Resize Multiple Tool (in Floor Plan Editor), Lane Resizing Tool (in Swim Lanes), and Lane Resizing Tool (in Swim Lanes Vertical).

### RotatingTool

When a Part is rotatable, the RotatingTool adds an Adornment containing one rotate handle a short distance from the object at the object's angle. Since the default GraphObject.angle is zero, the rotate handle typically starts to the right of the object.

If you want to let the user rotate the whole node, just set Part.rotatable to true. Rotating will set the Node's GraphObject.angle.

      diagram.add(
        new go.Node("Auto", { rotatable: true, locationSpot: go.Spot.Center })
          .add(
            new go.Shape("RoundedRectangle", { fill: "orange" }),
            new go.TextBlock("Hello!", { margin: 5 })
          ));
      diagram.commandHandler.selectAll();

If you want the user to rotate a particular object within the node, you need to name that object and assign Part.rotateObjectName. Rotating will set the Part.rotateObject's GraphObject.angle, in this case the Shape's angle.

      diagram.add(
        new go.Node("Vertical", {
            rotatable: true, rotateObjectName: "SHAPE",  // rotate the Shape, not the Node
            locationSpot: go.Spot.Center, locationObjectName: "SHAPE",
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE", fill: "orange",
                width: 50, height: 30
              }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

When an object is rotatable, it is commonplace to try to remember the new angle by updating the model data, so that it can be saved and loaded later. This can be accomplished with a TwoWay Binding on the GraphObject.angle property. But note that the binding needs to be on the actual GraphObject that is rotated, not on the whole Node. In this case, because the Part.rotateObjectName is referring to a Shape, that means the binding needs to be on the Shape.

      diagram.add(
        new go.Node("Vertical", {
            rotatable: true, rotateObjectName: "SHAPE",
            locationSpot: go.Spot.Center, locationObjectName: "SHAPE",
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
                name: "SHAPE", fill: "orange",
                width: 50, height: 30
              })
              .bindTwoWay("angle"),  // TwoWay Binding of angle
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

Another common customization is to position the rotate handle above the object when it is not rotated, i.e. when its GraphObject.angle is zero. This is accomplished by setting RotatingTool.handleAngle to 270.

      diagram.add(
        new go.Node("Auto", { rotatable: true, locationSpot: go.Spot.Center })
          .bindTwoWay("angle")  // TwoWay Binding of Node.angle
          .add(
            new go.Shape("RoundedRectangle", { fill: "orange" }),
            new go.TextBlock("Hello!", { margin: 5 })
          ));
      diagram.toolManager.rotatingTool.handleAngle = 270;
      diagram.commandHandler.selectAll();

You can customize the rotate handle by setting Part.rotateAdornmentTemplate.

      diagram.add(
        new go.Node("Vertical", {
            rotatable: true, rotateObjectName: "SHAPE",
            locationSpot: go.Spot.Center, locationObjectName: "SHAPE",
            rotateAdornmentTemplate:  // specify appearance of rotation handle
              new go.Adornment({ locationSpot: go.Spot.Center })
                .add(
                  new go.Shape("BpmnActivityLoop", {
                      width: 12, height: 12, cursor: "pointer",
                      background: "transparent", stroke: "dodgerblue", strokeWidth: 2
                    })
                ),
            selectionObjectName: "SHAPE"
          })
          .add(
            new go.Shape("RoundedRectangle", {
              name: "SHAPE", fill: "orange",
              width: 50, height: 30
            }),
            new go.TextBlock("Hello!", { margin: 3 })
          ));
      diagram.commandHandler.selectAll();

There are example custom rotating tools defined in the samples and extensions directories: Rotate Multiple Tool (in Floor Plan Editor) and Horizontal Text Rotating Tool (in Seating Chart).

### RelinkingTool

When a Link is Link.relinkableFrom and/or Link.relinkableTo, the RelinkingTool adds one or two Adornments, a diamond at each relinkable end of a selected link. The user can drag a relinking handle to reconnect that end of the link to another port.

The RelinkingTool will automatically update the relationships between the nodes/ports, both in the diagram and in the model. No Bindings are needed for such model updates.

      diagram.nodeTemplate =
        new go.Node("Auto")
          .add(
            new go.Shape("Rectangle", {
                fill: "lightgray",
                portId: "", fromLinkable: true, toLinkable: true
              }),
            new go.TextBlock({ margin: 5 })
              .bind("text")
          );

      diagram.linkTemplate =
        new go.Link({ relinkableFrom: true, relinkableTo: true })
          .add(
            new go.Shape(),
            new go.Shape({ toArrow: "Standard" })
          );

      const nodeDataArray = [
        { key: 1, text: "Alpha" },
        { key: 2, text: "Beta" },
        { key: 3, text: "Gamma" },
        { key: 4, text: "Delta" }
      ];
      const linkDataArray = [
        { from: 1, to: 4 }
      ];
      diagram.model = new go.GraphLinksModel(nodeDataArray, linkDataArray);

      diagram.select(diagram.findLinkForData(linkDataArray[0]));

The relinking handles can be customized by setting RelinkingTool.fromHandleArchetype and RelinkingTool.toHandleArchetype. At the current time they cannot be customized by setting a property on the Link.

You can limit which pairs of ports between which the user may draw new links or reconnect existing links. This topic is covered by Link Validation.

### LinkReshapingTool

When a Link is Part.reshapable, the LinkReshapingTool adds an Adornment with several reshape handles at the interior points of a selected link's route. When the user drags a reshape handle, the route of the Link, held by Link.points, is modified.

When a link is reshapable, it is commonplace to try to remember the new route by updating the link data in the GraphLinksModel, so that it can be saved and loaded later. This can be accomplished with a TwoWay Binding on the Link.points property. If one also uses the property name "points" on the link data, Model.toJson will automatically convert the List of Points into an Array of numbers and vice-versa.

      diagram.nodeTemplate =
        new go.Node("Auto")
          .bind("location", "loc", go.Point.parse)
          .add(
            new go.Shape("Rectangle", { fill: "lightgray" }),
            new go.TextBlock({ margin: 5 })
              .bind("text")
          );

      diagram.linkTemplate =
        new go.Link({ reshapable: true, routing: go.Routing.Orthogonal })
          .bindTwoWay("points")  // TwoWay Binding of Link.points
          .add(
            new go.Shape(),
            new go.Shape({ toArrow: "Standard" })
          );

      diagram.model = new go.GraphLinksModel([
        { key: 1, text: "Alpha", loc: "0 0" },
        { key: 2, text: "Beta", loc: "200 50" }
      ], [
        { from: 1, to: 2 }
      ]);

      diagram.select(diagram.findLinkForData(diagram.model.linkDataArray[0]));

The reshape handles are small blue squares. The reshape handles can be customized by setting LinkReshapingTool.handleArchetype. At the current time they cannot be customized by setting a property on the Link.

By setting Link.resegmentable to true, users can add or remove segments from links. The resegmenting handles are even smaller blue diamonds at the middle of each segment. When the user drags a resegmenting handle, a new segment is inserted into the link's route. For orthogonal links, two new segments are introduced in order to maintain orthogonality. When the user reshapes the link so that adjacent segments are co-linear (or nearly so), the segment(s) are removed from the route.

      diagram.nodeTemplate =
        new go.Node("Auto")
          .bind("location", "loc", go.Point.parse)
          .add(
            new go.Shape("Rectangle", { fill: "lightgray" }),
            new go.TextBlock({ margin: 5 })
              .bind("text")
          );

      diagram.linkTemplate =
        new go.Link({
            reshapable: true, resegmentable: true,
            routing: go.Routing.Orthogonal
          })
          .bindTwoWay("points")  // TwoWay Binding of Link.points
          .add(
            new go.Shape(),
            new go.Shape({ toArrow: "Standard" })
          );

      diagram.model = new go.GraphLinksModel([
        { key: 1, text: "Alpha", loc: "0 0" },
        { key: 2, text: "Beta", loc: "200 50" }
      ], [
        { from: 1, to: 2 }
      ]);

      diagram.select(diagram.findLinkForData(diagram.model.linkDataArray[0]));

The resegmenting handles can be customized by setting LinkReshapingTool.midHandleArchetype. At the current time they cannot be customized by setting a property on the Link. Also at the current time resegmenting is not supported on Bezier-curved links.

If you want your users to be able to reshape Shape geometries that are not Link paths, there is the Geometry Reshaping Tool used by the Polygon Drawing and Freehand Drawing samples in the extensions directory. It is defined in a separate JS file that you can load into your app.

## Tools and Tool Parts

Some tools make use of special Parts that they add to the "Tool" Layer as feedback during the tool's operation.

### DragSelectingTool

The DragSelectingTool uses the DragSelectingTool.box to show the area in which it will select Parts. Normally this is a simple magenta rectangular shape, which you can customize. For example here is a drag-selecting box that is in the shape of a blue-outlined cloud.

      diagram.nodeTemplate =
        new go.Node("Auto")
          .bind("location", "loc", go.Point.parse)
          .add(
            new go.Shape("Rectangle", { fill: "lightgray" }),
            new go.TextBlock({ margin: 5 })
              .bind("text")
          );

      diagram.toolManager.dragSelectingTool.isPartialInclusion = true;
      diagram.toolManager.dragSelectingTool.box =
        new go.Part({ layerName: "Tool" })
          .add(
            new go.Shape("Cloud", {
                name: "SHAPE",
                fill: null, stroke: "dodgerblue", strokeWidth: 2
              })
          );

      diagram.model = new go.GraphLinksModel([
        { key: 1, text: "Alpha", loc: "0 0" },
        { key: 2, text: "Beta", loc: "200 50" }
      ], [
        { from: 1, to: 2 }
      ]);

Note that the DragSelectingTool expects that the object in the "box" to be resized is named "SHAPE". The object should be rectangular too, or else the user might be misled by the area in which parts will be selected. Finally note also that the box is not an Adornment because it does not "adorn" any Part. It is just an unbound Part that is used temporarily by the DragSelectingTool.

There are examples of in-the-background-dragging tools defined in the extensions directory: Realtime Drag Selecting Tool, Drag Creating Tool, and Drag Zooming Tool. Each is defined in a separate JS file that you can load into your app.

### LinkingTool and RelinkingTool

The linking tools, LinkingTool and RelinkingTool, inherit from a base class, LinkingBaseTool, that uses several Parts: a temporary Link and temporary "to" and "from" Nodes.

To customize the appearance and behavior of the temporary Link that is shown during a linking operation, you need to modify or replace the LinkingBaseTool.temporaryLink. The default temporary link is a blue line with a standard arrowhead. The originating port and the potential target port are shown by the LinkingBaseTool.temporaryFromNode and LinkingBaseTool.temporaryToNode. The default temporary ports are magenta rectangles.

      diagram.nodeTemplate =
        new go.Node("Spot")
          .bind("location", "loc", go.Point.parse)
          .add(
            new go.Shape("RoundedRectangle", {
                width: 100, height: 40, fill: "lightyellow",
                portId: "", fromLinkable: true, toLinkable: true, cursor: "pointer"
              }),
            new go.TextBlock()
              .bind("text")
          );

      diagram.toolManager.linkingTool.temporaryLink =
        new go.Link({ layerName: "Tool" })
          .add(
            new go.Shape({ stroke: "red", strokeWidth: 2, strokeDashArray: [4, 2] })
          );

      const tempfromnode =
        new go.Node({ layerName: "Tool" })
          .add(
            new go.Shape("RoundedRectangle", {
                stroke: "chartreuse", strokeWidth: 3, fill: null,
                portId: "", width: 1, height: 1
              })
          );
      diagram.toolManager.linkingTool.temporaryFromNode = tempfromnode;
      diagram.toolManager.linkingTool.temporaryFromPort = tempfromnode.port;

      const temptonode =
        new go.Node({ layerName: "Tool" })
          .add(
            new go.Shape("RoundedRectangle", {
                stroke: "cyan", strokeWidth: 3, fill: null,
                portId: "", width: 1, height: 1
              })
          );
      diagram.toolManager.linkingTool.temporaryToNode = temptonode;
      diagram.toolManager.linkingTool.temporaryToPort = temptonode.port;

      diagram.model = new go.GraphLinksModel([
        { text: "Alpha", loc: "0 0" },
        { text: "Beta", loc: "200 50" },
        { text: "Gamma", loc: "400 0" }
      ]);  // start off with no links

Try drawing a link from one node to the other. You will notice that the nodes (actually the ports) are highlighted by the temporary nodes in chartreuse and cyan. The temporary link is a dashed red line without an arrowhead.

If your app also supports relinking you will probably want to do the same customizations on the RelinkingTool.

There are examples of linking tools defined in the samples and extensions directories: Polyline Linking Tool, Messaging Tool (in Sequence Diagram), and Custom Linking Tool (in Grafcet Diagram)

## Custom Tools

The GoJS samples and extensions demonstrate a number of custom tools, including:

- Column Resizing Tool and Row Resizing Tool
- Curved Link Reshaping Tool
- Drag Creating Tool
- Drag Zooming Tool
- Freehand Drawing Tool
- Geometry Reshaping Tool also used by the Polygon Drawing, and the Freehand Drawing samples
- Guided Dragging Tool
- Link Label Dragging Tool
- Link Label On Path Dragging Tool
- Link Shifting Tool
- Node Label Dragging Tool
- Non-Realtime Dragging Tool
- Orthogonal Link Reshaping Tool
- Overview Resizing Tool
- Polygon Drawing Tool
- Polyline Linking Tool
- Port Shifting Tool
- Realtime Drag Selecting Tool
- Rescaling Tool
- Resize Multiple Tool
- Rotate Multiple Tool
- Sector Reshaping Tool
- Snap Link Reshaping Tool
- Spot Rotating Tool
- Field Dragging Tool
- Lane Resizing Tool (in Swim Lanes)
- Lane Resizing Tool (in Swim Lanes Vertical)