├── .gitignore ├── .npmignore ├── LICENSE.md ├── README.md ├── index.js ├── package.json └── test.js /.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/ -------------------------------------------------------------------------------- /LICENSE.md: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014, Gaëtan Renaudeau 2 | 3 | All rights reserved. 4 | 5 | Redistribution and use in source and binary forms, with or without modification, 6 | are permitted provided that the following conditions are met: 7 | 8 | * Redistributions of source code must retain the above copyright notice, 9 | this list of conditions and the following disclaimer. 10 | * Redistributions in binary form must reproduce the above copyright notice, 11 | this list of conditions and the following disclaimer in the documentation 12 | and/or other materials provided with the distribution. 13 | * Neither the name of smoothstep nor the names of its contributors 14 | may be used to endorse or promote products derived from this software 15 | without specific prior written permission. 16 | 17 | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 18 | "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 19 | LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 20 | A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR 21 | CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 22 | EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 23 | PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 24 | PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF 25 | LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING 26 | NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS 27 | SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 28 | 29 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | [![browser support](https://ci.testling.com/gre/smoothstep.png)](https://ci.testling.com/gre/smoothstep) 2 | 3 | # smoothstep 4 | 5 | [![frozen](http://badges.github.io/stability-badges/dist/frozen.svg)](http://github.com/badges/stability-badges) 6 | 7 | Bare-bones [smoothstep function](http://en.wikipedia.org/wiki/Smoothstep) (cubic Hermite interpolation), returning a value in the range 0.0 to 1.0. 8 | 9 | Use: 10 | 11 | ```js 12 | var smoothstep = require('smoothstep') 13 | 14 | var res = smoothstep(a, b, t); 15 | ``` 16 | 17 | ## Usage 18 | 19 | [![NPM](https://nodei.co/npm/smoothstep.png)](https://nodei.co/npm/smoothstep/) 20 | 21 | ```smoothstep(min, max, t)``` 22 | 23 | Performs smoothstep from min to max using the given value, by clamping and then using cubic Hermite interpolation. 24 | 25 | ## License 26 | 27 | BSD, see [LICENSE.md](http://github.com/gre/smoothstep/blob/master/LICENSE.md) for details. 28 | -------------------------------------------------------------------------------- /index.js: -------------------------------------------------------------------------------- 1 | module.exports = function smoothstep (min, max, value) { 2 | var x = Math.max(0, Math.min(1, (value-min)/(max-min))); 3 | return x*x*(3 - 2*x); 4 | }; 5 | -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "smoothstep", 3 | "version": "1.0.1", 4 | "description": "bare-bones smoothstep interpolation function", 5 | "main": "index.js", 6 | "license": "BSD", 7 | "author": "Gaëtan Renaudeau ", 8 | "dependencies": {}, 9 | "devDependencies": { 10 | "tape": "~2.13.2" 11 | }, 12 | "scripts": { 13 | "test": "node test.js" 14 | }, 15 | "testling": { 16 | "files": "test.js", 17 | "browsers": [ 18 | "ie/6..latest", 19 | "chrome/22..latest", 20 | "firefox/16..latest", 21 | "safari/latest", 22 | "opera/11.0..latest", 23 | "iphone/6", 24 | "ipad/6", 25 | "android-browser/latest" 26 | ] 27 | }, 28 | "repository": { 29 | "type": "git", 30 | "url": "git://github.com/gre/smoothstep.git" 31 | }, 32 | "homepage": "https://github.com/gre/smoothstep", 33 | "bugs": { 34 | "url": "https://github.com/gre/smoothstep/issues" 35 | }, 36 | "keywords": [ 37 | "cubic", 38 | "hermite", 39 | "smooth", 40 | "smoothstep", 41 | "glsl", 42 | "interpolation", 43 | "interp", 44 | "lerp", 45 | "mix", 46 | "threshold" 47 | ] 48 | } 49 | -------------------------------------------------------------------------------- /test.js: -------------------------------------------------------------------------------- 1 | var test = require('tape').test; 2 | var smoothstep = require('./'); 3 | 4 | test('smoothstep returns correct values', function(t) { 5 | t.equal( smoothstep( 100, 200, 150 ), 0.5 ); 6 | t.equal( smoothstep( 10, 20, 9 ), 0 ); 7 | t.equal( smoothstep( 10, 20, 30 ), 1 ); 8 | t.equal( smoothstep( 0, 1, 0.6 ), 0.648 ); 9 | t.end(); 10 | }); --------------------------------------------------------------------------------