20
20
import { BasePlugin , BasePluginEvents } from '../base-plugin.js'
21
21
22
22
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
24
25
}
25
26
const defaultOptions = {
26
- scale : 0.2 ,
27
+ scale : 0.5 ,
27
28
}
28
29
29
30
export type ZoomPluginEvents = BasePluginEvents
@@ -52,7 +53,7 @@ class ZoomPlugin extends BasePlugin<ZoomPluginEvents, ZoomPluginOptions> {
52
53
}
53
54
54
55
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 ) ) {
56
57
return
57
58
}
58
59
// prevent scrolling the sidebar while zooming
@@ -64,7 +65,7 @@ class ZoomPlugin extends BasePlugin<ZoomPluginEvents, ZoomPluginOptions> {
64
65
const width = this . container . clientWidth
65
66
const scrollX = this . wavesurfer . getScroll ( )
66
67
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 )
68
69
const newLeftSec = ( width / newMinPxPerSec ) * ( x / width )
69
70
if ( newMinPxPerSec * duration < width ) {
70
71
this . wavesurfer . zoom ( width / duration )
@@ -75,6 +76,11 @@ class ZoomPlugin extends BasePlugin<ZoomPluginEvents, ZoomPluginOptions> {
75
76
}
76
77
}
77
78
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
+
78
84
destroy ( ) {
79
85
if ( this . wrapper ) {
80
86
this . wrapper . removeEventListener ( 'wheel' , this . onWheel )
0 commit comments