Created
October 19, 2017 18:13
-
-
Save rksm/828888a0909933f083feb5c5d1e577c7 to your computer and use it in GitHub Desktop.
lively.next Morph>>onMouseWheel (stopping scroll in windows and such)
This file contains 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
onMouseWheel(evt) { | |
var scrollTarget = evt.targetMorphs.find(ea => ea.isClip()); | |
if (this !== scrollTarget) return; | |
var {deltaY, deltaX} = evt.domEvt, | |
magnX = Math.abs(deltaX), | |
magnY = Math.abs(deltaY); | |
// This dance here is to avoid "overscroll", i.e. you scroll a clip morph | |
// and it reaches it's boundary. Normally the clip morphs up the scene graph | |
// would now be scrolled, e.g. the world, moving your view away from the morph | |
// you are looking at. This is highly undesirable. | |
var kind = "both directions"; | |
if (magnX <= 2 && magnY <= 2) kind = "tiny"; | |
else if (magnY / magnX <= 0.2) kind = "horizontal"; | |
else if (magnX / magnY <= 0.2) kind = "vertical"; | |
if (kind === "tiny") return; | |
var {x: scrollX, y: scrollY} = this.scroll, | |
newScrollTop = deltaY + scrollY, | |
newScrollLeft = deltaX + scrollX, | |
newScrollBottom = newScrollTop + this.height, | |
newScrollRight = newScrollLeft + this.width, | |
newScrollX, newScrollY; | |
if (kind === "vertical" || kind === "both directions") { | |
if (newScrollBottom >= this.scrollExtent.y) newScrollY = this.scrollExtent.y-1; | |
else if (newScrollTop <= 0) newScrollY = 1; | |
if (newScrollY !== undefined) { | |
this.scroll = pt(scrollX, newScrollY); | |
evt.stop(); | |
} | |
} else if (kind === "horizontal" || kind === "both directions") { | |
if (newScrollRight >= this.scrollExtent.x) newScrollX = this.scrollExtent.x-1; | |
else if (newScrollLeft <= 0) newScrollX = 1; | |
if (newScrollX !== undefined) { | |
this.scroll = pt(newScrollX, scrollY); | |
evt.stop(); | |
} | |
} | |
// Here we install a debouncer for letting the renderer know when it is | |
// safe to update the DOM for scroll values. | |
// See MorphAfterRenderHook in rendering/morphic-default.js and | |
// https://github.com/LivelyKernel/lively.morphic/issues/88 | |
// for more info | |
if (!evt.state.scroll.interactiveScrollInProgress) { | |
var {promise: p, resolve} = promise.deferred(); | |
evt.state.scroll.interactiveScrollInProgress = p; | |
p.debounce = fun.debounce(250, () => { | |
evt.state.scroll.interactiveScrollInProgress = null; | |
resolve(); | |
}); | |
} | |
evt.state.scroll.interactiveScrollInProgress.debounce(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment