0

我正在创建一个频谱图,单击按钮会显示但一直给我这个错误,我不明白问题出在哪里,如果有人能提供帮助,我将非常感激!

// used for color distribution
    var myColor = new chroma.ColorScale({
        colors:['#000000', '#ff0000', '#ffff00', '#ffffff'],
        positions:[0, .25, .75, 1],
        mode:'rgb',
        limits:[0, 300]
    });

这是定义的变量^^^

function Spectrogram() {
        // Start visualizer.
  requestAnimFrame(drawSpectrogram)
}

这是on按钮激活^^^^

//draw the spectrogram
    function drawSpectrogramVisualisation() {
    var canvas = document.getElementById("canvas2");
    var context = canvas.getContext("2d");

    //store a temp copy of the canvas
    //create a temp canvas we use for copying
    var tempCanvas = document.createElement("canvas");
    tempCanvas.width = spectrogramCanvasWIDTH;
    tempCanvas.height = spectrogramCanvasHEIGHT
    var tempCtx = tempCanvas.getContext("2d");

    tempCtx.drawImage(canvas,0,0, spectrogramCanvasWIDTH, spectrogramCanvasHEIGHT);

    var freqDomain = new Uint8Array(analyser.frequencyBinCount);

    analyser.getByteFrequencyData(freqDomain);

    if(spectrogramLeftPos == spectrogramCanvasWIDTH){
        for (var i = 0; i< analyser.frequencyBinCount; i++) { 

        var value = freqDomain[i]
        tempCtx.fillStyle = myColor.getColor(value).hex();
        tempCtx.fillRect(spectrogramCanvasWIDTH - 1, (spectrogramCanvasHEIGHT - i), 1, 1);
        }
        context.translate(-1, 0);
        context.drawImage(tempCanvas,0,0, spectrogramCanvasWIDTH, spectrogramCanvasHEIGHT);
        context.setTransform(1,0,0,1,0,0);
    } else {

            for (var i = 0; i < analyser.frequencyBinCount; i++) {
                var value = freqDomain [i];
                tempCtx.fillStyle = myColor.getColor(value).hex();
                tempCtx.fillRect(spectrogramLeftPos, (spectrogramCanvasHEIGHT - i), 1, 1);
            }

            context.drawImage(tempCanvas,0,0, spectrogramCanvasWIDTH, spectrogramCanvasHeight);
            spectrogramLeftPos++;
    }
}

这是谱图函数^^^^

<script type="text/javascript" src="chroma.js"></script>

这是在 Head 标记内的 html 部分中。^^

4

0 回答 0