AnalogArray by PropJockey
npm i analog-array
Importing
npm and StealJS:
// any method:;// orconst AnalogArray = ;// or;
npm and CJS:
const AnalogArray = ;
AMD:
require;// ...;
Global/Standalone:
<!-- or -->
Usage
let analogArray = 10 8 16 Array // true // overflow access returns highest or lowest available index respectively: analogArray999 // 16 analogArray-05 // 10 // index values accessed like a normal array: analogArray1 // 8
splitIndex values "slide" between the value at index = Math.floor(splitIndex) and the next index
let analogArray = 10 8 16 analogArray05 // 9 (half way between index 0 and index 1) analogArray125 // 10 (1/4 past the value at index 1 towards the value at index 2) analogArray150 // 12 (1/2 past the value at index 1 towards the value at index 2) analogArray175 // 14 (3/4 past the value at index 1 towards the value at index 2) analogArray200 // 16 (the value at index 2)
a custom slide function can be specified
analogArray = 10 8 16 mySlideFn // at init analogArrayslide = 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 { console return from } analogArray = 10 8 16 analogArray150 // 12 analogArrayslide = floorIndexValuesOnly analogArray150 // returns: 8, logs: 8 16 0.5
The values in the analogArray can be anything if you provide a way to slide between them.
Slide functions AnalogArray.slideNumber (the default slide) and AnalogArray.slideColor are included
These slide functions can be used directly outside of the implicit use in an AnalogArray instance if needed.
AnalogArray.slideColor
Accepts hex colors in the form "#RRGGBB". If "RRGGBB" is used (without "#") at Math.floor(splitIndex), the result will also omit "#".
let colorsAnalogArray = "#ff00ff" "888888" "#00ff00" AnalogArrayslideColor colorsAnalogArray0 // "#ff00ff"colorsAnalogArray05 // "#c444c4"colorsAnalogArray1 // "888888"colorsAnalogArray15 // "44c444"colorsAnalogArray2 // "#00ff00"
It will also slide hex colors in the form "#RRGGBBAA" and "RRGGBBAA" with similar results.