Skip to content

Instantly share code, notes, and snippets.

@zaynyatyi
Last active April 4, 2016 12:55
Show Gist options
  • Select an option

  • Save zaynyatyi/f6e3bd08b94e36dbee2fd20e8e82f3ad to your computer and use it in GitHub Desktop.

Select an option

Save zaynyatyi/f6e3bd08b94e36dbee2fd20e8e82f3ad to your computer and use it in GitHub Desktop.
Pie progress mask
function drawProgressMask(graphics:Graphics, progress:Float, radius:Float = 50, x:Float = 0, y:Float = 0, rotation:Float = 0, sides:Int = 6):Void
{
graphics.clear();
graphics.beginFill(0);
// graphics should have its beginFill function already called by now
graphics.moveTo(x, y);
if (sides < 3) sides = 3; // 3 sides minimum
// Increase the length of the radius to cover the whole target
radius /= Math.cos(1 / sides * Math.PI);
// Shortcut function
var lineToRadians:Float->Void = function(rads:Float):Void
{
graphics.lineTo(Math.cos(rads) * radius + x, Math.sin(rads) * radius + y);
};
// Find how many sides we have to draw
var sidesToDraw:Int = Math.floor(progress * sides) + 1;
for (i in 0...sidesToDraw) {
lineToRadians((i / sides) * (Math.PI * 2) + rotation);
}
// Draw the last fractioned side
if (progress * sides != sidesToDraw) lineToRadians(progress * (Math.PI * 2) + rotation);
graphics.endFill();
}
@zaynyatyi
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment