Skip to content

Commit 474daac

Browse files
committed
Added zoomview.update event
See #548
1 parent 3c3a090 commit 474daac

11 files changed

+154
-37
lines changed

demo/custom-markers/main.js

+10-2
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ Peaks.init(options, function(err, peaksInstance) {
332332
view.setSegmentDragMode(event.target.value);
333333
});
334334

335-
// Points mouse events
335+
// Point events
336336

337337
peaksInstance.on('points.mouseenter', function(event) {
338338
console.log('points.mouseenter:', event);
@@ -368,7 +368,7 @@ Peaks.init(options, function(err, peaksInstance) {
368368
console.log('points.dragend:', event);
369369
});
370370

371-
// Segments mouse events
371+
// Segment events
372372

373373
peaksInstance.on('segments.dragstart', function(event) {
374374
console.log('segments.dragstart:', event);
@@ -404,6 +404,8 @@ Peaks.init(options, function(err, peaksInstance) {
404404
console.log('segments.contextmenu:', event);
405405
});
406406

407+
// Zoomview waveform events
408+
407409
peaksInstance.on('zoomview.click', function(event) {
408410
console.log('zoomview.click:', event);
409411
});
@@ -418,6 +420,12 @@ Peaks.init(options, function(err, peaksInstance) {
418420
console.log('zoomview.contextmenu:', event);
419421
});
420422

423+
peaksInstance.on('zoomview.update', function(event) {
424+
console.log('zoomview.update:', event);
425+
});
426+
427+
// Overview waveform events
428+
421429
peaksInstance.on('overview.click', function(event) {
422430
console.log('overview.click:', event);
423431
});

demo/index.html

+5
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,7 @@ <h2>Points</h2>
523523
});
524524

525525
// Point events
526+
526527
peaksInstance.on('points.add', function(event) {
527528
console.log('points.add:', event);
528529
});
@@ -633,6 +634,10 @@ <h2>Points</h2>
633634
console.log('zoomview.contextmenu:', event);
634635
});
635636

637+
peaksInstance.on('zoomview.update', function(event) {
638+
console.log('zoomview.update:', event);
639+
});
640+
636641
// Overview waveform events
637642

638643
peaksInstance.on('overview.click', function(event) {

demo/overlay-segments.html

+4
Original file line numberDiff line numberDiff line change
@@ -623,6 +623,10 @@ <h2>Points</h2>
623623
console.log('zoomview.contextmenu:', event);
624624
});
625625

626+
peaksInstance.on('zoomview.update', function(event) {
627+
console.log('zoomview.update:', event);
628+
});
629+
626630
// Overview waveform events
627631

628632
peaksInstance.on('overview.click', function(event) {

demo/zoomable-waveform.html

+6
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,12 @@ <h2>Demo: Single Zoomable Waveform</h2>
174174
view.setStartTime(seconds);
175175
}
176176
});
177+
178+
// Zoomview waveform events
179+
180+
peaksInstance.on('zoomview.update', function(event) {
181+
console.log('zoomview.update:', event);
182+
});
177183
});
178184
})(peaks);
179185
</script>

doc/API.md

+18-6
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ This document describes the Peaks.js API, including configuration options, funct
9292
- [zoomview.click](#zoomviewclick)
9393
- [zoomview.dblclick](#zoomviewdblclick)
9494
- [zoomview.contextmenu](#zoomviewcontextmenu)
95+
- [zoomview.update](#zoomviewupdate)
9596
- [zoom.update](#zoomupdate)
9697
- [Point Events](#point-events)
9798
- [points.add](#pointsadd)
@@ -1700,21 +1701,32 @@ instance.on('zoomview.contextmenu', function(event) {
17001701
});
17011702
```
17021703

1703-
### `zoom.update`
1704+
### `zoomview.update`
17041705

1705-
This event is emitted when the zoom level in the zoomable waveform view changes.
1706+
This event is emitted when the time range visible in the zoomable waveform view changes.
17061707

17071708
The `event` parameter contains:
17081709

1709-
* `currentZoom`: The current zoom level, in samples per pixel
1710-
* `previousZoom`: The previous zoom level, in samples per pixel
1710+
* `startTime`: The time at the left edge of the waveform view.
1711+
* `endTime`: The time at the right edge of the waveform view.
1712+
1713+
Note that `startTime` may not be exactly the same value you set when calling [`view.setStartTime()`](#viewsetstarttimetime). This is because the time is rounded to a number of pixels at the view's zoom level.
17111714

17121715
```js
1713-
instance.on('zoom.update', function(event) {
1714-
console.log(`Zoom changed from ${event.previousZoom} to ${event.currentZoom}`);
1716+
instance.on('zoomview.update', function(event) {
1717+
console.log(`Start time: ${event.startTime}, end time: ${event.endTime}`);
17151718
});
17161719
```
17171720

1721+
### `zoom.update`
1722+
1723+
This event is emitted when the zoom level in the zoomable waveform view changes.
1724+
1725+
The `event` parameter contains:
1726+
1727+
* `currentZoom`: The current zoom level, in samples per pixel
1728+
* `previousZoom`: The previous zoom level, in samples per pixel
1729+
17181730
## Point Events
17191731

17201732
### `points.add`

src/scrollbar.js

+5-3
Original file line numberDiff line numberDiff line change
@@ -33,10 +33,10 @@ function Scrollbar(waveformData, container, peaks) {
3333
this._onScrollboxDragStart = this._onScrollboxDragStart.bind(this);
3434
this._onScrollboxDragMove = this._onScrollboxDragMove.bind(this);
3535
this._onScrollboxDragEnd = this._onScrollboxDragEnd.bind(this);
36-
this._onZoomviewDisplaying = this._onZoomviewDisplaying.bind(this);
36+
this._onZoomviewUpdate = this._onZoomviewUpdate.bind(this);
3737
this._onScrollbarClick = this._onScrollbarClick.bind(this);
3838

39-
peaks.on('zoomview.displaying', this._onZoomviewDisplaying);
39+
this._peaks.on('zoomview.update', this._onZoomviewUpdate);
4040

4141
this._width = container.clientWidth;
4242
this._height = container.clientHeight;
@@ -150,7 +150,7 @@ Scrollbar.prototype._onScrollboxDragMove = function() {
150150
}
151151
};
152152

153-
Scrollbar.prototype._onZoomviewDisplaying = function(/* startTime , endTime */) {
153+
Scrollbar.prototype._onZoomviewUpdate = function(/* event */) {
154154
if (!this._dragging) {
155155
this._updateScrollbarWidthAndPosition();
156156
}
@@ -218,6 +218,8 @@ Scrollbar.prototype.fitToContainer = function() {
218218
};
219219

220220
Scrollbar.prototype.destroy = function() {
221+
this._peaks.off('zoomview.update', this._onZoomviewUpdate);
222+
221223
this._layer.destroy();
222224

223225
this._stage.destroy();

src/waveform-overview.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,13 @@ function WaveformOverview(waveformData, container, peaks) {
3030
self._onTimeUpdate = self._onTimeUpdate.bind(self);
3131
self._onPlaying = self._onPlaying.bind(self);
3232
self._onPause = self._onPause.bind(self);
33-
self._onZoomviewDisplaying = self._onZoomviewDisplaying.bind(self);
33+
self._onZoomviewUpdate = self._onZoomviewUpdate.bind(self);
3434

3535
// Register event handlers
3636
peaks.on('player.timeupdate', self._onTimeUpdate);
3737
peaks.on('player.playing', self._onPlaying);
3838
peaks.on('player.pause', self._onPause);
39-
peaks.on('zoomview.displaying', self._onZoomviewDisplaying);
39+
peaks.on('zoomview.update', self._onZoomviewUpdate);
4040

4141
const time = self._peaks.player.getCurrentTime();
4242

@@ -82,8 +82,8 @@ WaveformOverview.prototype._onPause = function(time) {
8282
this._playheadLayer.stop(time);
8383
};
8484

85-
WaveformOverview.prototype._onZoomviewDisplaying = function(startTime, endTime) {
86-
this.showHighlight(startTime, endTime);
85+
WaveformOverview.prototype._onZoomviewUpdate = function(event) {
86+
this.showHighlight(event.startTime, event.endTime);
8787
};
8888

8989
WaveformOverview.prototype.showHighlight = function(startTime, endTime) {
@@ -156,7 +156,7 @@ WaveformOverview.prototype.destroy = function() {
156156
this._peaks.off('player.playing', this._onPlaying);
157157
this._peaks.off('player.pause', this._onPause);
158158
this._peaks.off('player.timeupdate', this._onTimeUpdate);
159-
this._peaks.off('zoomview.displaying', this._onZoomviewDisplaying);
159+
this._peaks.off('zoomview.update', this._onZoomviewUpdate);
160160

161161
this._mouseDragHandler.destroy();
162162

src/waveform-zoomview.js

+28-16
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ function WaveformZoomView(waveformData, container, peaks) {
6868
self._onWheelCaptureVerticalScroll = self._onWheelCaptureVerticalScroll.bind(self);
6969
self.setWheelMode(self._viewOptions.wheelMode);
7070

71-
self._peaks.emit('zoomview.displaying', 0, self.getEndTime());
71+
self._peaks.emit('zoomview.update', {
72+
startTime: 0,
73+
endTime: self.getEndTime()
74+
});
7275
}
7376

7477
WaveformZoomView.prototype = Object.create(WaveformView.prototype);
@@ -150,11 +153,11 @@ WaveformZoomView.prototype._onWheel = function(event) {
150153

151154
wheelEvent.preventDefault();
152155

153-
const newFrameOffset = clamp(
156+
const frameOffset = clamp(
154157
this._frameOffset + Math.floor(delta), 0, this._pixelLength - this._width
155158
);
156159

157-
this.updateWaveform(newFrameOffset);
160+
this.updateWaveform(frameOffset, false);
158161
};
159162

160163
WaveformZoomView.prototype._onWheelCaptureVerticalScroll = function(event) {
@@ -165,11 +168,11 @@ WaveformZoomView.prototype._onWheelCaptureVerticalScroll = function(event) {
165168

166169
wheelEvent.preventDefault();
167170

168-
const newFrameOffset = clamp(
171+
const frameOffset = clamp(
169172
this._frameOffset + Math.floor(delta), 0, this._pixelLength - this._width
170173
);
171174

172-
this.updateWaveform(newFrameOffset);
175+
this.updateWaveform(frameOffset, false);
173176
};
174177

175178
WaveformZoomView.prototype.setWaveformDragMode = function(mode) {
@@ -291,15 +294,17 @@ WaveformZoomView.prototype._syncPlayhead = function(time) {
291294
// the keyboard)
292295
const endThreshold = this._frameOffset + this._width - this._autoScrollOffset;
293296

294-
if (pixelIndex >= endThreshold || pixelIndex < this._frameOffset) {
297+
let frameOffset = this._frameOffset;
298+
299+
if (pixelIndex >= endThreshold || pixelIndex < frameOffset) {
295300
// Put the playhead at 100 pixels from the left edge
296-
this._frameOffset = pixelIndex - this._autoScrollOffset;
301+
frameOffset = pixelIndex - this._autoScrollOffset;
297302

298-
if (this._frameOffset < 0) {
299-
this._frameOffset = 0;
303+
if (frameOffset < 0) {
304+
frameOffset = 0;
300305
}
301306

302-
this.updateWaveform(this._frameOffset);
307+
this.updateWaveform(frameOffset, false);
303308
}
304309
}
305310
};
@@ -387,9 +392,9 @@ WaveformZoomView.prototype.setZoom = function(options) {
387392

388393
const apexPixel = this.timeToPixels(apexTime);
389394

390-
this._frameOffset = apexPixel - playheadOffsetPixels;
395+
const frameOffset = apexPixel - playheadOffsetPixels;
391396

392-
this.updateWaveform(this._frameOffset);
397+
this.updateWaveform(frameOffset, true);
393398

394399
this._playheadLayer.zoomLevelChanged();
395400

@@ -453,7 +458,7 @@ WaveformZoomView.prototype.setStartTime = function(time) {
453458
time = 0;
454459
}
455460

456-
this.updateWaveform(this.timeToPixels(time));
461+
this.updateWaveform(this.timeToPixels(time), false);
457462
};
458463

459464
/**
@@ -483,7 +488,7 @@ WaveformZoomView.prototype.scrollWaveform = function(options) {
483488
throw new TypeError('view.scrollWaveform(): Missing umber of pixels or seconds');
484489
}
485490

486-
this.updateWaveform(this._frameOffset + scrollAmount);
491+
this.updateWaveform(this._frameOffset + scrollAmount, false);
487492
};
488493

489494
/**
@@ -492,7 +497,7 @@ WaveformZoomView.prototype.scrollWaveform = function(options) {
492497
* @param {Number} frameOffset The new frame offset, in pixels.
493498
*/
494499

495-
WaveformZoomView.prototype.updateWaveform = function(frameOffset) {
500+
WaveformZoomView.prototype.updateWaveform = function(frameOffset, forceUpdate) {
496501
let upperLimit;
497502

498503
if (this._pixelLength < this._width) {
@@ -507,6 +512,10 @@ WaveformZoomView.prototype.updateWaveform = function(frameOffset) {
507512

508513
frameOffset = clamp(frameOffset, 0, upperLimit);
509514

515+
if (!forceUpdate && frameOffset === this._frameOffset) {
516+
return;
517+
}
518+
510519
this._frameOffset = frameOffset;
511520

512521
// Display playhead if it is within the zoom frame width.
@@ -528,7 +537,10 @@ WaveformZoomView.prototype.updateWaveform = function(frameOffset) {
528537
this._segmentsLayer.updateSegments(frameStartTime, frameEndTime);
529538
}
530539

531-
this._peaks.emit('zoomview.displaying', frameStartTime, frameEndTime);
540+
this._peaks.emit('zoomview.update', {
541+
startTime: frameStartTime,
542+
endTime: frameEndTime
543+
});
532544
};
533545

534546
WaveformZoomView.prototype.enableAutoScroll = function(enable, options) {

src/zoom-controller.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,15 @@ ZoomController.prototype.setZoomLevels = function(zoomLevels) {
3232
*/
3333

3434
ZoomController.prototype.zoomIn = function() {
35-
this.setZoom(this._zoomLevelIndex - 1);
35+
this.setZoom(this._zoomLevelIndex - 1, false);
3636
};
3737

3838
/**
3939
* Zoom out one level.
4040
*/
4141

4242
ZoomController.prototype.zoomOut = function() {
43-
this.setZoom(this._zoomLevelIndex + 1);
43+
this.setZoom(this._zoomLevelIndex + 1, false);
4444
};
4545

4646
/**

test/api-zoom-spec.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,14 @@ describe('Peaks.zoom', function() {
4545
expect(p.zoom.getZoom()).to.equal(1);
4646
});
4747

48-
it('should emit a zoom.update event with the new zoom level index', function() {
48+
it('should emit a zoom.update event with the new zoom level', function() {
4949
const spy = sinon.spy();
5050

5151
p.on('zoom.update', spy);
5252
p.zoom.setZoom(1);
5353

54-
expect(spy).to.have.been.calledWith({ currentZoom: 1024, previousZoom: 512 });
54+
expect(spy.callCount).to.equal(1);
55+
expect(spy).calledWith({ currentZoom: 1024, previousZoom: 512 });
5556
});
5657

5758
it('should limit the zoom level index value to the minimum valid index', function() {
@@ -80,6 +81,7 @@ describe('Peaks.zoom', function() {
8081
p.on('zoom.update', spy);
8182
p.zoom.zoomOut();
8283

84+
expect(spy.callCount).to.equal(1);
8385
expect(spy).to.have.been.calledWith({ currentZoom: 1024, previousZoom: 512 });
8486
});
8587
});
@@ -93,7 +95,8 @@ describe('Peaks.zoom', function() {
9395
p.on('zoom.update', spy);
9496
p.zoom.zoomIn();
9597

96-
expect(spy).to.have.been.calledWith({ currentZoom: 512, previousZoom: 1024 });
98+
expect(spy.callCount).to.equal(1);
99+
expect(spy).calledWith({ currentZoom: 512, previousZoom: 1024 });
97100
});
98101
});
99102
});

0 commit comments

Comments
 (0)