I need some help with my code. My goal is to make a 180 degrees progressive animation that goes clockwise for the upper half of a circle. My ultimate goal is to have two animations playing at the same time: upper half goes clockwise, bottom half goes counter-clockwise, both in the very same fashion.
So far I have this:
// Circle functions
function polarToCartesian(centerX, centerY, radius, angleInDegrees) {
const angleInRadians = (angleInDegrees-90) * Math.PI / 180.0;
return {
x: centerX + (radius * Math.cos(angleInRadians)),
y: centerY + (radius * Math.sin(angleInRadians))
};
}
function describeArc(x, y, radius, startAngle, endAngle){
const start = polarToCartesian(x, y, radius, endAngle);
const end = polarToCartesian(x, y, radius, startAngle);
const arcSweep = endAngle - startAngle <= 180 ? "0" : "1";
const d = [
"M", start.x, start.y,
"A", radius, radius, 0, arcSweep, 0, end.x, end.y,
"L", x,y,
"L", start.x, start.y
].join(" ");
return d;
}
.arc {
animation: moveForward 5s linear forwards;
}
@keyframes moveForward {
to {
d: path("M 450 150 A 300 300 0 0 0 450 150 L 150 150 L 450 150");
}
}
<svg viewBox="0 0 300 300" height="250" width="250" style="background-color: black;">
<path fill="white" stroke-width="0" d="M 450 150 A 300 300 0 0 0 -150 149.99999999999997 L 150 150 L 450 150" />
<path class="arc" fill="black" stroke-width="0" d="M 450 150 A 300 300 0 0 0 -145.44232590366238 97.90554669992092 L 150 150 L 450 150" />
</svg>
Don't mind the extrapolated radius, I want to confide the animation within the SVG area for this animation. As you can see, it goes along the path but something is missing because it starts to shrink over the course of the animation. I'm not an expert, but maybe something is missing in the shape interpolation.
I'm using formulas to generate the end points based on this answer from another somewhat related question. It basically translates my arc's angle to cartesian coordinates.
I appreciate any input and thank you so much.