Pezh commited on
Commit
0e97623
·
verified ·
1 Parent(s): 9b9e8e7

Update script.js

Browse files
Files changed (1) hide show
  1. script.js +28 -50
script.js CHANGED
@@ -1,59 +1,37 @@
1
- // Get the canvas element
2
- const canvas = document.getElementById('ecg-canvas');
3
  const ctx = canvas.getContext('2d');
4
 
5
- // Set the canvas dimensions
6
- canvas.width = 400;
7
  canvas.height = 100;
 
8
 
9
- // Define the ECG data
10
  const ecgData = [
11
- { time: 0, value: 0 },
12
- { time: 100, value: 10 },
13
- { time: 200, value: 20 },
14
- { time: 300, value: 30 },
15
- { time: 400, value: 40 },
16
- { time: 500, value: 50 },
17
- { time: 600, value: 60 },
18
- { time: 700, value: 70 },
19
- { time: 800, value: 80 },
20
- { time: 900, value: 90 },
21
- { time: 1000, value: 100 }
22
  ];
23
 
24
- // Define the beat data
25
- const beatData = [
26
- { time: 0, value: 0 },
27
- { time: 333, value: 33 },
28
- { time: 66, value: 66 },
29
- { time: 99, value: 99 }
30
- ];
31
-
32
- // Draw the ECG graph
33
- function drawECG() {
34
- ctx.clearRect(0, 0, canvas.width, canvas.height);
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
- // Show the ECG graph and beat markers
56
- document.getElementById('show-button').addEventListener('click', function() {
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
  });