<html>
<head></head>
<body>
<canvas id="canvas" style="border: 1px solid" width="900" height="300"></canvas>
<script>
var canvas = document.getElementById('canvas');
var ct = canvas.getContext('2d');
var raf;
var count = 0;
var dps = [];
var width = 900;
var height = 300;
var xInterval = 3;
for (let i = 0; i < 300; i++) {
dps[i] = Math.floor(Math.random() * height);
}
var graph = {
color: 'orange',
width: 1,
draw: function() {
ct.strokeStyle = this.color;
ct.lineWidth = this.width;
for (let i=0; i < 300; i++){
ct.lineTo(i*3, height - dps[i]);
}
ct.stroke();
dps.shift();
dps.push(Math.floor(Math.random() * height));
}
};
function draw() {
ct.clearRect(0, 0, width, height);
ct.beginPath();
ct.moveTo(0,0);
graph.draw();
raf = window.requestAnimationFrame(draw);
}
canvas.addEventListener('mouseover', function(e) {
raf = window.requestAnimationFrame(draw);
});
canvas.addEventListener('mouseout', function(e) {
window.cancelAnimationFrame(raf);
});
graph.draw();
</script>
</body>
</html>