JavaScript 绘制 sin 曲线代码如下 :
<! Doctype html> <html> <head> <style type = "text/css"> #mycanvas {background-color: cornflowerBlue; } </style> <script type = "text/javascript"> function draw () {var my_canvas = document.geteLementById ("myCanvas"); var content = my_canvas.getContext ("2d"); content.beginpath (); content.moveto (10, 100); untuk (var i = 1; i <200; i += 0.1) {var x = i * 10; var y = math.sin (i) * 10 + 100; content.lineto (x, y); } content.stroke (); content.closepath (); } </script> </head> <body onload = "draw ()"> <canvas id = "mycanvas"> </ Canvas> </body> </html>动态效果 :
<! Doctype html> <html> <head> <style type = "text/css"> #mycanvas {background-color: cornflowerBlue; } </tyle> <script type = "text/javascript"> var i = 1; var x = 1; var y = 100; function movesin () {var my_canvas = document.geteLementById ("mycanvas"); var content = my_canvas.getContext ("2d"); content.beginpath (); content.moveto (x, y); i += 0,1; x = i * 10; y = Math.sin (i) * 10 + 100; content.lineto (x, y); content.stroke (); content.closepath (); } setInterval (Movesin, 10); </script> </head> <body onload = "movesin ()"> <canvas id = "mycanvas"> </ Canvas> </body> </html>