6 ## ceil(val[, step]):Number
8 Round value up to full steps. Similar to `Math.ceil()` but can round value
9 to an arbitrary *radix*.
19 Round values by increments of 5/10/1000/etc.
21 See: [`round()`](#round), [`floor()`](#floor), [`countSteps()`](#countSteps)
25 ## clamp(val, min, max):Number
27 Clamps value inside range.
29 `clamp()` is extremely useful in cases where you need to limit a value inside
30 a certain range. So instead of doing a complex `if/else` to filter/process the
31 value you can restrict it to always be inside the desired range:
33 clamp(-5, 0, 10); // 0
36 clamp(10, 1, 10); // 10
37 clamp(11, 1, 10); // 10
39 If the value is smaller than `min` it returns the `min`, if `val` is higher
40 than `max` it returns `max`.
44 Any situation where you need to limit a number inside a range like: slider
45 position, image galleries <small>(so user can't skip to an image that doesn't
46 exist)</small>, drag and drop, scroll boundaries, etc.
48 See: [`loop()`](#loop)
53 ## countSteps(val, step[, overflow]):Number
55 Count number of full steps.
59 1. `val` (Number) : Value.
60 2. `step` (Number) : Step size.
61 3. `[overflow]` (Number) : Maximum number of steps, nSteps will loop if
65 Count steps is very useful for cases where you need to know how many "full
66 steps" the number *completed*. Think of it as a division that only returns
67 integers and ignore remainders.
69 countSteps(3, 5); // 0
70 countSteps(6, 5); // 1
71 countSteps(12, 5); // 2
72 countSteps(18, 5); // 3
73 countSteps(21, 5); // 4
75 You can also set an `overflow` which will reset the *counter* before reaching
78 countSteps(3, 5, 3); // 0
79 countSteps(6, 5, 3); // 1
80 countSteps(12, 5, 3); // 2
81 countSteps(18, 5, 3); // 0
82 countSteps(21, 5, 3); // 1
86 #### How many items fit inside an area:
88 var containerWidth = 100;
90 var howManyFit = countSteps(containerWidth, itemWidth); // 12
92 #### Split value into different scales or convert value from one scale to another
94 From [mout/time/parseMs](time.html#parseMs):
98 milliseconds : countSteps(ms, 1, 1000),
99 seconds : countSteps(ms, 1000, 60),
100 minutes : countSteps(ms, 60000, 60),
101 hours : countSteps(ms, 3600000, 24),
102 days : countSteps(ms, 86400000)
106 // {days:27, hours:4, minutes:26, seconds:5, milliseconds:454}
111 ## floor(val[, step]):Number
113 Round value down to full steps. Similar to `Math.floor()` but can round value
114 to an arbitrary *radix*. (formerly `snap`)
124 Round values by increments of 5/10/1000/etc.
126 See: [`round()`](#round), [`ceil()`](#ceil), [`countSteps()`](#countSteps)
130 ## inRange(val, min, max[, threshold]):Boolean
132 Checks if value is inside the range.
134 inRange(-6, 1, 10); // false
135 inRange( 5, 1, 10); // true
136 inRange(12, 1, 10); // false
138 The threshold can be useful when you want granular control of what should match
139 and/or the precision could change at runtime or by some configuration option,
140 avoids the clutter of adding/subtracting the `threshold` from `mix` and `max`.
142 inRange(12, 1, 10, 2); // true
143 inRange(13, 1, 10, 2); // false
147 Anything that needs to check if value is inside a range, be it collision
148 detection, limiting interaction by mouse position, ignoring parts of the logic
149 that shouldn't happen if value isn't valid, simplify `if/else` conditions,
150 making code more readable, etc...
155 ## isNear(val, target, threshold):Boolean
157 Check if value is close to target.
159 Similar to `inRange()` but used to check if value is close to a certain value
160 or match the desired value. Basically to simplify `if/else` conditions and to
163 isNear( 10.2, 10, 0.5); // true
164 isNear( 10.5, 10, 0.5); // true
165 isNear(10.51, 10, 0.5); // false
169 Games where a certain action should happen if an *actor* is close to a target,
170 gravity fields, any numeric check that has some tolerance.
175 ## lerp(ratio, start, end):Number
177 Linear interpolation.
179 lerp(0.5, 0, 10); // 5
180 lerp(0.75, 0, 10); // 7.5
184 Linear interpolation is commonly used to create animations of elements moving
185 from one point to another, where you simply update the current ratio (which in
186 this case represents time) and get back the position of the element at that
189 The core idea of `lerp` is that you are using a number that goes from `0` to
190 `1` to specify a ratio inside that scale. This concept can be applied to
191 convert numbers from different scales easily.
193 See: [`map()`](#map), [`norm()`](#norm)
198 ## loop(val, min, max):Number
200 Loops value inside range. Will return `min` if `val > max` and `max` if `val
201 < min`, otherwise it returns `val`.
203 loop(-1, 0, 10); // 10
204 loop( 1, 0, 10); // 1
205 loop( 5, 0, 10); // 5
206 loop( 9, 0, 10); // 9
207 loop(10, 0, 10); // 10
208 loop(11, 0, 10); // 0
210 Similar to [`clamp()`](#clamp) but *loops* the value inside the range when an
215 Image galleries, infinite scroll, any kind of logic that requires that the
216 first item should be displayed after the last one or the last one should be
217 displayed after first if going on the opposite direction.
219 See: [`clamp()`](#clamp)
224 ## map(val, min1, max1, min2, max2):Number
226 Maps a number from one scale to another.
228 map(3, 0, 4, -1, 1) // 0.5
229 map(3, 0, 10, 0, 100) // 30
233 Very useful to convert values from/to multiple scales.
235 Let's suppose we have a slider that needs to go from `2000` to `5000` and that slider
236 has `300px` of width, here is how we would translate the knob position into the
244 var curVal = map(knobX, 0, sliderWid, minVal, maxVal); // 3230
246 See: [`lerp()`](#lerp), [`norm()`](#norm)
251 ## norm(val, min, max):Number
253 Gets normalized ratio of value inside range.
255 If `val < min` or `val > max` it will throw a `RangeError` since we can't
258 norm(50, 0, 100); // 0.5
259 norm(75, 0, 100); // 0.75
260 norm(100, 0, 100); // 1
261 norm(-50, 0, 100); // RangeError: value (-50) must be between 0 and 100
265 Convert values between scales, used by [`map()`](#map) internally. Direct
266 opposite of [`lerp()`](#lerp).
268 See: [`lerp()`](#lerp), [`map()`](#map)
272 ## round(val[, step]):Number
274 Round value to full steps. Similar to `Math.round()` but allow setting an
283 round(0.22, 0.5); // 0
284 round(0.49, 0.5); // 0.5
285 round(0.51, 0.5); // 0.5
286 round(0.74, 0.5); // 0.5
287 round(0.75, 0.5); // 1
288 round(1.24, 0.5); // 1
289 round(1.25, 0.5); // 1.5
290 round(1.74, 0.5); // 1.5
294 Round values by increments of 0.5/5/10/1000/etc.
296 See: [`floor()`](#floor), [`ceil()`](#ceil), [`countSteps()`](#countSteps)
300 -------------------------------------------------------------------------------
302 For more usage more info check the specs and source code.