//绘制小方块
function videobg(x,y,canvasGradient){
var c1 = document.getElementById("canvas");
//var r=112+(Math.random()*223);
if(c1.getContext('2d')){
var ctx = c1.getContext("2d");
for(var i=0;i<Math.random()*149+1;i++){
ctx.beginPath();
ctx.fillRect(x+5,y-i*1.5,16,4);
canvasGradient=ctx.createLinearGradient(x+5,y-400,x+5,343);
canvasGradient.addColorStop(1,'rgb(42, 231, 0)');//绿
canvasGradient.addColorStop(0.4,'rgb(216, 9, 0)');//红
canvasGradient.addColorStop(0,'rgb(228, 219, 0)');//黄
ctx.fillStyle = canvasGradient;
ctx.fillRect(x+5,y-i*1.5,16,4);
ctx.stroke();
ctx.closePath();
i+=5;
}
}
}
window.onload = function () {
var heightR = 0;
var c1 = document.getElementById('canvas');
var ctx = c1.getContext('2d');
function clear(){
ctx.clearRect(20,120,500,300);
}
<pre name="code" class="html">function clear(){
ctx.clearRect(20,120,500,300);
}
function play(){
clear();
videobg(37,340,'');
videobg(58,340,'');
videobg(95,340,'');
videobg(116,340,'');
videobg(153,340,'');
videobg(174,340,'');
videobg(211,340,'');
videobg(232,340,'');
videobg(269,340,'');
videobg(290,340,'');
videobg(327,340,'');
videobg(348,340,'');
text('', 58, 360, '#ffffff');
}
var time = setInterval(play,200);
}