Class: Draw

ol/interaction/Draw~Draw


import Draw from 'ol/interaction/Draw';

Interaction for drawing feature geometries.

new Draw(options)

interaction/Draw.js, line 187
Name Type Description
options

Options.

Name Type Description
type module:ol/geom/GeometryType

Geometry type of the geometries being drawn with this instance.

clickTolerance number (defaults to 6)

The maximum distance in pixels between "down" and "up" for a "up" event to be considered a "click" event and actually add a point/vertex to the geometry being drawn. The default of 6 was chosen for the draw interaction to behave correctly on mouse as well as on touch devices.

features module:ol/Collection~Collection.<module:ol/Feature~Feature>

Destination collection for the drawn features.

source module:ol/source/Vector~VectorSource

Destination source for the drawn features.

dragVertexDelay number (defaults to 500)

Delay in milliseconds after pointerdown before the current vertex can be dragged to its exact position.

snapTolerance number (defaults to 12)

Pixel distance for snapping to the drawing finish.

stopClick boolean (defaults to false)

Stop click, singleclick, and doubleclick events from firing during drawing.

maxPoints number

The number of points that can be drawn before a polygon ring or line string is finished. By default there is no restriction.

minPoints number

The number of points that must be drawn before a polygon ring or line string can be finished. Default is 3 for polygon rings and 2 for line strings.

finishCondition module:ol/events/condition~Condition

A function that takes an module:ol/MapBrowserEvent~MapBrowserEvent and returns a boolean to indicate whether the drawing can be finished.

style module:ol/style/Style~StyleLike

Style for sketch features.

geometryFunction module:ol/interaction/Draw~GeometryFunction

Function that is called when a geometry's coordinates are updated.

geometryName string

Geometry name to use for features created by the draw interaction.

condition module:ol/events/condition~Condition

A function that takes an module:ol/MapBrowserEvent~MapBrowserEvent and returns a boolean to indicate whether that event should be handled. By default module:ol/events/condition~noModifierKeys, i.e. a click, adds a vertex or deactivates freehand drawing.

freehand boolean (defaults to false)

Operate in freehand mode for lines, polygons, and circles. This makes the interaction always operate in freehand mode and takes precedence over any freehandCondition option.

freehandCondition module:ol/events/condition~Condition

Condition that activates freehand drawing for lines and polygons. This function takes an module:ol/MapBrowserEvent~MapBrowserEvent and returns a boolean to indicate whether that event should be handled. The default is module:ol/events/condition~shiftKeyOnly, meaning that the Shift key activates freehand drawing.

wrapX boolean (defaults to false)

Wrap the world horizontally on the sketch overlay.

Fires:

Extends

Observable Properties

Name Type Settable ol/Object.ObjectEvent type Description
active boolean yes change:active

true if the interaction is active, false otherwise.

Methods

appendCoordinates(coordinates)

interaction/Draw.js, line 913

Append coordinates to the end of the geometry that is currently being drawn. This can be used when drawing LineStrings or Polygons. Coordinates will either be appended to the current LineString or the outer ring of the current Polygon.

Name Type Description
coordinates module:ol/interaction/Draw~LineCoordType

Linear coordinates to be appended into the coordinate array.

changed() inherited

Observable.js, line 36

Increases the revision counter and dispatches a 'change' event.

dispatchEvent(event){boolean|undefined} inherited

events/Target.js, line 96

Dispatches an event and calls all listeners listening for events of this type. The event parameter can either be a string or an Object with a type property.

Name Type Description
event Object | module:ol/events/Event~BaseEvent | string

Event object.

Returns:
false if anyone called preventDefault on the event object or if any of the listeners returned false.

extend(feature)

interaction/Draw.js, line 946

Initiate draw mode by starting from an existing geometry which will receive new additional points. This only works on features with LineString geometries, where the interaction will extend lines by adding points to the end of the coordinates array. This will change the original feature, instead of drawing a copy.

The function will dispatch a drawstart event.

Name Type Description
feature module:ol/Feature~Feature.<module:ol/geom/LineString~LineString>

Feature to be extended.

finishDrawing()

interaction/Draw.js, line 849

Stop drawing and add the sketch feature to the target layer. The module:ol/interaction/Draw~DrawEventType.DRAWEND event is dispatched before inserting the feature.

get(key){*} inherited

Object.js, line 119

Gets a value.

Name Type Description
key string

Key name.

Returns:
Value.

getActive(){boolean} inherited

interaction/Interaction.js, line 60

Return whether the interaction is currently active.

Returns:
true if the interaction is active, false otherwise.

getKeys(){Array.<string>} inherited

Object.js, line 132

Get a list of object property names.

Returns:
List of property names.

getMap(){module:ol/PluggableMap~PluggableMap} inherited

interaction/Interaction.js, line 69

Get the map associated with this interaction.

Returns:
Map.

getOverlay(){module:ol/layer/Vector~VectorLayer}

interaction/Draw.js, line 474

Get the overlay layer that this interaction renders sketch features to.

Returns:
Overlay layer.

getPointerCount(){number} inherited

interaction/Pointer.js, line 104

Returns the current number of pointers involved in the interaction, e.g. 2 when two fingers are used.

Returns:
The number of pointers.

getProperties(){Object.<string, *>} inherited

Object.js, line 141

Get an object of all property names and values.

Returns:
Object.

getRevision(){number} inherited

Observable.js, line 47

Get the version number for this object. Each time the object is modified, its version number will be incremented.

Returns:
Revision.

on(type, listener){module:ol/events~EventsKey|Array.<module:ol/events~EventsKey>} inherited

Observable.js, line 60

Listen for a certain type of event.

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

Returns:
Unique key for the listener. If called with an array of event types as the first argument, the return will be an array of keys.

once(type, listener){module:ol/events~EventsKey|Array.<module:ol/events~EventsKey>} inherited

Observable.js, line 82

Listen once for a certain type of event.

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

Returns:
Unique key for the listener. If called with an array of event types as the first argument, the return will be an array of keys.

removeLastPoint()

interaction/Draw.js, line 812

Remove last point of the feature currently being drawn.

set(key, value, opt_silent) inherited

Object.js, line 164

Sets a value.

Name Type Description
key string

Key name.

value *

Value.

silent boolean

Update without triggering an event.

setActive(active) inherited

interaction/Interaction.js, line 89

Activate or deactivate the interaction.

Name Type Description
active boolean

Active.

setProperties(values, opt_silent) inherited

Object.js, line 183

Sets a collection of key-value pairs. Note that this changes any existing properties and adds new ones (it does not remove any existing properties).

Name Type Description
values Object.<string, *>

Values.

silent boolean

Update without triggering an event.

un(type, listener) inherited

Observable.js, line 101

Unlisten for a certain type of event.

Name Type Description
type string | Array.<string>

The event type or array of event types.

listener function

The listener function.

unset(key, opt_silent) inherited

Object.js, line 195

Unsets a property.

Name Type Description
key string

Key name.

silent boolean

Unset without triggering an event.