-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
183 lines (161 loc) · 6.05 KB
/
index.js
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
import throttle from 'throttle-debounce/throttle';
import './styles.css';
function addButton(container, label, onclick) {
// Add a button to a container.
const button = document.createElement('button');
button.setAttribute('type', 'button');
button.className = 'button button-outline';
button.innerHTML = label;
button.addEventListener('click', onclick);
container.appendChild(button);
return button;
}
function calculateOverlap(d, r, R) {
// Calculate the DIFI overlap.
// d is the center-to-center spacing between the small and big circles.
// r is the radius of the small circle.
// R is the radius of the large circle.
const x = ((d * d) + (r * r) - (R * R)) / (2 * d * r);
const y = ((d * d) + (R * R) - (r * r)) / (2 * d * R);
const z = (-d + r + R) * (d + r - R) * (d - r + R) * (d + r + R);
const overlapArea = (r * r * Math.acos(x)) + (R * R * Math.acos(y)) - (Math.sqrt(z) / 2);
if (d > r + R) {
return 0;
} else if (d < R - r) {
return 100;
}
return 100 * (overlapArea / (Math.PI * r * r));
}
function outerLeft(el) {
// Find the left offset of an element (including border)
// relative to the page
const rect = el.getBoundingClientRect();
const left = rect.left + document.body.scrollLeft;
return left - parseFloat(getComputedStyle(el)['border-left-width'], 10);
}
function outerRight(el) {
// Find the right offset of an element (including border)
// relative to the page
return outerLeft(el) + el.offsetWidth;
}
export class DIFIInput {
constructor(el, options = {}) {
this.drag = throttle(20, this.drag.bind(this));
this.startDrag = this.startDrag.bind(this);
this.endDrag = this.endDrag.bind(this);
this.el = el;
this.options = options;
this.initializeDOM();
this.update();
}
initializeDOM() {
// Construct the widget.
const name = this.el.getAttribute('name');
this.el.setAttribute('name', `${name}_distance`);
this.el.setAttribute('type', 'hidden');
this.elOverlap = document.createElement('input');
this.elOverlap.setAttribute('name', `${name}_overlap`);
this.elOverlap.setAttribute('type', 'hidden');
this.el.insertAdjacentElement('afterend', this.elOverlap);
const elContent = document.createElement('div');
elContent.className = 'DIFI';
elContent.innerHTML = `
<div class="DIFI-controls"></div>
<div class="DIFI-group"><label>${this.options.groupLabel || 'Group'}</label></div>
<div class="DIFI-range">
<div class="DIFI-me"><label>${this.options.meLabel || 'Me'}</label></div>
</div>
`;
this.me = elContent.querySelector('.DIFI-me');
this.group = elContent.querySelector('.DIFI-group');
this.elRange = elContent.querySelector('.DIFI-range');
if (this.options.groupImage) {
this.group.style.backgroundImage = `url(${this.options.groupImage})`;
}
const controls = elContent.querySelector('.DIFI-controls');
addButton(controls, '◀◀', this.nudge.bind(this, -0.5));
addButton(controls, '◀', this.nudge.bind(this, -0.1));
addButton(controls, '▶', this.nudge.bind(this, 0.1));
addButton(controls, '▶▶', this.nudge.bind(this, 0.5));
this.el.insertAdjacentElement('beforebegin', elContent);
this.me.addEventListener('mousedown', this.startDrag.bind(this));
}
startDrag(e) {
// Start tracking mouse when clicked.
e.preventDefault();
e.stopPropagation();
this.me.className = 'DIFI-me dragging';
this.dragOrigLeft = parseFloat(getComputedStyle(this.me).left, 10);
this.dragOrigX = e.pageX;
document.addEventListener('mousemove', this.drag);
document.addEventListener('mouseup', this.endDrag);
}
drag(e) {
// Update position of Me while dragging.
e.preventDefault();
e.stopPropagation();
if (this.dragOrigX === null) {
return;
}
const deltaPixels = e.pageX - this.dragOrigX;
this.nudgePixels(deltaPixels, this.dragOrigLeft);
}
endDrag() {
// Stop tracking mouse when mouse released.
this.me.className = 'DIFI-me';
document.removeEventListener('mousemove', this.drag);
document.removeEventListener('mouseup', this.endDrag);
this.dragOrigX = null;
this.dragOrigLeft = null;
}
update() {
// Update distance and overlap values from current position.
// Distance is based on difference in position:
// - left circle separated from right circle: -100 to 0
// - left circle just touching right circle: 0
// - left circle overlapping right circle: 0 to 100
// - left circle contained within right circle: 100 to 125
// - left circle at the center of right circle: 125
const R = this.group.offsetWidth / 2;
const r = this.me.offsetWidth / 2;
const mePos = outerLeft(this.me);
const groupPos = outerLeft(this.group);
const d = mePos - groupPos;
let value = 100 + 50 * d / r;
// Snap to center
const elRange = this.me.parentNode;
if (outerRight(elRange) - outerRight(this.me) < 2) {
value = 125;
}
// Clip value to desired range (-100 to 125)
value = Math.max(Math.min(Math.round(value * 1000) / 1000, 125), -100);
this.el.setAttribute('value', value);
// Compute overlap.
const dCenterToCenter = (groupPos + R) - (mePos + r);
const overlap = calculateOverlap(dCenterToCenter, r, R);
this.elOverlap.setAttribute('value', overlap);
}
nudge(delta) {
// Move by `delta` units (1 unit = radius of Me circle)
const unit = this.me.offsetWidth / 2;
const deltaPixels = delta * unit;
this.nudgePixels(deltaPixels);
}
nudgePixels(delta, origLeft) {
// Move by `delta` pixels
// (relative to origLeft if specified, or to current position)
let start = origLeft;
if (start === undefined) {
start = parseFloat(getComputedStyle(this.me).left, 10);
}
let finish = start + delta;
if (finish < 0) {
finish = 0;
}
if (finish > this.elRange.offsetWidth - this.me.offsetWidth) {
finish = this.elRange.offsetWidth - this.me.offsetWidth;
}
this.me.style.left = `${finish / this.elRange.offsetWidth * 100}%`;
this.update();
}
}