0

私はWebオーディオAPIを使用してオーディオビジュアライゼーションを作成しています。視覚化の四角形を埋める線形グラデーションを設定できないという問題が発生しました。LinearGradient fillStyleが機能しません

ここに関連するコードです:

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); 
} 

が可視化が適切なサイズと適切な場所に長方形を描画しますが、彼らはではなく黒です勾配。

Vizualization

私は周りを見回していると私は私が間違ってやっているものを見つけることができませんでした。 洞察を深めていただければ幸いです。

答えて

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

+0

ありがとう!それはブラウザの問題でなければなりませんが、ブラウザで動作させるための解決策を見つけました。他の誰かがこの問題を抱えている場合は、「ctx.fillStyle = gradient;」を移動して解決しました。線を引く機能の中に入れる。 – mestaman

関連する問題