three实现三维立体音频沙盘可视化效果代码

代码语言:html

所属分类:三维

代码描述:three实现三维立体音频沙盘可视化效果代码,点击播放按钮,听一听音乐,感受一下立体彩色网格沙盘跟随音乐上下波动的音频可视化效果。

代码标签: three 三维 立体 音频 沙盘 可视化

下面为部分代码预览,完整代码请点击下载或在bfwstudio webide中打开


<!DOCTYPE html>
<html lang="en" >

<head>

  <meta charset="UTF-8">

  
  
  
<style>
* {
    margin: 0;
    padding: 0;
  }
  
  html {
    height: 100%;
    background-color: #FFF; 
  }
  
  body {  
    margin: 0;
    height: 100%;
    font-family: 'Noto Sans JP', sans-serif;
    height: 10000px;
  }

  #myCanvas {
    overflow: hidden;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;  
  }
  #myCanvas canvas {  
    display: block;
    width: 100%;
    height: 100%;
  }

  #overlay {
    position: absolute;
    z-index: 1;
    top: 0;
    left: 0;
    width: 100%;
    height:100%;
    display: none;
    align-items: center;
    justify-content: center;
    opacity: 1;
    background-color: #000000;
    color: #ffffff;
  }

  p{
    color: #ffffff;
  }

  .link{
    color: #000;
    background: #FFF;
    mix-blend-mode: exclusion;
  font-size: 12px;
  position: absolute;
  left: 10px;
  bottom: 10px;
  }

  .link a{
    text-decoration: underline;
    font-size: 10px;
      color: #000;

  }
</style>

 


</head>

<body>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/three.92.js"></script>
<script type="text/javascript" src="//repo.bfw.wiki/bfwrepo/js/OrbitControls.min.js"></script>

  <div id="overlay">
    <p>Play</p>
  </div>


  
  <div id="myCanvas"></div>

  
      <script  >
class Audio {
  constructor(soundUrl) {

    this.listener = new THREE.AudioListener();
    const audio = new THREE.Audio(this.listener);
    this.xhr = 0;

    this.audioLoader = new THREE.AudioLoader();
    this.audioLoader.load(soundUrl,
    function (buffer) {
      audio.setBuffer(buffer);
      audio.setLoop(true);
      audio.setVolume(1.0);
    },
    // onProgress callback
    function (xhr) {
      if (xhr.loaded / xhr.total * 100 === 100) {
        setTimeout(() => {
          document.getElementById('overlay').style.display = "block";
        }, 1000);
      } else {}
    },
    // onError callback
    function (err) {
      console.log('An error happened');
    });

    this.soundPlay = () => {
      audio.play();
    };
    this.analyser = [];
    this.analyser = new THREE.AudioAnalyser(audio, 512);

    this.dataArray = new Uint8Array();
  }

  update() {
    this.FrequencyData = this.analyser.getFrequencyData(this.dataArray);

    this.AverageFrequency = this.analyser.getAverageFrequency();
  }}




class Canvas {
  constructor() {
    /************************/
    /*インタラクション用*/
    /************************/

    //スクロール量
    this.scrollY = 0;
    //マウス座標
    this.mouse = new THREE.Vector2(0.5, 0.5);
    this.targetRadius = 0.05; // 半径の目標値
    //ウィンドウサイズ
    this.w = window.innerWidth;
    this.h = window.innerHeight;


    /************************/
    /*シーン設定*/
    /************************/

    // レンダラー
    this.renderer = new THREE.WebGLRenderer({
      antialias: true });

    this.renderer.setSize(this.w, this.h); // 描画サイズ
    this.renderer.setPixelRatio(window.devicePixelRatio); // ピクセル比
    this.renderer.setClearColor(0xFFFFFF);

    //#myCanvasにレンダラーのcanvasを追加
    const container = document.getElementById("myCanvas");
    container.appendChild(this.renderer.domElement);

    // カメラ
    /*js上の数値をpixelに変換する処理*/
    const fov = 60;
    const fovRad = fov / 2 * (Math.PI / 180);
    const dist = this.h / 2 / Math.tan(fovRad);
    /* */
    this.camera = new THREE.PerspectiveCamera(fov, this.w / this.h, 1, dist * 10);
    this.camera.position.set(dist, dist * 1.5, dist);

    // シーン
    this.scene = new THREE.Scene();
    this.scene.fog = new THREE.Fog(0xFFFFFF, 500, 2000);

    // ライト
    this.pointLight = new THREE.DirectionalLight(0xFFFFFF, 0.9);
    this.scene.add(this.pointLight);

    this.pointLight2 = new THREE.DirectionalLight(0xFFFFFF, 1);
    this.poi.........完整代码请登录后点击上方下载按钮下载查看

网友评论0