-
Notifications
You must be signed in to change notification settings - Fork 0
/
knobs.js
150 lines (142 loc) · 6.33 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
// Function to move the knob
function moveKnob(knobIndex, newAngle, anglesArray) {
if (newAngle >= minangle && newAngle <= maxangle && currentKnob !== null) {
anglesArray[knobIndex] = newAngle;
setAngle(knobIndex, newAngle);
}
if (newAngle >= minangle && newAngle <= 100 && fxCurrentKnob !== null) {
anglesArray[knobIndex] = newAngle;
setAngle(knobIndex, newAngle);
}
}
function calculateRotationAngle(angle) {
return angle * 270 / maxangle;
}
// Set angle and update corresponding value
function setAngle(knobIndex, angle) {
if(currentKnob !== null){
var knob = knobs[knobIndex];
knob.style.transform = 'rotate(' + calculateRotationAngle(angle) + 'deg)';
if(knobIndex === 0 && editModeActive){
const lamps1 = document.querySelectorAll('.lamp');
if(activeLamp[0] === 6){
anglesStart[6] = angles[0];
lampPosition[6] = (angles[0] / maxangle) * maxPosition;
updateLampPosition(0, lampPosition[6]);
videoPosition = parseInt((lampPosition[6]) / maxPosition * frameIndexMax, 10);
}
else {
lamps1.forEach((lamp, index) => {
if (lamps1[index].classList.contains('on')) {
// Mappa l'angolo da 0 a 270 gradi a una posizione orizzontale da 0px a 500px
anglesStart[index] = angles[0];
lampPosition[index] = (angles[0] / maxangle) * maxPosition; // Calcola la posizione orizzontale
// Trova la small_line e aggiorna la sua posizione
const smallLine = document.querySelectorAll('.small_line'); // Assicurati che la classe sia corretta
if (smallLine[index]) {
updateLampPosition(index+1, lampPosition[index]); // Imposta la posizione orizzontale
}
}
});
}
}
else {
if (!editModeActive){
// Activate Lamp 7 (Manual Lamp)
activeLamp = [6];
// Position of the Manual Line
anglesStart[6] = angles[0];
lampPosition[6] = (angles[0] / maxangle) * maxPosition;
updateLampPosition(0, lampPosition[6]);
if(activePad == 6) {
// Grain Length of the Manual Line
videoPosition = parseInt((lampPosition[6]) / maxPosition * frameIndexMax, 10);
updateGrainLengthFromKnob(angles[1]);
currentGrainLength = Math.max(1, grainLength[index]); // Assicurati che grainLength sia almeno 1
startFrameLoop(videoPosition, currentGrainLength, midiNote);
}
}
}
}
if(fxCurrentKnob !== null){
var knob = fxKnobs[knobIndex];
knob.style.transform = 'rotate(' + angle/100 * 270 + 'deg)';
}
}
// Calculate the new angle based on mouse position
function calculateAngleDelta(lastY, currentY, currentAngle) {
var deltaY = lastY - currentY; // Invert the change
var sensitivity = 1; // Sensitivity of angle change
var newAngle = currentAngle + deltaY * sensitivity;
// Clamp the new angle between the min and max angles
if(currentKnob !== null){
if (newAngle < minangle) newAngle = minangle;
if (newAngle > maxangle) newAngle = maxangle;
}
if(fxCurrentKnob !== null){
if (newAngle < minangle) newAngle = minangle;
if (newAngle > 100) newAngle = 100;
}
return newAngle;
}
// Funzione per aggiornare il frame in base all'angolo del knob
function updateFrameFromKnob(degrees) {
videoPosition = Math.floor((degrees / maxangle) * (frameIndexMax - 1)); // Calcola il frameIndex usando la proporzione
// Imposta il percorso dell'immagine corrispondente al frame
if (editModeActive){
document.getElementById("video_frame").src = `/frames/frame_${videoPosition}.jpg`;
}
}
// Handle dragging
function onDrag(e) {
if (isDragging) {
if(currentKnob !== null) {
var newAngle = calculateAngleDelta(lastY, e.pageY, lastAngle);
moveKnob(currentKnob, newAngle, angles);
lastY = e.pageY; // Update last Y position
lastAngle = newAngle; // Update the angle for continuous movement
// Aggiorna il frame o il grain durante il drag
if(editModeActive){
if (currentKnob === 0 ) {
updateFrameFromKnob(angles[0]);
} else if(currentKnob === 1){
document.getElementById("video_frame").src = `/frames/frame_${videoPosition + grainLength[index]}.jpg`;
}
updateGrainLengthFromKnob(angles[1]);
}
}
if(fxCurrentKnob !== null) {
var fxNewAngle = calculateAngleDelta(fxLastY, e.pageY, fxLastAngle);
moveKnob(fxCurrentKnob, fxNewAngle, fxAngles);
fxLastY = e.pageY; // Update last Y position
fxLastAngle = fxNewAngle; // Update the angle for continuous movement
// Aggiorna i filtri con i nuovi valori
let effects = getEffectValues();
document.getElementById("video_frame").style.filter = `
invert(${effects.invert}%)
hue-rotate(${effects.hueRotate}deg)
saturate(${effects.saturate}%)
grayscale(${effects.grayscale}%)
`;
}
}
}
// Stop dragging
function stopDrag() {
isDragging = false;
// Stop dragging the knob for frame and grain
if (currentKnob !== null) {
if ((grainPixels > (maxPosition-lampPosition[index]))) {
grainPixels = maxPosition-lampPosition[index];
console.log(grainPixels);
grainLength[index] = Math.floor((grainPixels / maxPosition) * frameIndexMax);
}
currentKnob = null; // Reset the current knob for frame and grain
}
// Stop dragging the knob for FX
if (fxCurrentKnob !== null) {
fxCurrentKnob = null; // Reset the current knob for FX
}
document.removeEventListener('mousemove', onDrag); // Remove drag listener
document.removeEventListener('mouseup', stopDrag); // Remove mouseup listener
}