-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEffectsUI.js
More file actions
97 lines (87 loc) · 2.59 KB
/
EffectsUI.js
File metadata and controls
97 lines (87 loc) · 2.59 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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
import 'jquery-ui-touch-punch';
/**
* Create and handle effects user interaction objects
*/
export class EffectsUI {
/**
* @param {string} tagId
* @param {array} effectLevels
* @param {*} effect1
* @param {*} effect2
*/
constructor(tagId, effectLevels, effect1 = null, effect2 = null) {
/**
* @type {string}
*/
this.tagId = tagId;
/**
* @type {HTMLElement}
*/
this.domObject = document.getElementById(this.tagId);
/**
* @type {*}
*/
this.effect1 = effect1;
/**
* @type {*}
*/
this.effect2 = effect2;
/**
* @type {number}
*/
this.effect1Value = effectLevels[0];
/**
* @type {number}
*/
this.effect2Value = effectLevels[1];
this.valueSpans = this.domObject.getElementsByClassName("effectValue")
this.valueSpans[0].innerText = this.effect1Value;
this.valueSpans[1].innerText = this.effect2Value;
this.createHandle();
}
/**
* Create handle for user interactions
*/
createHandle() {
var handle = document.createElement("span");
handle.classList.add("handle");
this.domObject.appendChild(handle);
var parent = $(handle).parent();
var parentWidth = parent.width();
var parentHeight = parent.height();
var handle = $(handle); // switch to jQuery
var handleWidth = handle.outerWidth();
var handleHeight = handle.outerHeight();
// calculate the corners of the handle, add a small buffer because of an error/negative result
handle.offset({
left: parent.offset().left - handleWidth * 0.5 + parentWidth * this.effect1Value,
top: parent.offset().top - handleHeight * 0.5 + parentHeight * this.effect2Value
})
var self = this;
handle.draggable({
containment: "parent",
drag: function() {
var handleOffset = handle.offset();
var left = (handleOffset.left - parent.offset().left) / parentWidth;
var top = (handleOffset.top - parent.offset().top) / parentHeight;
// guard against GUI issues where handle leaves the lower bound
self.effect1Value = left > 0 ? left : 0;
self.effect2Value = top > 0 ? top : 0;
self.updateEffects();
}
});
}
/**
* Read handle location and update effect levels
*/
updateEffects() {
if (this.effect1) {
this.effect1.wet.value = this.effect1Value;
}
if (this.effect2) {
this.effect2.wet.value = this.effect2Value;
}
this.valueSpans[0].innerText = Math.round(this.effect1Value * 10) / 10;
this.valueSpans[1].innerText = Math.round(this.effect2Value * 10) / 10;
}
}