This sample demonstrates how to prevent self-intersecting polylines from being drawn using Draw. To begin drawing, click the button located just below the zoom controls.
The draw experience is built upon specific classes referred to as draw actions. A draw action uses view events to generate a set of coordinates from which different types of geometries can be created. Each geometry type has a corresponding DrawAction class.
After initializing an instance of Draw, call draw.create() and a reference to a relevant draw action will be returned.
In this example, clicking the Draw polyline button calls the draw.create() method with the polyline parameter. Different events on the PolylineDrawAction are then monitored to handle drawing interactions.
// draw polyline buttondocument.getElementById("line-button").onclick = () => { view.graphics.removeAll();
// creates and returns an instance of PolyLineDrawAction const action = draw.create("polyline");
// focus the view to activate keyboard shortcuts for sketching view.focus();
// listen polylineDrawAction events to give immediate visual feedback // to users as the line is being drawn on the view. action.on("cursor-update", updateVertices); action.on("draw-complete", updateVertices); action.on("redo", updateVertices); action.on("undo", updateVertices); action.on("vertex-add", updateVertices); action.on("vertex-remove", updateVertices);})Then we implement custom logic using the simplifyOGCIsSimple() function to determine when users are drawing self-intersecting polylines. Additional functionality provides visual feedback when the polyline intersects itself.
The OGC Simple Feature Access specification specifies
that self-intersecting lines are not simple. So, we use the
logical NOT (!) operator to convert the value to its boolean opposite.
// function that checks if the line intersects itselffunction isSelfIntersecting(polyline) { if (polyline.paths[0].length < 2) { return false; }
return !simplifyOGCIsSimple(polyline);}