您好,欢迎访问一九零五行业门户网

canvas绘制多边形

效果图:
代码如下:
<!doctype html> <html> <head> <meta http-equiv="content-type" content="text/html;charset=utf-8" /> <title>canvas 画多边形</title> </head> <body> <canvas id="mycanvas" width="500" height="500"></canvas> </body> <script> var canvas = document.getelementbyid("mycanvas"); var ctx = canvas.getcontext('2d'); function draw(x,y,n,r){ var i,ang; ang= math.pi*2/n; ctx.save(); ctx.fillstyle = 'rgba(255,0,0,.3)'; ctx.strokestyle = 'hsl(120,50%,50%)'; ctx.linewidth = 1; ctx.translate(x,y); ctx.moveto(0,-r); ctx.beginpath(); for(i=0;i<n;i++){ ctx.rotate(ang); ctx.lineto(0,-r); } ctx.closepath(); ctx.fill(); ctx.stroke(); ctx.restore(); } draw(100,100,3,40); draw(200,100,4,40); draw(300,100,5,40); draw(100,200,6,40); draw(200,200,7,40); draw(300,200,8,40); </script> </html>
更多canvas绘制多边形。
其它类似信息

推荐信息