@hscmap/inertial-wheel
Advanced tools
Comparing version 0.4.0 to 0.5.0
@@ -9,3 +9,3 @@ import { InertialWheel } from "../src"; | ||
const iw = new InertialWheel(trackpad, { | ||
wheel: e => inertialWheelEventHistory.push(e.v), | ||
wheel: e => inertialWheelEventHistory.push(e.v * 75), | ||
wheelstart: e => inertialWheelEventHistory.canvas.classList.add('active'), | ||
@@ -22,2 +22,11 @@ wheelend: e => inertialWheelEventHistory.canvas.classList.remove('active'), | ||
document.addEventListener('change', e => { | ||
const target = e.target as HTMLInputElement | ||
if (target.matches('[name="accDuration"]')) | ||
iw.accDuration = Number(target.value) | ||
if (target.matches('[name="t1"]')) | ||
iw.t1 = Number(target.value) | ||
if (target.matches('[name="t2"]')) | ||
iw.t2 = Number(target.value) | ||
}) | ||
}) | ||
@@ -24,0 +33,0 @@ |
@@ -9,2 +9,7 @@ export interface Callbacks { | ||
private cb; | ||
static defaults: { | ||
accDuration: number; | ||
t1: number; | ||
t2: number; | ||
}; | ||
private a; | ||
@@ -14,15 +19,7 @@ private v; | ||
private lastA; | ||
private lifeA; | ||
private beta; | ||
private tau; | ||
private v0; | ||
private eta; | ||
private clientRect; | ||
accDuration: number; | ||
t1: number; | ||
t2: number; | ||
constructor(target: HTMLElement, cb: Callbacks); | ||
private readonly t0; | ||
setParams(override: Partial<{ | ||
beta: number; | ||
t0: number; | ||
v0: number; | ||
}>): void; | ||
teardown(): void; | ||
@@ -29,0 +26,0 @@ stop(): void; |
"use strict"; | ||
var __assign = (this && this.__assign) || Object.assign || function(t) { | ||
for (var s, i = 1, n = arguments.length; i < n; i++) { | ||
s = arguments[i]; | ||
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) | ||
t[p] = s[p]; | ||
} | ||
return t; | ||
}; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
var magic_trackpad_detector_1 = require("@hscmap/magic-trackpad-detector"); | ||
var defaults = { | ||
accDuration: 100, | ||
t1: 100, | ||
t2: 200, | ||
}; | ||
var InertialWheel = (function () { | ||
@@ -18,3 +15,5 @@ function InertialWheel(target, cb) { | ||
this.v = 0; | ||
this.lifeA = 35; | ||
this.accDuration = defaults.accDuration; | ||
this.t1 = defaults.t1; | ||
this.t2 = defaults.t2; | ||
this.magicTrackpadDetector = new magic_trackpad_detector_1.MagicTrackpadDetector(); | ||
@@ -38,22 +37,3 @@ this.wheel = function (e) { | ||
this.clientRect = target.getBoundingClientRect(); | ||
this.setParams({ | ||
beta: 1.e-1, | ||
t0: 25, | ||
v0: 100 | ||
}); | ||
} | ||
Object.defineProperty(InertialWheel.prototype, "t0", { | ||
get: function () { | ||
return this.tau / this.beta; | ||
}, | ||
enumerable: true, | ||
configurable: true | ||
}); | ||
InertialWheel.prototype.setParams = function (override) { | ||
var p = __assign({ beta: this.beta, t0: this.t0, v0: this.v0 }, override); | ||
this.beta = p.beta; | ||
this.tau = p.t0 * this.beta; | ||
this.v0 = p.v0; | ||
this.eta = this.beta / this.tau; | ||
}; | ||
InertialWheel.prototype.teardown = function () { | ||
@@ -81,6 +61,9 @@ this.target.removeEventListener('wheel', this.wheel); | ||
} | ||
if (now - this.lastA > this.lifeA) | ||
if (now - this.lastA > this.accDuration) | ||
this.a = 0; | ||
var dt = this.beta * (now - this.lastT); | ||
this.v += dt * this.eta * (this.v0 * this.a - this.v); | ||
var dt = now - this.lastT; | ||
this.v += dt * (this.a == 0 ? | ||
-1. / this.t2 * this.v : | ||
this.a / this.t1); | ||
this.v = clamp(this.v, -1, 1); | ||
this.cb.wheel && this.cb.wheel(new InertialWheelEvent(this.v, this.lastNativeEvent)); | ||
@@ -90,3 +73,3 @@ this.lastT = now; | ||
InertialWheel.prototype.moving = function (now) { | ||
return now - this.lastA <= this.lifeA || Math.abs(this.v) >= 0.005 * this.v0; | ||
return now - this.lastA <= this.accDuration || Math.abs(this.v) >= 0.005; | ||
}; | ||
@@ -97,2 +80,3 @@ InertialWheel.prototype.nextTick = function () { | ||
}; | ||
InertialWheel.defaults = defaults; | ||
return InertialWheel; | ||
@@ -121,1 +105,4 @@ }()); | ||
exports.InertialWheelEndEvent = InertialWheelEndEvent; | ||
function clamp(x, min, max) { | ||
return x < min ? min : (x > max ? max : x); | ||
} |
{ | ||
"name": "@hscmap/inertial-wheel", | ||
"version": "0.4.0", | ||
"version": "0.5.0", | ||
"main": "./lib/index.js", | ||
@@ -22,4 +22,4 @@ "types": "./lib/index.d.ts", | ||
"dependencies": { | ||
"@hscmap/magic-trackpad-detector": "0.0.0" | ||
"@hscmap/magic-trackpad-detector": "^0.1.0" | ||
} | ||
} |
import { MagicTrackpadDetector } from "@hscmap/magic-trackpad-detector" | ||
export interface Callbacks { | ||
@@ -9,4 +10,11 @@ wheel?: (e: InertialWheelEvent) => void | ||
const defaults = { | ||
accDuration: 100, | ||
t1: 100, | ||
t2: 200, | ||
} | ||
export class InertialWheel { | ||
static defaults = defaults | ||
private a: number // 0 | 1 | -1 | ||
@@ -16,31 +24,13 @@ private v = 0 | ||
private lastA: number | ||
private lifeA = 35 | ||
private beta: number | ||
private tau: number | ||
private v0: number | ||
private eta: number | ||
private clientRect: ClientRect | ||
accDuration = defaults.accDuration | ||
t1 = defaults.t1 | ||
t2 = defaults.t2 | ||
constructor(readonly target: HTMLElement, private cb: Callbacks) { | ||
target.addEventListener('wheel', this.wheel) | ||
this.clientRect = target.getBoundingClientRect() | ||
this.setParams({ | ||
beta: 1.e-1, | ||
t0: 25, | ||
v0: 100 | ||
}) | ||
} | ||
private get t0() { | ||
return this.tau / this.beta | ||
} | ||
setParams(override: Partial<{ beta: number, t0: number, v0: number }>) { | ||
const p = { beta: this.beta, t0: this.t0, v0: this.v0, ...override } | ||
this.beta = p.beta | ||
this.tau = p.t0 * this.beta | ||
this.v0 = p.v0 | ||
this.eta = this.beta / this.tau | ||
} | ||
teardown() { | ||
@@ -92,7 +82,14 @@ this.target.removeEventListener('wheel', this.wheel) | ||
if (now - this.lastA > this.lifeA) | ||
if (now - this.lastA > this.accDuration) | ||
this.a = 0 | ||
const dt = this.beta * (now - this.lastT) | ||
this.v += dt * this.eta * (this.v0 * this.a - this.v) | ||
const dt = now - this.lastT | ||
this.v += dt * ( | ||
this.a == 0 ? | ||
- 1. / this.t2 * this.v : | ||
this.a / this.t1 | ||
) | ||
this.v = clamp(this.v, -1, 1) | ||
this.cb.wheel && this.cb.wheel(new InertialWheelEvent(this.v, this.lastNativeEvent)) | ||
@@ -104,3 +101,3 @@ | ||
private moving(now: number) { | ||
return now - this.lastA <= this.lifeA || Math.abs(this.v) >= 0.005 * this.v0 | ||
return now - this.lastA <= this.accDuration || Math.abs(this.v) >= 0.005 | ||
} | ||
@@ -116,2 +113,7 @@ | ||
export class InertialWheelStartEvent { } | ||
export class InertialWheelEndEvent { } | ||
export class InertialWheelEndEvent { } | ||
function clamp(x: number, min: number, max: number) { | ||
return x < min ? min : (x > max ? max : x) | ||
} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
16424
332
+ Added@hscmap/magic-trackpad-detector@0.1.0(transitive)
- Removed@hscmap/magic-trackpad-detector@0.0.0(transitive)