forked from phaserjs/phaser
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPercent.js
More file actions
47 lines (42 loc) · 1.18 KB
/
Percent.js
File metadata and controls
47 lines (42 loc) · 1.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
/**
* Work out what percentage `value` is of the range between `min` and `max`.
* If `max` isn't given then it will return the percentage of `value` to `min`.
*
* You can optionally specify an `upperMax` value, which is a mid-way point in the range that represents 100%, after which the % starts to go down to zero again.
*
* @function Phaser.Math.Percent
* @since 3.0.0
*
* @param {number} value - [description]
* @param {number} min - [description]
* @param {number} [max] - [description]
* @param {number} [upperMax] - [description]
*
* @return {float} A value between 0 and 1 representing the percentage.
*/
var Percent = function (value, min, max, upperMax)
{
if (max === undefined) { max = min + 1; }
var percentage = (value - min) / (max - min);
if (percentage > 1)
{
if (upperMax !== undefined)
{
percentage = ((upperMax - value)) / (upperMax - max);
if (percentage < 0)
{
percentage = 0;
}
}
else
{
percentage = 1;
}
}
else if (percentage < 0)
{
percentage = 0;
}
return percentage;
};
module.exports = Percent;