Merge pull request #10960 from SDaron/clipping-layer
Improve Vector Clipping Layer example with a background layer and intersect
This commit is contained in:
@@ -1,13 +1,20 @@
|
|||||||
import GeoJSON from '../src/ol/format/GeoJSON.js';
|
import GeoJSON from '../src/ol/format/GeoJSON.js';
|
||||||
import Map from '../src/ol/Map.js';
|
import Map from '../src/ol/Map.js';
|
||||||
import OSM from '../src/ol/source/OSM.js';
|
|
||||||
import VectorSource from '../src/ol/source/Vector.js';
|
|
||||||
import View from '../src/ol/View.js';
|
import View from '../src/ol/View.js';
|
||||||
import {Fill, Style} from '../src/ol/style.js';
|
import {Fill, Style} from '../src/ol/style.js';
|
||||||
|
import {OSM, Stamen, Vector as VectorSource} from '../src/ol/source.js';
|
||||||
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
import {Tile as TileLayer, Vector as VectorLayer} from '../src/ol/layer.js';
|
||||||
import {fromLonLat} from '../src/ol/proj.js';
|
import {fromLonLat} from '../src/ol/proj.js';
|
||||||
import {getVectorContext} from '../src/ol/render.js';
|
import {getVectorContext} from '../src/ol/render.js';
|
||||||
|
|
||||||
|
//A distinct className is required to use another canvas for the background
|
||||||
|
const background = new TileLayer({
|
||||||
|
className: 'stamen',
|
||||||
|
source: new Stamen({
|
||||||
|
layer: 'toner',
|
||||||
|
}),
|
||||||
|
});
|
||||||
|
|
||||||
const base = new TileLayer({
|
const base = new TileLayer({
|
||||||
source: new OSM(),
|
source: new OSM(),
|
||||||
});
|
});
|
||||||
@@ -20,6 +27,11 @@ const clipLayer = new VectorLayer({
|
|||||||
}),
|
}),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
//Giving the clipped layer an extent is necessary to avoid rendering when the feature is outside the viewport
|
||||||
|
clipLayer.getSource().on('addfeature', function () {
|
||||||
|
base.setExtent(clipLayer.getSource().getExtent());
|
||||||
|
});
|
||||||
|
|
||||||
const style = new Style({
|
const style = new Style({
|
||||||
fill: new Fill({
|
fill: new Fill({
|
||||||
color: 'black',
|
color: 'black',
|
||||||
@@ -27,8 +39,8 @@ const style = new Style({
|
|||||||
});
|
});
|
||||||
|
|
||||||
base.on('postrender', function (e) {
|
base.on('postrender', function (e) {
|
||||||
e.context.globalCompositeOperation = 'destination-in';
|
|
||||||
const vectorContext = getVectorContext(e);
|
const vectorContext = getVectorContext(e);
|
||||||
|
e.context.globalCompositeOperation = 'destination-in';
|
||||||
clipLayer.getSource().forEachFeature(function (feature) {
|
clipLayer.getSource().forEachFeature(function (feature) {
|
||||||
vectorContext.drawFeature(feature, style);
|
vectorContext.drawFeature(feature, style);
|
||||||
});
|
});
|
||||||
@@ -36,7 +48,7 @@ base.on('postrender', function (e) {
|
|||||||
});
|
});
|
||||||
|
|
||||||
const map = new Map({
|
const map = new Map({
|
||||||
layers: [base, clipLayer],
|
layers: [background, base, clipLayer],
|
||||||
target: 'map',
|
target: 'map',
|
||||||
view: new View({
|
view: new View({
|
||||||
center: fromLonLat([8.23, 46.86]),
|
center: fromLonLat([8.23, 46.86]),
|
||||||
|
|||||||
Reference in New Issue
Block a user