i have Incorporated gravity mechanics and collision mechanics to my game. the game works fine for some period of time after that i am getting significant amount of frame rate drop.i am doing some heavy computation but i don't see any other approach to it.can anyone suggest improvement for frame rate drop. i have added code and output as snippet
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<style>
canvas {
border:1px solid #d3d3d3;
background-color: #f1f1f1;
width: 100%;
}
body
{
overflow-x: hidden;
overflow-y: hidden;
margin:0px 0px 0px 0px;
}
</style>
</head>
<body onload="startGame()">
</body>
<script>
var blackhole;
var planet=[];
var flag=[];
var canvasWidth=window.innerWidth;
var canvasHieght=window.innerHeight;
var mousex=0;
var mousey=0;
var numofplanets=15;
function startGame() {
myGameArea.start();
blackhole = new BlackHole(canvasWidth/2,canvasHieght/2, 30,"black",70);
for(let i=0;i<numofplanets;i++)
{
var plnt=new Planet(Math.floor((Math.random() *300) + 1),Math.floor((Math.random() * 300) + 1),10,"blue",20);
planet.push(plnt);
}
}
var myGameArea = {
canvas : document.createElement("canvas"),
start : function() {
this.canvas.width = canvasWidth;
this.canvas.height = canvasHieght;
this.context = this.canvas.getContext("2d");
document.body.insertBefore(this.canvas, document.body.childNodes[0]);
this.frameNo = 0;
this.interval = setInterval(updateCanvas,1);
},
clear : function() {
this.context.clearRect(0, 0, this.canvas.width, this.canvas.height);
}
}
function BlackHole(x,y,radius,color,mass) {
this.x = x;
this.y = y;
this.color=color;
this.mass=mass;
this.radius=radius;
this.outerradius=radius*6;
ctx = myGameArea.context;
this.stay=function(){
ctx = myGameArea.context;
ctx.save();
ctx.fillStyle=color;
ctx.beginPath();
ctx.arc(this.x, this.y,this.radius, 0, 2 * Math.PI);
ctx.stroke();
ctx.beginPath();
ctx.arc(this.x,this.y,this.outerradius,0,2*Math.PI);
ctx.stroke();
}
this.attract=function(i){
if(planet[i].r>this.radius)
{
planet[i].acclx=(blackhole.mass*(blackhole.x-planet[i].x))/Math.pow(planet[i].r,3);
planet[i].accly=(blackhole.mass*(blackhole.y-planet[i].y))/Math.pow(planet[i].r,3);
planet[i].speedx+=planet[i].acclx;
planet[i].speedy+=planet[i].accly;
planet[i].x+=planet[i].speedx;
planet[i].y+=planet[i].speedy;
ctx.save();
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(planet[i].x, planet[i].y, planet[i].radius, 0, 2 * Math.PI);
//ctx.stroke();
}
else
{
max=1;
min=-1;
planet[i].x=Math.floor((Math.random() *1000) + 1);
planet[i].y=Math.floor((Math.random() *900) + 1);
planet[i].acclx=0.001;
planet[i].accly=0.0001;
planet[i].dirx=Math.floor(Math.random() * (+max - +min)) + +min;
planet[i].diry=Math.floor(Math.random() * (+max - +min)) + +min;
if(planet[i].dirx==0 && planet[i].diry==0)
{
planet[i].dirx=-1;
planet[i].diry=1;
}
planet[i].speedx=Math.sqrt(Math.sqrt(Math.pow(this.x-planet[i].x,2)+Math.pow(this.y-planet[i].y,2))*planet[i].acclx)*planet[i].dirx;
planet[i].speedy=Math.sqrt(Math.sqrt(Math.pow(this.x-planet[i].x,2)+Math.pow(this.y-planet[i].y,2))*planet[i].accly)*planet[i].diry;
}
}
}
function Planet(x,y,radius,color,mass){
this.x = x;
this.y = y;
this.acclx=0.001;
this.accly=0.0001;
this.mass=mass;
this.r=0.0;
max=1;
min=-1;
this.radius=radius;
this.dirx=Math.floor(Math.random() * (+max - +min)) + +min;
this.diry=Math.floor(Math.random() * (+max - +min)) + +min;
if(this.dirx==0 && this.diry==0)
{
this.dirx=-1;
this.diry=1
}
this.speedx=Math.sqrt(Math.sqrt(Math.pow(blackhole.x-this.x,2)+Math.pow(blackhole.y-this.y,2))*this.acclx)*this.dirx;
this.speedy=Math.sqrt(Math.sqrt(Math.pow(blackhole.x-this.x,2)+Math.pow(blackhole.y-this.y,2))*this.accly)*this.diry;
this.move=function(){
if(this.x>canvasWidth)
this.x=0;
else if(this.x<0)
this.x=canvasWidth;
else if(this.y<0)
this.y=canvasHieght;
else if(this.y>canvasWidth)
this.y=0;
this.x+=this.speedx;
this.y+=this.speedy;
ctx.save();
ctx.fillStyle = color;
ctx.beginPath();
ctx.arc(this.x, this.y, this.radius, 0, 2 * Math.PI);
// ctx.stroke();
}
this.checkcollision=function(i)
{
for(let k=0;k<numofplanets;k++)
{
if(k==i)
continue;
if(this.x+this.radius+planet[k].radius>planet[k].x && this.x<planet[k].x+this.radius+planet[k].radius && this.y+this.radius+planet[k].radius>planet[k].y && this.y<planet[k].y+planet[k].radius+this.radius)
{
/* this.speedx=this.speedx-((2*planet[k].mass/this.mass+planet[k].mass)*(Math.sqrt(Math.pow(this.speedx-planet[k].speedx,2)+Math.pow(this.x-planet[k].x,2))))*(this.x-planet[k].x);
this.speedy=this.speedy-((2*planet[k].mass/this.mass+planet[k].mass)*(Math.sqrt(Math.pow(this.speedy-planet[k].speedy,2)+Math.pow(this.y-planet[k].y,2))))*(this.y-planet[k].y);
planet[k].speedx=planet[k].speedx-((2*this.mass/this.mass+planet[k].mass)*(Math.sqrt(Math.pow(planet[k].speedx-this.speedx,2)+Math.pow(planet[k].x-this.x,2))))*(planet[k].x-this.x);
planet[k].speedy=planet[k].speedy-((2*this.mass/this.mass+planet[k].mass)*(Math.sqrt(Math.pow(planet[k].speedy-this.speedy,2)+Math.pow(planet[k].y-this.y,2))))*(planet[k].y-this.y);*/
var tempx=this.speedx;
var tempy=this.speedy;
this.speedx=planet[k].speedx;
this.speedy=planet[k].speedy;
planet[k].speedx=tempx;
planet[k].speedy=tempy;
}
}
}
}
function getcordinates(event)
{
mousex=event.clientX;
mousey=event.clientY;
}
function updateCanvas()
{
myGameArea.clear();
blackhole.stay();
for(let i=0;i<numofplanets;i++)
{
planet[i].r=Math.sqrt(Math.pow(blackhole.x-planet[i].x,2)+Math.pow(blackhole.y-planet[i].y,2));
planet[i].move();
planet[i].checkcollision(i);
if(planet[i].r<blackhole.outerradius)
blackhole.attract(i);
ctx.stroke();
}
}
</script>