Skip to content
Advertisement

drawStar() with mouse inside canvas mouse up mouse down

What am I missing? The drawCircle and DrawPolygon (it is located in codepen https://codepen.io/mancod/pen/oNYdrVL work fine. I am still very new to all this still, and beat myself up as nothing in life should be this messy. A star is a circle as is a polygon. I get that the star has an inner and outer radius, but I cannot get this star. Thank you in advance for eyes that can fill in the part I am missing or have in the wrong order for function drawStar(). I have commented out the drawline and drawcircle. If you want to know it that even work you can view it on https://jsfiddle.net/mancod/mhbrqxk8/45/ where I have commented out the drawStar.

JavaScript

Advertisement

Answer

Let’s have a look at the parameter definition for the drawStar() function:

drawStar (position, points, outerRadius, innnerRadius)

and remind ourselves what a typical stylized star looks like

a star

Alright so far. There are two places the drawStar function gets called: inside draw and dragStop. In both cases you’re calling it like

JavaScript

This means we pass 6 as the number of points for the star shape – if we look above we can see that star is made up of 10 points. The second mistake here is the hardcoded values 2 and 15 for the radius of the star. I think you want to dynamically size it according to the movement of the mouse, so we need to recalculate the radii on mouse move. Well as we don’t have a use for the two parameters we can get rid of it altogether and just call it like:

JavaScript

Inside the drawStar function we need to calculate the points for the star shape like:

JavaScript

As you can see where dynamically calculating the radius for the inner and outer points, pushing the points into the coordinates array and ultimately adding 36° to the angle variable (360°/10 points=36°)

Finally let’s iterate over the coordinates array and draw the lines to screen:

JavaScript

Here’s a working example based on your fiddle:

JavaScript
JavaScript
JavaScript
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement