-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathknobs.js
312 lines (284 loc) · 9.68 KB
/
knobs.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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
/*********************************
JS Audio Knobs by Colin Bone Dodds
*********************************/
let madeGlobalEventHandlers = false; //checked during each Knob class instantiation. Only the first instantiation creates the event handlers.
let knobInUse = {
id: "",
initY: 0,
currentKnob: {}
};
class Knob {
constructor({
id = "knob1",
lowVal = 0,
highVal = 100,
value = 0,
size = "medium",
sensitivity = 1,
type = "LittlePhatty",
label = true,
lblTxtColor = "silver"
}) {
this.id = id;
this.lowVal = lowVal;
this.highVal = highVal;
if (value > highVal) {
this.currentValue = highVal;
} else if (value < lowVal) {
this.currentValue = lowVal;
} else {
this.currentValue = value;
}
this.sensitivity = sensitivity;
this.scaler = 100 / (this.highVal - this.lowVal);
this.type = type;
this.label = label;
this.lblTxtColor = lblTxtColor;
if (size == "xlarge") {
this.size = 128;
} else if (size == "large") {
this.size = 85;
} else if (size == "medium") {
this.size = 50;
} else if (size == "small") {
this.size = 40;
} else {
this.size = 30;
}
//set the image file. Format is e.g. "LittlePhatty/LittlePhatty_40.png";
this.imgFile = `${this.type}/${this.type}_${this.size}.png`;
//run the initializer method
this.setUpKnob();
}
setUpKnob() {
let div = document.getElementById(this.id);
//setup div contents
let imgDiv = document.createElement("div");
// let src = "./jsaudioknobs/knob_Images/" + this.imgFile;
let src =
//"./knob_Images/" +
"https://raw.githubusercontent.com/ColinBD/JSAudioKnobs/master/docs/knobs/" +
this.imgFile;
imgDiv.innerHTML = `<img draggable='false' style='pointer-events: none; transform: translateY(0px);' src=${src}>`;
let lblDiv = document.createElement("div");
div.appendChild(imgDiv);
div.appendChild(lblDiv);
//set the style
imgDiv.style = `overflow: hidden; height: ${
this.size
}px; user-select: none;`;
div.style = "position: absolute";
//add an event listener
imgDiv.addEventListener(
"mousedown",
function(e) {
//set the knobInUse object
knobInUse = {
id: this.id,
initY: e.pageY,
value: this.currentValue, //storing the value
currentKnob: this //storing the reference
};
}.bind(this) //we must bind 'this' to the event listener (or use 'let that = this', then set values to 'that')
);
//as above for touch event
imgDiv.addEventListener(
"touchstart",
function(e) {
//set the knobInUse object
knobInUse = {
id: this.id,
initY: e.targetTouches[0].pageY,
value: this.currentValue, //storing the value
currentKnob: this //storing the reference
};
}.bind(this) //we must bind 'this' to the event listener (or use 'let that = this', then set values to 'that')
);
//do we need to make the global event handlers? - they only get made on the first knob instantiation
if (madeGlobalEventHandlers == false) {
createGlobalEventHandlers();
madeGlobalEventHandlers = true;
}
//set the style for the label
lblDiv.style =
"text-align: center;width: 100%;margin: 0 auto;font-size: 12px";
lblDiv.style.color = this.lblTxtColor;
//set the image position
this.setImage();
}
setValue(val) {
//check the new value is within the acceptable range
if (val > this.highVal) {
this.currentValue = this.highVal;
console.log(
`you tried to set a value of ${val} which exceeded the upper limit of ${
this.highVal
}`
);
} else if (val < this.lowVal) {
this.currentValue = this.lowVal;
console.log(
`you tried to set a value of ${val} which exceeded the lower limit of ${
this.lowVal
}`
);
} else {
this.currentValue = val;
}
//set the image
this.setImage();
//call the users function
if (typeof knobChanged == "function") {
knobChanged(this.id, this.currentValue);
}
}
setImage() {
//change the image position to match
let sum =
(Math.floor(((this.currentValue - this.lowVal) * this.scaler) / 2) + 0) *
this.size;
let newY = `translateY(-${sum}px)`;
//access to the image goes: container div > image wrapper div > image tag
document.getElementById(
this.id
).childNodes[0].childNodes[0].style.transform = newY;
//update label (if user wants labels)
if (this.label != false) {
document.getElementById(
this.id
).childNodes[1].innerHTML = this.currentValue;
}
}
getValue() {
return this.currentValue;
}
} //end of class definition
function createGlobalEventHandlers() {
//also add the global style here (needed for the onmousemove > e.pageY call to work correctly)
document
.querySelectorAll("html, body")
.forEach(node => (node.style.height = "100%"));
//mouseup global event handler > resets the knobinuse object to show no knob in use
document.body.addEventListener("mouseup", function(e) {
//reset the knobInUse object
resetKnobInUse(e);
});
//as above for touchend event
document.body.addEventListener("touchend", function(e) {
//reset the knobInUse object
resetKnobInUse(e);
});
function resetKnobInUse() {
//set the knobInUse object
knobInUse = {
id: "",
initY: 0,
value: 0,
currentKnob: null
};
}
//mousemove global event handler > does the bulk of the work
document.body.addEventListener("mousemove", function(e) {
if (knobInUse.id != "") {
//console.log(e.pageY); //for testing
//freeze mouse drag activity if user hits top or bottom of the page
if (e.pageY <= 10 || e.pageY >= document.body.clientHeight - 10) {
knobInUse = { id: "", initY: 0, currentKnob: null };
return;
} else {
//calculate new knob value
knobInUse.currentKnob.currentValue = Math.round(
knobInUse.value +
((knobInUse.initY - e.pageY) * knobInUse.currentKnob.sensitivity) /
knobInUse.currentKnob.scaler
);
//use max/min variables for easier reading
let max = knobInUse.currentKnob.highVal,
min = knobInUse.currentKnob.lowVal;
//ensure the know value does not exceed max and/or minimum values
if (knobInUse.currentKnob.currentValue > max) {
knobInUse.currentKnob.currentValue = max;
} else if (knobInUse.currentKnob.currentValue < min) {
knobInUse.currentKnob.currentValue = min;
}
}
//update label (if user wants labels)
if (knobInUse.currentKnob.label != false) {
document.getElementById(knobInUse.id).childNodes[1].innerHTML =
knobInUse.currentKnob.currentValue;
}
//change the image position to match
let sum =
(Math.floor(
((knobInUse.currentKnob.currentValue - knobInUse.currentKnob.lowVal) *
knobInUse.currentKnob.scaler) /
2
) +
0) *
knobInUse.currentKnob.size;
let newY = `translateY(-${sum}px)`;
//access to the image goes: container div > image wrapper div > image tag
document.getElementById(
knobInUse.id
).childNodes[0].childNodes[0].style.transform = newY;
//the knob change function call that users can hook into
if (typeof knobChanged == "function") {
knobChanged(knobInUse.id, knobInUse.currentKnob.currentValue);
}
}
});
//touchmove global event handler > does the bulk of the work
document.body.addEventListener("touchmove", function(e) {
if (knobInUse.id != "") {
//console.log(e.pageY); //for testing
//freeze mouse drag activity if user hits top or bottom of the page
if (
e.targetTouches[0].pageY <= 10 ||
e.targetTouches[0].pageY >= document.body.clientHeight - 10
) {
knobInUse = { id: "", initY: 0, currentKnob: null };
return;
} else {
//calculate new knob value
knobInUse.currentKnob.currentValue = Math.round(
knobInUse.value +
((knobInUse.initY - e.targetTouches[0].pageY) *
knobInUse.currentKnob.sensitivity) /
knobInUse.currentKnob.scaler
);
//use max/min variables for easier reading
let max = knobInUse.currentKnob.highVal,
min = knobInUse.currentKnob.lowVal;
//ensure the know value does not exceed max and/or minimum values
if (knobInUse.currentKnob.currentValue > max) {
knobInUse.currentKnob.currentValue = max;
} else if (knobInUse.currentKnob.currentValue < min) {
knobInUse.currentKnob.currentValue = min;
}
}
//update label (if user wants labels)
if (knobInUse.currentKnob.label != false) {
document.getElementById(knobInUse.id).childNodes[1].innerHTML =
knobInUse.currentKnob.currentValue;
}
//change the image position to match
let sum =
(Math.floor(
((knobInUse.currentKnob.currentValue - knobInUse.currentKnob.lowVal) *
knobInUse.currentKnob.scaler) /
2
) -
1) *
knobInUse.currentKnob.size;
let newY = `translateY(-${sum}px)`;
//access to the image goes: container div > image wrapper div > image tag
document.getElementById(
knobInUse.id
).childNodes[0].childNodes[0].style.transform = newY;
//the knob change function call that users can hook into
if (typeof knobChanged == "function") {
knobChanged(knobInUse.id, knobInUse.currentKnob.currentValue);
}
}
});
}