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

具体实现

安装echarts

在终端下安装echarts

go 复制代码
npm install -D echarts

安装echarts-gl

在终端下安装echarts-gl

go 复制代码
npm install -D echarts-gl

earth3D组件

earth3D.vue

go 复制代码
<template>
    <div class="globe3d-earth-container" >
         <div class="globe3d-earth"  ref="Globe3d"></div>
    </div>
</template>

<script>
    import Vue from "vue";
    import echarts from "echarts";
    import "echarts-gl";
    import option from './Globe3d.js'
    import { debounce } from "./debounce.js";
    Vue.prototype.echarts = echarts;
    export default {
        name: "earth3D",
        data() {
            return {
                myChart: null,
                resizeHandler: null
            }
        },
        mounted() {
            // 基于准备好的dom,初始化echarts实例
            this.myChart = echarts.init(this.$refs.Globe3d);
            // 使用刚指定的配置项和数据显示图表。可视化3D
            this.myChart.setOption(option);
            this.resizeHandler = debounce(() => {
                if (this.myChart) {
                    this.myChart.resize();
                }
            }, 100);
            this.initResizeEvent();
        },

        methods: {
            //监听resize
            initResizeEvent() {
                window.addEventListener("resize", this.resizeHandler);
            },
            //移除resize
            destroyResizeEvent() {
              window.removeEventListener("resize", this.resizeHandler);
            }
        },

        beforeDestroy() {
            this.destroyResizeEvent();
            if (!this.myChart) {
              return;
            }
            this.myChart.dispose();
            this.myChart = null;
        },

        activated() {
            this.initResizeEvent();
            if (this.myChart) {
              this.myChart.resize();
            }
        },

        deactivated() {
            this.destroyResizeEvent();
        }
    } 
</script>
<style lang="scss" scoped>
.globe3d-earth-container {
    width: 100%;
    height: 100%;
    background:#2d3a4b;
    .globe3d-earth {
        width: 100%;
        height: 702px;
    }
}
</style>

debounce.js

go 复制代码
export function debounce(func, wait, immediate) {
    let timeout, args, context, timestamp, result;
  
    const later = function() {
      // 据上一次触发时间间隔
      const last = +new Date() - timestamp;
  
      // 上次被包装函数被调用时间间隔 last 小于设定时间间隔 wait
      if (last < wait && last > 0) {
        timeout = setTimeout(later, wait - last);
      } else {
        timeout = null;
        // 如果设定为immediate===true,因为开始边界已经调用过了此处无需调用
        if (!immediate) {
          result = func.apply(context, args);
          if (!timeout) context = args = null;
        }
      }
    };
  
    return function(...args) {
      context = this;
      timestamp = +new Date();
      const callNow = immediate && !timeout;
      // 如果延时不存在,重新设定延时
      if (!timeout) timeout = setTimeout(later, wait);
      if (callNow) {
        result = func.apply(context, args);
        context = args = null;
      }
  
      return result;
    };
  }

Globe3d.js

go 复制代码
import baseTextureImg from "./worldbathy.jpg";
import heightTextImg from "./worldbathy.jpg";
import environmentImg from "./starfield.jpg";
// import textureImg from "@/assets/images/pisa.jpg";

export default {
  backgroundColor: "#2d3a4b",
  globe: {
    baseTexture: baseTextureImg,
    heightTexture: heightTextImg,
    displacementScale: 0.04,
    shading: "realistic",
    environment: environmentImg,
    realisticMaterial: {
      roughness: 0.9
    },
    postEffect: {
      enable: true
    },
    light: {
      main: {
        intensity: 5,
        shadow: true
      },
      ambientCubemap: {
        // texture: textureImg,
        diffuseIntensity: 0.2
      }
    }
  }
};

<>

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

2023-09-05

<>

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

2023-09-04

<>

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

2023-09-03

<>

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

2023-09-02

<>

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

2023-09-01

<>

css中文本阴影特效
<>

2023-08-30

<>

css实现手风琴效果
<>

2023-08-29

<>

如何成为一个有格局的人
<>

2023-08-28

(能问答,能绘画)

相关推荐
RFCEO1 分钟前
学习前端编程:DOM 树、CSSOM 树、渲染树详解
前端·学习·渲染树·dom 树·cssom 树·浏览器的渲染流程·回流/重绘
笨蛋不要掉眼泪4 分钟前
Redis主从复制:原理、配置与实战演示
前端·redis·bootstrap·html
bigdata-rookie7 分钟前
Starrocks 数据模型
java·前端·javascript
白帽子凯哥哥9 分钟前
网络安全Web基础完全指南:从小白到入门安全测试
前端·sql·web安全·信息安全·渗透测试·漏洞
RFCEO15 分钟前
前端编程 课程十四、:CSS核心基础2:选择器优先级 + 伪类选择器(解决冲突+交互效果)
前端·css·交互·css选择器优先级判断规则详解·css important使用·css链接伪类lvha顺序·实现悬浮交互效果
web打印社区17 分钟前
前端实现浏览器预览打印:从原生方案到专业工具
前端·javascript·vue.js·electron
yuezhilangniao20 分钟前
# 告别乱码:用FastAPI特性与Next.js打造类型安全的API通信
javascript·安全·fastapi
jiayong2329 分钟前
Vue2 与 Vue3 生态系统及工程化对比 - 面试宝典
vue.js·面试·职场和发展
徐同保31 分钟前
vue.config.ts配置代理解决跨域,配置开发环境开启source-map
前端·javascript·vue.js
Hexene...1 小时前
【前端Vue】npm install时根据新的状态重新引入实际用到的包,不引入未使用到的
前端·vue.js·npm