Socialify

Folder ..

Viewing loading.js
307 lines (257 loc) • 6.7 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
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// ECMAScript 5 strict mode
/* jshint globalstrict: true*/
/* global THREE, $, document, window,  console */
/* global onLoaded, LOADING_BAR_SCALE,ROWS,COLS,PIECE_SIZE, BOARD_SIZE, FLOOR_SIZE, WIREFRAME, DEBUG, Cell, WHITE, BLACK, FEEDBACK, SHADOW */
"use strict";

var geometries = {};
var textures   = {};


(function() {

	var $bar,$tips;
	var glow;

	function loadResources () {
		// counter
		var loaded = 0;
		// list of all mesh and texture
		var resources = [
			'3D/json/knight.json',
			'3D/json/king.json',
			'3D/json/queen.json',
			'3D/json/bishop.json',
			'3D/json/rook.json',
			'3D/json/pawn.json',
			'3D/json/board.json',
			'3D/json/innerBoard.json',
			'texture/wood-0.jpg',
			'texture/wood-1.jpg',
			'texture/wood_N.jpg',
			'texture/wood_S.jpg',
			'texture/knight-ao.jpg',
			'texture/rook-ao.jpg',
			'texture/king-ao.jpg',
			'texture/bishop-ao.jpg',
			'texture/queen-ao.jpg',
			'texture/pawn-ao.jpg',
			'texture/floor.jpg',
			'texture/floor_N.jpg',
			'texture/floor_S.jpg',
			'texture/fakeShadow.jpg'
		];

		// for loading mesh
		function loadJSON (url) {
			var loader = new THREE.JSONLoader();
			loader.load(url, function(geometry) {

				geometries[url] = geometry;

				loaded++;
				checkLoad();
			});
		}

		// for loading texture
		function loadImage(url) {
			THREE.ImageUtils.loadTexture(
				url,
				THREE.UVMapping(),
				function(texture) {
					textures[url] = texture;
					loaded++;
					checkLoad();
				}
			);
		}

		// load all the resources from the list
		resources.forEach(function(url) {
			switch ( url.split('.').pop() ) {
			case 'json' :
				loadJSON(url);
				break;
			case 'jpg' :
				loadImage(url);
				break;
			default:
				throw 'invalid resource';
			}
		});

		// control the progressBar
		// and fire the onLoaded call back on completion
		function checkLoad () {
			$bar.update(loaded/resources.length);
			if (loaded === resources.length) {
				setTimeout(onLoaded,0.1);
			}
		}

	}

	function initGlow() {
		// create and set the green glow in the background
		var size = window.innerWidth*LOADING_BAR_SCALE*1.8;
		glow = document.createElement('canvas');
		glow.width  = size;
		glow.height = size;
		document.body.appendChild(glow);
		var ctx = glow.getContext('2d');

		// make it oval
		glow.style.width = size + "px";
		glow.style.height = Math.round(size/2) + "px";


		var requestId;
		function animate() {
			var dt = getDelta();
			update(dt);
			requestId = window.requestAnimationFrame(animate);
		}

		function update(dt) {

			ctx.clearRect(0,0,size,size);

			// for the pulse effect
			var cycle = Math.cos(Date.now()/1000 * Math.PI);
			var maxRadius = size/2.5;

			function lerp(a,b,p) {
				return a + (b-a)*p;
			}

			var amplitude = maxRadius * 0.015;
			var sizeOffset = cycle*amplitude;
			var radius = maxRadius - amplitude + sizeOffset;
			var saturation = lerp(70,100,(cycle+1)/2);


			var grd = ctx.createRadialGradient(size/2, size/2, 0, size/2, size/2, radius);
			// fake a non linear gradient
			grd.addColorStop(0,    'hsla(90,'+saturation+'%,50%,0.5)');
			grd.addColorStop(0.125,'hsla(90,'+saturation+'%,50%,0.3828125)');
			grd.addColorStop(0.25, 'hsla(90,'+saturation+'%,50%,0.28125)');
			grd.addColorStop(0.375,'hsla(90,'+saturation+'%,50%,0.1953125)');
			grd.addColorStop(0.5,  'hsla(90,'+saturation+'%,50%,0.125)');
			grd.addColorStop(0.75, 'hsla(90,'+saturation+'%,50%,0.03125)');
			grd.addColorStop(1,    'hsla(90,'+saturation+'%,50%,0.0)');

			// draw the gradient
			ctx.rect(0,0,size,size);
			ctx.fillStyle = grd;
			ctx.fill();
		}

		glow.remove = function() {
			window.cancelAnimationFrame(requestId);
			this.parentNode.removeChild(this);
		};

		var oldTime;
		function getDelta() {
			var now = Date.now();
			if (oldTime === undefined) {
				oldTime = now;
			}
			var delta = (now - oldTime)/1000;
			oldTime = now;
			return delta;
		}

		animate();
	}


	function initTips() {
		// list of tips
		var tips = [
			"Aggregating wood fibers",
			"Generating pieces census report",
			"Testing board resistance",
			"Generating Matrix 8x8",
			"Answering Queen's request",
			"Carving a princess for the knight",
			"Sanding the Bishop",
			"Enrolling Pawns",
			"Generating cheat sheet",
			"Mating the king",
			"Planting virtual trees",
			"Asking Deep Blue for advice",
			"Nominating Bishops",
			"Dubbing Knights",
			"Crowning the King",
			"Waxing chessboard",
			"Evaluating the idea of an hexagonal board, and rejecting it",
			"Gathering extra vertices (just in case)",
			"Trimming edges",
			"Intimidating opponent",
			"Learning the rules"
		];

		//jQuery object for tips
		$tips = $('<div>')
			.attr("id","tips")
			.css("color","white")
			.appendTo($('body'));

		// how often tips changes (in ms)
		var tipTiming = 5000;


		$tips.update = function() {
			var self = this;
			if( tips.length > 0 ) {
				var index = Math.floor(Math.random() * tips.length);

				var sentence = tips[index];
				tips.splice(index,1);
				$(this).text(sentence+"...");
			}
			this.timer = setTimeout(function(){self.update();},tipTiming);
		};

		// this little ugliness is just to clear the timer
		// automagically on .remove()
		var tipsRemove = $tips.remove;
		$tips.remove = function() {
			clearTimeout(this.timer);
			tipsRemove.call(this);
		};
		$tips.update();

	}

	function initBar() {
		// jQuery progress bar
		$bar = $('<div>')
			.attr("id","progressbar")
			.css("width",(LOADING_BAR_SCALE*100)+"%")
			.appendTo($('body'));

		// jQuery progress bar label
		var $label = $('<div>')
			.attr("id","progress-label")
			.appendTo($bar);

		// setting up the progressbar
		$bar.progressbar({
			value:false,
			change: function() {
				$label.text($bar.progressbar("value") + "%");
			}
		});

		// avoid rounded corners
		$bar.removeClass('ui-corner-all');
		$bar.children().removeClass('ui-corner-all');
		$bar.children().removeClass('ui-corner-left');


		// that's where the progression happens
		$bar.update = function(p) {
			p = Math.round(p*100);
			$bar.progressbar( "value", p );
			// somehow need to constantly remove it
			$bar.children().removeClass('ui-corner-right');
		};

		$bar.update(0);

	}

	function centering() {
		$bar.position({
			of:window,
			my:"center center",
			at:"center center"
		});
		$tips.position({
			of:$bar,
			my:"center bottom",
			at:"center top-10"
		});
		$(glow).position({
			of:window,
			my:"center center",
			at:"center center"
		});

		window.addEventListener('resize',centering );
	}

	function removeLoader() {
		$bar.remove();
		$tips.remove();
		glow.remove();
		window.removeEventListener('resize',centering );

	}

	window.onload = function () {
		// the page is loaded
		// start the resource loader
		initGlow();
		initTips();
		initBar();
		centering();

		loadResources();
		//$bar.update(1);
	};

	window.removeLoader = removeLoader;
})();