Skip to content

Instantly share code, notes, and snippets.

@Paratron
Created March 28, 2016 09:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Paratron/8aa1042a7bc24cb8f0c8 to your computer and use it in GitHub Desktop.
Save Paratron/8aa1042a7bc24cb8f0c8 to your computer and use it in GitHub Desktop.
/**
* 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