At first, thanks for all the useful replies! I woke up this morning and was thinking that the circle wasn’t drawn very smooth because I didn’t draw a line to all of the points, because the duration was too short. Just after that thought @itzikiap replied and was implying the same thing, haha.
I meant that the circle didn’t show all of the provided points, so the circle wasn’t ‘smooth’. It was if a circle was drawn with like 8 corners (instead of 360 degrees).
Your right about this part, initially I did it without clearing and if not drawn too fast, the circle would show up just fine. Actually I wanted my animation to have a variable time and not have a minimum of two seconds.
After all, this is the easiest way, I think. But the part about the complex math is still true .
I’ve tried switching to Linear.easeNone
before, but it wasn’t working much better.
So after a lot of thought I decided to draw a wedge and cut off the middle part, so it would become an arc (or curved line). The steps are now angle based and get tweened properly. I’ve tried the same thing with just lines (and no beginFill
), but I couldn’t get it to just create an arc, it would always be a wedge (with lines to the mid-point of the arc). Eventually, this is what works and looks pretty darn smooth (and has a nice animation easing):
private var currentAngle:Float = 0;
Actuate.tween(this, duration, { currentAngle: 360 }).ease(Quint.easeOut).onUpdate(render).onComplete(onAnimationComplete);
private function render()
{
graphics.clear();
graphics.beginFill(color, 1);
drawWedge(this, 0, 0, radius, currentAngle, -90);
// Subtract out the middle by drawing a new wedge over the top of the previous one.
drawWedge(this, 0, 0, radius - thickness, currentAngle, -90);
graphics.endFill();
}
private function drawWedge(sprite:Sprite, startX:Int, startY:Int, radius:Float, arc:Float, startAngle:Int = 0)
{
var segAngle:Float;
var angle:Float;
var angleMid:Float;
var numOfSegs:Int;
var ax:Float;
var ay:Float;
var bx:Float;
var by:Float;
var cx:Float;
var cy:Float;
// Move the pen.
sprite.graphics.moveTo(startX, startY);
// No need to draw more than 360.
if (Math.abs(arc) > 360)
{
arc = 360;
}
numOfSegs = Math.ceil(Math.abs(arc) / 45);
segAngle = arc / numOfSegs;
segAngle = (segAngle / 180) * Math.PI;
angle = (startAngle / 180) * Math.PI;
// Calculate the start point.
ax = startX + Math.cos(angle) * radius;
ay = startY + Math.sin(angle) * radius;
// Draw the first line.
sprite.graphics.lineTo(ax, ay);
// Draw the wedge.
for (i in 0...numOfSegs)
{
angle += segAngle;
angleMid = angle - (segAngle * .5);
bx = startX + Math.cos(angle) * radius;
by = startY + Math.sin(angle) * radius;
cx = startX + Math.cos(angleMid) * (radius / Math.cos(segAngle * .5));
cy = startY + Math.sin(angleMid) * (radius / Math.cos(segAngle * .5));
sprite.graphics.curveTo(cx, cy, bx, by);
}
// Close the wedge.
sprite.graphics.lineTo(startX, startY);
}
Thanks again for the help! I hope this will help anyone in the future .
Btw reading the topic title, kinda ironic that I actually ended up creating pie-slices.
Showcase (I just noticed that the circle is moving a little, does anyone know what may be causing that?):