magichome-platform
Version:
discover, control, and receive status for magichome devices
284 lines (283 loc) • 13.2 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.calculateSequenceSteps = exports.recursiveArrayToInt = exports.InterpolationType = exports.interpolate = void 0;
function interpolate(start, end, current, total, type) {
if (type === InterpolationType.LINEAR) {
return start + (end - start) * current / total;
}
else if (type === 'easeIn') {
return start + (end - start) * Math.pow(current / total, 2);
}
else if (type === 'easeOut') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 2));
}
else if (type === 'easeInOut') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 2)) / 2;
}
else if (type === 'easeOutIn') {
return start + (end - start) * Math.pow(current / total, 2) / 2;
}
else if (type === 'easeInCubic') {
return start + (end - start) * Math.pow(current / total, 3);
}
else if (type === 'easeOutCubic') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 3));
}
else if (type === 'easeInOutCubic') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 3)) / 2;
}
else if (type === 'easeOutInCubic') {
return start + (end - start) * Math.pow(current / total, 3) / 2;
}
else if (type === 'easeInQuart') {
return start + (end - start) * Math.pow(current / total, 4);
}
else if (type === 'easeOutQuart') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 4));
}
else if (type === 'easeInOutQuart') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 4)) / 2;
}
else if (type === 'easeOutInQuart') {
return start + (end - start) * Math.pow(current / total, 4) / 2;
}
else if (type === 'easeInQuint') {
return start + (end - start) * Math.pow(current / total, 5);
}
else if (type === 'easeOutQuint') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 5));
}
else if (type === 'easeInOutQuint') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 5)) / 2;
}
else if (type === 'easeOutInQuint') {
return start + (end - start) * Math.pow(current / total, 5) / 2;
}
else if (type === 'easeInSine') {
return start + (end - start) * (1 - Math.cos(current / total * (Math.PI / 2)));
}
else if (type === 'easeOutSine') {
return start + (end - start) * Math.sin(current / total * (Math.PI / 2));
}
else if (type === 'easeInOutSine') {
return start + (end - start) * (1 - Math.cos(current / total * Math.PI)) / 2;
}
else if (type === 'easeOutInSine') {
return start + (end - start) * Math.sin(current / total * Math.PI) / 2;
}
else if (type === 'easeInExpo') {
return start + (end - start) * Math.pow(2, 10 * (current / total - 1));
}
else if (type === 'easeOutExpo') {
return start + (end - start) * (1 - Math.pow(2, -10 * current / total));
}
else if (type === 'easeInOutExpo') {
return start + (end - start) * (1 - Math.pow(2, -10 * current / total)) / 2;
}
else if (type === 'easeOutInExpo') {
return start + (end - start) * Math.pow(2, 10 * (current / total - 1)) / 2;
}
else if (type === 'easeInCirc') {
return start + (end - start) * (1 - Math.sqrt(1 - Math.pow(current / total, 2)));
}
else if (type === 'easeOutCirc') {
return start + (end - start) * Math.sqrt(1 - Math.pow(current / total - 1, 2));
}
else if (type === 'easeInOutCirc') {
return start + (end - start) * (1 - Math.sqrt(1 - Math.pow(current / total, 2))) / 2;
}
else if (type === 'easeOutInCirc') {
return start + (end - start) * Math.sqrt(1 - Math.pow(current / total - 1, 2)) / 2;
}
else if (type === 'easeInBack') {
return start + (end - start) * (current / total) * (current / total) * ((1.70158 + 1) * current / total - 1.70158);
}
else if (type === 'easeOutBack') {
return start + (end - start) * ((current = current / total - 1) * current * ((1.70158 + 1) * current + 1.70158) + 1);
}
else if (type === 'easeInOutBack') {
return start + (end - start) * (1 - Math.pow(1 - current / total, 2)) / 2;
}
else if (type === 'easeOutInBack') {
return start + (end - start) * Math.pow(current / total, 2) / 2;
}
else if (type === 'easeInElastic') {
return start + (end - start) * Math.pow(2, 10 * (current / total - 1)) * Math.cos((current / total - 1.075) * (2 * Math.PI) / 0.3);
}
else if (type === 'easeOutElastic') {
return start + (end - start) * (1 - Math.pow(2, -10 * current / total)) * Math.cos((current / total - 0.075) * (2 * Math.PI) / 0.3) + 1;
}
else if (type === 'easeInOutElastic') {
return start + (end - start) * (1 - Math.pow(2, -10 * current / total)) * Math.cos((current / total - 0.075) * (2 * Math.PI) / 0.3) / 2;
}
else if (type === 'easeOutInElastic') {
return start + (end - start) * Math.pow(2, 10 * (current / total - 1)) * Math.cos((current / total - 1.075) * (2 * Math.PI) / 0.3) / 2;
}
else if (type === 'easeInBounce') {
return start + (end - start) - easeOutBounce(end - start, total - current, end - start, total);
}
else if (type === 'easeOutBounce') {
if ((current /= total) < (1 / 2.75)) {
return start + (end - start) * (7.5625 * current * current);
}
else if (current < (2 / 2.75)) {
return start + (end - start) * (7.5625 * (current -= (1.5 / 2.75)) * current + 0.75);
}
else if (current < (2.5 / 2.75)) {
return start + (end - start) * (7.5625 * (current -= (2.25 / 2.75)) * current + 0.9375);
}
else {
return start + (end - start) * (7.5625 * (current -= (2.625 / 2.75)) * current + 0.984375);
}
}
else if (type === 'easeInOutBounce') {
if (current < total / 2) {
return easeInBounce(start, current * 2, end - start, total) * 0.5 + start;
}
else {
return easeOutBounce(start + (end - start) / 2, current * 2 - total, end - start, total) * 0.5 + start + (end - start) / 2;
}
}
else if (type === 'easeOutInBounce') {
if (current < total / 2) {
return easeOutBounce(start, current * 2, end - start, total) * 0.5 + start;
}
else {
return easeInBounce(start + (end - start) / 2, current * 2 - total, end - start, total) * 0.5 + start + (end - start) / 2;
}
}
else {
return start + (end - start) * current / total;
}
}
exports.interpolate = interpolate;
function easeOutBounce(start, current, end, total) {
if ((current /= total) < (1 / 2.75)) {
return start + (end - start) * (7.5625 * current * current);
}
else if (current < (2 / 2.75)) {
return start + (end - start) * (7.5625 * (current -= (1.5 / 2.75)) * current + 0.75);
}
else if (current < (2.5 / 2.75)) {
return start + (end - start) * (7.5625 * (current -= (2.25 / 2.75)) * current + 0.9375);
}
else {
return start + (end - start) * (7.5625 * (current -= (2.625 / 2.75)) * current + 0.984375);
}
}
function easeInBounce(start, current, end, total) {
return start + (end - start) - easeOutBounce(end - start, total - current, end - start, total);
}
var InterpolationType;
(function (InterpolationType) {
InterpolationType["LINEAR"] = "linear";
InterpolationType["EASE_IN"] = "easeIn";
InterpolationType["EASE_OUT"] = "easeOut";
InterpolationType["EASE_IN_OUT"] = "easeInOut";
InterpolationType["EASE_OUT_IN"] = "easeOutIn";
InterpolationType["EASE_IN_SINE"] = "easeInSine";
InterpolationType["EASE_OUT_SINE"] = "easeOutSine";
InterpolationType["EASE_IN_OUT_SINE"] = "easeInOutSine";
InterpolationType["EASE_OUT_IN_SINE"] = "easeOutInSine";
InterpolationType["EASE_IN_QUAD"] = "easeInQuad";
InterpolationType["EASE_OUT_QUAD"] = "easeOutQuad";
InterpolationType["EASE_IN_OUT_QUAD"] = "easeInOutQuad";
InterpolationType["EASE_OUT_IN_QUAD"] = "easeOutInQuad";
InterpolationType["EASE_IN_CUBIC"] = "easeInCubic";
InterpolationType["EASE_OUT_CUBIC"] = "easeOutCubic";
InterpolationType["EASE_IN_OUT_CUBIC"] = "easeInOutCubic";
InterpolationType["EASE_OUT_IN_CUBIC"] = "easeOutInCubic";
InterpolationType["EASE_IN_QUART"] = "easeInQuart";
InterpolationType["EASE_OUT_QUART"] = "easeOutQuart";
InterpolationType["EASE_IN_OUT_QUART"] = "easeInOutQuart";
InterpolationType["EASE_OUT_IN_QUART"] = "easeOutInQuart";
InterpolationType["EASE_IN_QUINT"] = "easeInQuint";
InterpolationType["EASE_OUT_QUINT"] = "easeOutQuint";
InterpolationType["EASE_IN_OUT_QUINT"] = "easeInOutQuint";
InterpolationType["EASE_OUT_IN_QUINT"] = "easeOutInQuint";
InterpolationType["EASE_IN_EXPO"] = "easeInExpo";
InterpolationType["EASE_OUT_EXPO"] = "easeOutExpo";
InterpolationType["EASE_IN_OUT_EXPO"] = "easeInOutExpo";
InterpolationType["EASE_OUT_IN_EXPO"] = "easeOutInExpo";
InterpolationType["EASE_IN_CIRC"] = "easeInCirc";
InterpolationType["EASE_OUT_CIRC"] = "easeOutCirc";
InterpolationType["EASE_IN_OUT_CIRC"] = "easeInOutCirc";
InterpolationType["EASE_OUT_IN_CIRC"] = "easeOutInCirc";
InterpolationType["EASE_IN_BACK"] = "easeInBack";
InterpolationType["EASE_OUT_BACK"] = "easeOutBack";
InterpolationType["EASE_IN_OUT_BACK"] = "easeInOutBack";
InterpolationType["EASE_OUT_IN_BACK"] = "easeOutInBack";
InterpolationType["EASE_IN_ELASTIC"] = "easeInElastic";
InterpolationType["EASE_OUT_ELASTIC"] = "easeOutElastic";
InterpolationType["EASE_IN_OUT_ELASTIC"] = "easeInOutElastic";
InterpolationType["EASE_OUT_IN_ELASTIC"] = "easeOutInElastic";
InterpolationType["EASE_IN_BOUNCE"] = "easeInBounce";
InterpolationType["EASE_OUT_BOUNCE"] = "easeOutBounce";
InterpolationType["EASE_IN_OUT_BOUNCE"] = "easeInOutBounce";
InterpolationType["EASE_OUT_IN_BOUNCE"] = "easeOutInBounce";
})(InterpolationType = exports.InterpolationType || (exports.InterpolationType = {}));
//@ts-ignore
function recursiveArrayToInt(objOne, objTarget = {}) {
for (let k in objOne) {
const element = objOne[k];
if (typeof element == "object" && element !== null && !Array.isArray(element)) {
objTarget[k] = {};
//@ts-ignore
objTarget[k] = recursiveArrayToInt(objOne[k], objTarget[k]);
//@ts-ignore
}
else if (Array.isArray(objOne[k])) {
//@ts-ignore
objTarget[k] = Math.round(Math.random() * (objOne[k][1] - objOne[k][0]) + objOne[k][0]);
}
else {
//@ts-ignore
objTarget[k] = objOne[k];
}
}
return objTarget;
}
exports.recursiveArrayToInt = recursiveArrayToInt;
function calculateSequenceSteps(sequence, STEP_INTERVAL_MS) {
const animationSteps = [];
const transitionFrames = sequence.transitionDurationMS / STEP_INTERVAL_MS;
sequence.durationAtTargetMS = sequence.durationAtTargetMS || 0;
//@ts-ignore
const reverseTransitionFrames = sequence.reverseTransitionDurationMS / STEP_INTERVAL_MS;
//calculate transition delta
const transitionDelta = {};
for (let color in sequence.targetColor) {
//@ts-ignore
transitionDelta[color] = (sequence.targetColor[color] - sequence.startColor[color]) / transitionFrames;
}
//calculate and add transition frames between start and target color
for (let i = 0; i < transitionFrames; i++) {
const step = {};
for (let color in sequence.targetColor) {
//@ts-ignore
step[color] = interpolate(sequence.startColor[color], sequence.targetColor[color], i, transitionFrames, sequence.interpolationType);
}
animationSteps.push(step);
}
//calculate and add hold frames for target color
let durationFrames = sequence.durationAtTargetMS / STEP_INTERVAL_MS;
for (let i = 0; i < durationFrames; i++) {
const step = {};
for (let color in sequence.targetColor) {
//@ts-ignore
step[color] = sequence.targetColor[color];
}
animationSteps.push(step);
}
// calculate and add transition frames between target and start color
for (let i = 0; i < reverseTransitionFrames; i++) {
const step = {};
for (let color in sequence.targetColor) {
//@ts-ignore
step[color] = interpolate(sequence.targetColor[color], sequence.startColor[color], i, reverseTransitionFrames, sequence.interpolationType);
}
animationSteps.push(step);
}
return animationSteps;
}
exports.calculateSequenceSteps = calculateSequenceSteps;