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开发者_如何学Go 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:
var x,y,i=0;
setTimeout(function doDraw() {
var startx=5+x;
var starty=150-(data[i]*y);
var endx=(5+x)+1;
var endy=150-(data[i]*y);
drawline(yourargs);
i++;
if (i < data.length) {
setTimeout(doDraw, 100);
}
}, 100);
Basically you need to go through your loop and set up all your draw lines. Each should be N milliseconds later then the previous one, hence (i*100).
So after the loop finished one line will be drawn, the next one will be drawn in 1*100ms next one in 2*100ms and so on...
here is your code with few modifications:
<!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,0);
drawLine(context,5,150,7,150,0);
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
drawLine(context,startx,starty,copyendx,copyendy,i*100);
drawLine(context,startx,starty,copyendx,copyendy,i*100);
x=x+5;
copyendx=endx;
copyendy=endy;
}
}
//Draw line function
function drawLine(contextO, startx, starty, endx, endy,delay) {
setTimeout(function(){
contextO.beginPath();
contextO.moveTo(startx, starty);
contextO.lineTo(endx, endy);
contextO.closePath();
contextO.stroke();
},delay);
}
</script>
</head>
<body onload="init()">
<canvas id="graphSpace" width="800" height="400" style="background-color: #ffffff;"></canvas>
</body>
</html>
setInterval is bad. If takes long than the interval to run, you end up with a backup of calls to run. Stick to setTimeout and call it again at the end of the block you're running.
try setTimeout("drawLine(context,startx,starty,copyendx,copyendy)",100);. This will delay it by 100 milliseconds.
This won't do. you need to pass reference to setInterval function.
function drawLine(context,startx,starty,copyendx,copyendy){}
var func = partial(drawline, context,startx,starty,copyendx,copyendy);
setInterval(func, 100);
精彩评论