This repository was archived by the owner on Sep 11, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathLabelSphere.js
More file actions
379 lines (329 loc) · 12.3 KB
/
LabelSphere.js
File metadata and controls
379 lines (329 loc) · 12.3 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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
/*
* LabelSphere.js - Displays Blogger labels in a rotating sphere shape.
* Copyright (C) 2010, 2011 Alex Dioso (alex.dioso@ikaika.org)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* Only works in a Blogger gadget (xml).
* Requires Google Libraries API to be loaded before this script
* http://code.google.com/apis/libraries/devguide.html
* Then this script should be loaded with
* gadgets.util.registerOnLoadHandler(function(){ikaika(__MODULE_ID__)});
*/
ikaika = function(moduleId) {
// Provide variables for commonly used things so they can be optimized by
// the closure compiler
var mSqrt = Math['sqrt'];
var mSin = Math['sin'];
var mCos = Math['cos'];
var prefs = new gadgets['Prefs']();
var css = 'css';
var hover = 'hover';
var animate = 'animate';
var w = 'width';
var h = 'height';
var attr = 'attr';
// Variables needed by multiple functions
var container; // Div containing the labels
var size; // Smallest dimension of the container
var numLabels = 0; // Total number of labels found
var labels = new Object(); // Hash of labels
var radius; // TODO
var moveSphereTimer; // TODO
var clearMoveSphereTimer = 0; // If the time should be stopped
var angle; // TODO
var axis = new Object(); // TODO
// Supported skin properties
// http://code.google.com/apis/blogger/docs/gadgets/gadgets_for_blogger.html#BestUIPractices
var gSkinsGetProp = gadgets['skins']['getProperty'];
var fontFace = gSkinsGetProp('FONT_FACE');
// Status messages
var msg = new gadgets['MiniMessage'](moduleId);
// If the gadget is not being used on Blogger
if (!google['hasOwnProperty']('Blog')){
var notBloggerMessage =
msg['createStaticMessage']("Label Sphere only works in Blogger");
return;
}
// Display a loading status message to let the user know something is
// happening
var loadMessage = msg['createStaticMessage']("Loading...");
// Get the number of labels specified in the gadget options
var maxLabels = parseInt(prefs['getString']("maxLabels"), 10);
if (isNaN(maxLabels)){
// Default to 20 labels
maxLabels = 20;
}
// Load jquery and then finish initializing
google['load']('jquery', '1');
google['setOnLoadCallback'](init);
/*
* Performs all the setup for the gadget, gets list of labels from the
* blog's feed, arranges them around a sphere, and sets event handlers to
* rotate the sphere based on mouse position.
*/
function init() {
// Get the div that will contain the labels
container = $('#ikaika-container');
// Set some CSS properties to match the blog
$('*')[css]({
backgroundColor: gSkinsGetProp('CONTENT_BG_COLOR'),
color: gSkinsGetProp('CONTENT_LINK_COLOR'),
fontFamily: fontFace
});
$(container)[css]({
borderColor: gSkinsGetProp('BORDER_COLOR')
});
// Animate the div containing the ikaika link
$('#ikaika')[hover]( ikaikaHoverIn, ikaikaHoverOut);
var blog = new google['Blog'](function() {
blog['getPostsJson'](onLoadPosts);
}, window['name']);
}
/*
* Increase fontsize and opacity of an object when the mouse is over it
*/
function ikaikaHoverIn() {
$(this)[animate]({
opacity: '1',
fontSize: '100%'
},
'fast');
}
/*
* Decrease fontsize and opacity of an object when the mouse isn't over it
*/
function ikaikaHoverOut() {
$(this)[animate]({
opacity: '.5',
fontSize: '70%'
},
'fast');
}
/*
* When the posts are loaded, create the sphere of labels
*/
function onLoadPosts(data) {
// Display an error message if this is a private blog
if (data['text'] ==
"User does not have permission to read this blog.") {
errorMessage("This gadget only works on public blogs");
return;
}
// Find the url for the site
var blogUrl = getBlogUrl(data);
if (typeof blogUrl == "undefined") {
errorMessage("Error getting Website URL");
return;
}
createLabels(data, blogUrl);
if (!numLabels) {
errorMessage("No Labels");
return;
}
createSphere();
msg['dismissMessage'](loadMessage);
setupMouseMove();
setupMouseHover();
}
function errorMessage(txt) {
msg['dismissMessage'](loadMessage);
var errorMsg = msg['createStaticMessage'](txt);
}
function getBlogUrl(data) {
var link = data['data']['feed']['link'];
for (var i in link){
if (link[i]['rel'] == 'alternate'){
return link[i]['href'];
}
}
return undefined;
}
/*
* Create the label elements in html and set some basic css. IE does not
* allow javascript to set the color of links. This creates a div with
* the label as text, then a child div with the actual link. The div
* containing the link is set transparent. The div with the text is given
* the blog's link color.
*/
function createLabels(data, blogUrl) {
var entry = data['data']['feed']['entry'];
// For every blog entry
for (var i in entry) {
var cat = entry[i]['category'];
// For every category that the entry is tagged as
for (var a in cat) {
if (numLabels >= maxLabels){
break;
}
var label = cat[a]['term'];
if (labels['hasOwnProperty'](label)){
// Label has already been added, continue to next label
labels[label].num++;
continue;
}
$(container)['append'](
'<div id="'+label+'" class="ikaikaLabel" '+
'style="position:absolute;border-width:1">'+
'<a href="'+blogUrl+'search/label/'+label+
'" target="_parent">'+
label+
'</a>'+
'</div>');
labels[label] = new Object();
labels[label].num = 1;
numLabels++;
}
}
$('a')[css]({
color: gSkinsGetProp('CONTENT_LINK_COLOR'),
textDecoration: 'none'
});
$('.ikaikaLabel')[hover](
function() {$(this)[css]({borderStyle: 'solid'});},
function() {$(this)[css]({borderStyle: 'none'});}
);
}
/*
* Distribute the labels around the sphere
*/
function createSphere() {
// Find the smaller dimension
if ($(container)[w] > $(container)[h]) {
size = $(container)[h]();
} else {
size = $(container)[w]();
}
// Use the smaller dimension as a basis for the sphere's radius
radius = size * 3 / 11;
// New size to use for movement calculations
size = size / 2 - 1;
var increment = Math['PI'] * (3 - mSqrt(5));
var offset = 2 / numLabels;
// Calculate points around the sphere for each label
$('.ikaikaLabel')['each'](function(i, value){
var label = $(value)[attr]('id');
labels[label].e = value; // Save the label's id for later use
labels[label].y = i * offset - 1 + (offset / 2);
var r = mSqrt(1 - labels[label].y*labels[label].y);
var phi = i * increment;
labels[label].x = mCos(phi)*r;
labels[label].z = mSin(phi)*r;
placeLabel(labels[label], 0, 0);
});
}
/*
* Plae the label on the screen based on its sphere coordinates
*/
function placeLabel(label) {
// Convert to screen coordinates
var y = (label.y * radius) + size;
var z = (label.z * radius) + size;
var x = (label.x * radius);
var f_size = ((label.x + 1)/4*100) + 50;
var opacity = f_size/100;
// Modify the size of the label
$(label.e)[css]({fontSize: f_size + '%'});
// Adjust the screen position based on the new size
y -= $(label.e)[w]()/2;
z -= $(label.e)[h]()/2;
$(label.e)[css]({
bottom: z,
right: y,
zIndex: Math.round(x)
});
$(label.e)['fadeTo'](0,opacity);
}
/*
* When the mouse is over the container, determine the axis to rotate
* around and the angle (speed) to rotate the sphere
*/
function setupMouseMove() {
$(container)['mousemove'](function(e){
var offset = $(container)['offset']();
var mouse_y = (e['pageX'] - offset['left']) - size;
var mouse_z = size - (e['pageY'] - offset['top']);
var mouse_l = mSqrt(mouse_y * mouse_y + mouse_z * mouse_z);
angle = Math['atan2'](mouse_l, radius)/15;
axis.x = 0;
axis.y = (mouse_z)/mouse_l;
axis.z = mouse_y/mouse_l;
});
}
/*
* When the mouse moves into the container, set up a timer callback to
* move the sphere. When the mouse moves out, set a value to signal the
* callback to slow down and eventually stop.
*/
function setupMouseHover() {
$(container)[hover](
function(){
clearMoveSphereTimer = 0;
moveSphereTimer = setTimeout(moveSphere, 33);
},
function(){
clearMoveSphereTimer = 1;
}
);
}
/*
* Calculate each label's new screen coordinates based on the angle of
* the axis and the angle of rotation (speed).
*/
function moveSphere(){
var cos_angle = mCos(angle);
var one_cos_angle = 1 - cos_angle;
var sin_angle = mSin(angle);
for (var i in labels) {
// Change the labels position
var obj_rot = new Object();
var axis_x_obj = new Object();
axis_x_obj.x = (axis.y * labels[i].z) - (axis.z * labels[i].y);
axis_x_obj.y = (axis.z * labels[i].x) - (axis.x * labels[i].z);
axis_x_obj.z = (axis.x * labels[i].y) - (axis.y * labels[i].x);
var axis_d_obj = (axis.x * labels[i].x) + (axis.y * labels[i].y) +
(axis.z * labels[i].z);
// New x
obj_rot.x = (labels[i].x * cos_angle);
obj_rot.x += (axis_x_obj.x * sin_angle);
obj_rot.x += (axis.x * axis_d_obj * one_cos_angle);
// New y
obj_rot.y = (labels[i].y * cos_angle);
obj_rot.y += (axis_x_obj.y * sin_angle);
obj_rot.y += (axis.y * axis_d_obj * one_cos_angle);
// New z
obj_rot.z = (labels[i].z * cos_angle);
obj_rot.z += (axis_x_obj.z * sin_angle);
obj_rot.z += (axis.z * axis_d_obj * one_cos_angle);
labels[i].x = obj_rot.x;
labels[i].y = obj_rot.y;
labels[i].z = obj_rot.z;
placeLabel(labels[i]);
}
// If the mouse is no longer in the container, slow the sphere down
if (clearMoveSphereTimer) {
angle -= angle/20;
// Stop the sphere after it is slower than a certain speed
if (angle < 0.01) {
clearMoveSphereTimer = 0;
clearTimeout(moveSphereTimer);
} else {
moveSphereTimer = setTimeout(moveSphere, 33);
}
} else {
moveSphereTimer = setTimeout(moveSphere, 33);
}
}
};