Last active
December 21, 2015 15:09
-
-
Save trxcllnt/6324672 to your computer and use it in GitHub Desktop.
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
function initConcatFriction(Rx) { | |
Rx.Observable.prototype.concatFriction = function (u, xS, yS, vS, aS) { | |
if (typeof xS !== 'function') | |
xS = function (m) { return m.x; }; | |
if (typeof yS !== 'function') | |
yS = function (m) { return m.y; }; | |
if (typeof vS !== 'function') | |
vS = function (m) { return m.velocity; }; | |
if (typeof aS !== 'function') | |
aS = function (m) { return m.angle; }; | |
var latest = null; | |
return this | |
.doAction(saveLatest) | |
.concat(Rx.Observable.defer(fakeDrags)); | |
function saveLatest(val) { latest = val; }; | |
function fakeDrags() { | |
if (latest == null) return Rx.Observable.empty(); | |
var deceleration = Math.abs(u * 9.8); | |
return Rx.Observable | |
.timer(0, 16) | |
.scan(latest, decelerate) | |
.takeWhile(hasVelocity); | |
function decelerate(memo) { | |
var v = vS(memo), | |
a = aS(memo), | |
vx = v * Math.cos(a), | |
vy = v * Math.sin(a), | |
x = xS(memo) + vx, | |
y = yS(memo) + vy; | |
v -= deceleration; | |
return { x: x, y: y, angle: a, velocity: v }; | |
} | |
function hasVelocity(drag) { | |
return drag.velocity > 0; | |
} | |
} | |
} | |
} |
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
function initScanVelocity(Rx) { | |
Rx.Observable.prototype.scanVelocity = function(xS, yS) { | |
if (typeof xS !== 'function') | |
xS = function(m) { return m.x; }; | |
if (typeof yS !== 'function') | |
yS = function(m) { return m.y; }; | |
return this | |
.timeInterval() | |
.scan(function(memo, latest) { | |
var event = latest.value, | |
interval = latest.interval, | |
mx = xS(memo.value), | |
my = yS(memo.value), | |
ex = xS(event), | |
ey = yS(event), | |
vx = (ex - mx) / interval, | |
vy = (ey - my) / interval; | |
event.velocity = Math.abs( | |
Math.sqrt((ex * ex) + (ey * ey)) - | |
Math.sqrt((mx * mx) + (my * my)) | |
); | |
event.angle = Math.atan2(vy, vx); | |
return latest; | |
}) | |
.skip(1) | |
.select(function(wrapper) { | |
return wrapper.value; | |
}); | |
} | |
} |
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
$('.dragMe') | |
.dragAsObservable() | |
.scanVelocity() | |
.concatFriction(0.45) | |
.repeat() | |
.subscribe(function(drag) { | |
$('.dragMe').css({ | |
top: drag.global.x, | |
left: drag.global.y | |
}); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment