canvas繪製sin正弦曲線

螞蟻小編發表於2018-07-02
本章節分享一段程式碼例項,利用canvas繪製正弦曲線圖效果。

並且具有動態效果,程式碼例項如下:

[HTML] 純文字檢視 複製程式碼執行程式碼
<!DOCTYPE html> 
<html> 
<head> 
<style type="text/css">
#theCanvas{ 
  background-color:#63F; 
} 
</style> 
<script type="text/javascript"> 
var i = 1; 
var x = 1; 
var y = 100; 
function moveSin(){ 
  var oCanvas=document.getElementById("theCanvas"); 
  var content=oCanvas.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);
window.onload=function(){
  moveSin();
} 
</script> 
</head> 
<body> 
<canvas id="theCanvas" width="400" height="400"></canvas> 
</body> 
</html>


相關文章