Update script.js
Browse files
script.js
CHANGED
@@ -1,59 +1,37 @@
|
|
1 |
-
|
2 |
-
const canvas = document.
|
3 |
const ctx = canvas.getContext('2d');
|
4 |
|
5 |
-
|
6 |
-
canvas.width = 400;
|
7 |
canvas.height = 100;
|
|
|
8 |
|
9 |
-
// Define the ECG data
|
10 |
const ecgData = [
|
11 |
-
|
12 |
-
|
13 |
-
|
14 |
-
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
|
19 |
-
|
20 |
-
|
21 |
-
|
22 |
];
|
23 |
|
24 |
-
|
25 |
-
|
26 |
-
|
27 |
-
|
28 |
-
|
29 |
-
|
30 |
-
|
31 |
-
|
32 |
-
|
33 |
-
|
34 |
-
|
35 |
-
ctx.beginPath();
|
36 |
-
ctx.moveTo(0, canvas.height / 2);
|
37 |
-
for (let i = 0; i < ecgData.length; i++) {
|
38 |
-
const point = ecgData[i];
|
39 |
-
ctx.lineTo(point.time, point.value);
|
40 |
-
}
|
41 |
-
ctx.lineTo(canvas.width, canvas.height / 2);
|
42 |
-
ctx.stroke();
|
43 |
-
}
|
44 |
-
|
45 |
-
// Draw the beat markers
|
46 |
-
function drawBeats() {
|
47 |
-
ctx.beginPath();
|
48 |
-
for (let i = 0; i < beatData.length; i++) {
|
49 |
-
const beat = beatData[i];
|
50 |
-
ctx.fillStyle = 'red';
|
51 |
-
ctx.fillRect(beat.time, canvas.height / 2, 5, 5);
|
52 |
-
}
|
53 |
-
}
|
54 |
|
55 |
-
|
56 |
-
|
57 |
-
drawECG();
|
58 |
-
drawBeats();
|
59 |
});
|
|
|
1 |
+
const canvasContainer = document.getElementById('canvas-container');
|
2 |
+
const canvas = document.createElement('canvas');
|
3 |
const ctx = canvas.getContext('2d');
|
4 |
|
5 |
+
canvas.width = canvasContainer.offsetWidth;
|
|
|
6 |
canvas.height = 100;
|
7 |
+
canvasContainer.appendChild(canvas);
|
8 |
|
|
|
9 |
const ecgData = [
|
10 |
+
{ time: 0, value: 0 },
|
11 |
+
{ time: 10, value: 1 },
|
12 |
+
{ time: 20, value: 0.5 },
|
13 |
+
{ time: 30, value: -1 },
|
14 |
+
{ time: 40, value: -0.5 },
|
15 |
+
{ time: 50, value: 0 },
|
16 |
+
{ time: 60, value: 0.5 },
|
17 |
+
{ time: 70, value: 1 },
|
18 |
+
{ time: 80, value: 0.5 },
|
19 |
+
{ time: 90, value: -0.5 },
|
20 |
+
{ time: 100, value: 0 }
|
21 |
];
|
22 |
|
23 |
+
const drawEcg = () => {
|
24 |
+
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
25 |
+
ctx.lineWidth = 2;
|
26 |
+
ctx.strokeStyle = 'rgba(0, 0, 255, 1)';
|
27 |
+
ctx.beginPath();
|
28 |
+
ctx.moveTo(0, canvas.height / 2);
|
29 |
+
ecgData.forEach((data, i) => {
|
30 |
+
ctx.lineTo(i, (data.value + 1) * canvas.height / 4);
|
31 |
+
});
|
32 |
+
ctx.stroke();
|
33 |
+
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
34 |
|
35 |
+
document.getElementById('show').addEventListener('click', () => {
|
36 |
+
drawEcg();
|
|
|
|
|
37 |
});
|