Skip to content

Instantly share code, notes, and snippets.

@Mwni
Last active February 6, 2023 20:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Mwni/0faf2cc41032c30ad6d492998dee8174 to your computer and use it in GitHub Desktop.
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 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);
@KaBooMa
Copy link

KaBooMa commented May 29, 2019

I was having issues with this however there is a property _allowWheelToZoom. Setting this to true enables the default shift zoom without holding shift. FYI

@Francismb
Copy link

Francismb commented Jan 14, 2020

After spending plenty of time trying to get scrolling working, I found the only good solution.
Downgrade to version v5.29.0 of MapKit and enable _allowWheelToZoom.

const map = new mapkit.Map('#mount-point');
map._allowWheelToZoom = true;

@MiaoziYu
Copy link

After spending plenty of time trying to get scrolling working, I found the only good solution.
Downgrade to version v5.29.0 of MapKit and enable _allowWheelToZoom.

const map = new mapkit.Map('#mount-point');
map._allowWheelToZoom = true;

it works!!! thank you!!!

@hoogw
Copy link

hoogw commented Feb 6, 2023

Now it is 2023, with latest mapkit Version 5.75.47, you can
map._allowWheelToZoom = true;

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment