logarithmic scale
by Richard Hunter
HTML
<input type="range" id="range" min="0" max="10" step="any">
<input type="tel" id="tel">
JavaScript
/*
maps values between ranges of real numbers
*/
class Mapper {
constructor(A, B) {
this.A = A;
this.B = B;
let rangeA = this.A[1] - this.A[0];
let rangeB = this.B[1] - this.B[0];
this.AToBRatio = rangeA / rangeB;
this.BToARatio = rangeB / rangeA;
}
atob(a) {
let lowerBoundB = this.B[0];
let lowerBoundA = this.A[0];
let ratio = this.BToARatio;
return ((a - lowerBoundA) * ratio) + lowerBoundB;
}
btoa(b) {
let lowerBoundB = this.B[0];
let lowerBoundA = this.A[0];
let ratio = this.AToBRatio;
return ((b - lowerBoundB) * ratio) + lowerBoundA;
}
}
let A = [2, 10];
let B = [34, 234];
let map = new Mapper(A, B);
console.log(map.btoa(34));