자바스크립트 캔버스를 이용하여 사인파 그리기
웹소스 코드
<canvas id="myCanvas" width="360" height="400" style="border:1px solid #d3d3d3;">
Your browser does not support the canvas element.
</canvas>
<script>
var c = document.getElementById("myCanvas");
var ctx = c.getContext("2d");
// X축 그리기
for(x=0; x<360; x += 20){
ctx.moveTo(x+5,180);
ctx.lineTo(x,180);
}
// 청색 사인파 그리기
ctx.moveTo(0,180);
ctx.strokeStyle="blue";
for(x=0; x<=360; x+=1){
y = 180.0 - Math.sin(x*Math.PI/180)*120;
ctx.lineTo(x,y);
}
ctx.stroke();
// 적색 사인파 그리기
ctx.beginPath();
ctx.moveTo(0,180);
ctx.strokeStyle="red";
for(x=0; x<=360; x+=1){
y = 180.0 - Math.sin(2*x*Math.PI/180)*120;
ctx.lineTo(x,y);
}
ctx.stroke();
</script>
<실행결과>
참고: https://stackoverflow.com/questions/29917446/drawing-sine-wave-in-canvas
'웹' 카테고리의 다른 글
자바스크립트 캔버스 에니메이션 시소 (0) | 2017.09.05 |
---|---|
자바스크립트 캔버스 (에니메이션) 펜둘럼(Pendulumn) (0) | 2017.09.04 |
자바스크립트 instanceof 연산자 (0) | 2017.08.23 |
자바스크립트 캔버스 원 그리기 arc() (0) | 2017.08.18 |
자바스크립트 팩맨 Pacman 소스코드 알고리즘 분석 (0) | 2017.08.18 |