npm i analog-array
// any method:
import AnalogArray from "analog-array";
// or
const AnalogArray = require("analog-array");
// or
define(["analog-array"], function(AnalogArray){});
const AnalogArray = require("analog-array");
require.config({
packages: [{
name: 'analog-array',
location: 'node_modules/analog-array/dist/amd',
main: 'analog-array'
}]
});
// ...
define(["analog-array"], function(AnalogArray){});
<script src="dist/global/analog-array.js"></script>
<!-- or -->
<script src="dist/global/analog-array.min.js"></script>
<script>
const analogArray = new AnalogArray([10, 8, 999, 20000])
</script>
let analogArray = new AnalogArray([10, 8, 16])
Array.isArray(analogArray) // true
// overflow access returns highest or lowest available index respectively:
analogArray[999] // 16
analogArray[-0.5] // 10
// index values accessed like a normal array:
analogArray[1] // 8
let analogArray = new AnalogArray([10, 8, 16])
analogArray[0.5] // 9 (half way between index 0 and index 1)
analogArray[1.25] // 10 (1/4 past the value at index 1 towards the value at index 2)
analogArray[1.50] // 12 (1/2 past the value at index 1 towards the value at index 2)
analogArray[1.75] // 14 (3/4 past the value at index 1 towards the value at index 2)
analogArray[2.00] // 16 (the value at index 2)
analogArray = new AnalogArray([10, 8, 16], mySlideFn) // at init
analogArray.slide = mySlideFn // or later
Slide functions take 3 arguments, (from, to, amount)
from
is the value at the index Math.floor(splitIndex)
to
is the value at the index Math.floor(splitIndex) + 1
amount
is the scalar = splitIndex - fromIndex
, which will be a value in the range (0, 1)
(amounts 0 and 1 will access the array directly without sliding, otherwise the range would have been [0, 1])
Examples:
let floorIndexValuesOnly = (from, to, amount) => {
console.log(from, to, amount)
return from
}
analogArray = new AnalogArray([10, 8, 16])
analogArray[1.50] // 12
analogArray.slide = floorIndexValuesOnly
analogArray[1.50] // returns: 8, logs: 8 16 0.5
The values in the analogArray can be anything if you provide a way to slide between them.
These slide functions can be used directly outside of the implicit use in an AnalogArray instance if needed.
Accepts hex colors in the form "#RRGGBB". If "RRGGBB" is used (without "#") at Math.floor(splitIndex), the result will also omit "#".
let colorsAnalogArray = new AnalogArray(["#ff00ff", "888888", "#00ff00"], AnalogArray.slideColor)
colorsAnalogArray[0] // "#ff00ff"
colorsAnalogArray[0.5] // "#c444c4"
colorsAnalogArray[1] // "888888"
colorsAnalogArray[1.5] // "44c444"
colorsAnalogArray[2] // "#00ff00"
It will also slide hex colors in the form "#RRGGBBAA" and "RRGGBBAA" with similar results.