Last active
June 30, 2025 19:08
-
-
Save Mwni/0faf2cc41032c30ad6d492998dee8174 to your computer and use it in GitHub Desktop.
This snippet provides a polyfill for MapKit JS for zooming using mousewheel on Chrome, Firefox & IE.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| This snippet provides a polyfill for zooming using mousewheel on Chrome, Firefox & IE. | |
| It adds the method 'enableCompatibilityZoom' on the mapkit.Map prototype. | |
| usage: | |
| map.enableCompatibilityZoom(smooth, delta) | |
| smooth: wether or not to use animated zooming | |
| delta: the amount of zoom per mousewheel step. Default is 100 (as in Chrome) | |
| */ | |
| (function(mk){ | |
| var hasNativeCustomEvent = typeof window.CustomEvent === 'function' | |
| function createSyntheticWheelEvent(){ | |
| if(hasNativeCustomEvent) | |
| return new CustomEvent('wheel') | |
| else{ | |
| var ev = document.createEvent( 'CustomEvent' ) | |
| ev.initCustomEvent('wheel', false, false, null) | |
| return ev | |
| } | |
| } | |
| function ht(){ | |
| return performance && performance.now ? performance.now() : Date.now() | |
| } | |
| function onMouseWheel(e){ | |
| if(e.synthetic || e.shiftKey || e.ctrlKey) | |
| return | |
| this.deltaY += -Math.max(-1,Math.min(1,e.deltaY))*this.factor | |
| this.originEvent = e | |
| if(this.smooth){ | |
| if(!this.animating){ | |
| this.last_frame = ht() | |
| this.animate() | |
| } | |
| }else{ | |
| this.dispatch() | |
| this.deltaY = 0 | |
| } | |
| } | |
| function animate(){ | |
| this.animating = true | |
| this.dispatch() | |
| var now = ht() | |
| var delta = now-this.last_frame | |
| this.deltaY *= Math.pow(0.98,delta) | |
| this.last_frame = now | |
| if(Math.abs(this.deltaY)>1) | |
| requestAnimationFrame(this.animate) | |
| else | |
| this.animating = false | |
| } | |
| function dispatch(){ | |
| var ev = createSyntheticWheelEvent() | |
| for(var key in this.originEvent){ | |
| ev[key] = this.originEvent[key] | |
| } | |
| ev.synthetic = true | |
| ev.shiftKey = true | |
| ev.deltaY = this.deltaY | |
| this.element.querySelector('.mk-map-node-element').dispatchEvent(ev) | |
| } | |
| mk.Map.prototype.enableCompatibilityZoom = function(smooth,delta){ | |
| if(navigator.platform.indexOf('Mac')!==-1) | |
| return | |
| delta = delta || 100 | |
| smooth = smooth || smooth===undefined | |
| var instance = { | |
| factor: delta, | |
| smooth: smooth, | |
| deltaY: 0, | |
| last_frame: 0, | |
| element: this.element | |
| } | |
| this.element.addEventListener('wheel',onMouseWheel.bind(instance)) | |
| instance.animate = animate.bind(instance) | |
| instance.dispatch = dispatch.bind(instance) | |
| } | |
| })(mapkit); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Now it is 2023, with latest mapkit Version 5.75.47, you can
map._allowWheelToZoom = true;