source: imaps-frontend/node_modules/@use-gesture/core/dist/maths-0ab39ae9.esm.js

main
Last change on this file was d565449, checked in by stefan toskovski <stefantoska84@…>, 4 weeks ago

Update repo after prototype presentation

  • Property mode set to 100644
File size: 1.2 KB
Line 
1function clamp(v, min, max) {
2 return Math.max(min, Math.min(v, max));
3}
4const V = {
5 toVector(v, fallback) {
6 if (v === undefined) v = fallback;
7 return Array.isArray(v) ? v : [v, v];
8 },
9 add(v1, v2) {
10 return [v1[0] + v2[0], v1[1] + v2[1]];
11 },
12 sub(v1, v2) {
13 return [v1[0] - v2[0], v1[1] - v2[1]];
14 },
15 addTo(v1, v2) {
16 v1[0] += v2[0];
17 v1[1] += v2[1];
18 },
19 subTo(v1, v2) {
20 v1[0] -= v2[0];
21 v1[1] -= v2[1];
22 }
23};
24function rubberband(distance, dimension, constant) {
25 if (dimension === 0 || Math.abs(dimension) === Infinity) return Math.pow(distance, constant * 5);
26 return distance * dimension * constant / (dimension + constant * distance);
27}
28function rubberbandIfOutOfBounds(position, min, max, constant = 0.15) {
29 if (constant === 0) return clamp(position, min, max);
30 if (position < min) return -rubberband(min - position, max - min, constant) + min;
31 if (position > max) return +rubberband(position - max, max - min, constant) + max;
32 return position;
33}
34function computeRubberband(bounds, [Vx, Vy], [Rx, Ry]) {
35 const [[X0, X1], [Y0, Y1]] = bounds;
36 return [rubberbandIfOutOfBounds(Vx, X0, X1, Rx), rubberbandIfOutOfBounds(Vy, Y0, Y1, Ry)];
37}
38
39export { V, computeRubberband as c, rubberbandIfOutOfBounds as r };
Note: See TracBrowser for help on using the repository browser.