hunched-ego
Version:
A React Native library for posture-based screen dimming
86 lines • 3.91 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || (function () {
var ownKeys = function(o) {
ownKeys = Object.getOwnPropertyNames || function (o) {
var ar = [];
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
return ar;
};
return ownKeys(o);
};
return function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
__setModuleDefault(result, mod);
return result;
};
})();
Object.defineProperty(exports, "__esModule", { value: true });
const react_1 = __importStar(require("react"));
const react_native_1 = require("react-native");
const react_native_sensors_1 = require("react-native-sensors");
const operators_1 = require("rxjs/operators");
const postureUtils_1 = require("./postureUtils");
const PostureOverlay = ({ goodAngleThreshold = 30, badAngleThreshold = 60, maxOpacity = 1, overlayColor = '#000', }) => {
const [opacity] = (0, react_1.useState)(new react_native_1.Animated.Value(0));
(0, react_1.useEffect)(() => {
let latestAngle = 0;
let currentTargetOpacity = 0; // Track the last target to avoid redundant animations
const subscription = react_native_sensors_1.accelerometer
.pipe((0, operators_1.map)(({ x, y, z }) => (0, postureUtils_1.getZAxisAngle)(x, y, z)))
.subscribe((angle) => {
latestAngle = angle;
});
const interval = setInterval(() => {
let targetOpacity = 0;
if (latestAngle >= goodAngleThreshold) {
targetOpacity = 0;
}
else if (latestAngle <= badAngleThreshold) {
targetOpacity = maxOpacity;
}
else {
const ratio = (goodAngleThreshold - latestAngle) / (goodAngleThreshold - badAngleThreshold);
targetOpacity = ratio * maxOpacity;
}
// Only animate if the target opacity changes
if (targetOpacity !== currentTargetOpacity) {
currentTargetOpacity = targetOpacity;
react_native_1.Animated.spring(opacity, {
toValue: targetOpacity,
tension: 8, // Controls stiffness
friction: 4, // Controls bounciness / damping
useNativeDriver: true,
}).start();
}
console.log(`Angle: ${latestAngle}, Target Opacity: ${targetOpacity}`);
}, 250); // Reduced interval for smoother updates
return () => {
subscription.unsubscribe();
clearInterval(interval);
};
}, [goodAngleThreshold, badAngleThreshold, maxOpacity]);
return (react_1.default.createElement(react_native_1.Animated.View, { style: [
react_native_1.StyleSheet.absoluteFill,
{ backgroundColor: overlayColor, opacity, zIndex: 9999 },
], pointerEvents: "none" }));
};
exports.default = PostureOverlay;
//# sourceMappingURL=PostureOverlay.js.map