-
Notifications
You must be signed in to change notification settings - Fork 2
/
ws.js
482 lines (443 loc) · 13.6 KB
/
ws.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
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
(function() {
"use strict";
var word_node = {};
function extract_graph(synsets, query, max_synsets, negate) {
negate = !!negate;
var re = new RegExp(query, "i");
var count_synsets = 0;
var graph = { nodes: [], links: [] };
var word_added = {};
var i;
for (i = 0; i < synsets.length; ++i) {
var synset = synsets[i];
var matched = synset.words.some(function(w) { return re.exec(w); });
if (matched != negate) {
count_synsets += 1;
var synset_node = {
id: "synset:" + i,
synset_id: i,
gloss: synset.gloss
};
var word_nodes = [];
synset.words.forEach(function(w) {
if (!word_added[w]) {
if (!word_node[w]) {
word_node[w] = {
id: w,
word: w
};
}
var n = word_node[w];
n.matched = !!re.exec(w);
word_nodes.push(n);
word_added[w] = true;
}
});
var links = synset.words.map(function(w) {
var wn = word_node[w];
return {
id: "link:" + synset_node.id + ":" + wn.id,
source: synset_node,
target: wn
};
});
graph.nodes = graph.nodes.concat(synset_node, word_nodes);
graph.links = graph.links.concat(links);
if (count_synsets == max_synsets) {
++i;
break;
}
}
}
return {
graph: graph,
num_synsets_checked: i,
num_synsets_matched: count_synsets
};
}
function getCanvasSize() {
return [window.innerWidth, window.innerHeight];
}
function getopt(f) {
window.location.search.slice(1).split("&").forEach(function(param) {
var kv = param.split("=");
var key = decodeURIComponent(kv[0]);
var value = decodeURIComponent(kv[1]);
f(key, value);
});
}
var data_name = "synsets.json";
var limit = 100;
getopt(function(key, value) {
switch (key) {
case "lang":
switch (value) {
case "en":
data_name = "synsets.json";
break;
case "ja":
data_name = "synsets_ja.json";
break;
default:
alert('No data available for language "' + value + '".');
break;
}
break;
case "limit":
value = parseInt(value);
if (isNaN(value)) {
limit = 50;
}
else {
limit = value;
}
break;
}
});
var color = function(h) {
let colorStr = d3.interpolateRainbow(h);
let result = /rgb\((\d+), (\d+), (\d+)\)/.exec(colorStr);
return parseInt(result[1]) * 0x10000 + parseInt(result[2]) * 0x100 + parseInt(result[3]) * 0x1;
};
let [width, height] = getCanvasSize();
let scale = 1.0;
let fgSimulation = d3.forceSimulation()
.alphaTarget(1);
fgSimulation.forceLink = d3.forceLink()
.id(function(d) { return d.id; })
.distance(50);
fgSimulation.forceManyBody = d3.forceManyBody()
.strength(function() {
return -(20**scale);
});
fgSimulation.forceCenter = d3.forceCenter(0, 0);
fgSimulation.forceX = d3.forceX(0)
.strength(0.04);
fgSimulation.forceY = d3.forceY(0)
.strength(0.04);
fgSimulation
.force("link", fgSimulation.forceLink)
.force("charge", fgSimulation.forceManyBody)
.force("center", fgSimulation.forceCenter)
.force("x", fgSimulation.forceX)
.force("y", fgSimulation.forceY);
let bgSimulation = d3.forceSimulation()
.alphaTarget(1);
bgSimulation.forceLink = d3.forceLink()
.id(function(d) { return d.id; })
.distance(50);
bgSimulation.forceManyBody = d3.forceManyBody()
.strength(function() {
return -(20**scale);
});
bgSimulation.forceCenter = d3.forceCenter(0, 0);
bgSimulation.forceX = d3.forceX(0)
.strength(0.04);
bgSimulation.forceY = d3.forceY(0)
.strength(0.04);
bgSimulation
.force("link", bgSimulation.forceLink)
.force("charge", bgSimulation.forceManyBody)
.force("center", bgSimulation.forceCenter)
.force("x", bgSimulation.forceX)
.force("y", bgSimulation.forceY);
const app = new PIXI.Application({
backgroundColor: 0xffffff,
antialias: true,
resolution: window.devicePixelRatio,
});
app.renderer.autoResize = true;
app.stage.interactive = true;
const fgLayer = new PIXI.Container();
const bgLayer = new PIXI.Container();
app.stage.addChild(bgLayer);
app.stage.addChild(fgLayer);
bgLayer.alpha = 0.5;
bgLayer.filters = [new PIXI.filters.BlurFilter()];
bgLayer.interactive = false;
bgLayer.interactiveChildren = false;
document.body.insertBefore(app.view, document.body.firstChild);
window.addEventListener("resize", function() {
let [width, height] = getCanvasSize();
app.renderer.resize(width, height);
// Center the origin of the stage
app.stage.x = width / 2;
app.stage.y = height / 2;
app.stage.hitArea = app.screen.clone();
app.stage.hitArea.x = -width / 2;
app.stage.hitArea.y = -height / 2;
});
window.addEventListener("DOMContentLoaded", function() {
let [width, height] = getCanvasSize();
app.renderer.resize(width, height);
// Center the origin of the stage
app.stage.x = width / 2;
app.stage.y = height / 2;
app.stage.hitArea = app.screen.clone();
app.stage.hitArea.x = -width / 2;
app.stage.hitArea.y = -height / 2;
});
let grab = null;
app.stage.on("pointerdown", e => {
const initLayerPosition = new PIXI.Point(fgLayer.position.x, fgLayer.position.y);
grab = {
initLayerPosition: initLayerPosition,
initPointerPosition: e.data.global.clone(),
data: e.data,
};
});
app.stage.on("pointermove", e => {
if (nodeGrab) {
const dx = nodeGrab.data.global.x - nodeGrab.initPointerPosition.x;
const dy = nodeGrab.data.global.y - nodeGrab.initPointerPosition.y;
nodeGrab.node.fx = nodeGrab.initNodePosition.x + dx;
nodeGrab.node.fy = nodeGrab.initNodePosition.y + dy;
}
else if (grab) {
const dx = grab.data.global.x - grab.initPointerPosition.x;
const dy = grab.data.global.y - grab.initPointerPosition.y;
fgLayer.position.x = grab.initLayerPosition.x + dx;
fgLayer.position.y = grab.initLayerPosition.y + dy;
bgLayer.position.x = grab.initLayerPosition.x + dx;
bgLayer.position.y = grab.initLayerPosition.y + dy;
}
});
app.stage.on("pointerup", e => {
if (nodeGrab) {
nodeGrab.node.fx = null;
nodeGrab.node.fy = null;
}
nodeGrab = null;
grab = null;
});
app.stage.on("pointerupoutside", e => {
if (nodeGrab) {
nodeGrab.node.fx = null;
nodeGrab.node.fy = null;
}
nodeGrab = null;
grab = null;
});
var fgGraph = null;
var bgGraph = null;
function draw(graph) {
graph.links.forEach(d => {
let line = d.graphics;
line.clear();
line.lineStyle(2, 0x888888, 1);
line.moveTo(d.source.x, d.source.y);
line.lineTo(d.target.x, d.target.y);
});
graph.nodes.forEach(d => {
let container = d.graphics;
container.x = d.x;
container.y = d.y;
});
}
let nodeGrab = null;
function makeNodeDraggable(graphics, node) {
graphics.on("pointerdown", e => {
const initNodePosition = new PIXI.Point(node.x, node.y);
nodeGrab = {
initNodePosition: initNodePosition,
initPointerPosition: e.data.global.clone(),
node: node,
data: e.data,
};
e.stopPropagation();
});
}
function updateLayer(layer, graph, omitText) {
omitText = !!omitText;
layer.removeChildren();
graph.links.forEach(d => {
let line = new PIXI.Graphics();
line.lineStyle(2, 0x888888, 1);
line.moveTo(d.source.x, d.source.y);
line.lineTo(d.target.x, d.target.y);
layer.addChild(line);
d.graphics = line;
});
let textStyle = new PIXI.TextStyle({
fontSize: 12,
fill: "#666",
});
graph.nodes.forEach(d => {
let radius = d.word ? 5 : 10;
let fillColor = d.word ? 0x888888 : color((d.synset_id % limit) / limit);
let container = new PIXI.Container();
container.interactive = true;
container.buttonMode = true;
makeNodeDraggable(container, d);
let circle = new PIXI.Graphics();
circle.lineStyle(2, 0xffffff, 1);
circle.beginFill(fillColor)
circle.drawCircle(0, 0, radius)
circle.endFill()
container.addChild(circle);
if (!omitText && d.word) {
// FIXME Coloring according to d.matched is not implemented
let text = new PIXI.Text(d.word, textStyle);
text.anchor.set(0, 0.6);
text.x = 8;
text.y = 0;
container.addChild(text);
}
container.x = d.x
container.y = d.y
layer.addChild(container);
d.graphics = container;
});
}
function setNormalSimulation(simulation, graph) {
simulation.forceLink.distance(50);
simulation.forceManyBody.strength(function(d) {
return -(20**scale);
});
simulation.forceX.strength(0.04);
simulation.forceY.strength(0.04);
simulation
.nodes(graph.nodes)
.on("tick", function() { draw(graph); });
simulation.force("link")
.links(graph.links);
}
function toGraph(str) {
var ws = str.split(/\s/);
var graph = { nodes: [], links: [] };
graph.nodes[0] = { id: 0, text: ws[0] };
for (var i = 1; i < ws.length; ++i) {
graph.nodes.push({ id: i, text: ws[i] });
graph.links.push({ source: i - 1, target: i });
}
return graph;
}
function showMessage(msg) {
var graph = toGraph(msg);
fgGraph = graph;
graph.nodes.forEach(function(d, i) {
d.x = 100 * i;
d.y = 100 * (2 * Math.random() - 1);
});
fgLayer.removeChildren();
graph.links.forEach(d => {
let line = new PIXI.Graphics();
line.lineStyle(2, 0x888888, 1);
line.moveTo(d.source.x, d.source.y);
line.lineTo(d.target.x, d.target.y);
fgLayer.addChild(line);
d.graphics = line;
});
let textStyle = new PIXI.TextStyle({
fontSize: 20,
fill: "#000",
});
graph.nodes.forEach(d => {
let radius = 10 * d.text.length + 10;
let fillColor = color(d.id / graph.nodes.length);
let container = new PIXI.Container();
let circle = new PIXI.Graphics();
circle.lineStyle(2, 0xffffff, 1);
circle.beginFill(fillColor)
circle.drawCircle(0, 0, radius)
circle.endFill()
container.addChild(circle);
let text = new PIXI.Text(d.text, textStyle);
text.anchor.set(0.5, 0.5);
container.addChild(text);
container.x = d.x
container.y = d.y
fgLayer.addChild(container);
d.graphics = container;
});
fgSimulation.forceLink.distance(function(l) {
let r1 = 10 * l.source.text.length + 10;
let r2 = 10 * l.target.text.length + 10;
return 1.5 * (r1 + r2);
});
fgSimulation.forceManyBody.strength(function(d) {
return -5 * d.text.length;
});
fgSimulation.forceX.strength(0.001);
fgSimulation.forceY.strength(0.004);
fgSimulation
.nodes(graph.nodes)
.on("tick", function() { draw(graph); });
fgSimulation.force("link")
.links(graph.links);
}
var synsets = null;
showMessage("Now Loading . . .");
d3.json(data_name).then(function(data) {
synsets = data;
let query = document.querySelector("#query").value;
var r = extract_graph(synsets, query, limit);
if (r.num_synsets_checked < synsets.length) {
d3.select("#message").text(`(+${limit} synsets found)`);
}
else {
d3.select("#message").text("(" + r.num_synsets_matched + " synsets found)");
}
if (query == '') {
fgGraph = r.graph;
bgGraph = { nodes: [], links: [] };
updateLayer(fgLayer, fgGraph);
updateLayer(bgLayer, bgGraph, true);
}
else {
var r2 = extract_graph(synsets, query, limit, true);
fgGraph = r.graph;
bgGraph = r2.graph;
updateLayer(fgLayer, fgGraph);
updateLayer(bgLayer, bgGraph, true);
}
setNormalSimulation(fgSimulation, fgGraph);
setNormalSimulation(bgSimulation, bgGraph);
});
document.addEventListener("keydown", function() {
document.querySelector("#query").focus();
});
d3.select("#query").on("input", function() {
var query = this.value;
var r = extract_graph(synsets, query, limit);
if (r.num_synsets_checked < synsets.length) {
d3.select("#message").text(`(+${limit} synsets found)`);
}
else {
d3.select("#message").text("(" + r.num_synsets_matched + " synsets found)");
}
if (query == '') {
fgGraph = r.graph;
bgGraph = { nodes: [], links: [] };
updateLayer(fgLayer, fgGraph);
updateLayer(bgLayer, bgGraph, true);
}
else {
var r2 = extract_graph(synsets, query, limit, true);
fgGraph = r.graph;
bgGraph = r2.graph;
updateLayer(fgLayer, fgGraph);
updateLayer(bgLayer, bgGraph, true);
}
setNormalSimulation(fgSimulation, fgGraph);
setNormalSimulation(bgSimulation, bgGraph);
});
function on_wheel(e) {
e.stopPropagation();
e.preventDefault();
var delta = e.deltaY // 'wheel' event
|| -e.wheelDeltaY // Webkit's mousewheel event
|| -e.wheelDelta; // other's mousewheel event
if (delta > 0) {
scale -= 0.1;
}
else if (delta < 0) {
scale += 0.1;
}
fgSimulation.forceManyBody.strength(fgSimulation.forceManyBody.strength());
bgSimulation.forceManyBody.strength(bgSimulation.forceManyBody.strength());
}
document.addEventListener("wheel", on_wheel);
document.addEventListener("mousewheel", on_wheel);
document.addEventListener("DOMMouseScroll", on_wheel);
})();