-
Notifications
You must be signed in to change notification settings - Fork 1
/
index6.html
163 lines (137 loc) · 4.82 KB
/
index6.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
<!DOCTYPE html>
<html>
<head>
<title>球面ディスプレイ画面</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=no">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://d3js.org/d3.v3.min.js" charset="utf-8"></script>
<script src="https://cdn.socket.io/socket.io-1.4.5.js"></script>
<script>
var __extends = this.__extends || function(d, b) {
for (var p in b)
if (b.hasOwnProperty(p)) d[p] = b[p];
function __() {
this.constructor = d;
}
__.prototype = b.prototype;
d.prototype = new __();
};
// Socket.IOを使って接続
var socket = io.connect(location.origin);
// ルームID(1234)を作成
var roomID = 1234;
// roomIDに入室
socket.emit("pairingFromMain", {
"roomID": roomID
});
// サーバーからsuccessPairingというデータを受信
socket.on("successPairing", function() {
});
socket.on("connect", function() {
//サーバーとの接続に成功した時。
//ノードのデータをもらうためのリクエストを送る。
socket.emit("display-request");
});
socket.on("display-response", function(data) {
//帰ってきたデータを成型する。
var nodes = data.nodes;
var links = data.links;
console.log(data);
//レンダリングをもっかいしてやる。
mainFunc(nodes, links);
})
var mainFunc = function(nodes, links) {
//nodes,linksはサーバーから受け取る予定になっている。
//サーバーとの通信はこの関数は行わない。
//初期化する。#main下を全部けす。
$("#main").empty();
//D3の描画はここから
var width = 1000;
var height = 800;
var i = 0;
var force = d3.layout.force()
.nodes(nodes)
.links(links)
.size([width, height])
.distance(150) // node同士の距離
.charge(-1000)
.start();
var svg = d3.select("#main")
.append("svg")
.attr({
width: width,
height: height
});
var link = svg.selectAll("line")
.data(links)
.enter()
.append("line")
.style({
stroke: "#ccc",
"stroke-width": 1.0
});
var node = svg.selectAll("circle")
.data(nodes)
.enter()
.append("circle")
.attr("r", 48)
.style({
fill: "grey"
})
.call(force.drag);
// nodeのラベル周りの設定
var label = svg.selectAll('text')
.data(nodes)
.enter()
.append('text')
.attr({
"text-anchor": "middle",
"fill": "white",
"font-size": "18px"
})
.text(function(data) {
return data.label;
});
// tickイベント(力学計算が起こるたびに呼ばれるらしいので、座標追従などはここで)
force.on("tick", function() {
link.attr({
x1: function(data) {
return data.source.x;
},
y1: function(data) {
return data.source.y;
},
x2: function(data) {
return data.target.x;
},
y2: function(data) {
return data.target.y;
}
});
node.attr({
cx: function(data) {
return data.x;
},
cy: function(data) {
return data.y;
}
});
label.attr({
x: function(data) {
return data.x;
},
y: function(data) {
return data.y;
}
});
});
}
</script>
</head>
<body>
<div id="main">
</div>
</body>
</html>