forked from timdream/wordcloud2.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
537 lines (485 loc) · 20.1 KB
/
index.html
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
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>wordcloud2.js - tag cloud/Wordle presentation on 2D canvas or HTML</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta name="description" content="">
<meta name="author" content="">
<!-- Le styles -->
<link href="//netdna.bootstrapcdn.com/bootstrap/2.2.2/css/bootstrap.min.css" rel="stylesheet">
<link href="//netdna.bootstrapcdn.com/bootstrap/2.2.2/css/bootstrap-responsive.min.css" rel="stylesheet">
<link href="//fonts.googleapis.com/css?family=Finger+Paint" id="link-webfont" rel="stylesheet">
<style>
@media (min-width: 980px) {
body { padding-top: 60px; }
}
*[hidden] {
display: none;
}
#canvas-container {
overflow-x: auto;
overflow-y: visible;
position: relative;
margin-top: 20px;
margin-bottom: 20px;
}
.canvas {
display: block;
position: relative;
overflow: hidden;
}
.canvas.hide {
display: none;
}
#html-canvas > span {
transition: text-shadow 1s ease, opacity 1s ease;
-webkit-transition: text-shadow 1s ease, opacity 1s ease;
-ms-transition: text-shadow 1s ease, opacity 1s ease;
}
#html-canvas > span:hover {
text-shadow: 0 0 10px, 0 0 10px #fff, 0 0 10px #fff, 0 0 10px #fff;
opacity: 0.5;
}
#box {
pointer-events: none;
position: absolute;
box-shadow: 0 0 200px 200px rgba(255, 255, 255, 0.5);
border-radius: 50px;
cursor: pointer;
}
textarea {
height: 20em;
}
#config-option {
font-family: monospace;
}
select { width: 100%; }
#loading {
animation: blink 2s infinite;
-webkit-animation: blink 2s infinite;
}
@-webkit-keyframes blink {
0% { opacity: 1; }
100% { opacity: 0; }
}
@keyframes blink {
0% { opacity: 1; }
100% { opacity: 0; }
}
</style>
<script type="text/javascript">
if (window.location.hostname === 'timdream.org') {
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-4623408-2']);
_gaq.push(['_trackPageview']);
}
(function() {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
</head>
<body>
<div class="navbar navbar-fixed-top navbar-inverse">
<div class="navbar-inner">
<div class="container">
<a class="brand" href="./">wordcloud2.js</a>
<ul class="nav">
<li class="active"><a href="./">Demo</a></li>
<li><a href="https://github.com/timdream/wordcloud2.js">Code</a></li>
<li><a href="https://github.com/timdream/wordcloud2.js/blob/master/README.md">README</a></li>
<li><a href="https://github.com/timdream/wordcloud2.js/blob/master/API.md">API</a></li>
<li><a href="http://timdream.org/">timdream</a></li>
</ul>
</div>
</div>
</div>
<div class="container">
<p class="lead"><strong>wordcloud2.js</strong>, tag cloud/Wordle presentation on 2D canvas or HTML</p>
<div id="not-supported" class="alert" hidden>
<strong>Your browser is not supported.</strong>
</div>
<form id="form" method="get" action="">
<div class="row">
<div class="span12" id="canvas-container">
<canvas id="canvas" class="canvas"></canvas>
<div id="html-canvas" class="canvas hide"></div>
</div>
<div class="span6">
<button class="btn btn-primary" type="submit">Run</button>
<div class="btn-group">
<button class="btn" type="button" id="btn-canvas" disabled title="Show drawn canvas element.">Canvas</button>
<button class="btn" type="button" id="btn-html-canvas" title="Show Word Cloud with elements.">Elements</button>
</div>
<a class="btn" id="btn-save" href="#" download="wordcloud.png" title="Save canvas">Save Image</a>
<span id="loading" hidden>......</span>
</div>
<div class="span6">
<select id="examples" class="">
<option selected>Examples</option>
<option value="love">Love of the world</option>
<option value="web-tech">Web Technologies</option>
<option value="quick-fox">The quick brown fox</option>
<option value="les-miz">Les Misérables</option>
<option value="red-chamber" lang="zh-tw">紅樓夢</option>
</select>
</div>
</div>
<div class="tabbable">
<ul class="nav nav-tabs">
<li class="active"><a href="#tab-list" data-toggle="tab">List</a></li>
<li><a href="#tab-config" data-toggle="tab">Configuration</a></li>
<li><a href="#tab-dim" data-toggle="tab">Dimension</a></li>
<li><a href="#tab-webfont" data-toggle="tab">Web Font</a></li>
</ul>
<div class="tab-content">
<div class="tab-pane active" id="tab-list">
<textarea id="input-list" placeholder="Put your list here." rows="2" cols="30" class="span12"></textarea>
</div>
<div class="tab-pane" id="tab-config">
<label>Options as a literal Javascript object</label>
<textarea id="config-option" placeholder="Put your literal option object here." rows="2" cols="30" class="span12"></textarea>
<!--
XXX Security consideration:
Do not implement a feature that allow users to submit/share their script here
directly or indirectly. Doing so would invite XSS attack on your site.
-->
<span class="help-block">See <a href="https://github.com/timdream/wordcloud2.js/blob/master/API.md">API</a> document for available options.</span>
</div>
<div class="tab-pane" id="tab-dim">
<label for="config-width">Width</label>
<div class="input-append">
<input type="number" id="config-width" class="input-small" min="1">
<span class="add-on">px</span>
</div>
<span class="help-block">Leave blank to use page width.</span>
<label for="config-height">Height</label>
<div class="input-append">
<input type="number" id="config-height" class="input-small" min="1">
<span class="add-on">px</span>
</div>
<span class="help-block">Leave blank to use 0.65x of the width.</span>
<label for="config-height">Device pixel density (<span title="Dots per 'px' unit">dppx</span>)</label>
<div class="input-append">
<input type="number" id="config-dppx" class="input-mini" min="1" value="1" required>
<span class="add-on">x</span>
</div>
<span class="help-block">Adjust the weight, grid size, and canvas pixel size for high pixel density displays.</span>
</div>
<div class="tab-pane" id="tab-webfont">
<label for="config-css">Extra Web Font CSS</label>
<input type="url" id="config-css" size="40" class="input-xxlarge" placeholder="https://fonts.googleapis.com/css?family=Libre+Baskerville:700" value="https://fonts.googleapis.com/css?family=Finger+Paint">
<span class="help-block">Find your favorite font on <a href="https://www.google.com/webfonts">Google Web Fonts</a>. Re-run if the font didn't load in time.</span>
</div>
</div>
</div>
</form>
</div>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
<script src="//cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/2.2.2/bootstrap.min.js"></script>
<script src="./src/wordcloud2.js"></script>
<script>
'use strict';
jQuery(function ($) {
var $form = $('#form');
var $canvas = $('#canvas');
var $htmlCanvas = $('#html-canvas');
var $canvasContainer = $('#canvas-container');
var $loading = $('#loading');
var $list = $('#input-list');
var $options = $('#config-option');
var $width = $('#config-width');
var $height = $('#config-height');
var $dppx = $('#config-dppx');
var $css = $('#config-css');
var $webfontLink = $('#link-webfont');
if (!WordCloud.isSupported) {
$('#not-supported').prop('hidden', false);
$form.find('textarea, input, select, button').prop('disabled', true);
return;
}
var $box = $('<div id="box" hidden />');
$canvasContainer.append($box);
window.drawBox = function drawBox(item, dimension) {
if (!dimension) {
$box.prop('hidden', true);
return;
}
var dppx = $dppx.val();
$box.prop('hidden', false);
$box.css({
left: dimension.x / dppx + 'px',
top: dimension.y / dppx + 'px',
width: dimension.w / dppx + 'px',
height: dimension.h / dppx + 'px'
});
};
// Update the default value if we are running in a hdppx device
if (('devicePixelRatio' in window) &&
window.devicePixelRatio !== 1) {
$dppx.val(window.devicePixelRatio);
}
$canvas.on('wordcloudstop', function wordcloudstopped(evt) {
$loading.prop('hidden', true);
});
$form.on('submit', function formSubmit(evt) {
evt.preventDefault();
changeHash('');
});
$('#btn-save').on('click', function save(evt) {
var url = $canvas[0].toDataURL();
if ('download' in document.createElement('a')) {
this.href = url;
} else {
evt.preventDefault();
alert('Please right click and choose "Save As..." to save the generated image.');
window.open(url, '_blank', 'width=500,height=300,menubar=yes');
}
});
$('#btn-canvas').on('click', function showCanvas(evt) {
$canvas.removeClass('hide');
$htmlCanvas.addClass('hide');
$('#btn-canvas').prop('disabled', true);
$('#btn-html-canvas').prop('disabled', false);
});
$('#btn-html-canvas').on('click', function showCanvas(evt) {
$canvas.addClass('hide');
$htmlCanvas.removeClass('hide');
$('#btn-canvas').prop('disabled', false);
$('#btn-html-canvas').prop('disabled', true);
});
$('#btn-canvas').prop('disabled', true);
$('#btn-html-canvas').prop('disabled', false);
var $examples = $('#examples');
$examples.on('change', function loadExample(evt) {
changeHash(this.value);
this.selectedIndex = 0;
$examples.blur();
});
var run = function run() {
$loading.prop('hidden', false);
// Load web font
$webfontLink.prop('href', $css.val());
// devicePixelRatio
var devicePixelRatio = parseFloat($dppx.val());
// Set the width and height
var width = $width.val() ? $width.val() : $('#canvas-container').width();
var height = $height.val() ? $height.val() : Math.floor(width * 0.65);
var pixelWidth = width;
var pixelHeight = height;
if (devicePixelRatio !== 1) {
$canvas.css({'width': width + 'px', 'height': height + 'px'});
pixelWidth *= devicePixelRatio;
pixelHeight *= devicePixelRatio;
} else {
$canvas.css({'width': '', 'height': '' });
}
$canvas.attr('width', pixelWidth);
$canvas.attr('height', pixelHeight);
$htmlCanvas.css({'width': pixelWidth + 'px', 'height': pixelHeight + 'px'});
// Set the options object
var options = {};
if ($options.val()) {
options = (function evalOptions() {
try {
return eval('(' + $options.val() + ')');
} catch (error) {
alert('The following Javascript error occurred in the option definition; all option will be ignored: \n\n' +
error.toString());
return {};
}
})();
}
// Set devicePixelRatio options
if (devicePixelRatio !== 1) {
if (!('gridSize' in options)) {
options.gridSize = 8;
}
options.gridSize *= devicePixelRatio;
if (options.origin) {
if (typeof options.origin[0] == 'number')
options.origin[0] *= devicePixelRatio;
if (typeof options.origin[1] == 'number')
options.origin[1] *= devicePixelRatio;
}
if (!('weightFactor' in options)) {
options.weightFactor = 1;
}
if (typeof options.weightFactor == 'function') {
var origWeightFactor = options.weightFactor;
options.weightFactor =
function weightFactorDevicePixelRatioWrap() {
return origWeightFactor.apply(this, arguments) * devicePixelRatio;
};
} else {
options.weightFactor *= devicePixelRatio;
}
}
// Put the word list into options
if ($list.val()) {
var list = [];
$.each($list.val().split('\n'), function each(i, line) {
if (!$.trim(line))
return;
var lineArr = line.split(' ');
var count = parseFloat(lineArr.shift()) || 0;
list.push([lineArr.join(' '), count]);
});
options.list = list;
}
// All set, call the WordCloud()
// Order matters here because the HTML canvas might by
// set to display: none.
WordCloud([$canvas[0], $htmlCanvas[0]], options);
};
var loadExampleData = function loadExampleData(name) {
var example = examples[name];
$options.val(example.option || '');
$list.val(example.list || '');
$css.val(example.fontCSS || '');
$width.val(example.width || '');
$height.val(example.height || '');
};
var changeHash = function changeHash(name) {
if (window.location.hash === '#' + name ||
(!window.location.hash && !name)) {
// We got a same hash, call hashChanged() directly
hashChanged();
} else {
// Actually change the hash to invoke hashChanged()
window.location.hash = '#' + name;
}
};
var hashChanged = function hashChanged() {
var name = window.location.hash.substr(1);
if (!name) {
// If there is no name, run as-is.
run();
} else if (name in examples) {
// If the name matches one of the example, load it and run it
loadExampleData(name);
run();
} else {
// If the name doesn't match, reset it.
window.location.replace('#');
}
}
$(window).on('hashchange', hashChanged);
if (!window.location.hash ||
!(window.location.hash.substr(1) in examples)) {
// If the initial hash doesn't match to any of the examples,
// or it doesn't exist, reset it to #love
window.location.replace('#love');
} else {
hashChanged();
}
});
var examples = {
'web-tech': {
list: '26 Web Technologies\n20 HTML\n20 <canvas>\n' +
'15 CSS\n15 JavaScript\n12 Document Object Model\n12 <audio>\n12 <video>\n12 Web Workers\n12 XMLHttpRequest\n12 SVG\n' +
'9 JSON.parse()\n9 Geolocation\n9 data attribute\n9 transform\n9 transition\n9 animation\n' +
'7 setTimeout\n7 @font-face\n7 Typed Arrays\n7 FileReader API\n7 FormData\n7 IndexedDB\n' +
'7 getUserMedia()\n7 postMassage()\n7 CORS\n6 strict mode\n6 calc()\n6 supports()\n' +
'6 media queries\n6 full screen\n6 notification\n6 orientation\n6 requestAnimationFrame\n' +
'5 border-radius\n5 box-sizing\n5 rgba()\n5 text-shadow\n5 box-shadow\n5 flexbox\n5 viewpoint',
option: '{\n' +
' gridSize: 18,\n' +
' weightFactor: 3,\n' +
' fontFamily: \'Finger Paint, cursive, sans-serif\',\n' +
' color: \'#f0f0c0\',\n' +
' hover: window.drawBox,\n' +
' click: function(item) {\n' +
' alert(item[0] + \': \' + item[1]);\n' +
' },\n' +
' backgroundColor: \'#001f00\'\n' +
'}',
fontCSS: 'https://fonts.googleapis.com/css?family=Finger+Paint'
},
'les-miz': {
list: '30 Les Misérables\n20 Victor Hugo\n15 Jean Valjean\n15 Javert\n15 Fantine\n' +
'15 Cosette\n12 Éponine\n12 Marius\n12 Enjolras\n10 Thénardiers\n10 Gavroche\n' +
'10 Bishop Myriel\n10 Patron-Minette\n10 God\n8 ABC Café\n8 Paris\n8 Digne\n' +
'8 Elephant of the Bastille\n5 silverware\n5 Bagne of Toulon\n5 loaf of bread\n' +
'5 Rue Plumet\n5 revolution\n5 barricade\n4 sewers\n4 Fex urbis lex orbis',
option: '{\n' +
' gridSize: 18,\n' +
' weightFactor: 3,\n' +
' fontFamily: \'Average, Times, serif\',\n' +
' color: function() {\n' +
' return ([\'#d0d0d0\', \'#e11\', \'#44f\'])[Math.floor(Math.random() * 3)]\n' +
' },\n' +
' backgroundColor: \'#333\'\n' +
'}',
fontCSS: 'https://fonts.googleapis.com/css?family=Average'
},
'red-chamber': {
list: '6 紅樓夢\n3 賈寶玉\n3 林黛玉\n3 薛寶釵\n3 王熙鳳\n3 李紈\n3 賈元春\n3 賈迎春\n' +
'3 賈探春\n3 賈惜春\n3 秦可卿\n3 賈巧姐\n3 史湘雲\n3 妙玉\n2 賈政\n2 賈赦\n' +
'2 賈璉\n2 賈珍\n2 賈環\n2 賈母\n2 王夫人\n2 薛姨媽\n2 尤氏\n2 平兒\n2 鴛鴦\n' +
'2 襲人\n2 晴雯\n2 香菱\n2 紫鵑\n2 麝月\n2 小紅\n2 金釧\n2 甄士隱\n2 賈雨村',
option: '{\n' +
' gridSize: 8,\n' +
' weightFactor: 16,\n' +
' fontFamily: \'Hiragino Mincho Pro, serif\',\n' +
' color: \'random-dark\',\n' +
' backgroundColor: \'#f0f0f0\',\n' +
' rotateRatio: 0\n' +
'}'
},
'quick-fox': {
list: '2 The\n2 quick\n3 brown\n5 fox\n3 jumps\n3 over\n3.5 the\n3 lazy\n6 dog\n',
option: '{\n' +
' gridSize: 16,\n' +
' weightFactor: 16,\n' +
' origin: [90, 0],\n' +
' fontFamily: \'Times, serif\',\n' +
' color: \'random-light\',\n' +
' backgroundColor: \'#000\',\n' +
' shuffle: false,\n' +
' rotateRatio: 0\n' +
'}',
width: 180,
height: 480
},
'love' : {
list: (function generateLoveList() {
var list = ['12 Love'];
var nums = [5, 4, 3, 2, 2];
// This list of the word "Love" in language of the world was taken from
// the Language links of entry "Love" in English Wikipedia, with duplicate
// spelling removed.
var words = ('Liebe,ፍቅር,Lufu,حب,Aimor,Amor,Heyran,ভালোবাসা,Каханне,Любоў,Любов,བརྩེ་དུང་།,' +
'Ljubav,Karantez,Юрату,Láska,Amore,Cariad,Kærlighed,Armastus,Αγάπη,Amo,Amol,Maitasun,' +
'عشق,Pyar,Amour,Leafde,Gràdh,愛,爱,પ્રેમ,사랑,Սեր,Ihunanya,Cinta,ᑕᑯᑦᓱᒍᓱᑉᐳᖅ,Ást,אהבה,' +
'ಪ್ರೀತಿ,სიყვარული,Махаббат,Pendo,Сүйүү,Mīlestība,Meilė,Leefde,Bolingo,Szerelem,' +
'Љубов,സ്നേഹം,Imħabba,प्रेम,Ái,Хайр,အချစ်,Tlazohtiliztli,Liefde,माया,मतिना,' +
'Kjærlighet,Kjærleik,ପ୍ରେମ,Sevgi,ਪਿਆਰ,پیار,Miłość,Leevde,Dragoste,' +
'Khuyay,Любовь,Таптал,Dashuria,Amuri,ආදරය,Ljubezen,Jaceyl,خۆشەویستی,Љубав,Rakkaus,' +
'Kärlek,Pag-ibig,காதல்,ప్రేమ,ความรัก,Ишқ,Aşk,محبت,Tình yêu,Higugma,ליבע').split(',');
nums.forEach(function(n) {
words.forEach(function(w) {
list.push(n + ' ' + w);
});
});
return list.join('\n');
})(),
option: '{\n' +
' gridSize: Math.round(16 * $(\'#canvas\').width() / 1024),\n' +
' weightFactor: function (size) {\n' +
' return Math.pow(size, 2.3) * $(\'#canvas\').width() / 1024;\n' +
' },\n' +
' fontFamily: \'Times, serif\',\n' +
' color: function (word, weight) {\n' +
' return (weight === 12) ? \'#f02222\' : \'#c09292\';\n' +
' },\n' +
' rotateRatio: 0.5,\n' +
' backgroundColor: \'#ffe0e0\'\n' +
'}'
}
};
</script>
</body>
</html>