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.
import ExtentInteraction from 'ol/interaction/Extent.js';
import Map from 'ol/Map.js';
import OSM from 'ol/source/OSM.js';
import TileLayer from 'ol/layer/Tile.js';
import View from 'ol/View.js';
import {shiftKeyOnly} from 'ol/events/condition.js';
const map = new Map({
layers: [
new TileLayer({
source: new OSM(),
}),
],
target: 'map',
view: new View({
center: [0, 0],
zoom: 2,
}),
});
const extent = new ExtentInteraction({condition: shiftKeyOnly});
map.addInteraction(extent);
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Extent Interaction</title>
<link rel="stylesheet" href="node_modules/ol/ol.css">
<style>
.map {
width: 100%;
height: 400px;
}
</style>
</head>
<body>
<div id="map" class="map"></div>
<script type="module" src="main.js"></script>
</body>
</html>
{
"name": "extent-interaction",
"dependencies": {
"ol": "10.3.1"
},
"devDependencies": {
"vite": "^3.2.3"
},
"scripts": {
"start": "vite",
"build": "vite build"
}
}