JSFiddle - React, Tailwind, and code Playground
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<canvas id="circle" height="450px" width="450px"></canvas>
</body>
</html>
CSS
#circle {
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
JavaScript
const canvas = document.getElementById("circle");
const ctx = canvas.getContext("2d");
ctx.fillStyle = "#000000";
ctx.lineWidth = 7;
const speed = 40;
const drawCircle = (
x,
y,
r,
startPoint = 0,
endPoint = 2 * Math.PI,
fill = true
) => {
ctx.beginPath();
ctx.arc(x, y, r, startPoint, endPoint);
if (fill) ctx.fill();
ctx.stroke();
};
let endPoint = 1.75 * Math.PI;
let startPoint = 1.75 * Math.PI;
function animateCircle() {
if (startPoint >= 0.75 * Math.PI && endPoint <= 2.75 * Math.PI) {
drawCircle(280, 210, 20, startPoint, endPoint);
endPoint += Math.PI / speed;
startPoint -= Math.PI / speed;
requestAnimationFrame(animateCircle);
}
}
requestAnimationFrame(animateCircle)