├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | -------------------------------------------------------------------------------- /.npmignore: -------------------------------------------------------------------------------- 1 | bower_components 2 | node_modules 3 | *.log 4 | .DS_Store 5 | bundle.js 6 | test 7 | test.js 8 | demo/ 9 | .npmignore 10 | LICENSE.md -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | Copyright (c) 2015 Marcin Ignac 3 | 4 | Permission is hereby granted, free of charge, to any person obtaining a copy 5 | of this software and associated documentation files (the "Software"), to deal 6 | in the Software without restriction, including without limitation the rights 7 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 8 | copies of the Software, and to permit persons to whom the Software is 9 | furnished to do so, subject to the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be included in all 12 | copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. 17 | IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, 18 | DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR 19 | OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE 20 | OR OTHER DEALINGS IN THE SOFTWARE. 21 | 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # interpolate-arrays 2 | 3 | [![stable](http://badges.github.io/stability-badges/dist/stable.svg)](http://github.com/badges/stability-badges) 4 | 5 | Linear interpolation between multiple arrays 6 | 7 | ```js 8 | var interpolateArrays = require('interpolate-arrays') 9 | 10 | var red = [255, 0, 0] 11 | var green = [0, 255, 0] 12 | var blue = [0, 0, 255] 13 | 14 | var yellow = interpolateArrays([red, green, blue], 0.25); //-> [ 127.5, 127.5, 0 ] 15 | ``` 16 | 17 | ## Usage 18 | 19 | [![NPM](https://nodei.co/npm/interpolate-arrays.png)](https://www.npmjs.com/package/interpolate-arrays) 20 | 21 | #### `interpolateArrays(arraysList, t[, out])` 22 | 23 | Linearly interpolates between all arrays in the `arraysList` using the `t` component. Returns an interpolated array optionally reusing `opt` array to avoid allocation. 24 | 25 | ## License 26 | 27 | MIT, see [LICENSE.md](http://github.com/vorg/interpolate-arrays/blob/master/LICENSE.md) for details. 28 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | var lerp = require('lerp-array'); 2 | 3 | function copyArrayTo(array, out) { 4 | for (let i = 0; i < array.length; i++) { 5 | out[i] = array[i] 6 | } 7 | return out 8 | } 9 | 10 | function interpolateArrays(arrays, t, out) { 11 | if (t >= 1) { 12 | const lastItem = arrays[arrays.length - 1]; 13 | return out ? copyArrayTo(lastItem, out) : lastItem 14 | } 15 | if (arrays.length == 1) { 16 | const firstItem = arrays[0] 17 | return out ? copyArrayTo(firstItem, out) : firstItem; 18 | } 19 | var numStops = arrays.length - 1; 20 | var stopF = t * numStops; 21 | var stop = Math.floor(stopF); 22 | var k = stopF - stop; 23 | return lerp(arrays[stop], arrays[stop+1], k, out); 24 | } 25 | 26 | module.exports = interpolateArrays; 27 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "interpolate-arrays", 3 | "version": "1.0.4", 4 | "description": "Linear interpolation between multiple arrays", 5 | "main": "index.js", 6 | "license": "MIT", 7 | "author": { 8 | "name": "Marcin Ignac", 9 | "email": "marcin.ignac@gmail.com", 10 | "url": "https://github.com/vorg" 11 | }, 12 | "dependencies": { 13 | "lerp-array": "^1.1.1" 14 | }, 15 | "devDependencies": {}, 16 | "scripts": { 17 | "test": "node test.js" 18 | }, 19 | "keywords": [ 20 | "interpolation" 21 | ], 22 | "repository": { 23 | "type": "git", 24 | "url": "git://github.com/vorg/interpolate-arrays.git" 25 | }, 26 | "homepage": "https://github.com/vorg/interpolate-arrays", 27 | "bugs": { 28 | "url": "https://github.com/vorg/interpolate-arrays/issues" 29 | } 30 | } 31 | --------------------------------------------------------------------------------