-
-
Save remy/4250863 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function sampled(arr, size) { | |
var sampled = [], i, n, idx, l = arr.length; | |
if (arr.length <= size){ | |
return arr | |
} else { | |
for (i = 0; i < size; i++) { | |
n = (i / size) * l; | |
// faster Math.ceil (because I'm doing this with large arrays inside raf) | |
idx = (n << 0); | |
idx = (idx == n)? idx: idx + 1; | |
sampled[i] = arr[idx]; | |
} | |
return sampled; | |
} | |
} | |
var letters = 'a b c d e f g h i j k l m n o p'.split(' '); | |
sampled(letters, 5); // a e h k n | |
sampled(letters, 10); // a c e f h i k m n p | |
// TODO I would like this to expand out to fill all the available space - but that's a nice to have | |
sampled(letters, 100); // a b c d e f g h i j k l m n o p |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment