Created
March 28, 2016 09:18
-
-
Save Paratron/8aa1042a7bc24cb8f0c8 to your computer and use it in GitHub Desktop.
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
/** | |
* Draws a poligon - can draw anything from a triangle to a star. | |
* Source and demo: http://jsfiddle.net/tohan/8vwjn4cx/ | |
*/ | |
function drawShape(ctx, x, y, points, radius1, radius2, alpha0) { | |
//points: number of points (or number of sides for polygons) | |
//radius1: "outer" radius of the star | |
//radius2: "inner" radius of the star (if equal to radius1, a polygon is drawn) | |
//angle0: initial angle (clockwise), by default, stars and polygons are 'pointing' up | |
var i, angle, radius; | |
if (radius2 !== radius1) { | |
points = 2 * points; | |
} | |
for (i = 0; i <= points; i++) { | |
angle = i * 2 * Math.PI / points - Math.PI / 2 + alpha0; | |
radius = i % 2 === 0 ? radius1 : radius2; | |
ctx.lineTo(x + radius * Math.cos(angle), y + radius * Math.sin(angle)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment