Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion index.html
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
/>
</head>
<body>
<my-map zoom="18" drawMode />
<my-map zoom="18" maxZoom="20" drawMode />

<script>
const map = document.querySelector("my-map");
Expand Down
61 changes: 44 additions & 17 deletions src/draw.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,23 @@
import MultiPoint from 'ol/geom/MultiPoint';
import { Draw, Modify, Snap } from "ol/interaction";
import { Vector as VectorLayer } from "ol/layer";
import { Vector as VectorSource } from "ol/source";
import { Circle as CircleStyle, Fill, Stroke, Style } from "ol/style";
import { Circle as CircleStyle, Fill, RegularShape, Stroke, Style } from "ol/style";

const redLineStroke = new Stroke({
color: "#ff0000",
const redLineBase = {
color: '#ff0000',
width: 3,
};

const redLineStroke = new Stroke(redLineBase);

const redDashedStroke = new Stroke({
...redLineBase,
lineDash: [2, 8],
});

const redLineFill = new Fill({
color: "rgba(255, 0, 0, 0.1)",
});

const drawingPointer = new CircleStyle({
Expand All @@ -15,37 +27,52 @@ const drawingPointer = new CircleStyle({
}),
});

const drawingVertices = new Style({
image: new RegularShape({
fill: new Fill({
color: "#fff"
}),
stroke: new Stroke({
color: "#ff0000",
width: 2,
}),
points: 4, // squares
radius: 5,
angle: Math.PI / 4,
}),
geometry: function (feature) {
// return the coordinates of the drawn polygon
const coordinates = feature.getGeometry().getCoordinates()[0];
return new MultiPoint(coordinates);
},
});

export const drawingSource = new VectorSource();

export const drawingLayer = new VectorLayer({
source: drawingSource,
style: new Style({
fill: new Fill({
color: "rgba(255, 255, 255, 0.4)",
style: [
new Style({
fill: redLineFill,
stroke: redLineStroke,
}),
stroke: redLineStroke,
}),
drawingVertices,
]
});

export const draw = new Draw({
source: drawingSource,
type: "Polygon",
style: new Style({
stroke: new Stroke({
color: "#ff0000",
width: 3,
lineDash: [2, 8],
}),
fill: new Fill({
color: "rgba(255, 255, 255, 0.2)",
}),
stroke: redDashedStroke,
fill: redLineFill,
image: drawingPointer,
}),
});

export const snap = new Snap({
source: drawingSource,
pixelTolerance: 5,
pixelTolerance: 15,
});

export const modify = new Modify({
Expand Down