-
Notifications
You must be signed in to change notification settings - Fork 7
/
seismographutil.ts
214 lines (195 loc) · 7.07 KB
/
seismographutil.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
import { LuxonTimeScale } from "./axisutil";
import { SeismogramDisplayData } from "./seismogram";
import { isDef } from "./util";
import type { ScaleLinear } from "d3-scale";
export const DEFAULT_MAX_SAMPLE_PER_PIXEL = 3;
export function clearCanvas(canvas: HTMLCanvasElement) {
// clear the canvas from previous drawing
canvas.getContext("2d")?.clearRect(0, 0, canvas.width, canvas.height);
}
export function drawAllOnCanvas(
canvas: HTMLCanvasElement,
sddList: Array<SeismogramDisplayData>,
xScaleList: Array<LuxonTimeScale>,
yScaleList: Array<ScaleLinear<number, number, never>>,
colornameList: Array<string>,
lineWidth = 1,
connectSegments = false,
maxSamplePerPixelForLineDraw = DEFAULT_MAX_SAMPLE_PER_PIXEL,
): void {
if (canvas.height === 0) { return;}
const context = canvas.getContext("2d") as CanvasRenderingContext2D;
if (!isDef(context)) {// for typescript
throw new Error("canvas 2d context is null, should not happen...");
}
sddList.forEach((sdd, i) => {
const xScale = xScaleList[i];
const yScale = yScaleList[i];
const s = xScale.domain().start?.valueOf();
const e = xScale.domain().end?.valueOf();
if (s == null || e == null || s === e) {
return;
} else {
const seismogram = sdd.seismogram;
if (!seismogram) { return; }
context.save();
drawSeismogramAsLine(sdd, context, canvas.width, xScale, yScale, colornameList[i],
lineWidth, connectSegments, maxSamplePerPixelForLineDraw);
context.restore();
}
});
}
export function drawSeismogramAsLine(
sdd: SeismogramDisplayData,
context: CanvasRenderingContext2D,
width: number,
xScale: LuxonTimeScale,
yScale: ScaleLinear<number, number, never>,
color: string,
lineWidth: number = 1,
connectSegments = false,
maxSamplePerPixelForLineDraw = 20
): void {
const seismogram = sdd.seismogram;
if (!seismogram) { return; }
let firstTime = true;
const s = xScale.domain().start?.valueOf();
const e = xScale.domain().end?.valueOf();
if (s == null || e == null) {
throw new Error(`Bad xscale domain: ${String(xScale.domain())}`);
}
let horizontalPixel: null | number = null;
seismogram.segments.forEach((segment) => {
if (
xScale.for(segment.startTime) > xScale.range[1] ||
xScale.for(segment.endTime) < xScale.range[0]
) {
// segment either totally off to left or right of visible
return;
}
const secondsPerPixel =
(e - s) /
1000 /
(xScale.range[1] - xScale.range[0]);
const samplesPerPixel = 1.0 * segment.sampleRate * secondsPerPixel;
const pixelsPerSample = 1.0 / samplesPerPixel;
const startPixel = xScale.for(segment.startTime);
const endPixel = xScale.for(segment.endTime);
let leftVisibleSample = 0;
let rightVisibleSample = segment.y.length;
let leftVisiblePixel = startPixel;
if (startPixel < 0) {
leftVisibleSample =
Math.floor(-1 * startPixel * samplesPerPixel) - 1;
leftVisiblePixel = 0;
}
if (endPixel > xScale.range[1] + 1) {
rightVisibleSample =
leftVisibleSample +
Math.ceil((width + 1) * samplesPerPixel) +
1;
}
if (firstTime || !connectSegments) {
context.beginPath();
context.strokeStyle = color;
context.lineWidth = lineWidth;
horizontalPixel = yScale(segment.y[leftVisibleSample]);
context.moveTo(
leftVisiblePixel,
horizontalPixel,
);
firstTime = false;
}
if ( samplesPerPixel <= maxSamplePerPixelForLineDraw) {
// draw all samples
for (
let i = leftVisibleSample;
i < rightVisibleSample + 2 && i < segment.y.length;
i++
) {
context.lineTo(startPixel + i * pixelsPerSample ,
Math.round(yScale(segment.y[i])));
}
} else {
// draw min-max
// this tends to be better even with as few as 3 point per pixel,
// especially in near horizontal line
let i = leftVisibleSample;
while (i < rightVisibleSample + 2 && i < segment.y.length) {
const curPixel = Math.floor(startPixel + i * pixelsPerSample);
let min = segment.y[i];
let max = segment.y[i];
let minIdx = i;
let maxIdx = i;
while (curPixel === Math.floor(startPixel + i * pixelsPerSample)) {
if (min > segment.y[i]) { min = segment.y[i]; minIdx = i; }
if (max < segment.y[i]) { max = segment.y[i]; maxIdx = i; }
i++;
}
const topPixelFlt = yScale(max); // note pixel coord flipped
const botPixelFlt = yScale(min); // so botPixelFlt>topPixelFlt
const botPixel = Math.round(botPixelFlt);
const topPixel = Math.round(topPixelFlt);
if (topPixel === botPixel) {
// horizontal
if (horizontalPixel === topPixel) {
// and previous was also same horizontal, keep going to draw one
// longer horizontal line
continue;
} else {
if (horizontalPixel !== null) {
const y = horizontalPixel as number; // typescript
context.lineTo(curPixel, y);
}
horizontalPixel = topPixel;
}
} else if (botPixelFlt-topPixelFlt < 1.25) {
// very horizontal, just draw single
context.lineTo(curPixel, topPixel);
horizontalPixel = topPixel;
} else {
// drawing min to max vs max to min depending on order
// and offseting by half a pixel
// helps a lot with avoiding fuzziness due to antialiasing
if (horizontalPixel !== null) {
const y = horizontalPixel as number; // typescript
context.lineTo(curPixel-0.5, y);
horizontalPixel = null;
}
if (minIdx < maxIdx) {
// min/bot occurs before max/top
context.lineTo(curPixel-0.5, botPixel-0.5);
context.lineTo(curPixel+0.5, topPixel+0.5);
} else {
// max/top occurs before min/bot
context.lineTo(curPixel-0.5, topPixel+0.5);
context.lineTo(curPixel+0.5, botPixel-0.5);
}
}
}
if (horizontalPixel !== null) {
// in case end of segment is horizontal line we did not finish drawing
const curPixel = Math.floor(startPixel + (rightVisibleSample+1) * pixelsPerSample);
const y = horizontalPixel as number; // typescript
context.lineTo(curPixel-0.5, y);
horizontalPixel = null;
}
}
if (!connectSegments) {
context.stroke();
}
});
context.stroke();
}
export function rgbaForColorName(name: string): Uint8ClampedArray {
const out = new Uint8ClampedArray(4);
const ctx = document.createElement('canvas').getContext('2d') as CanvasRenderingContext2D;
if (!ctx) { return out; }
ctx.fillStyle = name;
const color_rgb = ctx.fillStyle.substring(1); // form of #87ceeb, chop the #
out[0] = Number.parseInt(`0x${color_rgb.substring(0, 2)}`, 16);
out[1] = Number.parseInt(`0x${color_rgb.substring(2, 4)}`, 16);
out[2] = Number.parseInt(`0x${color_rgb.substring(4, 6)}`, 16);
out[3] = 255;
return out;
}