JavaScript 绘制 Sin : :
<! Doctype html> <html> <head> <style type = "text/css"> #mycanvas {background-color: mainflowerblue; } </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); para (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: mainflowerblue; } </style> <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>