JavaScript 绘制 sin 曲线代码如下 :
<! DocType html> <html> <kopf> <style type = "text/css"> #mycanvas {Hintergrund-Color: CornflowerBlue; } </style> <script type = "text/javaScript"> Funktion Draw () {var my_canvas = document.getElementById ("mycanvas"); var content = my_canvas.getContext ("2d"); Content.BeginPath (); content.moveto (10, 100); für (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> <kopf> <style type = "text/css"> #mycanvas {Hintergrund-Color: CornflowerBlue; } </style> <script type = "text/javaScript"> var i = 1; var x = 1; var y = 100; Funktion 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>