在前文完成了mapboxGL中区域掩膜的实现。近日有人问到说在openlayers中如何实现,本文就带大家看看如何在openlayers中实现区域掩膜。
canvas
,设置其在map之上;postrender
事件,每次事件触发重新绘制掩膜;canvas
const { offsetWidth, offsetHeight } = map.getViewport()
canvas = document.createElement('canvas');
canvas.width = offsetWidth;
canvas.height = offsetHeight;
canvas.style.position = 'absolute';
canvas.style.top = '0px';
canvas.style.left = '0px';
canvas.style.zIndex = '1';
ctx = canvas.getContext('2d');
map.getViewport().appendChild(canvas);
map.on('postrender', e => {
addMapModal()
})
function addMapModal(params) {
const { fillStyle, strokeStyle, lineWidth } = {
fillStyle: 'rgba(255,255,255,0.8)',
strokeStyle: '#f00',
lineWidth: 3,
...params
}
ctx.fillStyle = fillStyle;
ctx.strokeStyle = strokeStyle;
ctx.lineWidth = lineWidth;
ctx.clearRect(0, 0, canvas.width, canvas.height)
const coords = modalData.map(coord => {
return map.getPixelFromCoordinate(ol.proj.fromLonLat(coord))
})
ctx.beginPath();
coords.forEach((coord, index) => {
index === 0 ? ctx.moveTo(coord[0], coord[1]) : ctx.lineTo(coord[0], coord[1])
})
ctx.closePath();
ctx.fill();
ctx.globalCompositeOperation = 'source-out';
ctx.rect(0, 0, canvas.width, canvas.height)
ctx.fill();
ctx.globalCompositeOperation = 'source-over';
ctx.beginPath();
coords.forEach((coord, index) => {
index === 0 ? ctx.moveTo(coord[0], coord[1]) : ctx.lineTo(coord[0], coord[1])
})
ctx.closePath();
ctx.stroke()
}