wavesurfer.js 6.5.1 → 7.0.0-alpha.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (132) hide show
  1. package/.eslintrc.json +24 -0
  2. package/.prettierrc +8 -0
  3. package/README.md +26 -117
  4. package/dist/base-plugin.d.ts +10 -0
  5. package/dist/base-plugin.js +13 -0
  6. package/dist/decoder.d.ts +10 -0
  7. package/dist/decoder.js +43 -0
  8. package/dist/event-emitter.d.ts +11 -0
  9. package/dist/event-emitter.js +21 -0
  10. package/dist/fetcher.d.ts +4 -0
  11. package/dist/fetcher.js +17 -0
  12. package/dist/index.d.ts +92 -0
  13. package/dist/index.js +166 -0
  14. package/dist/player-webaudio.d.ts +8 -0
  15. package/dist/player-webaudio.js +31 -0
  16. package/dist/player.d.ts +16 -0
  17. package/dist/player.js +40 -0
  18. package/dist/plugins/regions.d.ts +48 -0
  19. package/dist/plugins/regions.js +183 -0
  20. package/dist/renderer.d.ts +34 -0
  21. package/dist/renderer.js +193 -0
  22. package/dist/timer.d.ts +10 -0
  23. package/dist/timer.js +17 -0
  24. package/examples/audio.ogg +0 -0
  25. package/examples/bars.js +19 -0
  26. package/examples/basic.js +8 -0
  27. package/examples/gradient.js +28 -0
  28. package/examples/regions.js +63 -0
  29. package/examples/video.js +19 -0
  30. package/examples/webaudio.js +14 -0
  31. package/package.json +19 -99
  32. package/src/base-plugin.ts +20 -0
  33. package/src/decoder.ts +41 -0
  34. package/src/event-emitter.ts +35 -0
  35. package/src/fetcher.ts +7 -0
  36. package/src/index.ts +252 -0
  37. package/src/player-webaudio.ts +34 -0
  38. package/src/player.ts +50 -0
  39. package/src/plugins/regions.ts +240 -0
  40. package/src/renderer.ts +250 -0
  41. package/src/timer.ts +27 -0
  42. package/tsconfig.json +105 -0
  43. package/tutorial/index.html +47 -0
  44. package/tutorial/src/editor.js +70 -0
  45. package/tutorial/src/init.js +66 -0
  46. package/tutorial/src/url.js +25 -0
  47. package/tutorial/style.css +211 -0
  48. package/yarn-error.log +1049 -0
  49. package/LICENSE +0 -29
  50. package/dist/plugin/wavesurfer.cursor.js +0 -418
  51. package/dist/plugin/wavesurfer.cursor.js.map +0 -1
  52. package/dist/plugin/wavesurfer.cursor.min.js +0 -7
  53. package/dist/plugin/wavesurfer.cursor.min.js.map +0 -1
  54. package/dist/plugin/wavesurfer.elan.js +0 -381
  55. package/dist/plugin/wavesurfer.elan.js.map +0 -1
  56. package/dist/plugin/wavesurfer.elan.min.js +0 -7
  57. package/dist/plugin/wavesurfer.elan.min.js.map +0 -1
  58. package/dist/plugin/wavesurfer.markers.js +0 -520
  59. package/dist/plugin/wavesurfer.markers.js.map +0 -1
  60. package/dist/plugin/wavesurfer.markers.min.js +0 -7
  61. package/dist/plugin/wavesurfer.markers.min.js.map +0 -1
  62. package/dist/plugin/wavesurfer.mediasession.js +0 -181
  63. package/dist/plugin/wavesurfer.mediasession.js.map +0 -1
  64. package/dist/plugin/wavesurfer.mediasession.min.js +0 -7
  65. package/dist/plugin/wavesurfer.mediasession.min.js.map +0 -1
  66. package/dist/plugin/wavesurfer.microphone.js +0 -466
  67. package/dist/plugin/wavesurfer.microphone.js.map +0 -1
  68. package/dist/plugin/wavesurfer.microphone.min.js +0 -7
  69. package/dist/plugin/wavesurfer.microphone.min.js.map +0 -1
  70. package/dist/plugin/wavesurfer.minimap.js +0 -440
  71. package/dist/plugin/wavesurfer.minimap.js.map +0 -1
  72. package/dist/plugin/wavesurfer.minimap.min.js +0 -7
  73. package/dist/plugin/wavesurfer.minimap.min.js.map +0 -1
  74. package/dist/plugin/wavesurfer.playhead.js +0 -300
  75. package/dist/plugin/wavesurfer.playhead.js.map +0 -1
  76. package/dist/plugin/wavesurfer.playhead.min.js +0 -7
  77. package/dist/plugin/wavesurfer.playhead.min.js.map +0 -1
  78. package/dist/plugin/wavesurfer.regions.js +0 -1335
  79. package/dist/plugin/wavesurfer.regions.js.map +0 -1
  80. package/dist/plugin/wavesurfer.regions.min.js +0 -7
  81. package/dist/plugin/wavesurfer.regions.min.js.map +0 -1
  82. package/dist/plugin/wavesurfer.spectrogram.js +0 -736
  83. package/dist/plugin/wavesurfer.spectrogram.js.map +0 -1
  84. package/dist/plugin/wavesurfer.spectrogram.min.js +0 -7
  85. package/dist/plugin/wavesurfer.spectrogram.min.js.map +0 -1
  86. package/dist/plugin/wavesurfer.timeline.js +0 -649
  87. package/dist/plugin/wavesurfer.timeline.js.map +0 -1
  88. package/dist/plugin/wavesurfer.timeline.min.js +0 -7
  89. package/dist/plugin/wavesurfer.timeline.min.js.map +0 -1
  90. package/dist/wavesurfer-html-init.js +0 -381
  91. package/dist/wavesurfer-html-init.js.map +0 -1
  92. package/dist/wavesurfer-html-init.min.js +0 -7
  93. package/dist/wavesurfer-html-init.min.js.map +0 -1
  94. package/dist/wavesurfer.js +0 -6145
  95. package/dist/wavesurfer.js.map +0 -1
  96. package/dist/wavesurfer.min.js +0 -7
  97. package/dist/wavesurfer.min.js.map +0 -1
  98. package/src/drawer.canvasentry.js +0 -427
  99. package/src/drawer.js +0 -413
  100. package/src/drawer.multicanvas.js +0 -617
  101. package/src/html-init.js +0 -241
  102. package/src/mediaelement-webaudio.js +0 -77
  103. package/src/mediaelement.js +0 -443
  104. package/src/peakcache.js +0 -127
  105. package/src/plugin/cursor/index.js +0 -350
  106. package/src/plugin/elan/index.js +0 -307
  107. package/src/plugin/markers/index.js +0 -436
  108. package/src/plugin/mediasession/index.js +0 -94
  109. package/src/plugin/microphone/index.js +0 -415
  110. package/src/plugin/minimap/index.js +0 -403
  111. package/src/plugin/playhead/index.js +0 -226
  112. package/src/plugin/regions/index.js +0 -478
  113. package/src/plugin/regions/region.js +0 -875
  114. package/src/plugin/spectrogram/fft.js +0 -222
  115. package/src/plugin/spectrogram/index.js +0 -522
  116. package/src/plugin/timeline/index.js +0 -600
  117. package/src/util/absMax.js +0 -16
  118. package/src/util/clamp.js +0 -11
  119. package/src/util/fetch.js +0 -222
  120. package/src/util/frame.js +0 -13
  121. package/src/util/get-id.js +0 -22
  122. package/src/util/index.js +0 -14
  123. package/src/util/max.js +0 -16
  124. package/src/util/min.js +0 -16
  125. package/src/util/observer.js +0 -144
  126. package/src/util/orientation.js +0 -98
  127. package/src/util/prevent-click.js +0 -19
  128. package/src/util/request-animation-frame.js +0 -15
  129. package/src/util/silence-mode.js +0 -42
  130. package/src/util/style.js +0 -16
  131. package/src/wavesurfer.js +0 -1797
  132. package/src/webaudio.js +0 -779
@@ -1,600 +0,0 @@
1
- /**
2
- * @typedef {Object} TimelinePluginParams
3
- * @desc Extends the `WavesurferParams` wavesurfer was initialised with
4
- * @property {!string|HTMLElement} container CSS selector or HTML element where
5
- * the timeline should be drawn. This is the only required parameter.
6
- * @property {number} notchPercentHeight=90 Height of notches in percent
7
- * @property {string} unlabeledNotchColor='#c0c0c0' The colour of the notches
8
- * that do not have labels
9
- * @property {string} primaryColor='#000' The colour of the main notches
10
- * @property {string} secondaryColor='#c0c0c0' The colour of the secondary
11
- * notches
12
- * @property {string} primaryFontColor='#000' The colour of the labels next to
13
- * the main notches
14
- * @property {string} secondaryFontColor='#000' The colour of the labels next to
15
- * the secondary notches
16
- * @property {number} labelPadding=5 The padding between the label and the notch
17
- * @property {?number} zoomDebounce A debounce timeout to increase rendering
18
- * performance for large files
19
- * @property {string} fontFamily='Arial'
20
- * @property {number} fontSize=10 Font size of labels in pixels
21
- * @property {?number} duration Length of the track in seconds. Overrides
22
- * getDuration() for setting length of timeline
23
- * @property {function} formatTimeCallback (sec, pxPerSec) -> label
24
- * @property {function} timeInterval (pxPerSec) -> seconds between notches
25
- * @property {function} primaryLabelInterval (pxPerSec) -> cadence between
26
- * labels in primary color
27
- * @property {function} secondaryLabelInterval (pxPerSec) -> cadence between
28
- * labels in secondary color
29
- * @property {?number} offset Offset for the timeline start in seconds. May also be
30
- * negative.
31
- * @property {?boolean} deferInit Set to true to manually call
32
- * `initPlugin('timeline')`
33
- */
34
-
35
- /**
36
- * Adds a timeline to the waveform.
37
- *
38
- * @implements {PluginClass}
39
- * @extends {Observer}
40
- * @example
41
- * // es6
42
- * import TimelinePlugin from 'wavesurfer.timeline.js';
43
- *
44
- * // commonjs
45
- * var TimelinePlugin = require('wavesurfer.timeline.js');
46
- *
47
- * // if you are using <script> tags
48
- * var TimelinePlugin = window.WaveSurfer.timeline;
49
- *
50
- * // ... initialising wavesurfer with the plugin
51
- * var wavesurfer = WaveSurfer.create({
52
- * // wavesurfer options ...
53
- * plugins: [
54
- * TimelinePlugin.create({
55
- * // plugin options ...
56
- * })
57
- * ]
58
- * });
59
- */
60
- export default class TimelinePlugin {
61
- /**
62
- * Timeline plugin definition factory
63
- *
64
- * This function must be used to create a plugin definition which can be
65
- * used by wavesurfer to correctly instantiate the plugin.
66
- *
67
- * @param {TimelinePluginParams} params parameters use to initialise the plugin
68
- * @return {PluginDefinition} an object representing the plugin
69
- */
70
- static create(params) {
71
- return {
72
- name: 'timeline',
73
- deferInit: params && params.deferInit ? params.deferInit : false,
74
- params: params,
75
- instance: TimelinePlugin
76
- };
77
- }
78
-
79
- // event handlers
80
- _onScroll = () => {
81
- if (this.wrapper && this.drawer.wrapper) {
82
- this.wrapper.scrollLeft = this.drawer.wrapper.scrollLeft;
83
- }
84
- };
85
-
86
- /**
87
- * @returns {void}
88
- */
89
- _onRedraw = () => this.render();
90
-
91
- _onReady = () => {
92
- const ws = this.wavesurfer;
93
- this.drawer = ws.drawer;
94
- this.pixelRatio = ws.drawer.params.pixelRatio;
95
- this.maxCanvasWidth = ws.drawer.maxCanvasWidth || ws.drawer.width;
96
- this.maxCanvasElementWidth =
97
- ws.drawer.maxCanvasElementWidth ||
98
- Math.round(this.maxCanvasWidth / this.pixelRatio);
99
-
100
- // add listeners
101
- ws.drawer.wrapper.addEventListener('scroll', this._onScroll);
102
- ws.on('redraw', this._onRedraw);
103
- ws.on('zoom', this._onZoom);
104
-
105
- this.render();
106
- };
107
-
108
- /**
109
- * @param {object} e Click event
110
- */
111
- _onWrapperClick = e => {
112
- e.preventDefault();
113
- const relX = 'offsetX' in e ? e.offsetX : e.layerX;
114
- this.fireEvent('click', relX / this.wrapper.scrollWidth || 0);
115
- };
116
-
117
- /**
118
- * Creates an instance of TimelinePlugin.
119
- *
120
- * You probably want to use TimelinePlugin.create()
121
- *
122
- * @param {TimelinePluginParams} params Plugin parameters
123
- * @param {object} ws Wavesurfer instance
124
- */
125
- constructor(params, ws) {
126
- this.container =
127
- 'string' == typeof params.container
128
- ? document.querySelector(params.container)
129
- : params.container;
130
-
131
- if (!this.container) {
132
- throw new Error('No container for wavesurfer timeline');
133
- }
134
-
135
- this.wavesurfer = ws;
136
- this.util = ws.util;
137
- this.params = Object.assign(
138
- {},
139
- {
140
- height: 20,
141
- notchPercentHeight: 90,
142
- labelPadding: 5,
143
- unlabeledNotchColor: '#c0c0c0',
144
- primaryColor: '#000',
145
- secondaryColor: '#c0c0c0',
146
- primaryFontColor: '#000',
147
- secondaryFontColor: '#000',
148
- fontFamily: 'Arial',
149
- fontSize: 10,
150
- duration: null,
151
- zoomDebounce: false,
152
- formatTimeCallback: this.defaultFormatTimeCallback,
153
- timeInterval: this.defaultTimeInterval,
154
- primaryLabelInterval: this.defaultPrimaryLabelInterval,
155
- secondaryLabelInterval: this.defaultSecondaryLabelInterval,
156
- offset: 0
157
- },
158
- params
159
- );
160
-
161
- this.canvases = [];
162
- this.wrapper = null;
163
- this.drawer = null;
164
- this.pixelRatio = null;
165
- this.maxCanvasWidth = null;
166
- this.maxCanvasElementWidth = null;
167
- /**
168
- * This event handler has to be in the constructor function because it
169
- * relies on the debounce function which is only available after
170
- * instantiation
171
- *
172
- * Use a debounced function if `params.zoomDebounce` is defined
173
- *
174
- * @returns {void}
175
- */
176
- this._onZoom = this.params.zoomDebounce
177
- ? this.wavesurfer.util.debounce(
178
- () => this.render(),
179
- this.params.zoomDebounce
180
- )
181
- : () => this.render();
182
- }
183
-
184
- /**
185
- * Initialisation function used by the plugin API
186
- */
187
- init() {
188
- // Check if ws is ready
189
- if (this.wavesurfer.isReady) {
190
- this._onReady();
191
- } else {
192
- this.wavesurfer.once('ready', this._onReady);
193
- }
194
- }
195
-
196
- /**
197
- * Destroy function used by the plugin API
198
- */
199
- destroy() {
200
- this.unAll();
201
- this.wavesurfer.un('redraw', this._onRedraw);
202
- this.wavesurfer.un('zoom', this._onZoom);
203
- this.wavesurfer.un('ready', this._onReady);
204
- this.wavesurfer.drawer.wrapper.removeEventListener(
205
- 'scroll',
206
- this._onScroll
207
- );
208
- if (this.wrapper && this.wrapper.parentNode) {
209
- this.wrapper.removeEventListener('click', this._onWrapperClick);
210
- this.wrapper.parentNode.removeChild(this.wrapper);
211
- this.wrapper = null;
212
- }
213
- }
214
-
215
- /**
216
- * Create a timeline element to wrap the canvases drawn by this plugin
217
- *
218
- */
219
- createWrapper() {
220
- const wsParams = this.wavesurfer.params;
221
- this.container.innerHTML = '';
222
- this.wrapper = this.container.appendChild(
223
- document.createElement('timeline')
224
- );
225
- this.util.style(this.wrapper, {
226
- display: 'block',
227
- position: 'relative',
228
- userSelect: 'none',
229
- webkitUserSelect: 'none',
230
- height: `${this.params.height}px`
231
- });
232
-
233
- if (wsParams.fillParent || wsParams.scrollParent) {
234
- this.util.style(this.wrapper, {
235
- width: '100%',
236
- overflowX: 'hidden',
237
- overflowY: 'hidden'
238
- });
239
- }
240
-
241
- this.wrapper.addEventListener('click', this._onWrapperClick);
242
- }
243
-
244
- /**
245
- * Render the timeline (also updates the already rendered timeline)
246
- *
247
- */
248
- render() {
249
- if (!this.wrapper) {
250
- this.createWrapper();
251
- }
252
- this.updateCanvases();
253
- this.updateCanvasesPositioning();
254
- this.renderCanvases();
255
- }
256
-
257
- /**
258
- * Add new timeline canvas
259
- *
260
- */
261
- addCanvas() {
262
- const canvas = this.wrapper.appendChild(
263
- document.createElement('canvas')
264
- );
265
- this.canvases.push(canvas);
266
- this.util.style(canvas, {
267
- position: 'absolute',
268
- zIndex: 4
269
- });
270
- }
271
-
272
- /**
273
- * Remove timeline canvas
274
- *
275
- */
276
- removeCanvas() {
277
- const canvas = this.canvases.pop();
278
- canvas.parentElement.removeChild(canvas);
279
- }
280
-
281
- /**
282
- * Make sure the correct of timeline canvas elements exist and are cached in
283
- * this.canvases
284
- *
285
- */
286
- updateCanvases() {
287
- const totalWidth = Math.round(this.drawer.wrapper.scrollWidth);
288
- const requiredCanvases = Math.ceil(
289
- totalWidth / this.maxCanvasElementWidth
290
- );
291
-
292
- while (this.canvases.length < requiredCanvases) {
293
- this.addCanvas();
294
- }
295
-
296
- while (this.canvases.length > requiredCanvases) {
297
- this.removeCanvas();
298
- }
299
- }
300
-
301
- /**
302
- * Update the dimensions and positioning style for all the timeline canvases
303
- *
304
- */
305
- updateCanvasesPositioning() {
306
- // cache length for performance
307
- const canvasesLength = this.canvases.length;
308
- this.canvases.forEach((canvas, i) => {
309
- // canvas width is the max element width, or if it is the last the
310
- // required width
311
- const canvasWidth =
312
- i === canvasesLength - 1
313
- ? this.drawer.wrapper.scrollWidth -
314
- this.maxCanvasElementWidth * (canvasesLength - 1)
315
- : this.maxCanvasElementWidth;
316
- // set dimensions and style
317
- canvas.width = canvasWidth * this.pixelRatio;
318
- // on certain pixel ratios the canvas appears cut off at the bottom,
319
- // therefore leave 1px extra
320
- canvas.height = (this.params.height + 1) * this.pixelRatio;
321
- this.util.style(canvas, {
322
- width: `${canvasWidth}px`,
323
- height: `${this.params.height}px`,
324
- left: `${i * this.maxCanvasElementWidth}px`
325
- });
326
- });
327
- }
328
-
329
- /**
330
- * Render the timeline labels and notches
331
- *
332
- */
333
- renderCanvases() {
334
- const duration =
335
- this.params.duration ||
336
- this.wavesurfer.backend.getDuration();
337
-
338
- if (duration <= 0) {
339
- return;
340
- }
341
- const wsParams = this.wavesurfer.params;
342
- const fontSize = this.params.fontSize * wsParams.pixelRatio;
343
- const totalSeconds = parseInt(duration, 10) + 1;
344
- const width =
345
- wsParams.fillParent && !wsParams.scrollParent
346
- ? this.drawer.getWidth()
347
- : this.drawer.wrapper.scrollWidth * wsParams.pixelRatio;
348
- const height1 = this.params.height * this.pixelRatio;
349
- const height2 =
350
- this.params.height *
351
- (this.params.notchPercentHeight / 100) *
352
- this.pixelRatio;
353
- const pixelsPerSecond = width / duration;
354
-
355
- const formatTime = this.params.formatTimeCallback;
356
- // if parameter is function, call the function with
357
- // pixelsPerSecond, otherwise simply take the value as-is
358
- const intervalFnOrVal = option =>
359
- typeof option === 'function' ? option(pixelsPerSecond) : option;
360
- const timeInterval = intervalFnOrVal(this.params.timeInterval);
361
- const primaryLabelInterval = intervalFnOrVal(
362
- this.params.primaryLabelInterval
363
- );
364
- const secondaryLabelInterval = intervalFnOrVal(
365
- this.params.secondaryLabelInterval
366
- );
367
-
368
- let curPixel = pixelsPerSecond * this.params.offset;
369
- let curSeconds = 0;
370
- let i;
371
- // build an array of position data with index, second and pixel data,
372
- // this is then used multiple times below
373
- const positioning = [];
374
-
375
- // render until end in case we have a negative offset
376
- const renderSeconds = (this.params.offset < 0)
377
- ? totalSeconds - this.params.offset
378
- : totalSeconds;
379
-
380
- for (i = 0; i < renderSeconds / timeInterval; i++) {
381
- positioning.push([i, curSeconds, curPixel]);
382
- curSeconds += timeInterval;
383
- curPixel += pixelsPerSecond * timeInterval;
384
- }
385
-
386
- // iterate over each position
387
- const renderPositions = cb => {
388
- positioning.forEach(pos => {
389
- cb(pos[0], pos[1], pos[2]);
390
- });
391
- };
392
-
393
- // render primary labels
394
- this.setFillStyles(this.params.primaryColor);
395
- this.setFonts(`${fontSize}px ${this.params.fontFamily}`);
396
- this.setFillStyles(this.params.primaryFontColor);
397
- renderPositions((i, curSeconds, curPixel) => {
398
- if (i % primaryLabelInterval === 0) {
399
- this.fillRect(curPixel, 0, 1, height1);
400
- this.fillText(
401
- formatTime(curSeconds, pixelsPerSecond),
402
- curPixel + this.params.labelPadding * this.pixelRatio,
403
- height1
404
- );
405
- }
406
- });
407
-
408
- // render secondary labels
409
- this.setFillStyles(this.params.secondaryColor);
410
- this.setFonts(`${fontSize}px ${this.params.fontFamily}`);
411
- this.setFillStyles(this.params.secondaryFontColor);
412
- renderPositions((i, curSeconds, curPixel) => {
413
- if (i % secondaryLabelInterval === 0) {
414
- this.fillRect(curPixel, 0, 1, height1);
415
- this.fillText(
416
- formatTime(curSeconds, pixelsPerSecond),
417
- curPixel + this.params.labelPadding * this.pixelRatio,
418
- height1
419
- );
420
- }
421
- });
422
-
423
- // render the actual notches (when no labels are used)
424
- this.setFillStyles(this.params.unlabeledNotchColor);
425
- renderPositions((i, curSeconds, curPixel) => {
426
- if (
427
- i % secondaryLabelInterval !== 0 &&
428
- i % primaryLabelInterval !== 0
429
- ) {
430
- this.fillRect(curPixel, 0, 1, height2);
431
- }
432
- });
433
- }
434
-
435
- /**
436
- * Set the canvas fill style
437
- *
438
- * @param {DOMString|CanvasGradient|CanvasPattern} fillStyle Fill style to
439
- * use
440
- */
441
- setFillStyles(fillStyle) {
442
- this.canvases.forEach(canvas => {
443
- const context = canvas.getContext('2d');
444
- if (context) {
445
- context.fillStyle = fillStyle;
446
- }
447
- });
448
- }
449
-
450
- /**
451
- * Set the canvas font
452
- *
453
- * @param {DOMString} font Font to use
454
- */
455
- setFonts(font) {
456
- this.canvases.forEach(canvas => {
457
- const context = canvas.getContext('2d');
458
- if (context) {
459
- context.font = font;
460
- }
461
- });
462
- }
463
-
464
- /**
465
- * Draw a rectangle on the canvases
466
- *
467
- * (it figures out the offset for each canvas)
468
- *
469
- * @param {number} x X-position
470
- * @param {number} y Y-position
471
- * @param {number} width Width
472
- * @param {number} height Height
473
- */
474
- fillRect(x, y, width, height) {
475
- this.canvases.forEach((canvas, i) => {
476
- const leftOffset = i * this.maxCanvasWidth;
477
-
478
- const intersection = {
479
- x1: Math.max(x, i * this.maxCanvasWidth),
480
- y1: y,
481
- x2: Math.min(x + width, i * this.maxCanvasWidth + canvas.width),
482
- y2: y + height
483
- };
484
-
485
- if (intersection.x1 < intersection.x2) {
486
- const context = canvas
487
- .getContext('2d');
488
- if (context) {
489
- context
490
- .fillRect(
491
- intersection.x1 - leftOffset,
492
- intersection.y1,
493
- intersection.x2 - intersection.x1,
494
- intersection.y2 - intersection.y1
495
- );
496
- }
497
- }
498
- });
499
- }
500
-
501
- /**
502
- * Fill a given text on the canvases
503
- *
504
- * @param {string} text Text to render
505
- * @param {number} x X-position
506
- * @param {number} y Y-position
507
- */
508
- fillText(text, x, y) {
509
- let textWidth;
510
- let xOffset = 0;
511
-
512
- this.canvases.forEach(canvas => {
513
- const context = canvas.getContext('2d');
514
- if (context) {
515
- const canvasWidth = context.canvas.width;
516
-
517
- if (xOffset > x + textWidth) {
518
- return;
519
- }
520
-
521
- if (xOffset + canvasWidth > x && context) {
522
- textWidth = context.measureText(text).width;
523
- context.fillText(text, x - xOffset, y);
524
- }
525
-
526
- xOffset += canvasWidth;
527
- }
528
- });
529
- }
530
-
531
- /**
532
- * Turn the time into a suitable label for the time.
533
- *
534
- * @param {number} seconds Seconds to format
535
- * @param {number} pxPerSec Pixels per second
536
- * @returns {number} Time
537
- */
538
- defaultFormatTimeCallback(seconds, pxPerSec) {
539
- if (seconds / 60 > 1) {
540
- // calculate minutes and seconds from seconds count
541
- const minutes = parseInt(seconds / 60, 10);
542
- seconds = parseInt(seconds % 60, 10);
543
- // fill up seconds with zeroes
544
- seconds = seconds < 10 ? '0' + seconds : seconds;
545
- return `${minutes}:${seconds}`;
546
- }
547
- return Math.round(seconds * 1000) / 1000;
548
- }
549
-
550
- /**
551
- * Return how many seconds should be between each notch
552
- *
553
- * @param {number} pxPerSec Pixels per second
554
- * @returns {number} Time
555
- */
556
- defaultTimeInterval(pxPerSec) {
557
- if (pxPerSec >= 25) {
558
- return 1;
559
- } else if (pxPerSec * 5 >= 25) {
560
- return 5;
561
- } else if (pxPerSec * 15 >= 25) {
562
- return 15;
563
- }
564
- return Math.ceil(0.5 / pxPerSec) * 60;
565
- }
566
-
567
- /**
568
- * Return the cadence of notches that get labels in the primary color.
569
- *
570
- * @param {number} pxPerSec Pixels per second
571
- * @returns {number} Cadence
572
- */
573
- defaultPrimaryLabelInterval(pxPerSec) {
574
- if (pxPerSec >= 25) {
575
- return 10;
576
- } else if (pxPerSec * 5 >= 25) {
577
- return 6;
578
- } else if (pxPerSec * 15 >= 25) {
579
- return 4;
580
- }
581
- return 4;
582
- }
583
-
584
- /**
585
- * Return the cadence of notches that get labels in the secondary color.
586
- *
587
- * @param {number} pxPerSec Pixels per second
588
- * @returns {number} Cadence
589
- */
590
- defaultSecondaryLabelInterval(pxPerSec) {
591
- if (pxPerSec >= 25) {
592
- return 5;
593
- } else if (pxPerSec * 5 >= 25) {
594
- return 2;
595
- } else if (pxPerSec * 15 >= 25) {
596
- return 2;
597
- }
598
- return 2;
599
- }
600
- }
@@ -1,16 +0,0 @@
1
- import utilMax from './max';
2
- import utilmin from './min';
3
-
4
- /**
5
- * Get the largest absolute value in an array
6
- *
7
- * @param {Array} values Array of numbers
8
- * @returns {Number} Largest number found
9
- * @example console.log(max([-3, 2, 1]), max([-3, 2, 4])); // logs 3 4
10
- * @since 4.3.0
11
- */
12
- export default function absMax(values) {
13
- const max = utilMax(values);
14
- const min = utilmin(values);
15
- return -min > max ? -min : max;
16
- }
package/src/util/clamp.js DELETED
@@ -1,11 +0,0 @@
1
- /**
2
- * Returns a number limited to the given range.
3
- *
4
- * @param {number} val The number to be limited to a range
5
- * @param {number} min The lower boundary of the limit range
6
- * @param {number} max The upper boundary of the limit range
7
- * @returns {number} A number in the range [min, max]
8
- */
9
- export default function clamp(val, min, max) {
10
- return Math.min(Math.max(min, val), max);
11
- }