-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathecgviewer.ts
268 lines (214 loc) · 7.72 KB
/
ecgviewer.ts
1
2
3
4
5
6
7
8
9
10
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
import { Point2D } from "./graphics.js"
import { EventBase } from './events.js'
import { Distance, IMeasurement, MeasurementUnit, MeasurementType } from "./ecgmeasurements.js";
enum MouseButtonPressedState {
NONE_PRESSED,
LEFT_PRESSED,
RIGHT_PRESSED,
MIDDLE_PRESSED
};
export enum ViewerAction {
NONE,
CALIB_X_REF,
CALIB_X_MEASURE,
MEASURE_DIST_1,
MEASURE_DIST_2,
SELECT
};
export enum PaperSpeed {
MM50,
MM25
}
export class CalibrationResult {
public pixelsPerMillisecond: number
public pixelsPerMillivolts: number
constructor(pixelsPerMS: number, pixelsPerMV: number) {
this.pixelsPerMillisecond = pixelsPerMS
this.pixelsPerMillivolts = pixelsPerMV
}
}
const SMOOTHING = 1;
export class ECGViewer extends EventBase {
canvas: HTMLCanvasElement;
img: HTMLImageElement;
zoomFactor: number = 1;
mousePos: Point2D = new Point2D(0, 0);
anchorPos: Point2D = new Point2D(0, 0);
scrollPos: Point2D = new Point2D(0, 0);
bgDragStartPos: Point2D = new Point2D(0, 0);
mouseDownPos: Point2D = new Point2D(0, 0);
mouseButton: MouseButtonPressedState;
currentViewerAction: ViewerAction = ViewerAction.NONE;
paperSpeed = PaperSpeed.MM50
kbModifiers = {
ALT: false,
SHIFT: false,
CTRL: false
}
pixelsPerMillisecond = 0
pixelsPerMillivolts = 0
millimetersPerSecond = 50
currentMeasurementData: IMeasurement
measurements: IMeasurement[] = []
constructor(id: string, imgPath: string) {
super()
this.canvas = window.document.getElementById(id) as HTMLCanvasElement;
this.mouseButton = MouseButtonPressedState.NONE_PRESSED;
window.document.addEventListener('keydown', (ev: KeyboardEvent) => {
this.kbModifiers = {
ALT: ev.altKey,
SHIFT: ev.shiftKey,
CTRL: ev.ctrlKey
}
})
window.document.addEventListener('keyup', (ev: KeyboardEvent) => {
this.kbModifiers = {
ALT: ev.altKey,
SHIFT: ev.shiftKey,
CTRL: ev.ctrlKey
}
})
this.canvas.addEventListener('mousedown', (ev: MouseEvent) => {
this.mouseDownPos.x = ev.clientX - this.canvas.getBoundingClientRect().left
this.mouseDownPos.y = ev.clientY - this.canvas.getBoundingClientRect().top
this.bgDragStartPos.x = this.scrollPos.x;
this.bgDragStartPos.y = this.scrollPos.y;
this.mouseButton = MouseButtonPressedState.LEFT_PRESSED;
switch(this.currentViewerAction) {
case ViewerAction.CALIB_X_REF: {
this.anchorPos = new Point2D(
ev.clientX - this.canvas.getBoundingClientRect().left,
ev.clientY - this.canvas.getBoundingClientRect().top
)
this.setViewerAction(ViewerAction.CALIB_X_MEASURE)
break
}
case ViewerAction.CALIB_X_MEASURE: {
let factor = (this.paperSpeed == PaperSpeed.MM25 ? 200 : 100)
console.log(Math.abs(this.anchorPos.x - this.mouseDownPos.x))
this.pixelsPerMillisecond = factor / Math.abs(this.anchorPos.x - this.mouseDownPos.x)
this.pixelsPerMillivolts = Math.abs(this.anchorPos.y - this.mouseDownPos.y) / 2
this.setViewerAction(ViewerAction.NONE)
this.emit('calibrationFinished', new CalibrationResult(this.pixelsPerMillisecond, this.pixelsPerMillivolts))
break
}
case ViewerAction.MEASURE_DIST_1: {
this.anchorPos = new Point2D(
ev.clientX - this.canvas.getBoundingClientRect().left,
ev.clientY - this.canvas.getBoundingClientRect().top
)
this.setViewerAction(ViewerAction.MEASURE_DIST_2)
break
}
case ViewerAction.MEASURE_DIST_2: {
/*let d_px = Point2D.distance(
this.currentMeasurementData.start,
this.currentMeasurementData.end,
)
let newMeasurement = new Distance(this.currentMeasurementData.start, this.currentMeasurementData.end, MeasurementUnit.MILLISECONDS)
*/
this.measurements.push(this.currentMeasurementData)
this.emit('measurementsUpdated', this.currentMeasurementData)
this.setViewerAction(ViewerAction.NONE)
break
}
}
});
this.canvas.addEventListener('mouseup', (ev: MouseEvent) => {
this.mouseButton = MouseButtonPressedState.NONE_PRESSED;
})
this.canvas.addEventListener('mousemove', (ev: MouseEvent) => {
this.mousePos = new Point2D(
ev.clientX - this.canvas.getBoundingClientRect().left,
ev.clientY - this.canvas.getBoundingClientRect().top
)
console.log(this.mousePos)
if (this.mouseButton == MouseButtonPressedState.LEFT_PRESSED) {
this.scrollPos.x = ev.clientX - this.canvas.getBoundingClientRect().left - this.mouseDownPos.x + this.bgDragStartPos.x
this.scrollPos.y = ev.clientY - this.canvas.getBoundingClientRect().top - this.mouseDownPos.y + this.bgDragStartPos.y
}
this.paint()
})
this.img = new Image()
this.img.src = imgPath
this.img.addEventListener('load', () => {
this.paint();
})
}
setZoomFactor(factor: number) {
this.zoomFactor = factor;
this.paint();
}
setViewerAction(action: ViewerAction) {
this.currentViewerAction = action;
}
getMeasurements(): IMeasurement[] {
return this.measurements
}
getPixelDistanceAsMS(dist: number): number {
return dist * this.pixelsPerMillisecond
}
paint() {
const ctx = this.canvas.getContext('2d')
ctx.clearRect(0, 0, this.canvas.offsetWidth, this.canvas.offsetHeight)
ctx.drawImage(this.img, this.scrollPos.x, this.scrollPos.y, this.img.width * this.zoomFactor,
this.img.height * this.zoomFactor)
for (let m of this.measurements) {
this.paintMeasurement(m)
}
switch (this.currentViewerAction) {
case ViewerAction.CALIB_X_MEASURE: {
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
ctx.beginPath()
ctx.rect(
this.anchorPos.x, this.anchorPos.y,
this.mousePos.x - this.anchorPos.x,
this.mousePos.y - this.anchorPos.y,
)
ctx.stroke()
break
}
case ViewerAction.MEASURE_DIST_2: {
ctx.strokeStyle = 'red';
ctx.lineWidth = 2;
const markerHeight = 50
let start = new Point2D(this.anchorPos.x, this.anchorPos.y)
let end
if (this.kbModifiers.ALT) {
end = new Point2D(this.mousePos.x, start.y)
} else {
end = new Point2D(this.mousePos.x, this.mousePos.y)
}
this.currentMeasurementData = new Distance(start, end, MeasurementUnit.MILLISECONDS)
this.paintMeasurement(this.currentMeasurementData)
break
}
}
}
private paintMeasurement(data: IMeasurement): void {
// TODO: add bg position
const ctx = this.canvas.getContext('2d')
ctx.strokeStyle = 'red'
ctx.lineWidth = 2;
const markerHeight = 50
switch (data.type) {
case MeasurementType.DISTANCE:
let dist = <Distance> data
ctx.beginPath();
// marker start
ctx.moveTo(dist.start.x + this.scrollPos.x, dist.start.y + this.scrollPos.y - markerHeight / 2)
ctx.lineTo(dist.start.x + this.scrollPos.x, dist.start.y + this.scrollPos.y + markerHeight / 2)
ctx.stroke()
// marker end
ctx.moveTo(dist.end.x + this.scrollPos.x, dist.end.y + this.scrollPos.y - markerHeight / 2)
ctx.lineTo(dist.end.x + this.scrollPos.x, dist.end.y + this.scrollPos.y + markerHeight / 2)
ctx.stroke()
// measurement line
ctx.moveTo(dist.start.x + this.scrollPos.x, dist.start.y + this.scrollPos.y)
ctx.lineTo(dist.end.x + this.scrollPos.x, dist.end.y + this.scrollPos.y)
ctx.stroke()
break
}
}
}