基于vue3.x与 three.js模拟地球内部结构

6,529次阅读
没有评论

共计 2422 个字符,预计需要花费 7 分钟才能阅读完成。

基于 vue3.x 与 three.js 模拟地球内部结构


前言

基于 threejs、vue3.x、热力图实现了地球结构可视化展示。


1. 引入库

threejs、vue3.x

热力图代码



let getTemperature = () => {
  let temperatureArray = new Array();
  for (let i = 0; i  9; i++) {
    temperatureArray[i] = new Array();
    for (let j = 0; j  9; j++) {
      temperatureArray[i][j] = parseInt(JSON.stringify(Math.random() * 35));
    }
  }
  return temperatureArray;
}

let getPositionXY = (i: any, j: any) => {
  let positionX = [5, 20, 35, 50, 65, 80, 95, 80, 90];
  let positionY = [5, 20, 35, 50, 65, 80, 95, 80, 90];
  return {
    x: positionX[i],
    y: positionY[j]
  }
}

let drawCircular = (context: any, opts: any) => {
  let { x, y, radius, weight } = opts;
  radius = parseInt(JSON.stringify(radius * weight));
  
  let rGradient = context.createRadialGradient(x, y, 0, x, y, radius);
  rGradient.addColorStop(0, "rgba(0, 1, 0, 1)");
  rGradient.addColorStop(1, "rgba(1, 0, 0, 0)");
  context.fillStyle = rGradient;
  
  context.globalAlpha = weight;
  context.beginPath();
  context.arc(x, y, radius, 0, 2 * Math.PI);
  context.closePath();
  context.fill();
}
let createPalette = () => {
  
  let colorStops: any = {
    0: "#FDF5E6",
    0.2: "#FFEBCD",
    0.4: "#FFFCF5",
    0.6: "#FFE4B5",
    0.8: "#FFFF00",
    1: "#FF4500"
  };
  
  let width = 256, height = 1;
  
  let paletteCanvas = document.createElement("canvas");
  paletteCanvas.width = width;
  paletteCanvas.height = height;
  let ctx: any = paletteCanvas.getContext("2d");

  
  let linearGradient = ctx.createLinearGradient(0, 0, width, 0);
  for (const key in colorStops) {
    linearGradient.addColorStop(key, colorStops[key]);
  }

  
  ctx.fillStyle = linearGradient;
  ctx.fillRect(0, 0, width, height);

  let imageData = ctx.getImageData(0, 0, width, height).data;

  return {
    canvas: paletteCanvas,
    pickColor: function (position: any) {
      return imageData.slice(position * 4, position * 4 + 3)
    }
  }
}

let heatMap = (width: any, height: any) => {
  let canvas = document.createElement("canvas");
  canvas.width = width;
  canvas.height = height;
  let context: any = canvas.getContext("2d");
  let tenperature = getTemperature();

  for (let i = 0; i  9; i++) {
    for (let j = 0; j  9; j++) {
      let weight = tenperature[i][j] / 30;  
      drawCircular(context, {
        x: getPositionXY(i, j).x,
        y: getPositionXY(i, j).y,
        radius: 20.,
        weight: weight
      })
    }
  }
  let palette = createPalette();
  document.body.appendChild(palette.canvas);
  let imageData = context.getImageData(0, 0, width, height);
  let data = imageData.data;

  for (let i = 3; i  data.length; i += 4) {
    let alpha = data[i];
    let color = palette.pickColor(alpha);
    data[i - 3] = color[0];
    data[i - 2] = color[1];
    data[i - 1] = color[2];
  }

  for (let i = 0; i  imageData.data.length; i += 4) {
    if (imageData.data[i + 3] == 0) {
      imageData.data[i] = 0;
      imageData.data[i + 1] = 255;
      imageData.data[i + 2] = 255;
      imageData.data[i + 3] = 255;
    }
  }
  context.putImageData(imageData, 0, 0);
  return canvas;
}
export default heatMap;```



可视化展示

模拟地球结构

预览地址

原文地址: 基于 vue3.x 与 three.js 模拟地球内部结构

    正文完
     0
    Yojack
    版权声明:本篇文章由 Yojack 于2024-10-14发表,共计2422字。
    转载说明:
    1 本网站名称:优杰开发笔记
    2 本站永久网址:https://yojack.cn
    3 本网站的文章部分内容可能来源于网络,仅供大家学习与参考,如有侵权,请联系站长进行删除处理。
    4 本站一切资源不代表本站立场,并不代表本站赞同其观点和对其真实性负责。
    5 本站所有内容均可转载及分享, 但请注明出处
    6 我们始终尊重原创作者的版权,所有文章在发布时,均尽可能注明出处与作者。
    7 站长邮箱:laylwenl@gmail.com
    评论(没有评论)