Programmatically use RGBa values in fillStyle in ?

前端 未结 5 535
栀梦
栀梦 2021-01-03 17:13

Using , I want to set the RGBa value of the rectangle using a variable.

for example:

ctx.fillStyle = \"rgba(32, 45, 21, 0         


        
5条回答
  •  走了就别回头了
    2021-01-03 17:57

    I was looking for something similar and ran into your post, after reading it, I came up with a solution for myself, I'm working with the audio API and wanted a dynamic color based on a variable coming from the frequency in the sound file. Here's what I came up with and it woks for now, thought I'd post it in case it helps since I got the inspiration from your question:

    function frameLooper(){ 
        window.requestAnimationFrame(frameLooper); 
        fbc_array = new Uint8Array(analyser.frequencyBinCount); 
        analyser.getByteFrequencyData(fbc_array); 
        ctx.clearRect(0, 0, canvas.width, canvas.height); 
    // Clear the canvas 
    
        bars = 100; 
            for (var i = 0; i < bars; i++) { 
                bar_x = i * 3; 
                bar_width = 2; 
                bar_height = -(fbc_array[i] / 2); 
                bar_color = i * 3;
    
        //fillRect( x, y, width, height ) 
        // Explanation of the parameters below 
            ctx.fillRect(bar_x, canvas.height, bar_width, bar_height); 
                ctx.fillStyle = "rgb(100," + bar_color + "," + bar_color + ")" ; 
    // Color of the bars
    

提交回复
热议问题