in src/utils/animation.js [26:67]
export function twoPointSpring(
initialHoverValue,
initialReferenceValue,
scale,
colorMap = () => 'black'
) {
function ptToSpringValue(pt) {
if (pt === undefined) return undefined;
const out = { ...pt };
Object.keys(out).forEach((k) => {
out[k] = scale(out[k]);
});
return out;
}
const hovParams = { damping: 1, stiffness: 0.7 };
const refParams = { damping: 0.55, stiffness: 1 };
const leftValues = spring(ptToSpringValue(initialHoverValue), hovParams);
const rightValues = spring(ptToSpringValue(initialReferenceValue), refParams);
const dotsAndLines = derived([leftValues, rightValues], ([$left, $right]) => {
if (!$left || !$right) return [];
const dal = Object.keys($right).reduce((acc, k) => {
const rightY = $right[k];
const leftY = $left[k];
const color = colorMap(k);
acc[k] = { leftY, rightY, color };
return acc;
}, {});
return dal;
});
return {
transform: ptToSpringValue,
subscribe: dotsAndLines.subscribe,
setHover: (p, hard = false) => {
leftValues.set(ptToSpringValue(p), { hard });
},
setReference: (p, hard = false) => {
rightValues.set(ptToSpringValue(p), { hard });
},
};
}