Extent Interaction

Using an Extent interaction to draw an extent.

This example shows how to use an Extent interaction to draw a modifiable extent.

Use Shift+Drag to draw an extent. Shift+Drag on the corners or edges of the extent to resize it. Shift+Click off the extent to remove it.

index.html<!DOCTYPE html>
<html lang="en">
    <title>Extent Interaction</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=requestAnimationFrame,Element.prototype.classList,URL"></script>
      .map {
        width: 100%;
    <div id="map" class="map"></div>
    <script src="index.js"></script>
index.jsimport 'ol/ol.css';
import Map from 'ol/Map';
import View from 'ol/View';
import GeoJSON from 'ol/format/GeoJSON';
import ExtentInteraction from 'ol/interaction/Extent';
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

var extent = new ExtentInteraction();

//Enable interaction by holding shift
window.addEventListener('keydown', function(event) {
  if (event.keyCode == 16) {
window.addEventListener('keyup', function(event) {
  if (event.keyCode == 16) {
  "name": "extent-interaction",
  "dependencies": {
    "ol": "6.1.1"
  "devDependencies": {
    "parcel": "1.11.0"
  "scripts": {
    "start": "parcel index.html",
    "build": "parcel build --experimental-scope-hoisting --public-url . index.html"