Last active
December 19, 2015 16:18
-
-
Save sbrl/a691edb6c2fca7431333 to your computer and use it in GitHub Desktop.
Draws a n sided shape of radius r to the given context.
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 drawShape(context, pos, sides, radius, rotation) | |
| { | |
| if(typeof radius !== "number") | |
| rotation = 0; | |
| console.log(`Drawing shape at ${pos.toString()}. Sides: ${sides}, Radius: ${radius}`); | |
| context.save(); | |
| context.translate(pos.x, pos.y); | |
| context.beginPath(); | |
| for(var i = 0; i < sides; i++) | |
| { | |
| let nextCoords = new Vector( | |
| radius * Math.cos(Math.PI * 2 * (i / sides + rotation)), | |
| radius * Math.sin(Math.PI * 2 * (i / sides + rotation)) | |
| ); | |
| if(i == 0) | |
| context.moveTo(nextCoords.x, nextCoords.y); | |
| else | |
| context.lineTo(nextCoords.x, nextCoords.y); | |
| } | |
| context.closePath(); | |
| context.stroke(); | |
| context.restore(); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment