当前位置: 首页 > news >正文

OpenLayers 综合案例-动态闪烁

看过的知识不等于学会。唯有用心总结、系统记录,并通过温故知新反复实践,才能真正掌握一二
作为一名摸爬滚打三年的前端开发,开源社区给了我饭碗,我也将所学的知识体系回馈给大家,助你少走弯路!
OpenLayers、Leaflet 快速入门 ,每周保持更新 2 个案例
Cesium 快速入门,每周保持更新 4 个案例

OpenLayers 综合案例-动态闪烁

Vue 3 + OpenLayers 实现的 WebGIS 应用提供了完整的动态闪烁功能

主要功能

  1. 通过postrender事件监听实现点位半径渐变和线段透明度变化的动画效果,使用getVectorContext获取绘制上下文。
  2. 采用高德地图底图,支持自定义动画参数

在这里插入图片描述

MP4效果动画链接地址

技术栈

该环境下代码即拿即用

Vue 3.5.13+
OpenLayers 10.5.0+
Vite 6.3.5+
<template><div ref="mapContainer" id="map"></div>
</template><script setup>
import { ref, onMounted } from "vue";
import Map from "ol/Map.js";
import XYZ from "ol/source/XYZ.js";
import TileLayer from "ol/layer/Tile.js";
import VectorLayer from "ol/layer/Vector.js";
import VectorSource from "ol/source/Vector.js";
import Feature from "ol/Feature.js";
import Point from "ol/geom/Point.js";
import LineString from "ol/geom/LineString.js";
import { Style, Circle, Stroke } from "ol/style.js";
import { getVectorContext } from "ol/render.js"; // 获取用于在事件画布上绘制的矢量上下文
import View from "ol/View.js";
import "ol/ol.css";const mapContainer = ref(null);
let map = null;
let pointLayer = null;
let lineLayer = null;
let pointFeatures = [];
let lineFeatures = [];// 动画参数
let pointRadius = 0;
let lineOpacity = 1;
let lineDirection = -1;onMounted(() => {// 初始化地图map = new Map({target: mapContainer.value,layers: [new TileLayer({source: new XYZ({url: "https://webrd04.is.autonavi.com/appmaptile?lang=zh_cn&size=1&scale=1&style=7&x={x}&y={y}&z={z}",}),}),],view: new View({center: [118.7784, 32.0647],zoom: 16,projection: "EPSG:4326",}),});// 创建点位要素const point1 = new Feature({ geometry: new Point([118.7784, 32.0647]) });const point2 = new Feature({ geometry: new Point([118.7884, 32.0657]) });const point3 = new Feature({ geometry: new Point([118.7684, 32.0677]) });pointFeatures = [point1, point2, point3];// 创建线段要素const line1 = new Feature({geometry: new LineString([[118.7784, 32.0647],[118.7884, 32.0657],]),});const line2 = new Feature({geometry: new LineString([[118.7884, 32.0657],[118.7684, 32.0677],]),});lineFeatures = [line1, line2];// 创建点位图层pointLayer = new VectorLayer({source: new VectorSource({ features: pointFeatures }),style: new Style({image: new Circle({radius: 5,stroke: new Stroke({ color: "#ff0000", width: 2 }),}),}),});// 创建线段图层lineLayer = new VectorLayer({source: new VectorSource({ features: lineFeatures }),style: new Style({stroke: new Stroke({ color: "#ff0000", width: 3 }),}),});map.addLayer(pointLayer);map.addLayer(lineLayer);// 点位闪烁动画pointLayer.on("postrender", (evt) => {if (pointRadius >= 20) pointRadius = 0;const opacity = (20 - pointRadius) * (1 / 20);const pointStyle = new Style({image: new Circle({radius: pointRadius,stroke: new Stroke({color: `rgba(255,0,0,${opacity})`,width: 3 - pointRadius / 10,}),}),});const vectorContext = getVectorContext(evt);vectorContext.setStyle(pointStyle);pointFeatures.forEach((feature) => {vectorContext.drawGeometry(feature.getGeometry());});pointRadius += 0.3;map.render();});// 线段闪烁动画lineLayer.on("postrender", (evt) => {if (lineOpacity <= 0.2) lineDirection = 1;if (lineOpacity >= 1) lineDirection = -1;lineOpacity += lineDirection * 0.05;const lineStyle = new Style({stroke: new Stroke({color: `rgba(255,0,0,${lineOpacity})`,width: 5,}),});const vectorContext = getVectorContext(evt);vectorContext.setStyle(lineStyle);lineFeatures.forEach((feature) => {vectorContext.drawGeometry(feature.getGeometry());});map.render();});
});
</script><style scoped>
#map {width: 100vw;height: 100vh;
}
</style>
http://www.dtcms.com/a/302756.html

相关文章:

  • YOLO11 改进、魔改|低分辨率自注意力机制LRSA ,提取全局上下文建模与局部细节,提升小目标、密集小目标的检测能力
  • Python将Word转换为Excel
  • eclipse更改jdk环境和生成webservice客户端代码
  • Linux应用管理与YUM/DNF指南
  • 迅为RK3568开发板OpeHarmony学习开发手册-配置电源管理芯片和点亮HDMI屏幕-配置电源管理芯片
  • ARM share memory
  • 智慧工地系统:科技赋能建筑新未来
  • 电子签章(PDF)
  • 阿里云可观测 2025 年 6 月产品动态
  • 【机器学习-4】 | 集成学习 / 随机森林篇
  • 以科力锐为例介绍常见的数据中心4种灾备方式
  • HDFS Block与Spark的partition对比
  • MCU+RTOS调试
  • unisS5800XP-G交换机配置命令之端口篇
  • 经典算法题解析:从思路到实现,掌握核心编程思维
  • 分布式数据库中的“分布式连接”(Distributed Joins)
  • YOLOv8 基于RTSP流目标检测
  • 【C++详解】深入解析继承 类模板继承、赋值兼容转换、派生类默认成员函数、多继承与菱形继承
  • 把振动数据转成音频并播放
  • Kubernetes --存储入门
  • 实时YOLO目标检测与跟踪系统设计
  • [蓝牙通信] 临界区管理 | volatile | 同步(互斥锁与信号量) | handle
  • 谷歌浏览器深入用法全解析:解锁高效网络之旅
  • UVA11990 ``Dynamic‘‘ Inversion
  • kotlin基础【3】
  • 第一章:Go语言基础入门之流程控制
  • Power Query合并数据
  • 力扣 hot100 Day58
  • JAVA东郊到家按摩服务同款同城家政服务按摩私教茶艺师服务系统小程序+公众号+APP+H5
  • EXCEL 怎么把汉字转换成拼音首字母