fdf
Classified in Computers
Written at on English with a size of 4.18 KB.
var shapes=[];
var tCount=0;
var mCount=0;
function init()
{
draw();
timer = setInterval(update,20);
}
function update()
{
if(tCount<5 && Math.floor((Math.random()*200)+1) == 1){
var shape = new Shape();
shape.X=400;
shape.XVel=-5;
shape.Y=Math.Floor((Math.Random()*40)+41);
shape.width=Math.Floor((Math.Random()*20)+21);
shape.ChooseColor();
tCount++;
shapes.Push(shape);
}
if(mCount<5 && Math.Floor((Math.Random()*200)+1) == 1){
var shape = new Shape();
shape.X=400;
shape.XVel=-5;
shape.Y=400-Math.Floor((Math.Random()*40)+41);
shape.Width=Math.Floor((Math.Random()*20)+21);
shape.ChooseColor();
mCount++;
shapes.Push(shape);
}
for(i=0;i<shapes.Length;i++){
shapes[i].X=shapes[i].X+shapes[i].XVel;
if(shapes[i].X<=0){
if(shapes[i].Y>200){
tCount--;
}
else{
mCount--;
}
shapes.Splice(i,1);
}
}
draw();
}
function draw(){
var c = document.GetElementById("myCanvas");
var ctx = c.GetContext("2d");
ctx.ClearRect(0,0,c.Width,c.Height);
for(i=0;i<shapes.Length;i++){
ctx.BeginPath();
ctx.MoveTo(shapes[i].X, shapes[i].Y);
if(shapes[i].Y>200){
ctx.LineTo(shapes[i].X+shapes[i].Width/2, 400);
ctx.LineTo(shapes[i].X-shapes[i].Width/2, 400);
}
else{
ctx.LineTo(shapes[i].X+shapes[i].Width/2, 0);
ctx.LineTo(shapes[i].X-shapes[i].Width/2, 0);
}
ctx.FillStyle= shapes[i].Color;
ctx.Fill();
ctx.ClosePath();
}
}
function Shape(){
this.X=0;
this.Y=0;
this.XVel=0;
this.YVel=0;
this.Width=0;
this.Height=0;
this.Color="red";
this.ChooseColor=function(){
var number=Math.Floor(Math.Random()*3);
if(number==0){
this.Color="red";
}
else if(number==1){
this.Color="blue";
}
else{
this.Color="yellow";
}
}
}