This repository has been archived by the owner on Jul 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
sc-signal.js
327 lines (269 loc) · 8.81 KB
/
sc-signal.js
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import { html, css, nothing } from 'lit';
import ScElement from './ScElement.js';
import { theme, fontFamily } from './styles.js';
class ScSignal extends ScElement {
static get properties() {
return {
width: {
type: Number,
},
height: {
type: Number,
},
duration: {
type: Number,
},
min: {
type: Number,
},
max: {
type: Number,
reflect: true,
},
colors: {
type: Array,
},
lineWidth: {
type: Number,
reflect: true,
attribute: 'line-width',
},
displayMinMax: {
type: Boolean,
attribute: 'display-min-max',
reflect: true,
},
};
}
static get styles() {
return css`
:host {
vertical-align: top;
display: inline-block;
box-sizing: border-box;
background-color: white;
line-height: 0;
outline: 1px solid ${theme['--color-primary-2']};
position: relative;
}
canvas {
margin: 0;
}
.min, .max {
display: block;
width: 50px;
height: 14px;
line-height: 14px;
font-size: 10px;
font-family: ${fontFamily};
position: absolute;
right: 0px;
text-align: right;
padding-right: 2px;
color: ${theme['--color-primary-0']};;
}
.min {
bottom: 0px;
}
.max {
top: 0px;
}
`;
}
set value(frame) {
frame.data = Array.isArray(frame.data) ? frame.data : [frame.data];
// need to copy values at some point
this._frameStack.push(frame);
if (this.displayMinMax) {
let changed = false;
for (let i = 0; i < frame.data.length; i++) {
if (frame.data[i] > this._maxValue) {
this._maxValue = frame.data[i];
changed = true;
}
if (frame.data[i] < this._minValue) {
this._minValue = frame.data[i];
changed = true;
}
}
if (changed) {
this.requestUpdate();
}
}
}
constructor() {
super();
this.width = 300;
this.height = 150;
this.duration = 1;
this.colors = ['#4682B4', '#ffa500', '#00e600', '#ff0000', '#800080', '#224153'];
this.displayMinMax = false;
this.min = -1;
this.max = 1;
this._frameStack = [];
this._pixelIndex = null;
this._dirty = true;
this._maxValue = -Infinity;
this._minValue = +Infinity;
this._renderSignal = this._renderSignal.bind(this);
}
render() {
return html`
<div
@contextmenu="${this._preventContextMenu}"
>
<canvas
style="
width: ${this.width}px;
height: ${this.height}px;
"
></canvas>
${this.displayMinMax
? html`
<span class="max">${this._maxValue.toFixed(3)}</span>
<span class="min">${this._minValue.toFixed(3)}</span>
`
: nothing
}
</div>
`;
}
firstUpdated() {
const scale = window.devicePixelRatio;
this.canvas = this.shadowRoot.querySelector('canvas');
this.ctx = this.canvas.getContext('2d');
this.cachedCanvas = document.createElement('canvas');
this.cachedCtx = this.cachedCanvas.getContext('2d');
// debug cached canvas
// this.cachedCanvas.style.position = 'absolute';
// this.cachedCanvas.style.top = 0;
// this.cachedCanvas.style.right = 0;
// this.cachedCanvas.style.outline = '1px solid red';
// document.body.appendChild(this.cachedCanvas);
super.firstUpdated();
}
connectedCallback() {
this._frameStack.length = 0;
this._pixelIndex = null;
super.connectedCallback();
this.rAFId = window.requestAnimationFrame(this._renderSignal);
}
update(changedProperties) {
this._dirty = true;
super.update(changedProperties);
}
disconnectedCallback() {
window.cancelAnimationFrame(this.rAFId);
this._frameStack.length = 0;
this.ctx.clearRect(0, 0, this._logicalWidth, this._logicalHeight);
this.cachedCtx.clearRect(0, 0, this._logicalWidth, this._logicalHeight);
super.disconnectedCallback();
}
_renderSignal() {
if (this._dirty) {
// @todo - fix weird behavior when updating `width`
const scale = window.devicePixelRatio;
this._logicalWidth = this.width * scale;
this._logicalHeight = this.height * scale;
this.canvas.width = this._logicalWidth;
this.canvas.height = this._logicalHeight;
this.cachedCanvas.width = this._logicalWidth;
this.cachedCanvas.height = this._logicalHeight;
// create y scale (should be called on height, min or max change)
const min = this.min;
const max = this.max;
const height = this._logicalHeight;
const a = (0 - height) / (max - min);
const b = height - (a * min);
this._getYPosition = (x) => a * x + b;
this._frameStack.length = 0;
this._pixelIndex = null;
this.ctx.clearRect(0, 0, this._logicalWidth, this._logicalHeight);
this.cachedCtx.clearRect(0, 0, this._logicalWidth, this._logicalHeight);
this._dirty = false;
}
const frameStackSize = this._frameStack.length;
if (frameStackSize > 0) {
let shiftCanvasPixels = 0;
let abort = false;
const pixelDuration = this.duration / this.width;
if (this._pixelIndex === null) {
this._pixelIndex = Math.floor(this._frameStack[0].time / pixelDuration);
}
// while we have some frames to display, we go through pixels and display
while (this._frameStack.length > 0) {
shiftCanvasPixels += 1;
const pixelStartTime = this._pixelIndex * pixelDuration;
const pixelStopTime = (this._pixelIndex + 1) * pixelDuration;
// console.log(pixelStartTime, pixelStopTime, this._pixelIndex);
let candidateIndex = null;
// find candidates for display in current pixel
for (let i = 0; i < this._frameStack.length; i++) {
const frame = this._frameStack[i];
const frameTime = frame.time;
// ignore old pixel
if (frameTime < pixelStartTime) {
// if last frame in stack, abort and wait for new frames
if (i + 1 === this._frameStack.length) {
// console.log('im there', frameTime, pixelStartTime, this._frameStack.length, i);
this._frameStack.length = 0;
abort = true;
}
// we take the last frame we find in the pixel time interval
} else if (frameTime >= pixelStartTime && frameTime < pixelStopTime) {
candidateIndex = i;
}
}
// we abort before incrementing this._pixelIndex,
// as we want to recheck the same pixel with new data later
if (abort) {
break;
}
// @note - there is probably room for optimizations here as
// we store and retrieve the comlete canvas at each pixel. This could
// probably be batched in some way.
if (candidateIndex !== null) {
const frame = this._frameStack[candidateIndex];
// draw line since last frame
if (this.lastFrame) {
const width = this._logicalWidth;
const height = this._logicalHeight;
const lastFramePixel = width - shiftCanvasPixels;
// shift canvas from `shiftCanvasPixels`
this.ctx.clearRect(0, 0, width, height);
this.ctx.drawImage(this.cachedCanvas,
shiftCanvasPixels, 0, lastFramePixel, height,
0, 0, lastFramePixel, height
);
this.ctx.lineWidth = this.lineWidth;
this.ctx.lineCap = 'round';
for (let i = 0; i < frame.data.length; i++) {
this.ctx.strokeStyle = this.colors[i];
// draw line between lastFrame (width - shiftCanvasPixels, y)
// and currentFrame (width, y)
const lastY = this._getYPosition(this.lastFrame.data[i]);
const currentY = this._getYPosition(frame.data[i]);
this.ctx.beginPath();
this.ctx.moveTo(lastFramePixel, lastY);
this.ctx.lineTo(width, currentY);
this.ctx.stroke();
}
// save currentState into cache
this.cachedCtx.clearRect(0, 0, width, height);
this.cachedCtx.drawImage(this.canvas, 0, 0, width, height);
}
this.lastFrame = frame;
shiftCanvasPixels = 0;
// remove frames from stack including rendered candidate
this._frameStack.splice(0, candidateIndex + 1);
}
this._pixelIndex += 1;
} // end while
} // end if
this.rAFId = window.requestAnimationFrame(this._renderSignal);
}
}
if (customElements.get('sc-signal') === undefined) {
customElements.define('sc-signal', ScSignal);
}
export default ScSignal;