vue3-openlayers marker 光晕扩散(光环扩散)(postrender 事件和 render 方法)

本篇介绍一下使用 vue3-openlayers marker 光晕扩散(光环扩散)(postrender 事件和 render 方法)

1 需求

  • marker 光晕扩散(光环扩散)

2 分析

marker 光晕扩散(光环扩散)使用 postrender 事件和 render 方法

关于即时渲染的知识点请看上篇《openlayers marker 光晕扩散(光环扩散)(postrender 事件和 render 方法)》

3 实现

3.1 实现一(光环)

javascript 复制代码
<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
    ref="mapRef"
  >
    <ol-view ref="view" :center="center" :zoom="zoom" :projection="projection" />

    <ol-tile-layer>
      <ol-source-tianditu
        layerType="img"
        :projection="projection"
        :tk="key"
        :hidpi="true"
        ref="sourceRef"
      ></ol-source-tianditu>
    </ol-tile-layer>
    <ol-tile-layer>
      <ol-source-tianditu
        :isLabel="true"
        layerType="img"
        :projection="projection"
        :tk="key"
        :hidpi="true"
      ></ol-source-tianditu>
    </ol-tile-layer>
    <ol-vector-layer ref="vectorLayerRef" @postrender="handlePostRender">
      <ol-source-vector>
        <ol-feature>
          <ol-geom-point :coordinates="[110, 30]"></ol-geom-point>
          <ol-style>
            <ol-style-icon :src="iconSrc" :scale="0.05"></ol-style-icon>
          </ol-style>
        </ol-feature>
        <ol-feature>
          <ol-geom-point :coordinates="[112, 31]"></ol-geom-point>
          <ol-style>
            <ol-style-icon :src="iconSrc" :scale="0.05"></ol-style-icon>
          </ol-style>
        </ol-feature>
      </ol-source-vector>
    </ol-vector-layer>
  </ol-map>
  <div class="toolbar">
    <el-button type="primary" @click="handleClick">{{ animationFlag ? '停止' : '开始' }}</el-button>
  </div>
</template>

<script setup lang="ts">
import iconSrc from '@/assets/image/truck.png';
import { getVectorContext } from 'ol/render.js';
import { easeOut } from 'ol/easing.js';
import { Circle, Stroke, Style } from 'ol/style';

const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const mapRef = ref();
const key = '替换为天地图key';
const sourceRef = ref(null);
const vectorLayerRef = ref(null);
const animationFlag = ref(false);
const duration = ref([3000, 1000]);
const start = ref([0, 0]);

const handleClick = () => {
  if (!animationFlag.value) {
    start.value = start.value.map(i => Date.now());
    vectorLayerRef.value.vectorLayer.changed();
  }
  animationFlag.value = !animationFlag.value;
};

const handlePostRender = e => {
  if (animationFlag.value) {
    const time = e.frameState.time;
    vectorLayerRef.value.vectorLayer
      .getSource()
      .getFeatures()
      .forEach((f, idx) => {
        // 时间戳差(毫秒)
        let elapsedTime = time - start.value[idx];
        if (elapsedTime >= duration.value[idx]) {
          start.value[idx] = Date.now();
          elapsedTime = duration.value[idx];
        }
        // 获取矢量上下文
        const vectorContext = getVectorContext(e);
        // elapsedRatio值0到1之间
        const elapsedRatio = elapsedTime / duration.value[idx];
        const radius = easeOut(elapsedRatio) * 25 + 5;
        const opacity = easeOut(1 - elapsedRatio);

        const style = new Style({
          image: new Circle({
            radius: radius,
            stroke: new Stroke({
              color: 'rgba(255, 0, 0, ' + opacity + ')',
              width: 1 + opacity
            })
          })
        });
        // 将feature渲染到画布中。
        vectorContext.drawFeature(f.clone(), style);
      });
    mapRef.value.map.render();
  }
};
</script>
<style scoped lang="scss">
.toolbar {
  position: absolute;
  top: 20px;
  left: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>

3.2 实现二(光晕)

javascript 复制代码
<template>
  <ol-map
    :loadTilesWhileAnimating="true"
    :loadTilesWhileInteracting="true"
    style="width: 100%; height: 100%"
    ref="mapRef"
  >
    <ol-view ref="view" :center="center" :zoom="zoom" :projection="projection" />

    <ol-tile-layer>
      <ol-source-tianditu
        layerType="img"
        :projection="projection"
        :tk="key"
        :hidpi="true"
        ref="sourceRef"
      ></ol-source-tianditu>
    </ol-tile-layer>
    <ol-tile-layer>
      <ol-source-tianditu
        :isLabel="true"
        layerType="img"
        :projection="projection"
        :tk="key"
        :hidpi="true"
      ></ol-source-tianditu>
    </ol-tile-layer>
    <ol-vector-layer ref="vectorLayerRef" @postrender="handlePostRender">
      <ol-source-vector>
        <ol-feature>
          <ol-geom-point :coordinates="[110, 30]"></ol-geom-point>
        </ol-feature>
        <ol-feature>
          <ol-geom-point :coordinates="[112, 31]"></ol-geom-point>
        </ol-feature>
      </ol-source-vector>
    </ol-vector-layer>
    <ol-vector-layer >
      <ol-source-vector>
        <ol-feature>
          <ol-geom-point :coordinates="[110, 30]"></ol-geom-point>
          <ol-style>
            <ol-style-icon :src="iconSrc" :scale="0.05"></ol-style-icon>
          </ol-style>
        </ol-feature>
        <ol-feature>
          <ol-geom-point :coordinates="[112, 31]"></ol-geom-point>
          <ol-style>
            <ol-style-icon :src="iconSrc" :scale="0.05"></ol-style-icon>
          </ol-style>
        </ol-feature>
      </ol-source-vector>
    </ol-vector-layer>
  </ol-map>
  <div class="toolbar">
    <el-button type="primary" @click="handleClick">{{ animationFlag ? '停止' : '开始' }}</el-button>
  </div>
</template>

<script setup lang="ts">
import iconSrc from '@/assets/image/truck.png';
import { getVectorContext } from 'ol/render.js';
import { easeOut } from 'ol/easing.js';
import { Circle, Fill, Stroke, Style } from 'ol/style';

const center = ref([121, 31]);
const projection = ref('EPSG:4326');
const zoom = ref(5);
const mapRef = ref();
const key = '替换为天地图key';
const sourceRef = ref(null);
const vectorLayerRef = ref(null);
const animationFlag = ref(false);
const duration = ref([3000, 1000]);
const start = ref([0, 0]);

const handleClick = () => {
  if (!animationFlag.value) {
    start.value = start.value.map(i => Date.now());
    vectorLayerRef.value.vectorLayer.changed();
  }
  animationFlag.value = !animationFlag.value;
};

const handlePostRender = e => {
  if (animationFlag.value) {
    const time = e.frameState.time;
    vectorLayerRef.value.vectorLayer
      .getSource()
      .getFeatures()
      .forEach((f, idx) => {
        // 时间戳差(毫秒)
        let elapsedTime = time - start.value[idx];
        if (elapsedTime >= duration.value[idx]) {
          start.value[idx] = Date.now();
          elapsedTime = duration.value[idx];
        }
        // 获取矢量上下文
        const vectorContext = getVectorContext(e);
        // elapsedRatio值0到1之间
        const elapsedRatio = elapsedTime / duration.value[idx];
        const radius = easeOut(elapsedRatio) * 25 + 5;
        const opacity = easeOut(1 - elapsedRatio);

        const style = new Style({
          image: new Circle({
            radius: radius,
            fill: new Fill({
              color: 'rgba(228, 147, 87, ' + opacity + ')'
            }),
            stroke: new Stroke({
              color: 'rgba(255, 0, 0, ' + opacity + ')',
              width: 1 + opacity
            })
          })
        });
        // 将feature渲染到画布中。
        vectorContext.drawFeature(f.clone(), style);
      });
    mapRef.value.map.render();
  }
};
</script>
<style scoped lang="scss">
.toolbar {
  position: absolute;
  top: 20px;
  left: 100px;
  display: flex;
  justify-content: center;
  align-items: center;
}
</style>
相关推荐
上海合宙LuatOS10 分钟前
全栈工程师实战手册:LuatOS日志系统开发指南!
java·开发语言·单片机·嵌入式硬件·物联网·php·硬件工程
多敲代码防脱发10 分钟前
导出导入Excel文件(详解-基于EasyExcel)
java·开发语言·jvm·数据库·mysql·excel
struggle202542 分钟前
continue通过我们的开源 IDE 扩展和模型、规则、提示、文档和其他构建块中心,创建、共享和使用自定义 AI 代码助手
javascript·ide·python·typescript·开源
来自星星的坤1 小时前
深入理解 NumPy:Python 科学计算的基石
开发语言·python·numpy
x-cmd1 小时前
[250512] Node.js 24 发布:ClangCL 构建,升级 V8 引擎、集成 npm 11
前端·javascript·windows·npm·node.js
小声读源码1 小时前
【技巧】使用UV创建python项目的开发环境
开发语言·python·uv·dify
yxc_inspire1 小时前
基于Qt的app开发第七天
开发语言·c++·qt·app
zm-v-159304339861 小时前
解锁遥感数据密码:DeepSeek、Python 与 OpenCV 的协同之力
开发语言·python·opencv
夏之小星星1 小时前
el-tree结合checkbox实现数据回显
前端·javascript·vue.js
周Echo周2 小时前
20、map和set、unordered_map、un_ordered_set的复现
c语言·开发语言·数据结构·c++·算法·leetcode·list