Implementing Drag Zooming Tool in TS / Angular 2+

I’m at a bit of a loss on how I should be using DragZoomingTool.js from Drag Zooming Tool in my typescript based code (Angular 2+ CLI).

This is the message I’m getting if I simply copy/paste the code globally in my component:

If I try to create a DragZoomingTool class, I run in to various errors.

If I try to extend go.Tool:
class DragZoomingTool extends go.Tool { foo(){} }
I get “Argument of type ‘typeof Tool’ is not assignable to parameter of type ‘Constructor’” at:

go.Diagram.inherit(DragZoomingTool, go.Tool);

Would you be able to steer me into the right way of using DragZoomingTool in Angular 2+?

Thanks for your help.

Don’t use the traditional JavaScript classes in the extensions directory – use the classes that we translated into TypeScript in the extensionsTS directory.

If you don’t need any modifications to them, you should be able to do something like this:

import { DragZoomingTool } from "./DragZoomingTool";

but with the appropriate path to the extensionsTS directory for your environment.

Ah, ok, my intellisense wasn’t picking up the TS version. Let me give it a go…

I don’t suppose there’s any guidance you can give on including the extensionsTS files in the Angular 2+ environment? I’ve included:

import { DragZoomingTool } from "gojs/extensionsTS/DragZoomingTool";

and I get the error:

<project root, node modules>/gojs/extensionsTS/DragZoomingTool.ts is not part of the compilation output. Please check the other error messages for details. (no other errors given)

I must be missing a step in order to use types in a subdirectory of gojs.

I’ve searched but am coming up empty on the googlesphere.



If you want a relative import, the path needs to start with “./”.

I got the compiler to take it, and seems to be functioning. However, two undesirable things happen when I implement the DragZoomingTool in the toolManager. Added:

  .insertAt(2, new DragZoomingTool()


It shows the bounding box when I click-drag (without long press, then drag). How can I disable that, I just want the behavior of the map moving

Second, sub part 1:

If I do the proper select-drag (click, pause, drag), and select an area…the geometry is zoomed in but the map stays unzoomed. Any ideas why this might be the case?

Second, sub part 2:

When it does zoom, it magnifies all geometry (# of pixels increases in width, radii, etc). In a graph over mapping use case, I’d want the scale of the geometry to remain, but just being “stretched apart” as I get closer.



OK, so you need to change the behavior of the DragZoomingTool. Maybe you do want to copy the code to your own directory.

The “bounding box” is the If you don’t want it, change the code not to create it, add it to the Diagram, position and size it, and later remove it from the Diagram.

You can see what DragZoomingTool.doMouseUp does: it calls DragZoomingTool.zoomToRect. You’ll want to change that method not to just set the Diagram.scale but to change the zoom level and center point of your map.

ok sounds like a great start, thanks. Could I simply extend the class and override?

Also, a big issue is that I can’t select a rectangle of the map. if I click drag, the map moves, so I can’t put a boundary around that which I’d like to zoom in.


Maybe I’m approaching this problem backwards.

When I use the DragZoom tool it scales all lines and nodes, I don’t want it to do that even if I do get it to zoom the map in custom code.

If I double click on the map (which I believe is the native Leaflet zoom handler), the map zooms great, the geometry is left the same pixel width, and everything is still in synch. When I attempt zoom using the drag zooming tool, the lines become thick and circle nodes large, not what I want.

So my question is, is it possible to just allow Leaflet to handle the native zoom to bounds (shift-drag)? That’s how I had my map originally zooming before layering GoJS on top.

Thanks for your help on this.

I believe all mouse/finger/stylus events are being handled by GoJS because the GoJS canvas is in front of the leaflet canvas.

As I said before, you want to avoid changing the Diagram.scale, which is the purpose of the DragZoomingTool. Overriding DragZoomingTool.zoomToRect could do that.

ahhh I see, ok I’ll give that a whirl!



Do you have an example of where a tool (like DragZoomingTool) is properly extended using typescript?



Yes, there are lots of them in the extensionsTS directory.