Animating canvas to look like tv noise

前端 未结 7 2126
野趣味
野趣味 2020-12-22 17:22

I have a function named generateNoise() which creates a canvas element and paints random RGBA values to it; which, gives the appearance of noise.


<
7条回答
  •  伪装坚强ぢ
    2020-12-22 17:52

    I happen to have just written a script that does just this, by getting the pixels from a black canvas and just altering random alpha values and using putImageData

    Result can be found at http://mouseroot.github.io/Video/index.html

    var currentAnimationFunction = staticScreen

    var screenObject = document.getElementById("screen").getContext("2d");

    var pixels = screenObject.getImageData(0,0,500,500);

    function staticScreen()
            {
                requestAnimationFrame(currentAnimationFunction);
                //Generate static
                for(var i=0;i < pixels.data.length;i+=4)
                {
                    pixels.data[i] = 255;
                    pixels.data[i + 1] = 255;
                    pixels.data[i + 2] = 255;
                    pixels.data[i + 3] = Math.floor((254-155)*Math.random()) + 156;
                }
                screenObject.putImageData(pixels,0,0,0,0,500,500);
                //Draw 'No video input'
                screenObject.fillStyle = "black";
                screenObject.font = "30pt consolas";
                screenObject.fillText("No video input",100,250,500);
            }
    

提交回复
热议问题