我正在使用网络音频API创建音频可视化,我遇到了一个问题,我无法设置线性渐变来填充我的可视化中的矩形。
以下是相关代码:
HTML
<div style="background-color: white; width:100%; height: 256px; position: relative; top: 50%; transform: translateY(-50%);">
<canvas id="viz" style="background-color: white; height: 256px;"></canvas>
</div>
的JavaScript
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
context = new AudioContext();
//set context for viz
var ctx = document.getElementById('viz').getContext('2d');
//create rainbow gradient and set fill style
var gradient = ctx.createLinearGradient(0,0,0,256);
gradient.addColorStop(0, '#FF0000');
gradient.addColorStop(0.2, '#FF7F00');
gradient.addColorStop(0.4, '#FFFF00');
gradient.addColorStop(0.55, '#00FF00');
gradient.addColorStop(0.7, '#0000FF');
gradient.addColorStop(0.85, '#4B0082');
gradient.addColorStop(1, '#9400D3');
ctx.fillStyle = gradient;
var vizAnalyser = context.createAnalyser();
vizAnalyser.fftsize=2048;
vizAnalyser.smoothingTimeConstant=.95;
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
navigator.getMedia({
audio: true,
video: false
}, function(localMediaStream) {
//connect audio nodes
source = context.createMediaStreamSource(localMediaStream);
source.connect(vizAnalyser)
draw();
}, function(err) {
console.log(err);
});
function draw() {
//get frequency data from analyser node
var bufferLength = vizAnalyser.frequencyBinCount;
var hueData = new Uint8Array(bufferLength);
vizAnalyser.getByteFrequencyData(hueData);
var viz = document.getElementById("viz");
ctx.clearRect(0, 0, viz.width, 256);
for (var i = 0; i < bufferLength; i++)
{
ctx.fillRect(i * 3, 256-hueData[i], 2, 256);
}
requestAnimationFrame(draw);
}
可视化通过适当的大小在适当的位置绘制矩形,但它们是黑色而不是渐变。
我环顾四周,但我找不到我做错了什么。 任何见解将不胜感激,谢谢!
答案 0 :(得分:0)
您的代码似乎没有任何问题。
window.requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame;
context = new AudioContext();
//set context for viz
var ctx = document.getElementById('viz').getContext('2d');
//create rainbow gradient and set fill style
var gradient = ctx.createLinearGradient(0,0,0,256);
gradient.addColorStop(0, '#FF0000');
gradient.addColorStop(0.2, '#FF7F00');
gradient.addColorStop(0.4, '#FFFF00');
gradient.addColorStop(0.55, '#00FF00');
gradient.addColorStop(0.7, '#0000FF');
gradient.addColorStop(0.85, '#4B0082');
gradient.addColorStop(1, '#9400D3');
ctx.fillStyle = gradient;
var vizAnalyser = context.createAnalyser();
vizAnalyser.fftsize=2048;
vizAnalyser.smoothingTimeConstant=.95;
navigator.getMedia = (navigator.getUserMedia || navigator.webkitGetUserMedia || navigator.mozGetUserMedia || navigator.msGetUserMedia);
navigator.getMedia({
audio: true,
video: false
}, function(localMediaStream) {
//connect audio nodes
source = context.createMediaStreamSource(localMediaStream);
source.connect(vizAnalyser)
draw();
}, function(err) {
console.log(err);
});
function draw() {
//get frequency data from analyser node
var bufferLength = vizAnalyser.frequencyBinCount;
var hueData = new Uint8Array(bufferLength);
vizAnalyser.getByteFrequencyData(hueData);
var viz = document.getElementById("viz");
ctx.clearRect(0, 0, viz.width, 256);
for (var i = 0; i < bufferLength; i++)
{
ctx.fillRect(i * 3, 256-hueData[i], 2, 256);
}
requestAnimationFrame(draw);
}
也许这是一个浏览器问题。
它在MacBook上的Chrome中完美运行:https://codepen.io/anon/pen/dNPvyJ?editors=1111