Draw

AMD: require(["esri/views/draw/Draw"], (Draw) => { /* code goes here */ });
ESM: import Draw from "@arcgis/core/views/draw/Draw.js";
Class: esri/views/draw/Draw
Inheritance: DrawAccessor
Since: ArcGIS Maps SDK for JavaScript 4.5

The Draw class provides advanced drawing capabilities for developers who need complete control over creating temporary graphics with different geometries. For example, if you want to prevent users from drawing graphics with self-intersecting lines or overlapping polygons, then you can use this class to implement these rules. The draw experience is built upon draw actions, which use the view events to generate a set of coordinates for creating new geometries. Each geometry type has a corresponding draw action class.

This class provides a simple interface for interacting with draw actions. After initializing a Draw instance, you can call create() to return a reference to the relevant draw action. This draw action may then be used to create new geometries of the specified type.

Pointer and keyboard gestures

Pointer and keyboard gestures for drawing points, polylines, and polygons are described in the tables below.

Drawing points

Gesture Action
Left-click Adds a point at the pointer location.
Enter Adds a point at the pointer location.

Drawing polylines and polygons

Gesture Action
Left-click Adds a vertex at the pointer location.
Left-drag Adds a vertex for each pointer move.
Enter or Double-click Completes the polyline or polygon.
F Adds a vertex to the polyline or polygon.
Z Incrementally undos actions recorded in the stack.
R Incrementally redos actions recorded in the stack.

To provide users with a simpler drawing experience, then use SketchViewModel class.

See also
Example
// create a new instance of draw
let draw = new Draw({
  view: view
});

// create an instance of draw polyline action
// the polyline vertices will be only added when
// the pointer is clicked on the view
let action = draw.create("polyline", {mode: "click"});

// fires when a vertex is added
action.on("vertex-add", function (evt) {
  measureLine(evt.vertices);
});

// fires when the pointer moves
action.on("cursor-update", function (evt) {
  measureLine(evt.vertices);
});

// fires when the drawing is completed
action.on("draw-complete", function (evt) {
  measureLine(evt.vertices);
});

// fires when a vertex is removed
action.on("vertex-remove", function (evt) {
  measureLine(evt.vertices);
});

function measureLine(vertices) {
  view.graphics.removeAll();

  let line = createLine(vertices);
  let lineLength = geometryEngine.geodesicLength(line, "miles");
  let graphic = createGraphic(line);
  view.graphics.add(graphic);
}

function createLine(vertices) {
  let polyline = {
    type: "polyline", // autocasts as new Polyline()
    paths: vertices,
    spatialReference: view.spatialReference
  }
  return polyline;
}

Constructors

Draw

Constructor
new Draw(properties)
Parameter
properties Object
optional

See the properties for a list of all the properties that may be passed into the constructor.

Example
// Typical usage
let draw = new Draw({
  view: view
});

Property Overview

Any properties can be set, retrieved or listened to. See the Watch for changes topic.
Show inherited properties Hide inherited properties
Name Type Summary Class

A reference to the active draw action.

Draw

The name of the class.

Accessor

The view in which geometries will be drawn by the user.

Draw

Property Details

activeAction

Property
activeAction DrawAction

A reference to the active draw action. An instance of the draw action is created when create() method is called.

declaredClass

Inherited
Property
declaredClass Stringreadonly
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.7 Accessor since 4.0, declaredClass added at 4.7.

The name of the class. The declared class name is formatted as esri.folder.className.

view

Property
view MapView |SceneView

The view in which geometries will be drawn by the user.

Method Overview

Show inherited methods Hide inherited methods
Name Return Type Summary Class

Adds one or more handles which are to be tied to the lifecycle of the object.

Accessor

Complete the current active drawing.

Draw

Creates an instance of the requested draw action.

Draw

Returns true if a named group of handles exist.

Accessor

Removes a group of handles owned by the object.

Accessor

Resets the drawing by clearing the active action.

Draw

Method Details

addHandles

Inherited
Method
addHandles(handleOrHandles, groupKey)
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, addHandles added at 4.25.

Adds one or more handles which are to be tied to the lifecycle of the object. The handles will be removed when the object is destroyed.

// Manually manage handles
const handle = reactiveUtils.when(
  () => !view.updating,
  () => {
    wkidSelect.disabled = false;
  },
  { once: true }
);

this.addHandles(handle);

// Destroy the object
this.destroy();
Parameters
handleOrHandles WatchHandle|WatchHandle[]

Handles marked for removal once the object is destroyed.

groupKey *
optional

Key identifying the group to which the handles should be added. All the handles in the group can later be removed with Accessor.removeHandles(). If no key is provided the handles are added to a default group.

complete

Method
complete()
Since: ArcGIS Maps SDK for JavaScript 4.7 Draw since 4.5, complete added at 4.7.

Complete the current active drawing.

create

Method
create(drawAction, drawOptions){DrawAction}

Creates an instance of the requested draw action.

Parameters
drawAction String

Name of the draw action to create. See the table below for a list of possible values and type of draw action it creates.

Possible Values

Geometry type Draw action instance
point PointDrawAction
multipoint MultipointDrawAction (only supported in MapView)
polyline PolylineDrawAction
polygon PolygonDrawAction
rectangle, circle, ellipse SegmentDrawAction

Possible Values:"point"|"multipoint"|"polyline"|"polygon"|"rectangle"|"circle"|"ellipse"

drawOptions Object
optional

Object of the drawing options for the geometry to be created.

Specification
mode String
optional

The drawing mode. The drawing mode applies only when creating polygon, polyline, segment draw actions.

Possible Values

Value Description
hybrid Vertices are added while the pointer is clicked or dragged. Applies to and is the default for polygon and polyline draw actions.
freehand Vertices are added while the pointer is dragged. Applies to polygon, polyline and segment draw actions. Default for segment draw actions.
click Vertices are added when the pointer is clicked.

Possible Values:"hybrid"|"freehand"|"click"

Returns
Type Description
DrawAction Returns an instance of the requested draw action.
Example
let pointAction = draw.create("point");

hasHandles

Inherited
Method
hasHandles(groupKey){Boolean}
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, hasHandles added at 4.25.

Returns true if a named group of handles exist.

Parameter
groupKey *
optional

A group key.

Returns
Type Description
Boolean Returns true if a named group of handles exist.
Example
// Remove a named group of handles if they exist.
if (obj.hasHandles("watch-view-updates")) {
  obj.removeHandles("watch-view-updates");
}

removeHandles

Inherited
Method
removeHandles(groupKey)
Inherited from Accessor
Since: ArcGIS Maps SDK for JavaScript 4.25 Accessor since 4.0, removeHandles added at 4.25.

Removes a group of handles owned by the object.

Parameter
groupKey *
optional

A group key or an array or collection of group keys to remove.

Example
obj.removeHandles(); // removes handles from default group

obj.removeHandles("handle-group");
obj.removeHandles("other-handle-group");

reset

Method
reset()
Since: ArcGIS Maps SDK for JavaScript 4.6 Draw since 4.5, reset added at 4.6.

Resets the drawing by clearing the active action.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.