Box Selection

DragBox feature selection box

No countries selected

Using a DragBox interaction to select features.

This example shows how to use a DragBox interaction to select features. Selected features are added to the feature overlay of a select interaction (ol/interaction/Select) for highlighting.

Use Ctrl+Drag (Command+Drag on Mac) to draw boxes.

index.html<!DOCTYPE html>
<html lang="en">
    <title>Box Selection</title>
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=fetch,requestAnimationFrame,Element.prototype.classList,URL"></script>
      .map {
        width: 100%;
      .ol-dragbox {
        background-color: rgba(255,255,255,0.4);
        border-color: rgba(100,150,0,1);
    <div id="map" class="map"></div>
    <div id="info">No countries selected</div>
    <script src="index.js"></script>
index.jsimport 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import {platformModifierKeyOnly} from 'ol/events/condition';
import GeoJSON from 'ol/format/GeoJSON';
import {DragBox, Select} from 'ol/interaction';
import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer';
import {OSM, Vector as VectorSource} from 'ol/source';

var vectorSource = new VectorSource({
  url: 'data/geojson/countries.geojson',
  format: new GeoJSON()

var map = new Map({
  layers: [
    new TileLayer({
      source: new OSM()
    new VectorLayer({
      source: vectorSource
  target: 'map',
  view: new View({
    center: [0, 0],
    zoom: 2,
    constrainRotation: 16

// a normal select interaction to handle click
var select = new Select();

var selectedFeatures = select.getFeatures();

// a DragBox interaction used to select features by drawing boxes
var dragBox = new DragBox({
  condition: platformModifierKeyOnly


dragBox.on('boxend', function() {
  // features that intersect the box geometry are added to the
  // collection of selected features

  // if the view is not obliquely rotated the box geometry and
  // its extent are equalivalent so intersecting features can
  // be added directly to the collection
  var rotation = map.getView().getRotation();
  var oblique = rotation % (Math.PI / 2) !== 0;
  var candidateFeatures = oblique ? [] : selectedFeatures;
  var extent = dragBox.getGeometry().getExtent();
  vectorSource.forEachFeatureIntersectingExtent(extent, function(feature) {

  // when the view is obliquely rotated the box extent will
  // exceed its geometry so both the box and the candidate
  // feature geometries are rotated around a common anchor
  // to confirm that, with the box geometry aligned with its
  // extent, the geometries intersect
  if (oblique) {
    var anchor = [0, 0];
    var geometry = dragBox.getGeometry().clone();
    geometry.rotate(-rotation, anchor);
    var extent$1 = geometry.getExtent();
    candidateFeatures.forEach(function(feature) {
      var geometry = feature.getGeometry().clone();
      geometry.rotate(-rotation, anchor);
      if (geometry.intersectsExtent(extent$1)) {


// clear selection when drawing a new box and when clicking on the map
dragBox.on('boxstart', function() {

var infoBox = document.getElementById('info');

selectedFeatures.on(['add', 'remove'], function() {
  var names = selectedFeatures.getArray().map(function(feature) {
    return feature.get('name');
  if (names.length > 0) {
    infoBox.innerHTML = names.join(', ');
  } else {
    infoBox.innerHTML = 'No countries selected';
  "name": "box-selection",
  "dependencies": {
    "ol": "6.2.1"
  "devDependencies": {
    "parcel": "1.11.0"
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"