Javascript 绘制 sin 曲线过程附图

时间:2021-05-28

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 ); for( 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" width="400" height="400"></canvas> </body> </html>

动态效果:

<!DOCTYPE html> <html> <head> <style type="text/css"> #MyCanvas { background-color: cornflowerblue; } </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" width="400" height="400"></canvas> </body> </html>

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章