d6b9994b6d427d145c6cfda5fe8ea65fccfdf2b9
[platform/framework/web/crosswalk-tizen.git] /
1 # math #
2
3 Math utilities.
4
5
6 ## ceil(val[, step]):Number
7
8 Round value up to full steps. Similar to `Math.ceil()` but can round value
9 to an arbitrary *radix*.
10
11     ceil(7.2);   // 8
12     ceil(7.8);   // 8
13     ceil(7, 5);  // 10
14     ceil(11, 5); // 15
15     ceil(15, 5); // 15
16
17 ### Common use cases
18
19 Round values by increments of 5/10/1000/etc.
20
21 See: [`round()`](#round), [`floor()`](#floor), [`countSteps()`](#countSteps)
22
23
24
25 ## clamp(val, min, max):Number
26
27 Clamps value inside range.
28
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:
32
33     clamp(-5, 0, 10); // 0
34     clamp(7, 1, 10);  // 7
35     clamp(8, 1, 10);  // 8
36     clamp(10, 1, 10); // 10
37     clamp(11, 1, 10); // 10
38
39 If the value is smaller than `min` it returns the `min`, if `val` is higher
40 than `max` it returns `max`.
41
42 ### Common use cases
43
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.
47
48 See: [`loop()`](#loop)
49
50
51
52
53 ## countSteps(val, step[, overflow]):Number
54
55 Count number of full steps.
56
57 ### Arguments:
58
59  1. `val` (Number)        : Value.
60  2. `step` (Number)       : Step size.
61  3. `[overflow]` (Number) : Maximum number of steps, nSteps will loop if
62 `>=` than overflow.
63
64
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.
68
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
74
75 You can also set an `overflow` which will reset the *counter* before reaching
76 this number.
77
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
83
84 ### Common use cases
85
86 #### How many items fit inside an area:
87
88     var containerWidth = 100;
89     var itemWidth = 8;
90     var howManyFit = countSteps(containerWidth, itemWidth); // 12
91
92 #### Split value into different scales or convert value from one scale to another
93
94 From [mout/time/parseMs](time.html#parseMs):
95
96     function parseMs(ms){
97         return {
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)
103         };
104     }
105
106     // {days:27, hours:4, minutes:26, seconds:5, milliseconds:454}
107     parseMs(2348765454);
108
109
110
111 ## floor(val[, step]):Number
112
113 Round value down to full steps. Similar to `Math.floor()` but can round value
114 to an arbitrary *radix*. (formerly `snap`)
115
116     floor(7.2);   // 7
117     floor(7.8);   // 7
118     floor(7, 5);  // 5
119     floor(11, 5); // 10
120     floor(15, 5); // 15
121
122 ### Common use cases
123
124 Round values by increments of 5/10/1000/etc.
125
126 See: [`round()`](#round), [`ceil()`](#ceil), [`countSteps()`](#countSteps)
127
128
129
130 ## inRange(val, min, max[, threshold]):Boolean
131
132 Checks if value is inside the range.
133
134     inRange(-6, 1, 10);    // false
135     inRange( 5, 1, 10);    // true
136     inRange(12, 1, 10);    // false
137
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`.
141
142     inRange(12, 1, 10, 2); // true
143     inRange(13, 1, 10, 2); // false
144
145 ### Common use cases
146
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...
151
152
153
154
155 ## isNear(val, target, threshold):Boolean
156
157 Check if value is close to target.
158
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
161 make code clearer.
162
163     isNear( 10.2, 10, 0.5); // true
164     isNear( 10.5, 10, 0.5); // true
165     isNear(10.51, 10, 0.5); // false
166
167 ### Common use cases
168
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.
171
172
173
174
175 ## lerp(ratio, start, end):Number
176
177 Linear interpolation.
178
179     lerp(0.5, 0, 10);  // 5
180     lerp(0.75, 0, 10); // 7.5
181
182 ### Common use cases
183
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
187 "frame".
188
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.
192
193 See: [`map()`](#map), [`norm()`](#norm)
194
195
196
197
198 ## loop(val, min, max):Number
199
200 Loops value inside range. Will return `min` if `val > max` and `max` if `val
201 < min`, otherwise it returns `val`.
202
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
209
210 Similar to [`clamp()`](#clamp) but *loops* the value inside the range when an
211 overflow occurs.
212
213 ### Common use cases
214
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.
218
219 See: [`clamp()`](#clamp)
220
221
222
223
224 ## map(val, min1, max1, min2, max2):Number
225
226 Maps a number from one scale to another.
227
228     map(3, 0, 4, -1, 1)   // 0.5
229     map(3, 0, 10, 0, 100) // 30
230
231 ### Common use cases
232
233 Very useful to convert values from/to multiple scales.
234
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
237 current value:
238
239     var knobX = 123;
240     var sliderWid = 300;
241     var minVal = 2000;
242     var maxVal = 5000;
243
244     var curVal = map(knobX, 0, sliderWid, minVal, maxVal); // 3230
245
246 See: [`lerp()`](#lerp), [`norm()`](#norm)
247
248
249
250
251 ## norm(val, min, max):Number
252
253 Gets normalized ratio of value inside range.
254
255 If `val < min` or `val > max` it will throw a `RangeError` since we can't
256 normalize the value.
257
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
262
263 ### Common use cases
264
265 Convert values between scales, used by [`map()`](#map) internally. Direct
266 opposite of [`lerp()`](#lerp).
267
268 See: [`lerp()`](#lerp), [`map()`](#map)
269
270
271
272 ## round(val[, step]):Number
273
274 Round value to full steps. Similar to `Math.round()` but allow setting an
275 arbitrary *radix*.
276
277     // default
278     round(0.22);      // 0
279     round(0.49);      // 0
280     round(0.51);      // 1
281
282     // custom radix
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
291
292 ### Common use cases
293
294 Round values by increments of 0.5/5/10/1000/etc.
295
296 See: [`floor()`](#floor), [`ceil()`](#ceil), [`countSteps()`](#countSteps)
297
298
299
300 -------------------------------------------------------------------------------
301
302 For more usage more info check the specs and source code.
303