@hscmap/magic-trackpad-detector
Advanced tools
Comparing version 0.0.2 to 0.1.0
export declare class MagicTrackpadDetector { | ||
private history; | ||
tolerance: number; | ||
interval: number; | ||
minN: number; | ||
minN1: number; | ||
minN2: number; | ||
private history; | ||
inertial(e: WheelEvent): boolean; | ||
} |
@@ -5,6 +5,7 @@ "use strict"; | ||
function MagicTrackpadDetector() { | ||
this.history = new RingBuffer(20); | ||
this.tolerance = 7; // ms | ||
this.interval = 1000 / 60; // events per second | ||
this.minN = 5; | ||
this.minN1 = 7; | ||
this.minN2 = 15; | ||
this.history = new RingBuffer(Math.max(this.minN1, this.minN2)); | ||
} | ||
@@ -16,13 +17,32 @@ MagicTrackpadDetector.prototype.inertial = function (e) { | ||
h.push([t0, d0]); | ||
if (h.length < this.minN) | ||
if (h.length < this.minN1) | ||
return false; | ||
for (var i = this.minN - 1; i > 1; --i) { | ||
var o = h.at(-i); | ||
var n = h.at(-i + 1); | ||
var dt = n[0] - o[0]; | ||
var dv = n[1] - o[1]; | ||
if (dv != 0 && (dt < this.interval - this.tolerance || this.interval + this.tolerance < dt)) | ||
if (Math.abs(e.deltaY) > 1) { | ||
for (var i = this.minN1 - 1; 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; | ||
} | ||
} | ||
else { | ||
if (h.length < this.minN2) | ||
return false; | ||
if (n[1] * o[1] < 0 || n[1] / o[1] > 1) | ||
var _a = h.at(-this.minN2), to = _a[0], vo = _a[1]; | ||
if (Math.abs(vo) <= 1 || t0 - to > 2 * this.interval * this.minN2) { | ||
return false; | ||
} | ||
for (var i = this.minN2 - 1; i > 1; --i) { | ||
var o = h.at(-i); | ||
var n = h.at(-i + 1); | ||
var dt = n[0] - o[0]; | ||
if (!(this.interval - this.tolerance <= dt && dt <= this.interval + this.tolerance || | ||
2 * this.interval - this.tolerance <= dt && dt <= 2 * this.interval + this.tolerance)) | ||
return false; | ||
if (n[1] * o[1] < 0 || n[1] / o[1] > 1) | ||
return false; | ||
} | ||
} | ||
@@ -29,0 +49,0 @@ return true; |
{ | ||
"name": "@hscmap/magic-trackpad-detector", | ||
"version": "0.0.2", | ||
"version": "0.1.0", | ||
"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 | ||
minN1 = 7 | ||
minN2 = 15 | ||
private history = new RingBuffer<[number, number]>(Math.max(this.minN1, this.minN2)) | ||
@@ -12,13 +13,38 @@ inertial(e: WheelEvent) { | ||
h.push([t0, d0]) | ||
if (h.length < this.minN) | ||
if (h.length < this.minN1) | ||
return false | ||
for (let i = this.minN - 1; i > 1; --i) { | ||
const o = h.at(-i) | ||
const n = h.at(-i + 1) | ||
const dt = n[0] - o[0] | ||
const dv = n[1] - o[1] | ||
if (dv != 0 && (dt < this.interval - this.tolerance || this.interval + this.tolerance < dt)) | ||
if (Math.abs(e.deltaY) > 1) { | ||
for (let i = this.minN1 - 1; 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 | ||
} | ||
} | ||
else { | ||
if (h.length < this.minN2) | ||
return false | ||
if (n[1] * o[1] < 0 || n[1] / o[1] > 1) | ||
const [to, vo] = h.at(- this.minN2) | ||
if (Math.abs(vo) <= 1 || t0 - to > 2 * this.interval * this.minN2) { | ||
return false | ||
} | ||
for (let i = this.minN2 - 1; i > 1; --i) { | ||
const o = h.at(-i) | ||
const n = h.at(-i + 1) | ||
const dt = n[0] - o[0] | ||
if ( | ||
!( | ||
this.interval - this.tolerance <= dt && dt <= this.interval + this.tolerance || | ||
2 * this.interval - this.tolerance <= dt && dt <= 2 * this.interval + this.tolerance | ||
) | ||
) | ||
return false | ||
if (n[1] * o[1] < 0 || n[1] / o[1] > 1) | ||
return false | ||
} | ||
} | ||
@@ -25,0 +51,0 @@ return true |
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
147159
272