UNPKG

device-orientation-manager

Version:

A manager which will help you to control DeviceOrientation easily.

78 lines (77 loc) 2.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); /** * @File : LPF.ts * @Author : dtysky(dtysky@outlook.com) * @Date : 2018-6-22 14:38:29 * @Description: */ var AngleLPF = /** @class */ (function () { function AngleLPF() { this.smoothing = .2; this.maxSize = 10; this.buffer = []; this.defaultValue = []; this.defaultResult = []; } AngleLPF.prototype.init = function (size, values) { if (values === void 0) { values = []; } this.size = size; this.defaultValue = []; this.buffer = []; for (var i = 0; i < values.length; i += 1) { this.push(values[i]); } for (var i = 0; i < size; i += 1) { this.defaultValue.push([0, 1]); this.defaultResult.push(0); } return this.buffer; }; AngleLPF.prototype.next = function (nextValue) { var _this = this; var removed = this.push(nextValue); var sI = 1 - this.smoothing; var result = this.buffer.reduce(function (last, current) { return current.map(function (v, index) { return [ _this.smoothing * current[index][0] + sI * last[index][0], _this.smoothing * current[index][1] + sI * last[index][1] ]; }); }, removed); this.buffer[this.buffer.length - 1] = result; }; AngleLPF.prototype.reset = function (value) { this.buffer = []; this.push(value); }; Object.defineProperty(AngleLPF.prototype, "current", { get: function () { var length = this.buffer.length; if (length === 0) { return this.defaultResult; } return this.buffer[length - 1].map(function (point) { return Math.atan2(point[1], point[0]); }); }, enumerable: true, configurable: true }); Object.defineProperty(AngleLPF.prototype, "ready", { get: function () { return this.buffer.length === this.maxSize; }, enumerable: true, configurable: true }); AngleLPF.prototype.push = function (value) { if (value.length !== this.size) { throw new Error("Size of value must be " + this.size + " !"); } var removed = (this.buffer.length === this.maxSize) ? this.buffer.shift() : this.defaultValue; var tmp = []; for (var index = 0; index < this.size; index += 1) { tmp.push([Math.cos(value[index]), Math.sin(value[index])]); } this.buffer.push(tmp); return removed; }; return AngleLPF; }()); exports.default = AngleLPF;