The draw toolbar is a simple tool for drawing on the map, but it could use a little more API loving. The variety of drawing geometries is good, ranging from points, lines, polygons, to arrows, circles, and triangles. You can collect the drawn geometry after it’s done through the “draw-end” or “draw-complete” events, but there’s no other event hooks for this tool. While the tooltips that guide you while you draw on the map hint at other events, they’re just not not exposed in the current version of the API (3.13).
After a bit of tinkering, I was able to cobble together a system that would let you catch the first map mouse-down event after the draw toolbar was either activated, or after the toolbar finished drawing the last item. Once I successfully rigged the events to work, I attempted to extend the draw toolbar through a custom module, to contain existing and new events. I kept running into errors when adding the extra event handlers into the draw toolbar constructor, so I gave up that route. Here’s what I came up with.
In the code above, a pausable map mouse-down event is created to tell the draw toolbar to emit our custom “draw-begin” event. We immediately pause the event after it’s created. The event is resumed after the draw toolbar is activated, and after it finished drawing something on the map. The event pauses after the tool is deactivated, and within the map mouse-down event handler.
For an example of this “draw-begin” functionality, I put up a sample on CodePen. I tweaked the draw toolbar sample provided by ESRI. In my sample, the graphics already drawn on the map change to a random color. About 10 drawings in, it looks like quite the party. Note that the color changes occur when you start to draw a new graphic.
Special thanks goes out to Rene Rubalcava (a.k.a. odoenet) for recommending the github plugin that lets me publish gists on my site. If you’re reading this and you haven’t checked his site out, you should.