我尝试使用canvas html5绘制ecg系统。
几乎我接近完成我的波动正在移动,但不是连续不断重复,但我想画波是从左到右连续移动?
以下链接就是示例。
例:https://www.youtube.com/watch?v=wuwBfSpVEgw
我每1秒收到一次数据。
Data Ex:
var ydata = [
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
148,149,149,150,150,150,143,82,82,82,82,82,82,82,
];
这是我的代码:
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<canvas id="canvas" width="160" height="160" style="background-color: black;"></canvas>
<script type='text/javascript'>
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
ctx.fillStyle = "#dbbd7a";
ctx.fill();
var fps = 60;
var n = 1;
var data = [
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82,
148, 149, 149, 150, 150, 150, 143, 82, 82, 82, 82, 82, 82, 82, ];
drawWave();
function drawWave() {
setTimeout(function() {
requestAnimationFrame(drawWave);
ctx.lineWidth = "2";
ctx.strokeStyle = 'green';
// Drawing code goes here
n += 1;
if (n >= data.length) {
ctx.clearRect(0, 0, canvas.width, canvas.height);
n = 1;
}
ctx.beginPath();
ctx.moveTo(n - 1, data[n - 1]);
ctx.lineTo(n, data[n]);
ctx.stroke();
}, 1000 / fps);
}
</script>
</body>
</html>
答案 0 :(得分:4)
如果您想让它看起来像视频,请不要在每次通过结束时清除帧,只需将其清除到路径末尾的右侧。
// Drawing code goes here
n += 1;
if (n >= data.length) {
n = 1;
}
ctx.beginPath();
ctx.moveTo(n - 1, data[n - 1]);
ctx.lineTo(n, data[n]);
ctx.stroke();
ctx.clearRect(n+1, 0, 10, canvas.height);
这就是你想要的吗?
答案 1 :(得分:0)
setTimeout()
只运行一次回调
你从未告诉过你的代码不止一次运行。
每次浏览器渲染帧时,都应调用requestAnimationFrame()
来运行动画
然后,您需要检查自上一帧以来经过的时间,并以适当的距离更新动画。
答案 2 :(得分:0)
var EcgData=[];
var canvas = document.getElementById("canvas");
var ctx = canvas.getContext("2d");
var w = canvas.width,
h = canvas.height,
speed = 1,
scanBarWidth = 20,
i=0,
data = EcgData[0].split(','),
color='#00ff00';
var px = 0;
var opx = 0;
var py = h/2;
var opy = py;
ctx.strokeStyle = color;
ctx.lineWidth = 3;
ctx.setTransform(1,0,0,-1,0,h);
drawWave();
function drawWave() {
px += speed;
ctx.clearRect( px,0, scanBarWidth, h);
ctx.beginPath();
ctx.moveTo( opx, opy);
ctx.lineJoin= 'round';
py=(data[++i>=data.length? i=0 : i++]/450+10);
ctx.lineTo(px, py);
ctx.stroke();
opx = px;
opy = py;
if (opx > w) {px = opx = -speed;}
requestAnimationFrame(drawWave);
}
答案 3 :(得分:0)
我创建了最易于使用的Web组件来显示来自蓝牙心率监视器或外部api的数据。您要做的就是在每个出现的节拍上拨打bang()
。
document.body.innerHTML += '<ecg-line></ecg-line>';
ecgLine((bang) => setInterval(() => bang(), 1000));
源代码已发布here。我认为它看起来比提出的替代方案好很多