three实现可配置参数的声音驱动水波纹鼠标交互动画效果代码

代码语言:html

所属分类:动画

代码描述:three实现可配置参数的声音驱动水波纹鼠标交互动画效果代码,按h键修改参数,鼠标移动有涟漪动画交互效果。

代码标签: three 配置 参数 声音 驱动 水波纹 鼠标 交互

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

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

<head>
  <meta charset="UTF-8">

  
<style>
@font-face {
  font-family: "GT Standard";
  src: url("https://assets.codepen.io/7558/GT-Standard-VF-Trial.woff2")
    format("woff2-variations");
  font-weight: 300 900;
  font-stretch: 0% 150%;
  font-style: normal;
  font-display: swap;
}

@font-face {
  font-family: "PPSupplyMono";
  src: url("https://assets.codepen.io/7558/PPSupplyMono-Regular.ttf")
    format("truetype");
  font-weight: normal;
  font-style: normal;
  font-display: swap;
}

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  overflow: hidden;
  background: #000;
  font-family: "PPSupplyMono", monospace;
  -webkit-user-select: none;
     -moz-user-select: none;
      -ms-user-select: none;
          user-select: none;
}

#container {
  position: relative;
  width: 100vw;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

#controls {
  position: absolute;
  top: 20px;
  left: 20px;
  z-index: 100;
  opacity: 0.3;
  transition: opacity 0.3s ease;
}

#controls:hover {
  opacity: 1;
}

button {
  background: none;
  color: white;
  border: none;
  cursor: pointer;
  font-family: inherit;
  font-size: 12px;
  text-transform: uppercase;
  transition: opacity 0.2s ease;
}

button:hover {
  opacity: 0.7;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.5;
}

canvas {
  display: block;
}

.tp-dfwv {
  position: fixed !important;
  top: 20px !important;
  right: 20px !important;
  z-index: 1000 !important;
  display: none !important;
  transition: opacity 0.3s ease !important;
}

.tp-dfwv.visible {
  display: block !important;
}

.info {
  position: absolute;
  bottom: 20px;
  left: 50%;
  transform: translateX(-50%);
  color: white;
  font-size: 12px;
  text-transform: uppercase;
  text-align: center;
  z-index: 1000;
  line-height: 1.4;
  opacity: 0.8;
}

.info a {
  color: white;
  text-decoration: none;
  border-bottom: 1px solid rgba(255, 255, 255, 0.3);
  transition: border-bottom-color 0.2s ease;
}

.info a:hover {
  border-bottom-color: white;
}

.credit {
  margin-top: 4px;
  opacity: 0.6;
  font-size: 11px;
}

.help-hint {
  position: absolute;
  top: 20px;
  right: 20px;
  color: white;
  font-size: 11px;
  text-transform: uppercase;
  opacity: 0.5;
  z-index: 1000;
  transition: opacity 0.3s ease;
}

.help-hint.hidden {
  opacity: 0;
}

@media (max-width: 768px) {
  .info {
    font-size: 10px;
    bottom: 10px;
  }

  #controls {
    top: 10px;
    left: 10px;
  }

  .help-hint {
    top: 10px;
    right: 10px;
    font-size: 10px;
  }
}
</style>


  
  
</head>

<body translate="no">
  <div id="container">
  <div id="controls">
    <button id="audioBtn">[ play ]</button>
  </div>
  <div class="help-hint" id="helpHint">Press 'H' for controls</div>
  <div class="info">
    <div>Move mouse to create audio-reactive fluid ripples</div>
    <div class="credit">Music by me • Shader inspiration by <a href="" target="_blank">XOR</a></div>
  </div>
</div>
  
      <script  type="module">
import * as THREE from "//repo.bfw.wiki/bfwrepo/js/module/three/build/177/three.module.js";
import { Pane } from "//repo.bfw.wiki/bfwrepo/js/tweakpane.4.0.3.js";
const scene = new THREE.Scene();
const camera = new THREE.OrthographicCamera(-1, 1, 1, -1, 0.1, 10);
const renderer = new THREE.WebGLRenderer({
  antialias: true });

renderer.setSize(window.innerWidth, window.innerHeight);
renderer.setPixelRatio(Math.min(window.devicePixelRatio, 2));
document.getElementById("container").appendChild(renderer.domElement);
let isPlaying = false;
let audioContext = null;
let analyser = null;
let dataArray = null;
let source = null;
let panelVisible = false;
let audioLevels = {
  bassLevel: 0,
  midLevel: 0,
  trebleLevel: 0,
  overallLevel: 0 };

let bassMonitor, midMonitor, trebleMonitor, overallMonitor;
const audio = new Audio();
audio.src = "//repo.bfw.wiki/bfwrepo/sound/5e148aa3821f2.mp3";
audio.preload = "auto";
audio.volume = 1.0;
audio.crossOrigin = "anonymous";
audio.addEventListener("ended", () => {
  if (isPlaying) {
    audio.currentTime = 0;
    audio.play();
  }
});
audio.load();

function initAudioAnalysis() {
  try {
    if (!audioContext) {
      audioContext = new (window.AudioContext || window.webkitAudioContext)();
      analyser = audioContext.createAnalyser();
      analyser.fftSize = 256;
      analyser.smoothingTimeConstant = 0.8;
      const bufferLength = analyser.frequencyBinCount;
      dataArray = new Uint8Array(bufferLength);
      source = audioContext.createMediaElementSource(audio);
      source.connect(analyser);
      analyser.connect(audioContext.destination);
      console.log("Web Audio API initialized successfully");
    }
  } catch (e) {
    console.warn("Web Audio API failed to initialize:", e);
    analyser = null;
    dataArray = null;
  }
}
const waterSettings = {
  resolution: 256,
  damping: 0.913,
  tension: 0.02,
  rippleStrength: 0.2,
  mouseIntensity: 1.2,
  clickIntensity: 3.0,
  rippleRadius: 8,
  splatForce: 50000,
  splatThickness: 0.1,
  vorticityInfluence: 0.2,
  swirlIntensity: 0.2,
  pressure: 0.3,
  velocityDissipation: 0.08,
  densityDissipation: 1.0,
  displacementScale: 0.01 };

const resolution = waterSettings.resolution;
let waterBuffers = {
  current: new Float32Array(re.........完整代码请登录后点击上方下载按钮下载查看

网友评论0