forked from unconed/fuse10
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.js
215 lines (182 loc) · 4.89 KB
/
main.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
var classes = document.documentElement.classList;
classes.add('js');
classes.remove('no-js');
DomReady.ready(function () {
if (Acko.Fallback.isRequired()) {
Acko.Fallback();
}
else {
Acko.Main();
}
});
Acko.Main = function () {
// Do intro or not
var doIntro = location.pathname == '/';
doIntro = true; // For Github release
// Scroll mechanism
var scroll = new Acko.NativeScroll(0, 20000);
var camera = Acko.Effect.Camera.camera;
scroll.init();
// FPS stats
var stats = window.stats = new Stats();
stats.domElement.style.position = 'fixed';
stats.domElement.style.zIndex = 100;
stats.domElement.style.display = 'none';
document.body.appendChild(stats.domElement);
// WebGL renderer
var gl = new Acko.Renderer({
effects: Acko.Effects,
klass: THREE.WebGLRenderer,
camera: camera,
type: 'webgl',
parameters: {
stencil: false,
alpha: false,
antialias: true,
},
autoSize: true,
autoRender: false,
dead: true,
});
gl.init();
// CSS 3D renderer
var cameraElement = document.getElementById('camera-frame');
var css3d = new Acko.Renderer({
effects: Acko.Effects,
klass: THREE.CSS3DRenderer,
camera: camera,
type: 'css3d',
parameters: {
cameraElement: cameraElement,
},
dead: true,
});
css3d.init();
// Navigation and behavior for native content
var nav = new Acko.Nav('native-frame', scroll, gl);
nav.updateExtents();
window.onload = nav.updateExtents();
gl.exports.scrollController = css3d.exports.scrollController = scroll;
gl.exports.doIntro = doIntro;
// Scroll down if not intro
if (!doIntro && (scroll.get().y == 0)) {
scroll.set(0, gl.exports.pageStart - 200);
}
// Export into global scope for console and spaghetti
window.nav = nav;
window.gl = gl;
window.css3d = css3d;
window.scroller = scroll;
// Apply HTML behaviors
Acko.Behaviors.apply(document.body);
// Render loop
var focus = true;
var visible = true;
var last = null;
var force = false;
var cssActive = true;
var idle = false;
var resetTimer = null;
var lastOn = false;
// Delay rendering until mathjax is done
if (window.MathJax) {
visible = false;
MathJax.Hub.Queue(function () {
visible = true;
});
}
// Core
var ii = 0;
function loop() {
requestAnimationFrame(loop);
// Skip frames for debug
/*
ii = (ii+1)%16;
if (ii > 0) return;
*/
// Begin rendering
idle = false;
var s = scroll.get();
var scrolled = !(last && last.x == s.x && last.y == s.y);
// Don't render if not visible or
// a) not doing the intro and
// b) scrolled past the header and
// c) is not scrolling right now and
// d) camera is centered
// e) nothing fancy is happening
var on = visible
&& (false
|| (Time.clock() < 15)
|| !gl.exports.pastMasthead
|| scrolled
|| Math.abs(gl.exports.cameraController.phiT) > .0001
|| gl.exports.visualizer.playing
|| !gl.exports.cameraController.bound
);
last = s;
// Fix for excessive downscaling: reset at top.
if (scrolled && s.y == 0) {
resetTimer = setTimeout(function () {
gl.resize(true);
}, 500);
}
else if (scrolled && resetTimer) {
clearTimeout(resetTimer);
resetTimer = null;
}
// Maintain clocks if visible
if (visible) {
gl.tick();
css3d.tick();
}
// Render conservatively but always do 1 frame more to ensure cleanups happen
if (on || lastOn || force) {
// Sync up two renderer's effect states
if (css3d.exports.masthead) {
gl.exports.masthead = css3d.exports.masthead;
}
// Update GL
gl.visible(visible);
gl.scroll(s);
gl.update();
if (!gl.exports.ride) {
// Sync up two renderer's effect states
css3d.exports.cameraLerp = gl.exports.cameraLerp;
css3d.exports.cameraController = gl.exports.cameraController;
css3d.exports.ride = gl.exports.ride;
css3d.exports.tracks = gl.exports.tracks;
// Update CSS 3D
css3d.visible(visible);
css3d.scroll(s);
css3d.update();
cssActive = !gl.exports.pastMasthead;
}
else {
// Render one last frame of css3d to clean things up
css3d.visible(false);
cssActive && css3d.update();
cssActive = false;
}
}
gl.exports.updateStats && stats.update();
lastOn = on;
force = false;
idle = true;
}
window.addEventListener('resize', function () {
force = true;
});
window.addEventListener('focus', function () {
force = true;
focus = true;
gl.resize(true);
}.bind(this));
window.addEventListener('blur', function () {
focus = false;
});
requestAnimationFrame(loop);
// Debug
window.getIdle = function () {
return idle;
};
};