Javascript 繪制 sin 曲線過程附圖
更新時間:2014年08月21日 09:05:48 投稿:whsnow
這篇文章主要介紹了Javascript 繪制 sin 曲線過程,需要的朋友可以參考下
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>
動態(tài)效果:
<!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>
相關文章
javascript獲取所有同類checkbox選項(實例代碼)
javascript獲取所有同類checkbox選項的簡單實例。需要的朋友可以過來參考下,希望對大家有所幫助2013-11-11