<..._vue 旋转的地球">
当前位置:   article > 正文

Vue中实现3D得球自动旋转

vue 旋转的地球

1e7c9687bff03f95ad2f6a86a7429d5d.png

具体实现

安装echarts

在终端下安装echarts

npm install -D echarts

安装echarts-gl

在终端下安装echarts-gl

npm install -D echarts-gl

earth3D组件

earth3D.vue

  1. <template>
  2.     <div class="globe3d-earth-container" >
  3.          <div class="globe3d-earth"  ref="Globe3d"></div>
  4.     </div>
  5. </template>
  6. <script>
  7.     import Vue from "vue";
  8.     import echarts from "echarts";
  9.     import "echarts-gl";
  10.     import option from './Globe3d.js'
  11.     import { debounce } from "./debounce.js";
  12.     Vue.prototype.echarts = echarts;
  13.     export default {
  14.         name: "earth3D",
  15.         data() {
  16.             return {
  17.                 myChart: null,
  18.                 resizeHandler: null
  19.             }
  20.         },
  21.         mounted() {
  22.             // 基于准备好的dom,初始化echarts实例
  23.             this.myChart = echarts.init(this.$refs.Globe3d);
  24.             // 使用刚指定的配置项和数据显示图表。可视化3D
  25.             this.myChart.setOption(option);
  26.             this.resizeHandler = debounce(() => {
  27.                 if (this.myChart) {
  28.                     this.myChart.resize();
  29.                 }
  30.             }, 100);
  31.             this.initResizeEvent();
  32.         },
  33.         methods: {
  34.             //监听resize
  35.             initResizeEvent() {
  36.                 window.addEventListener("resize", this.resizeHandler);
  37.             },
  38.             //移除resize
  39.             destroyResizeEvent() {
  40.               window.removeEventListener("resize", this.resizeHandler);
  41.             }
  42.         },
  43.         beforeDestroy() {
  44.             this.destroyResizeEvent();
  45.             if (!this.myChart) {
  46.               return;
  47.             }
  48.             this.myChart.dispose();
  49.             this.myChart = null;
  50.         },
  51.         activated() {
  52.             this.initResizeEvent();
  53.             if (this.myChart) {
  54.               this.myChart.resize();
  55.             }
  56.         },
  57.         deactivated() {
  58.             this.destroyResizeEvent();
  59.         }
  60.     }
  61. </script>
  62. <style lang="scss" scoped>
  63. .globe3d-earth-container {
  64.     width: 100%;
  65.     height: 100%;
  66.     background:#2d3a4b;
  67.     .globe3d-earth {
  68.         width: 100%;
  69.         height: 702px;
  70.     }
  71. }
  72. </style>

debounce.js

  1. export function debounce(func, wait, immediate) {
  2.     let timeout, args, context, timestamp, result;
  3.   
  4.     const later = function() {
  5.       // 据上一次触发时间间隔
  6.       const last = +new Date() - timestamp;
  7.   
  8.       // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
  9.       if (last < wait && last > 0) {
  10.         timeout = setTimeout(later, wait - last);
  11.       } else {
  12.         timeout = null;
  13.         // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
  14.         if (!immediate) {
  15.           result = func.apply(context, args);
  16.           if (!timeout) context = args = null;
  17.         }
  18.       }
  19.     };
  20.   
  21.     return function(...args) {
  22.       context = this;
  23.       timestamp = +new Date();
  24.       const callNow = immediate && !timeout;
  25.       // 如果延时不存在,重新设定延时
  26.       if (!timeout) timeout = setTimeout(later, wait);
  27.       if (callNow) {
  28.         result = func.apply(context, args);
  29.         context = args = null;
  30.       }
  31.   
  32.       return result;
  33.     };
  34.   }

Globe3d.js

  1. import baseTextureImg from "./worldbathy.jpg";
  2. import heightTextImg from "./worldbathy.jpg";
  3. import environmentImg from "./starfield.jpg";
  4. // import textureImg from "@/assets/images/pisa.jpg";
  5. export default {
  6.   backgroundColor: "#2d3a4b",
  7.   globe: {
  8.     baseTexture: baseTextureImg,
  9.     heightTexture: heightTextImg,
  10.     displacementScale: 0.04,
  11.     shading: "realistic",
  12.     environment: environmentImg,
  13.     realisticMaterial: {
  14.       roughness: 0.9
  15.     },
  16.     postEffect: {
  17.       enable: true
  18.     },
  19.     light: {
  20.       main: {
  21.         intensity: 5,
  22.         shadow: true
  23.       },
  24.       ambientCubemap: {
  25.         // texture: textureImg,
  26.         diffuseIntensity: 0.2
  27.       }
  28.     }
  29.   }
  30. };

视频号如何做视频任务进行变现

2023-09-05

fccfbfd47ab651bbc04554ca78952540.jpeg

视频号如何插入带货商品链接进行变现

2023-09-04

d5696fcf56dcad58a40956a526eaf161.jpeg

36岁男子自称被裁,曾是前500强公司市场总监,最后接受做外买

2023-09-03

3bc15ec7c487d96bf9b1c798e86e672f.jpeg

聊一下互联网红利并牢牢抓住

2023-09-02

afa03c6e84f28940d147e9fde2e5503c.jpeg

关于大学考研与不考研自己一点看法

2023-09-01

8644fb3e9e5d93efc57888e107f176a8.jpeg

css中文本阴影特效

2023-08-30

99a9db363ea5b65d5e46dfda29839a2b.jpeg

css实现手风琴效果

2023-08-29

845f86d6751a90190ba8859732dab512.jpeg

如何成为一个有格局的人

2023-08-28

f5fb2b5dba7ba0ac816181036560f8dd.jpeg

49a700201d1cbedc8316a0a6e920763c.png

(能问答,能绘画)

511241ac59580afbbf7497dfe12e25cf.jpeg

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/AllinToyou/article/detail/325816
推荐阅读
相关标签
  

闽ICP备14008679号