Skip to content

Commit a5b15fc

Browse files
authored
Zoom plugin improvements (#3388)
* Fix pinch to zoom * Remove code to set scroll * Change zoom calculation * Allow zooming with 0 minPxPerSec * Increase default scale * Add maxZoom * Revert "Remove code to set scroll"
1 parent bd5c231 commit a5b15fc

File tree

2 files changed

+14
-6
lines changed

2 files changed

+14
-6
lines changed

examples/zoom-plugin.js

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,10 @@ const wavesurfer = WaveSurfer.create({
1919
// Initialize the Zoom plugin
2020
wavesurfer.registerPlugin(
2121
ZoomPlugin.create({
22-
// the amount of zoom per wheel step, e.g. 0.1 means a 10% magnification per scroll
23-
scale: 0.2,
22+
// the amount of zoom per wheel step, e.g. 0.5 means a 50% magnification per scroll
23+
scale: 0.5,
24+
// Optionally, specify the maximum pixels-per-second factor while zooming
25+
maxZoom: 100,
2426
}),
2527
)
2628

src/plugins/zoom.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,11 @@
2020
import { BasePlugin, BasePluginEvents } from '../base-plugin.js'
2121

2222
export type ZoomPluginOptions = {
23-
scale?: number // the amount of zoom per wheel step, e.g. 0.1 means a 10% magnification per scroll
23+
scale?: number // the amount of zoom per wheel step, e.g. 0.5 means a 50% magnification per scroll
24+
maxZoom?: number // the maximum pixels-per-second factor while zooming
2425
}
2526
const defaultOptions = {
26-
scale: 0.2,
27+
scale: 0.5,
2728
}
2829

2930
export type ZoomPluginEvents = BasePluginEvents
@@ -52,7 +53,7 @@ class ZoomPlugin extends BasePlugin<ZoomPluginEvents, ZoomPluginOptions> {
5253
}
5354

5455
private onWheel = (e: WheelEvent) => {
55-
if (!this.wavesurfer?.options.minPxPerSec || !this.container) {
56+
if (!this.wavesurfer || !this.container || Math.abs(e.deltaX) >= Math.abs(e.deltaY)) {
5657
return
5758
}
5859
// prevent scrolling the sidebar while zooming
@@ -64,7 +65,7 @@ class ZoomPlugin extends BasePlugin<ZoomPluginEvents, ZoomPluginOptions> {
6465
const width = this.container.clientWidth
6566
const scrollX = this.wavesurfer.getScroll()
6667
const pointerTime = (scrollX + x) / oldMinPxPerSec
67-
const newMinPxPerSec = oldMinPxPerSec * (e.deltaY > 0 ? 1 - this.options.scale : 1 + this.options.scale)
68+
const newMinPxPerSec = this.calculateNewZoom(oldMinPxPerSec, -e.deltaY)
6869
const newLeftSec = (width / newMinPxPerSec) * (x / width)
6970
if (newMinPxPerSec * duration < width) {
7071
this.wavesurfer.zoom(width / duration)
@@ -75,6 +76,11 @@ class ZoomPlugin extends BasePlugin<ZoomPluginEvents, ZoomPluginOptions> {
7576
}
7677
}
7778

79+
private calculateNewZoom = (oldZoom: number, delta: number) => {
80+
const newZoom = Math.max(0, oldZoom + delta * this.options.scale)
81+
return typeof this.options.maxZoom === 'undefined' ? newZoom : Math.min(newZoom, this.options.maxZoom)
82+
}
83+
7884
destroy() {
7985
if (this.wrapper) {
8086
this.wrapper.removeEventListener('wheel', this.onWheel)

0 commit comments

Comments
 (0)