-
Notifications
You must be signed in to change notification settings - Fork 8
/
Demo.js
185 lines (165 loc) · 5.99 KB
/
Demo.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
/**
* Created by vlad.chirkov on 10/9/17.
*/
import TrackballControls from 'three-trackballcontrols';
import Stats from 'stats.js';
import dat from 'dat.gui/build/dat.gui';
import WebglStuff from '../src/index';
import * as clipboard from 'clipboard-polyfill/text';
import Noty from 'noty';
import 'noty/lib/noty.css';
const KEYS = [
65, // 'a'
17, // 'ctrl',
16 // 'alt'
];
export default class Demo {
constructor(el, preset = {...WebglStuff.presets.normal}, duration = 3000) {
this.el = el;
this.preset = preset;
this.duration = duration;
this.wgs = new WebglStuff(this.el, WebglStuff.initial, this.preset);
this.sceneSet = this.wgs.sceneSet;
this.gui = new dat.GUI();
this.controls = new TrackballControls(this.sceneSet.camera, this.sceneSet.el);
this.stats = new Stats();
this.initGui();
this.initControls();
this.initStats();
this.bindUpdate();
}
initGui() {
// duration
this.gui.add(this, 'duration')
.min(0).max(60 * 1000)
.step(100)
.onChange(() => this.transit());
this.initGuiPresetButtons().open();
this.initGuiCircles().open();
this.initGuiHighlight();
this.initGuiBackground();
this.initClipboard();
}
initGuiCircles() {
let circles = this.gui.addFolder('Circles settings');
circles.add(this.preset, 'visible')
.min(0).max(WebglStuff.initial.circles)
.step(1)
.onChange(() => this.transit());
circles.add(this.preset, 'opacityStep')
.min(0).max(1)
.step(0.1)
.onChange(() => this.transit());
circles.addColor(this.preset, 'pointsColor')
.onChange(() => this.transit());
circles.add(this.preset, 'impact')
.min(-1).max(1)
.step(0.001)
.onChange(() => this.transit());
circles.add(this.preset, 'stabilityStart')
.min(0.5).max(2)
.step(0.001)
.onChange(() => this.transit());
circles.add(this.preset, 'stabilityEnd')
.min(0.5).max(2)
.step(0.001)
.onChange(() => this.transit());
circles.add(this.preset, 'rotation')
.min(-0.01).max(0.01)
.step(0.00001)
.onChange(() => this.transit());
circles.add(this.preset, 'perlin')
.min(-0.01).max(0.01)
.step(0.00001)
.onChange(() => this.transit());
return circles;
}
initGuiHighlight() {
let highlight = this.gui.addFolder('Highlight settings');
highlight.addColor(this.preset, 'ringColor')
.onChange(() => this.transit());
highlight.add(this.preset, 'opacity')
.min(0).max(1)
.step(0.1)
.onChange(() => this.transit());
return highlight;
}
initGuiBackground() {
let background = this.gui.addFolder('Background settings');
background.addColor(this.preset, 'background')
.onChange(() => this.transit());
background.add(this.preset, 'floatsOpacity')
.min(0).max(1)
.step(0.01)
.onChange(() => this.transit());
return background;
}
initGuiPresetButtons() {
let presetActions = {
'neutral': () => this.transit(WebglStuff.presets.progress.normal, true),
'positive': () => this.transit(WebglStuff.presets.progress.good, true),
'negative': () => this.transit(WebglStuff.presets.progress.bad, true),
'more negative': () => this.transit(WebglStuff.presets.end.bad, true)
};
let presetButtons = this.gui.addFolder('Presets');
presetButtons.add(presetActions, 'neutral');
presetButtons.add(presetActions, 'positive');
presetButtons.add(presetActions, 'negative');
presetButtons.add(presetActions, 'more negative');
return presetButtons;
}
initClipboard() {
let clipboardAction = {
'copy preset': () => {
try {
clipboard.writeText(JSON.stringify(this.preset, null, 4))
.then(() => new Noty({
type: 'success',
text: 'Yay, copied :)',
layout: 'bottomRight',
progressBar: false,
killer: true,
timeout: 1000,
animation: {
close: null
}
}).show());
} catch (e) {
new Noty({
type: 'error',
text: 'Huh, couldn\'t copy :(',
layout: 'bottomRight',
killer: true,
timeout: 1000
}).show()
}
}
};
this.gui.add(clipboardAction, 'copy preset');
return this.gui;
}
transit(preset, update = false) {
if (preset) {
Object.assign(this.preset, preset);
Object.values(this.gui.__folders).forEach(folder => folder.__controllers.forEach(c => c.updateDisplay()));
}
this.wgs.transitTo(this.preset, this.duration);
}
initControls() {
this.controls.zoomSpeed = 1.2;
this.controls.panSpeed = 0.9;
this.controls.noRotate = true;
this.controls.staticMoving = true;
this.controls.dynamicDampingFactor = 0.3;
this.controls.keys = KEYS;
}
initStats() {
this.stats.showPanel(0); // 0: fps, 1: ms, 2: mb, 3+: custom
document.body.appendChild(this.stats.dom);
}
bindUpdate() {
this.wgs.on(WebglStuff.ON_BEFORE_UPDATE, () => this.stats.begin());
this.wgs.on(WebglStuff.ON_UPDATE, () => this.controls.update());
this.wgs.on(WebglStuff.ON_AFTER_UPDATE, () => this.stats.end());
}
}