นักเล่นอินเทอร์เน็ตมืออาชีพ • ผู้ที่ชื่นชอบเกม • ผู้สร้างเทคโนโลยี
นักเล่นอินเทอร์เน็ตมืออาชีพ • ผู้ที่ชื่นชอบเกม • ผู้สร้างเทคโนโลยี

สร้างเครื่องมือสร้างภาพเสียงโดยใช้ HTML พื้นฐาน CSS และ JavaScript

วิธีการสร้างเครื่องมือสร้างเสียงแนวนอนแบบทีละขั้นตอนตั้งแต่เริ่มต้นโดยใช้ HTML, CSS และ JavaScript ขั้นพื้นฐาน!
หน้านี้ได้รับการแปลจากภาษาอังกฤษโดยนักศึกษาฝึกงานด้าน AI ที่มีแรงบันดาลใจสูงของฉัน เพื่อความสะดวกของคุณ พวกเขายังคงเรียนรู้อยู่ ดังนั้นอาจมีข้อผิดพลาดหลงเหลืออยู่บ้าง สำหรับข้อมูลที่ถูกต้องที่สุด โปรดดูเวอร์ชันภาษาอังกฤษ
บ้าน บล็อก สร้างเครื่องมือสร้างภาพเสียงโดยใช้ HTML พื้นฐาน CSS และ JavaScript

โปรดทราบว่าโพสต์บล็อกนี้เผยแพร่เมื่อเดือนตุลาคม 2020 ดังนั้น เนื้อหาบางส่วนอาจล้าสมัย ขึ้นอยู่กับว่าคุณอ่านเมื่อใด ขออภัยที่ฉันไม่สามารถอัปเดตโพสต์เหล่านี้ให้ทันสมัยอยู่เสมอเพื่อให้แน่ใจว่าข้อมูลยังคงถูกต้อง

นี่คือคู่มือทีละขั้นตอนเกี่ยวกับวิธีการสร้างเครื่องมือสร้างเสียงแนวนอนตั้งแต่เริ่มต้นโดยใช้ HTML, CSS และ JavaScript พื้นฐาน

ชมวิดีโอ

โปรดดูวิดีโอที่ฉันอธิบายรายละเอียดเพิ่มเติม

ซอร์สโค้ดแบบเต็ม

ด้านล่างนี้เป็นโค้ดต้นฉบับแบบเต็มสำหรับเครื่องมือสร้างภาพเสียงแนวนอน

<!-- License MIT, Author Special Agent Squeaky (specialagentsqueaky.com) --> <!doctype html> <html lang="en"> <head> <title>My Eq</title> <style> body { overflow: hidden; } .background { position: absolute; top: -50px; right: -50px; bottom: -50px; left: -50px; background-image: url("background.jpg"); background-position: center center; background-size: cover; filter: blur(15px); } .track-title { position: absolute; top: 200px; right: 0; left: 0; color: white; font-family: Calibri; font-size: 100px; text-align: center; } .visualizer-container { position: absolute; bottom: 450px; right: 0; left: 0; text-align: center; } .visualizer-container__bar { display: inline-block; background: white; margin: 0 2px; width: 25px; } </style> </head> <body> <audio src="track.mp3" controls></audio> <div class="background"></div> <div class="track-title">Awesome song</div> <div class="visualizer-container"></div> <script> (function () { // The number of bars that should be displayed const NBR_OF_BARS = 50; // Get the audio element tag const audio = document.querySelector("audio"); // Create an audio context const ctx = new AudioContext(); // Create an audio source const audioSource = ctx.createMediaElementSource(audio); // Create an audio analyzer const analayzer = ctx.createAnalyser(); // Connect the source, to the analyzer, and then back the the context's destination audioSource.connect(analayzer); audioSource.connect(ctx.destination); // Print the analyze frequencies const frequencyData = new Uint8Array(analayzer.frequencyBinCount); analayzer.getByteFrequencyData(frequencyData); console.log("frequencyData", frequencyData); // Get the visualizer container const visualizerContainer = document.querySelector(".visualizer-container"); // Create a set of pre-defined bars for( let i = 0; i < NBR_OF_BARS; i++ ) { const bar = document.createElement("DIV"); bar.setAttribute("id", "bar" + i); bar.setAttribute("class", "visualizer-container__bar"); visualizerContainer.appendChild(bar); } // This function has the task to adjust the bar heights according to the frequency data function renderFrame() { // Update our frequency data array with the latest frequency data analayzer.getByteFrequencyData(frequencyData); for( let i = 0; i < NBR_OF_BARS; i++ ) { // Since the frequency data array is 1024 in length, we don't want to fetch // the first NBR_OF_BARS of values, but try and grab frequencies over the whole spectrum const index = (i + 10) * 2; // fd is a frequency value between 0 and 255 const fd = frequencyData[index]; // Fetch the bar DIV element const bar = document.querySelector("#bar" + i); if( !bar ) { continue; } // If fd is undefined, default to 0, then make sure fd is at least 4 // This will make make a quiet frequency at least 4px high for visual effects const barHeight = Math.max(4, fd || 0); bar.style.height = barHeight + "px"; } // At the next animation frame, call ourselves window.requestAnimationFrame(renderFrame); } renderFrame(); audio.volume = 0.10; audio.play(); })(); </script> </body> </html>

เขียนโดย Special Agent Squeaky เผยแพร่ครั้งแรก 2020-10-20 อัปเดตล่าสุด 2020-10-20

📺 ดูวิดีโอล่าสุดของ Squeaky!

วิธีเพิ่มคำบรรยายเรียลไทม์แบบง่ายๆ สำหรับสตรีมสดของคุณ