@hscmap/magic-trackpad-detector
Advanced tools
Comparing version 0.0.0 to 0.0.1
export declare class MagicTrackpadDetector { | ||
private history; | ||
private T; | ||
private maxN; | ||
private N; | ||
tolerance: number; | ||
interval: number; | ||
minN: number; | ||
inertial(e: WheelEvent): boolean; | ||
} |
@@ -6,40 +6,22 @@ "use strict"; | ||
this.history = new RingBuffer(20); | ||
this.T = 160; | ||
this.maxN = Math.ceil(this.T / (1000 / 60)); | ||
this.N = Math.floor(0.8 * this.T / (1000 / 60)); | ||
this.tolerance = 7; // ms | ||
this.interval = 1000 / 60; // events per second | ||
this.minN = 5; | ||
} | ||
// private halfLife = 200 | ||
MagicTrackpadDetector.prototype.inertial = function (e) { | ||
var h = this.history; | ||
var t0 = performance.now(); | ||
var d0 = e.deltaY; | ||
this.history.push([t0, d0]); | ||
var samples = []; | ||
for (var i = 1; i <= this.history.length; ++i) { | ||
var _a = this.history.at(-i), t = _a[0], d = _a[1]; | ||
if (t0 - t > this.T || d * d0 < 0) | ||
break; | ||
samples.push([t, d]); | ||
} | ||
if (samples.length < this.N || samples.length > this.maxN) { | ||
// console.log({ N: samples.length }) | ||
h.push([t0, d0]); | ||
if (h.length < this.minN) | ||
return false; | ||
for (var i = this.minN; i > 1; --i) { | ||
var o = h.at(-i); | ||
var n = h.at(-i + 1); | ||
var dt = n[0] - o[0]; | ||
if (dt < this.interval - this.tolerance || this.interval + this.tolerance < dt) | ||
return false; | ||
if (n[1] * o[1] < 0 || n[1] / o[1] > 1) | ||
return false; | ||
} | ||
// const s1 = samples[samples.length - 1] | ||
// const s0 = samples[0] | ||
// const dt = s0[0] - s1[0] | ||
var ng = 0; | ||
for (var i = 0; i < samples.length - 1; ++i) { | ||
var dNew = samples[i][1]; | ||
var dOld = samples[i + 1][1]; | ||
if (dNew * dOld < 0 || dNew / dOld > 1) { | ||
ng++; | ||
continue; | ||
} | ||
} | ||
if (ng > 0) { | ||
// console.log({ ng }) | ||
return false; | ||
} | ||
// if (s0[1] / s1[1] <= 1.1 * Math.pow(0.5, dt / this.halfLife)) | ||
// return true | ||
return true; | ||
@@ -57,3 +39,3 @@ }; | ||
var j = (this.o + i + this.length) % this.xs.length; | ||
return j < this.xs.length ? this.xs[j] : undefined; | ||
return this.xs[j]; | ||
}; | ||
@@ -60,0 +42,0 @@ RingBuffer.prototype.push = function (x) { |
{ | ||
"name": "@hscmap/magic-trackpad-detector", | ||
"version": "0.0.0", | ||
"version": "0.0.1", | ||
"main": "./lib/index.js", | ||
@@ -5,0 +5,0 @@ "types": "./lib/index.d.ts", |
export class MagicTrackpadDetector { | ||
private history = new RingBuffer<[number, number]>(20) | ||
tolerance = 7 // ms | ||
interval = 1000 / 60 // events per second | ||
minN = 5 | ||
private T = 160 | ||
private maxN = Math.ceil(this.T / (1000 / 60)) | ||
private N = Math.floor(0.8 * this.T / (1000 / 60)) | ||
// private halfLife = 200 | ||
inertial(e: WheelEvent) { | ||
const h = this.history | ||
const t0 = performance.now() | ||
const d0 = e.deltaY | ||
this.history.push([t0, d0]) | ||
const samples: [number, number][] = [] | ||
for (let i = 1; i <= this.history.length; ++i) { | ||
const [t, d] = this.history.at(-i)! | ||
if (t0 - t > this.T || d * d0 < 0) | ||
break | ||
samples.push([t, d]) | ||
} | ||
if (samples.length < this.N || samples.length > this.maxN) { | ||
// console.log({ N: samples.length }) | ||
h.push([t0, d0]) | ||
if (h.length < this.minN) | ||
return false | ||
for (let i = this.minN; i > 1; --i) { | ||
const o = h.at(-i) | ||
const n = h.at(-i + 1) | ||
const dt = n[0] - o[0] | ||
if (dt < this.interval - this.tolerance || this.interval + this.tolerance < dt) | ||
return false | ||
if (n[1] * o[1] < 0 || n[1] / o[1] > 1) | ||
return false | ||
} | ||
// const s1 = samples[samples.length - 1] | ||
// const s0 = samples[0] | ||
// const dt = s0[0] - s1[0] | ||
let ng = 0 | ||
for (let i = 0; i < samples.length - 1; ++i) { | ||
const dNew = samples[i][1] | ||
const dOld = samples[i + 1][1] | ||
if (dNew * dOld < 0 || dNew / dOld > 1) { | ||
ng++ | ||
continue | ||
} | ||
} | ||
if (ng > 0) { | ||
// console.log({ ng }) | ||
return false | ||
} | ||
// if (s0[1] / s1[1] <= 1.1 * Math.pow(0.5, dt / this.halfLife)) | ||
// return true | ||
return true | ||
@@ -53,3 +27,2 @@ } | ||
class RingBuffer<T> { | ||
@@ -63,5 +36,5 @@ private o: number | ||
at(i: number): T | undefined { | ||
at(i: number): T { | ||
const j = (this.o + i + this.length) % this.xs.length | ||
return j < this.xs.length ? this.xs[j] : undefined | ||
return this.xs[j] | ||
} | ||
@@ -68,0 +41,0 @@ |
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
145194
225