I want to draw one line then wait for some milliseconds then draw next line again wait for next line and so on, so that its visualize that how line by line it will be drawn(like ECG waveform).
How i can do that in this code?
This is my code:
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
var ms = 0;
var y=5;
var x=5;
var copyendx=0;
var copyendy=0;
var context;
var temp,total=0;
//var data=new Array(-1,3,-3,-7,5,1,-1,-3,2,5,7,7,7,7,1,3,3,4-,8,5,6,6,7,7);
var data=new Array(-1,3,-3,-7,5,1,-1,-3,2,5,7,7,7,7,4,7,9,2,2,8);
//alert(data);
function init() {
var graphCanvas = document.getElementById('graphSpace');
context =graphCanvas.getContext('2d');
drawgraph();
}
function drawgraph() {
drawLine(context,5,50,5,250);
drawLine(context,5,150,7,150);
// setTimeout(drawgraph(),600);
for (var i=0;i<data.length;i++) {
var startx=5+x;
var starty=150-(data[i]*y);
var endx=(5+x)+1;
var endy=150-(data[i]*y);
if (i==0) {
copyendx=endx;
copyendy=endy;
startx=5+x;
starty=150;
}
//draw line and wait for some milliseconds
setInterval(function () {
drawLine(context,startx,starty,copyendx,copyendy); }, 100);
drawLine(context,startx,starty,copyendx,copyendy);
x=x+5;
// pausecomp(100);
copyendx=endx;
copyendy=endy;
}
}
}
//Draw line function
function drawLine(contextO, startx, starty, endx, endy) {
contextO.beginPath();
contextO.moveTo(startx, starty);
contextO.lineTo(endx, endy);
contextO.closePath();
contextO.stroke();
}
</script>
</head>
<body onload="init()">
<canvas id="graphSpace" width="800" height="400" style="background-color: #ffff00;"></canvas>
</body>
</html>
I have tried with setInterval
setInterval(function () {
drawLine(context,startx,starty,copyendx,copyendy);
}, 100);
but i didn’t get desired output.
I want wait for some milliseconds before calling method drawLine(contextO, startx, starty, endx, endy) for each line drawing
I got solution for above problem. i have following problem
If i want to draw more points on canvas if that points not fit on my canvas width i am redrawing the canvas but here is problem that my graph is not looking steady (as like ECG wave form applicaion) during first redraw it looks slower, during second redraw it looking faster than first redraw ,during third redraw it looks faster than second redraw and so on.
How to overcome that? I want steady flow till i draw my last graph point.
The concept of a loop with sleep(100) is something that is not designed to be done in javascript.
get rid of the loop, do something like this: