Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Here is the code>>>>
XML
<!doctype html>
<html>
<head>
<link rel="stylesheet" type="text/css" media="all" href="css/reset.css" /> <!-- reset css -->
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<style>
    body{ background-color: ivory; }
    canvas{border:1px solid red;}
</style>
<script>

        var canvas = document.getElementById("canvas");
        var ctx = canvas.getContext("2d");

        function star(ctx, x, y, r, p, m)
        {
            ctx.save();
            ctx.beginPath();
            ctx.translate(x, y);
            ctx.moveTo(0, 0 - r);
            for (var i = 0; i < p; i++)
            {
                ctx.rotate(Math.PI / p);
                ctx.lineTo(0, 0 - (r * m));
                ctx.rotate(Math.PI / p);
                ctx.lineTo(0, 0 - r);
            }
            ctx.fill();
            ctx.restore();
        }
        star(ctx, 100, 100, 90, 5, 0.5);

</script>
</head>
<body>
    <canvas id="canvas" width=300 height=300></canvas>
</body>
</html>


In output i didn't get the star...
Posted

That is because star function is not invoked. You should run that in document ready event.

Demo - [Demo] Display Star on Canvas[^]
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 7-Mar-15 11:51am    
This is even simpler than that. Please see Solution 2.
—SA
Haha. Yes I know that technique as well. As OP has referenced jQuery, so I thought to tell him about the document ready event. ;) :P
Sergey Alexandrovich Kryukov 7-Mar-15 13:08pm    
Right. 5ed, by the way.
—SA
Thanks man. :)
Move your script element to body, make it a child of the body element. You will observe the star. The problem is solved.

Now read about using script element and think about it.

—SA
 
Share this answer
 
Comments
+5 bro. :)
Sergey Alexandrovich Kryukov 7-Mar-15 13:09pm    
Thank you, Tadit.
—SA

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900