-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Expand file tree
/
Copy pathdial.js
More file actions
75 lines (61 loc) · 1.37 KB
/
dial.js
File metadata and controls
75 lines (61 loc) · 1.37 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
function dial(cb, options) {
"ram";
const SCREEN_W = g.getWidth();
const SCREEN_H = g.getHeight();
options = Object.assign(
{ stepsPerWholeTurn : 7, // 7 chosen as it felt the best in use.
dialRect : {
x: 0,
y: 0,
w: SCREEN_W,
h: SCREEN_H,
},
}, options);
const DIAL_RECT = options.dialRect;
const CENTER = {
x: DIAL_RECT.x + DIAL_RECT.w / 2,
y: DIAL_RECT.y + DIAL_RECT.h / 2,
};
const BASE_SCREEN_W = 176;
const STEPS_PER_TURN = options.stepsPerWholeTurn;
const BASE_THRESHOLD = 50;
const THRESHOLD =
BASE_THRESHOLD *
(10 / STEPS_PER_TURN) *
(DIAL_RECT.w / BASE_SCREEN_W);
let cumulative = 0;
function onDrag(e) {
"ram";
if (
e.x < DIAL_RECT.x ||
e.x > DIAL_RECT.x+DIAL_RECT.w-1 ||
e.y < DIAL_RECT.y ||
e.y > DIAL_RECT.y+DIAL_RECT.h-1
) {
return;
}
if (e.y < CENTER.y) {
cumulative += e.dx;
} else {
cumulative -= e.dx;
}
if (e.x < CENTER.x) {
cumulative -= e.dy;
} else {
cumulative += e.dy;
}
function stepHandler(step) {
cumulative -= THRESHOLD * step;
cb(step);
}
while (cumulative > THRESHOLD) {
stepHandler(1);
}
while (cumulative < -THRESHOLD) {
stepHandler(-1);
}
E.stopEventPropagation();
}
return onDrag;
}
exports = dial;