一応999角形まで表示できますが、これくらいの大きさだと50角形を超える辺りで全部円にしか見えなくなりますね。
三角形
六角形
十二角形
五十角形
<!DOCTYPE html> <html> <head> <script language="javascript"> var radius = 150; var center_x = 200; var center_y = 150; function draw_polygon(){ var vertex = document.forms["vertex"]["quantity"].value; var a = 2 * Math.PI / vertex; var arr_x = []; var arr_y = []; for(var i= 0; i < vertex; i++){ var rad =(i + 1) * 2 * Math.PI / vertex; arr_x[i] = center_x + radius * Math.cos(rad); arr_y[i] = center_y + radius * Math.sin(rad); } var canvas = document.getElementById("myCanvas"); var ctx = canvas.getContext("2d"); ctx.clearRect(0,0,canvas.width,canvas.height); ctx.beginPath(); ctx.moveTo(arr_x[0],arr_y[0]); for(var i= 1; i < vertex; i++){ ctx.lineTo(arr_x[i],arr_y[i]); } ctx.lineTo(arr_x[0],arr_y[0]); ctx.stroke(); } </script> </head> <body> <form id = "vertex" onsubmit="draw_polygon();return false;"> Specify the Quantity (between 1 and 999): <input type="number" name="quantity" min="1" max="999" placeholder="4"> <input type="submit"> </form> <canvas id="myCanvas" width="600" height="300" style="border:1px solid #c3c3c3;"> Your browser does not support the HTML5 canvas tag. </canvas> </body> </html>
0 件のコメント:
コメントを投稿