I have a basic output of a circle, of which I want to optimize by only drawing angle ranges that are visible. For me this should hopefully improve performance, as I currently am getting serious performance loss drawing very large circles and need to reduce the amount of output to keep it efficient.
My current draw code:
function draw(){
ctx.clearRect(0,0,canvas.width,canvas.height);
ctx.save();
ctx.translate(camera.x,camera.y);
ctx.beginPath();
ctx.arc(pos.x,pos.y,radius,0,2*Math.PI);
ctx.stroke();
ctx.restore();
requestAnimationFrame(draw);
}
draw();
I only need to calculate the perimeter of the circle not the inner body of it, so if the edge of the circle is off screen it would not draw a circle.
This is a visual example of what I refer to:
There are certain situations that have to be taking into account for calculating the angles. The size of the radius, the origin position of the circle and position of the camera. I initially tried comparing radius size versus (canvas width/2) but soon realized that was incorrect math logic. And so, i have no idea how i would approach the issue.
What is the best way to achieve this in JavaScript?
EDIT: added basic javascript code example: https://jsfiddle.net/44tawd81/ you can click and drag the circle.