HTML Canvas - Dotted stroke around circle

Chris picture Chris · Jun 9, 2011 · Viewed 11.1k times · Source

I do know there is no native support for doing dotted stroke lines rendered on a canvas, but I have seen the clever ways people have been able to generate support for this.

What I am wondering is if there is any way to translate this to allow for rendering dotted strokes around shapes (specifically circles)?

Answer

user8003769 picture user8003769 · Oct 22, 2017

the simplest way using context.setLineDash()

ctx.beginPath();
ctx.setLineDash([5, 5]);
ctx.beginPath();
ctx.arc(100, 60, 50, 0, Math.PI * 2);
ctx.closePath();
ctx.stroke();