phaser/src/math/SmoothStep.js

42 lines
1.2 KiB
JavaScript
Raw Normal View History

2018-02-12 16:01:20 +00:00
/**
* @author Richard Davey <rich@photonstorm.com>
2023-01-02 17:36:27 +00:00
* @copyright 2013-2023 Photon Storm Ltd.
2019-05-10 15:15:04 +00:00
* @license {@link https://opensource.org/licenses/MIT|MIT License}
2018-02-12 16:01:20 +00:00
*/
2017-10-06 03:52:41 +00:00
/**
* Calculate a smooth interpolation percentage of `x` between `min` and `max`.
*
2018-05-24 16:03:42 +00:00
* The function receives the number `x` as an argument and returns 0 if `x` is less than or equal to the left edge,
* 1 if `x` is greater than or equal to the right edge, and smoothly interpolates, using a Hermite polynomial,
* between 0 and 1 otherwise.
*
2017-10-06 03:52:41 +00:00
* @function Phaser.Math.SmoothStep
* @since 3.0.0
* @see {@link https://en.wikipedia.org/wiki/Smoothstep}
2017-10-06 03:52:41 +00:00
*
2018-05-24 16:03:42 +00:00
* @param {number} x - The input value.
* @param {number} min - The minimum value, also known as the 'left edge', assumed smaller than the 'right edge'.
* @param {number} max - The maximum value, also known as the 'right edge', assumed greater than the 'left edge'.
2017-10-06 03:52:41 +00:00
*
* @return {number} The percentage of interpolation, between 0 and 1.
2017-10-06 03:52:41 +00:00
*/
var SmoothStep = function (x, min, max)
{
2018-05-24 16:03:42 +00:00
if (x <= min)
{
return 0;
}
2018-05-24 16:03:42 +00:00
if (x >= max)
{
return 1;
}
x = (x - min) / (max - min);
return x * x * (3 - 2 * x);
};
module.exports = SmoothStep;