二維地圖中立體陰影效果實(shí)現(xiàn)

概述

前兩天有個(gè)學(xué)員在群里發(fā)出來一張截圖,效果是一個(gè)區(qū)域掩膜+邊框立體陰影效果,咨詢我怎么實(shí)現(xiàn),我看了下心里大概有了一個(gè)想法,只是前兩天比較忙就沒實(shí)現(xiàn),趁著周末就想著驗(yàn)證實(shí)現(xiàn)一下。鑒于學(xué)員的要求,本文使用的是leaflet框架。

效果

效果

實(shí)現(xiàn)思路

  1. 掩膜和陰影都使用矢量圖層;
  2. 掩膜借助turf.difference實(shí)現(xiàn)數(shù)據(jù)的處理;
  3. 注冊(cè)地圖zoomend事件,計(jì)算當(dāng)前級(jí)別的分辨率,計(jì)算偏移量對(duì)坐標(biāo)點(diǎn)數(shù)據(jù)進(jìn)行偏移,疊加圖層實(shí)現(xiàn)陰影效果。

實(shí)現(xiàn)

1. 使用技術(shù)

  • leaflet
  • turf.js

2. 實(shí)現(xiàn)代碼

fetch('./data/beijing-boundry.json').then(res => res.json()).then(res => {
  res = res.features[0]
  const coordsBoundry = res.geometry.coordinates
  let shadowLayer = L.layerGroup().addTo(map);
  const mask = turf.difference(turf.bboxPolygon([-180, -90, 180, 90]), res);
  L.geoJSON(mask, {
    style: {
      color: '#098dde',
      weight: 3,
      opacity: 1,
      fillColor: '#ffffff',
      fillOpacity: 0.5
    }
  }).addTo(map);
  let addShadowLayer = function () {
    shadowLayer.clearLayers()
    const zoom = map.getZoom()
    const res = 360 / (Math.pow(2, zoom) * 256)
    const offset  = 2 * res
    let coords = [...coordsBoundry[0]].map(([lon, lat]) => {
      return [lon - offset * 2, lat - offset]
    })
    const feature = new Feature([coords])
    shadowLayer.addLayer(L.geoJSON(feature, {
      style: {
        color: '#098dde',
        weight: 8,
        opacity: 0.5,
        fill: false
      }
    }))
  }
  addShadowLayer()
  map.on('zoomend', () => {
    addShadowLayer()
  })
})

3. 測(cè)試數(shù)據(jù)

測(cè)試數(shù)據(jù)可從https://wwus.lanzouk.com/iAiwo10x18ze獲取。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容