-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathui.js
More file actions
148 lines (121 loc) · 4.88 KB
/
ui.js
File metadata and controls
148 lines (121 loc) · 4.88 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
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
import Slider from './ui/slider.js';
import ToggleButton from './ui/toggleButton.js';
import Button from './ui/button.js';
import { icons } from './ui/icons.js';
import { getRandomNum } from './helper.js';
// functions
function createEmptyPanel (label) {
const ui = document.querySelector('#ui');
const div = document.createElement('div');
const p = document.createElement('p');
p.textContent = label;
div.appendChild(p);
ui.appendChild(div);
return div;
}
function setRandomSliderValue (slider, min = slider.min, max = slider.max) {
slider.input.value = getRandomNum(min, max, false);
slider.eventHandler();
}
export function createControlPanel ({
mesh, oscillator, updateFn, eraseFn, restartFn, invertFn,
animationToggleFn, saveImgFn
}) {
const buttonsDiv = document.querySelector('#buttons');
const erase = new Button({
parent: buttonsDiv, fn: eraseFn, updateFn: null, label: icons.erase,
title: 'erase', id: 'erase'
});
const restart = new Button({
parent: buttonsDiv, fn: restartFn, updateFn: null, label: icons.restart,
title: 'restart', id: 'restart'
});
const toggleAnimation = new ToggleButton({
parent: buttonsDiv, prop: 'toggle-animation-btn', value: true,
labelTrue: icons.pause, labelFalse: icons.play,
updateFn: animationToggleFn, title: 'play/pause'
});
const invert = new Button({
parent: buttonsDiv, fn: invertFn, updateFn: null, label: icons.yingYang,
title: 'invert colors', id: 'invert-colors'
});
const saveImg = new Button({
parent: buttonsDiv, fn: saveImgFn, updateFn: null, label: icons.saveImg,
title: 'download png', id: 'saveImg'
});
const div1 = createEmptyPanel('Grid options');
const rowNum = new Slider({
parent: div1, max: 30, min: 2, value: 10, label: 'row #',
prop: 'rowNumber', scope: mesh, updateFn: updateFn
});
const sep = new Slider({
parent: div1, max: 65, min: 10, value: 35, label: 'nodes distance',
prop: 'sep', scope: mesh, updateFn: updateFn
});
const radius = new Slider({
parent: div1, max: 10, min: 0, value: 2, label: 'particles radius',
prop: 'radius', scope: mesh, updateFn: updateFn
});
const lineWidth = new Slider({
parent: div1, max: 10, min: 0, value: .3, step: .01, label: 'line width',
prop: 'lineWidth', scope: mesh, updateFn: updateFn
});
const div2 = createEmptyPanel('Spring forces');
const structuralSprings = new Slider({
parent: div2, max: .3, min: 0, value: .2, step: .01, label: 'structural',
prop: 'structuralSpringStrength', scope: mesh, updateFn: updateFn
});
const shearSprings = new Slider({
parent: div2, max: .3, min: 0, value: .08, step: .01, label: 'shear',
prop: 'shearSpringStrength', scope: mesh, updateFn: updateFn
});
const bendSprings = new Slider({
parent: div2, max: .3, min: 0, value: .09, step: .01, label: 'bend',
prop: 'bendSpringStrength', scope: mesh, updateFn: updateFn
});
const div3 = createEmptyPanel('Other options');
const oscAngleVel = new Slider({
parent: div3, max: .1, min: 0, value: .03, step: .001,
label: 'oscillator speed', prop: 'angleVel', scope: oscillator,
updateFn: updateFn
});
const oscVshift = new Slider({
parent: div3, max: 255, min: 0, value: 127.5, step: .5,
label: 'color lightness', prop: 'vShift', scope: oscillator,
updateFn: updateFn
});
const randomOptions = new Button({
parent: buttonsDiv, updateFn: null, label: icons.randomOptions,
title: 'randomize values', id: 'randomOptions',
fn: _ => {
setRandomSliderValue(rowNum);
setRandomSliderValue(sep);
setRandomSliderValue(radius);
setRandomSliderValue(lineWidth);
setRandomSliderValue(structuralSprings);
setRandomSliderValue(shearSprings);
setRandomSliderValue(bendSprings);
setRandomSliderValue(oscAngleVel);
setRandomSliderValue(oscVshift, 87.5, 167.5);
}
});
return {
rowNum, sep, radius, lineWidth,
structuralSprings, shearSprings, bendSprings,
oscAngleVel, oscVshift,
erase, toggleAnimation, saveImg, randomOptions
};
}
export function createTogglePanelButton () {
const panel = document.querySelector('#panel');
const togglePanelDiv = document.querySelector('#toggle-panel');
const toggleButton = new ToggleButton({
parent: togglePanelDiv, prop: 'toggle-panel-btn', value: false,
labelTrue: icons.circleSolid, labelFalse: icons.cross, title: 'open/close menu',
updateFn: function () {
if (this.value) panel.classList.add('hide');
else panel.classList.remove('hide');
}
});
return toggleButton;
}