74.在 Vue 3 中使用 OpenLayers 实现游龙动画效果

news/2025/2/8 22:04:36 标签: vue.js, 前端, javascript, openlayers, 深度学习

1. 引言

在 WebGIS 开发中,OpenLayers 是一个强大的开源库,可以用于渲染地图、绘制矢量图层等功能。本篇文章将介绍如何在 Vue 3 中使用 OpenLayers 结合 Composition API,实现一个“游龙”动画效果,该动画通过数学公式计算轨迹,并动态渲染在地图上。


2. 项目环境与依赖

在开始之前,我们需要确保已经安装 Vue 3 以及 OpenLayers。

2.1 安装 Vue 3 项目

如果你还没有 Vue 3 项目,可以使用 Vite 快速创建:

javascript">npm create vite@latest vue-openlayers --template vue
cd vue-openlayers
npm install

2.2 安装 OpenLayers

npm install ol

3. 代码实现

javascript"><!--
 * @Author: 彭麒
 * @Date: 2025/2/6
 * @Email: 1062470959@qq.com
 * @Description: 此源码版权归吉檀迦俐所有,可供学习和借鉴或商用。
 -->
<template>
  <div class="container">
    <div class="w-full flex justify-center flex-wrap">
      <div class="font-bold text-[24px]">在Vue3中使用OpenLayers实现游龙动画效果</div>
    </div>
    <div id="vue-openlayers"></div>
  </div>
</template>

<script setup>
import {onMounted, ref} from "vue";
import "ol/ol.css";
import Map from "ol/Map";
import OSM from "ol/source/OSM";
import TileLayer from "ol/layer/Tile";
import View from "ol/View";
import {Circle as CircleStyle, Fill, Stroke, Style} from "ol/style";
import {MultiPoint, Point} from "ol/geom";
import {getVectorContext} from "ol/render";

const map = ref(null);

const initMap = () => {
  const tileLayer = new TileLayer({
    source: new OSM(),
  });

  map.value = new Map({
    target: "vue-openlayers",
    layers: [tileLayer],
    view: new View({
      center: [0, 0],
      zoom: 2,
    }),
  });

  const imageStyle = new Style({
    image: new CircleStyle({
      radius: 10,
      fill: new Fill({
        color: "LimeGreen",
      }),
      stroke: new Stroke({
        color: "yellow",
        width: 1,
      }),
    }),
  });

  const headInnerImageStyle = new Style({
    image: new CircleStyle({
      radius: 8,
      fill: new Fill({
        color: "red",
      }),
    }),
  });

  const headOuterImageStyle = new Style({
    image: new CircleStyle({
      radius: 10,
      fill: new Fill({
        color: "black",
      }),
    }),
  });

  const n = 200;
  const omegaTheta = 30000; // Rotation period in ms
  const R = 7e6;
  const r = 2e6;
  const p = 2e6;

  tileLayer.on("postrender", (event) => {
    const vectorContext = getVectorContext(event);
    const frameState = event.frameState;
    const theta = (2 * Math.PI * frameState.time) / omegaTheta;
    const coordinates = [];

    for (let i = 0; i < n; ++i) {
      const t = theta + (2 * Math.PI * i) / n;
      const x = (R + r) * Math.cos(t) + p * Math.cos(((R + r) * t) / r);
      const y = (R + r) * Math.sin(t) + p * Math.sin(((R + r) * t) / r);
      coordinates.push([x, y]);
    }

    vectorContext.setStyle(imageStyle);
    vectorContext.drawGeometry(new MultiPoint(coordinates));

    const headPoint = new Point(coordinates[coordinates.length - 1]);

    vectorContext.setStyle(headOuterImageStyle);
    vectorContext.drawGeometry(headPoint);

    vectorContext.setStyle(headInnerImageStyle);
    vectorContext.drawGeometry(headPoint);

    map.value.render();
  });

  map.value.render();
};

onMounted(() => {
  initMap();
});
</script>

<style scoped>
.container {
  width: 840px;
  height: 590px;
  margin: 50px auto;
  border: 1px solid #42b983;
}

#vue-openlayers {
  width: 800px;
  height: 470px;
  margin: 0 auto;
  border: 1px solid #42b983;
  position: relative;
}
</style>


4. 代码解析

4.1 轨迹动画原理

游龙动画的轨迹基于外摆线公式计算得出:

x = (R + r) \cos(t) + p \cos\left(\frac{(R + r) t}{r}\right)$$

y = (R + r) \sin(t) + p \sin\left(\frac{(R + r) t}{r}\right)$$

其中:

  • R 是大圆半径,
  • r 是小圆半径,
  • p 是轨迹偏移量,
  • t 代表时间。

4.2 OpenLayers 动态渲染

我们利用 postrender 事件,在每一帧中重新计算轨迹点,并使用 getVectorContext 绘制动画。


5. 运行效果

OpenLayers.vue 组件引入 App.vue,运行项目:

npm run dev

最终,我们可以看到 OpenLayers 中动态渲染出一个游龙轨迹动画


6. 结语

本篇文章介绍了如何在 Vue 3 中使用 OpenLayers 实现游龙动画效果,涉及 OpenLayers 基础知识、数学公式计算轨迹以及 Vue 3 的 Composition API 逻辑拆分。如果你觉得这篇文章对你有帮助,欢迎点赞收藏!🚀


http://www.niftyadmin.cn/n/5845324.html

相关文章

《语义捕捉全解析:从“我爱自然语言处理”到嵌入向量的全过程》

首先讲在前面&#xff0c;介绍一些背景 RAG&#xff08;Retrieval-Augmented Generation&#xff0c;检索增强生成&#xff09; 是一种结合了信息检索与语言生成模型的技术&#xff0c;通过从外部知识库中检索相关信息&#xff0c;并将其作为提示输入给大型语言模型&#xff…

javaEE初阶————多线程初阶(3)

大家新年快乐呀&#xff0c;今天是第三期啦&#xff0c;大家前几期的内容掌握的怎么样啦&#xff1f; 1&#xff0c;线程死锁 1.1 构成死锁的场景 a&#xff09;一个线程一把锁 这个在java中是不会发生的&#xff0c;因为我们之前讲的可重入机制&#xff0c;在其他语言中可…

wxWidgets生成HTML文件,带图片转base64数据

编译环境大家可以看我之前的文章,CodeBlocks + msys2 + wx3.2,win10 这里功能就是生成HTML文件,没用HTML库,因为是自己固定的格式,图片是一个vector,可以动态改变数量的。 效果如下: #include <wx/string.h> #include <wx/file.h> #include <wx/ima…

Go 语言 | 入门 | 快速入门

快速入门 1.第一份代码 先检查自己是否有正确下载 Go&#xff0c;如果没有直接去 Go 安装 进行安装。 # 检查是否有 Go $ go version go version go1.23.4 linux/amd64然后根据 Go 的入门教程 开始进行学习。 # 初始化 Go 项目 $ mkdir example && cd example # Go…

【MySQL】向后兼容设计规范(无回滚场景)

MySQL 向后兼容设计规范&#xff08;无回滚场景&#xff09; 在 不支持数据库回滚 且需保证 长期向后兼容性 的系统中&#xff0c;需通过 架构设计 和 流程管控 规避风险。以下是关键设计规范&#xff1a; 一、变更流程规范 变更分类分级 变更类型风险评估等级审批流程测试要求…

TCN时间卷积神经网络多变量多步光伏功率预测(Matlab)

代码下载&#xff1a;TCN时间卷积神经网络多变量多步光伏功率预测&#xff08;Matlab&#xff09; TCN时间卷积神经网络多变量多步光伏功率预测 一、引言 1.1、研究背景和意义 随着全球能源危机的加剧和环保意识的提升&#xff0c;可再生能源&#xff0c;尤其是太阳能&…

JS实现一个通用的循环填充数组的方法

function createFilledArray(length, pattern) {return Array.from({ length }, (_, i) > pattern[i % pattern.length]); }// 示例 const result createFilledArray(8, [1, 2, 3]);console.log(result); // [1, 2, 3, 1, 2, 3, 1, 2]解析&#xff1a; createFilledArray(…

Linux运维——文件内容查看编辑

文件内容查看编辑 一、Linux 文件内容查看编辑要点二、命令常见用法2.1、cat2.2、head2.3、tail2.4、more2.5、less2.6、sed2.7、vi2.8、grep 一、Linux 文件内容查看编辑要点 连接文件并打印到标准输出设备 - 使用 cat显示指定文件的开头若干行 - 使用 head显示指定文件的末尾…