Last active
April 4, 2016 12:55
-
-
Save zaynyatyi/f6e3bd08b94e36dbee2fd20e8e82f3ad to your computer and use it in GitHub Desktop.
Pie progress mask
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); | |
| } |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
based on http://flassari.is/2009/11/pie-mask-in-as3/