mirror of
https://github.com/photonstorm/phaser
synced 2025-02-17 06:28:30 +00:00
Updated docs and added native clamping
This commit is contained in:
parent
9a76f40f55
commit
2684e7d988
1 changed files with 20 additions and 8 deletions
|
@ -5,24 +5,36 @@
|
|||
*/
|
||||
|
||||
/**
|
||||
* Smoothly interpolate between two values.
|
||||
* 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.
|
||||
*
|
||||
* Computes a smooth step interpolation between `min` and `max` by `x`.
|
||||
* https://en.wikipedia.org/wiki/Smoothstep
|
||||
*
|
||||
* @function Phaser.Math.SmoothStep
|
||||
* @since 3.0.0
|
||||
*
|
||||
* @param {number} x - The percentage of interpolation, between 0 and 1.
|
||||
* @param {number} min - The minimum value.
|
||||
* @param {number} max - The maximum value.
|
||||
* @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'.
|
||||
*
|
||||
* @return {number} The smoothly interpolated value.
|
||||
* @return {number} A number between 0 and 1.
|
||||
*/
|
||||
var SmoothStep = function (x, min, max)
|
||||
{
|
||||
x = Math.max(0, Math.min(1, (x - min) / (max - min)));
|
||||
if (x <= min)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
return x * x * (3 - 2 * x);
|
||||
if (x >= max)
|
||||
{
|
||||
return 1;
|
||||
}
|
||||
|
||||
x = (x - min) / (max - min);
|
||||
|
||||
return x * x * ( 3 - 2 * x );
|
||||
};
|
||||
|
||||
module.exports = SmoothStep;
|
||||
|
|
Loading…
Add table
Reference in a new issue