OSDN Git Service

4e43a43861cb3bbf9300ebb36d58088ccea95665
[chnosproject/AI004.git] / mgcanvas / mgcanvas.js
1 //文字列をキーとする。
2
3 //まず、最も多数のエッジを持つノードを探す。
4 //それを起点にして、エッジをたどる。
5
6 function MGCanvas(canvasDOMObj){
7         var that = this;
8
9         this.initGraphicContext(canvasDOMObj);
10         this.tickPerSecond = 30;
11         this.tickCount = 0;
12         this.tickTimer = window.setInterval(function(){ that.tick(); }, 1000 / this.tickPerSecond);
13         this.nodeList = new Array();
14         this.edgeList = new Array();
15         
16         var that = this;
17         window.addEventListener('keydown', function(event){
18                 switch(event.keyCode){
19                         case 37:        //左カーソル
20                                 that.moveViewRelative(-10, 0);
21                                 break;
22                         case 39:        //右カーソル
23                                 that.moveViewRelative(10, 0);
24                                 break;
25                         case 38:        //上カーソル
26                                 that.moveViewRelative(0, -10);
27                                 break;
28                         case 40:        //下カーソル
29                                 that.moveViewRelative(0, 10);
30                                 break;
31                 }
32         }, true);
33         
34         this.isMouseDown = false;
35         this.mouseDownPosition = new Point2D(0, 0);
36         this.lastMousePosition = new Point2D(0, 0);
37         this.canvas.onmousemove = function (e){
38                 if(that.isMouseDown){
39                         if(!e){
40                                 //for IE
41                                 e = window.event;
42                         }
43                         that.lastMousePosition = that.getMousePositionOnElement(e);
44                 }
45         };
46         this.canvas.onmousedown = function (e){
47                 if(!e){
48                         //for IE
49                         e = window.event;
50                 }
51                 that.lastMousePosition = that.getMousePositionOnElement(e);
52                 that.mouseDownPosition = that.lastMousePosition;
53                 var p = that.convertPointToGraphLayerFromCanvasLayerP(that.lastMousePosition);
54                 console.log(p.x + "," + p.y);
55                 var node = that.getNodeAtPointP(p);
56                 if(node){
57                         node.isSelected = true;
58                 }
59                 that.isMouseDown = true;
60         };
61         this.canvas.onmouseup = function (e){
62                 that.isMouseDown = false;
63         };
64 }
65 MGCanvas.prototype = {
66         setGraph: function(gArray){
67                 //gArray = [[Node1, Node2, Node3, ...], [[Node1, Node3], [Node3, Node2], ...]]
68                 var that = this;
69                 var p = gArray[0];
70                 var n = function(identifier){ return that.nodeList.isIncluded(identifier, function(a, b){ return (a.identifier == b); }); };
71                 
72                 for(var i = 0, iLen = p.length; i < iLen; i++){
73                         this.nodeList.push(new MGNode(this, p[i]));
74                 }
75                 
76                 p = gArray[1];
77                 for(var i = 0, iLen = p.length; i < iLen; i++){
78                         this.edgeList.push(new MGEdge(this, p[i][2], n(p[i][0]), n(p[i][1])));
79                 }
80         },
81         bringToCenter: function(){
82                 // 重心を求めて、それを表示オフセットに設定する
83                 var g = new Point2D(0, 0);
84                 var p;
85                 p = this.nodeList;
86                 for(var i = 0, iLen = p.length; i < iLen; i++){
87                         g.x += p[i].position.x;
88                         g.y += p[i].position.y;
89                 }
90                 g.x /= p.length;
91                 g.y /= p.length;
92                 
93                 this.positionOffset.x = -g.x;
94                 this.positionOffset.y = -g.y;
95         },
96         zoomIn: function(){
97                 this.context.scale(2, 2);
98                 this.currentScale *= 2;
99         },
100         zoomOut: function(){
101                 this.context.scale(0.5, 0.5);
102                 this.currentScale *= 0.5;
103         },
104         moveViewRelative: function(x, y){
105                 this.positionOffset.x += -x;
106                 this.positionOffset.y += -y;
107         },
108         /*
109         bringInScreen: function(){
110                 //大きく外れていないときには動かさない
111                 var g = new Point2D(0, 0);
112                 var p;
113                 p = this.nodeList;
114                 for(var i = 0, iLen = p.length; i < iLen; i++){
115                         g.x += p[i].position.x;
116                         g.y += p[i].position.y;
117                 }
118                 g.x /= p.length;
119                 g.y /= p.length;
120                 if(     g.x < this.displayRect.origin.x / 2 || 
121                         g.x > -this.displayRect.origin.x / 2 || 
122                         g.y < this.displayRect.origin.y / 2 || 
123                         g.y > -this.displayRect.origin.x / 2){
124                         
125                         this.positionOffset.x = -g.x;
126                         this.positionOffset.y = -g.y;
127                 }
128         },
129         */
130         tick: function(){
131                 var p;
132                 var t;
133                 var dr;
134                 var n = null;
135                 var nMax = 0;
136                 var nTemp;
137                 
138                 this.tickCount++;
139                 
140                 //
141                 // View moving with mouse
142                 //
143                 if(this.isMouseDown){
144                         this.moveViewRelative(
145                                 (this.mouseDownPosition.x - this.lastMousePosition.x) * 4 / this.tickPerSecond,
146                                 (this.mouseDownPosition.y - this.lastMousePosition.y) * 4 / this.tickPerSecond
147                         );
148                 }
149                 
150                 //
151                 // Check
152                 //
153                 
154                 p = this.nodeList;
155                 for(var i = 0, iLen = p.length; i < iLen; i++){
156                         nTemp = this.getVectorLength(p[i].vector);
157                         if(nMax < nTemp){
158                                 n = p[i];
159                                 nMax = nTemp;
160                         }
161                 }
162                 if(n){
163                         n.ignoreEdgeRepulsion = 10;
164                 }
165                 
166                 
167                 //
168                 // Move
169                 //
170                 p = this.nodeList;
171                 for(var i = 0, iLen = p.length; i < iLen; i++){
172                         this.nodeList[i].tick();
173                 }
174                 p = this.edgeList;
175                 for(var i = 0, iLen = p.length; i < iLen; i++){
176                         this.edgeList[i].tick();
177                 }
178                 
179                 //
180                 // Refresh
181                 //
182                 dr = this.displayRect;
183                 
184                 this.context.scale(1 / this.currentScale, 1 / this.currentScale);
185                 this.context.clearRect(dr.origin.x, dr.origin.y, dr.size.x, dr.size.y);
186                 this.context.scale(this.currentScale, this.currentScale);
187                 
188                 this.context.translate(this.positionOffset.x, this.positionOffset.y);
189                 
190                 p = this.nodeList;
191                 for(var i = 0, iLen = p.length; i < iLen; i++){
192                         this.nodeList[i].draw();
193                 }
194                 
195                 p = this.edgeList;
196                 for(var i = 0, iLen = p.length; i < iLen; i++){
197                         this.edgeList[i].draw();
198                 }
199                 
200                 this.context.translate(-this.positionOffset.x, -this.positionOffset.y);
201                 
202         },
203         getMousePositionOnElement: function(e){
204                 // http://tmlife.net/programming/javascript/javascript-mouse-pos.html
205                 // retv:
206                 var retv = new Object();
207                 var rect = e.target.getBoundingClientRect();
208                 retv.x = e.clientX - rect.left;
209                 retv.y = e.clientY - rect.top;
210                 return retv;
211         },
212         fillRect: function(x, y, w, h){
213                 var d = this.drawCoordinatesInInteger;
214                 this.context.fillRect(d(x), d(y), d(w), d(h));
215         },
216         strokeRect: function(x, y, w, h){
217                 var d = this.drawCoordinatesInInteger;
218                 this.context.strokeRect(d(x), d(y), d(w), d(h));
219         },
220         drawCoordinatesInInteger: function(coordinateElement){
221                 //from http://www.html5rocks.com/ja/tutorials/canvas/performance/
222                 // With a bitwise or.
223                 return ((0.5 + coordinateElement) | 0);
224         },
225         drawCircle: function(x, y, r){
226                 var d = this.drawCoordinatesInInteger;
227                 this.context.beginPath();
228                 this.context.arc(d(x), d(y), d(r), 0, 2 * Math.PI);
229                 this.context.closePath();
230                 this.context.fill();
231                 this.context.stroke();
232         },
233         drawLineP: function(p, q){
234                 var d = this.drawCoordinatesInInteger;
235                 this.context.beginPath();
236                 this.context.moveTo(d(p.x), d(p.y));
237                 this.context.lineTo(d(q.x), d(q.y));
238                 this.context.closePath();
239                 this.context.stroke();
240         },
241         drawText: function(text, x, y){
242                 //背景をfillStyle
243                 //前景をstrokeStyleで塗りつぶした文字列を描画する
244                 //塗りつぶし高さは20px固定
245                 //座標は文字列の左上の座標となる
246                 var textsize = this.context.measureText(text);
247                 this.context.fillRect(x, y, textsize.width, 20);
248                 this.context.save();
249                 this.context.fillStyle = this.context.strokeStyle;
250                 //fillText引数の座標は文字列の左下!
251                 this.context.fillText(text, x, y + 20 - 1);
252                 this.context.restore();
253         },
254         getVectorLengthP: function(p, q){
255                 return this.getVectorLength(this.getVectorP(p, q));
256         },
257         getVectorLength: function(a){
258                 return Math.sqrt(a.x * a.x + a.y * a.y);
259         },
260         getVectorP: function(p, q){
261                 return new Point2D(q.x - p.x, q.y - p.y);
262         },
263         getUnitVectorP: function(p, q){
264                 var e = this.getVectorP(p, q);
265                 return this.getUnitVector(e);
266         },
267         getUnitVector: function(a){
268                 var l = Math.sqrt(a.x * a.x + a.y * a.y);
269                 a.x /= l;
270                 a.y /= l;
271                 return a;
272         },
273         getNormalUnitVectorSideOfP: function(a, b, p){
274                 //直線ab上にない点pが存在する側へ向かう単位法線ベクトルを返す。
275                 return this.getUnitVector(this.getNormalVectorSideOfP(a, b, p));
276         },
277         getNormalVectorSideOfP: function(a, b, p){
278                 //直線ab上にない点pが存在する側へ向かう法線ベクトルを返す。
279                 //pがab上にある場合は零ベクトルとなる。
280                 var n = this.getVectorP(a, b);
281                 var t = n.x;
282                 var i;
283                 n.x = -n.y;
284                 n.y = t;
285                 
286                 i = this.getInnerVector2D(n, this.getVectorP(a, p));
287                 if(i < 0){
288                         //この法線ベクトルとapの向きが逆なので反転する。
289                         n.x = -n.x;
290                         n.y = -n.y;
291                 } else if(i == 0){
292                         n.x = 0;
293                         n.y = 0;
294                 }
295                 return n;
296         },
297         getExteriorVector2D: function(a, b){
298                 return a.x * b.y - a.y * b.x;
299         },
300         getInnerVector2D: function(a, b){
301                 return a.x * b.x + a.y * b.y;
302         },
303         getDistanceDotAndLineP: function(p, a, b){
304                 // http://www.sousakuba.com/Programming/gs_dot_line_distance.html
305                 var ab;
306                 var ap;
307                 var s;
308                 var l;
309                 var d;
310                 
311                 ab = this.getVectorP(a, b);
312                 ap = this.getVectorP(a, p);
313                 
314                 s = Math.abs(this.getExteriorVector2D(ab, ap));
315                 l = this.getVectorLengthP(a, b);
316                 d = (s / l);
317                 
318                 s = this.getInnerVector2D(ap, ab);
319                 if(s < 0){
320                         //線分の範囲外なので端点aからの距離に変換
321                         //端点から垂線の足までの距離
322                         l = - (s / l);
323                         d = Math.sqrt(d * d + l * l);
324                 } else if(s > l * l){
325                         //同様に端点bからの距離に変換
326                         l = s / l;
327                         d = Math.sqrt(d * d + l * l);
328                 }
329                 return d;
330         },
331         initGraphicContext: function(newCanvas){
332                 this.canvas = newCanvas;
333                 this.context = this.canvas.getContext('2d');
334                 this.context.fillStyle = "rgba(255,255,255,0.5)";
335                 this.context.strokeStyle = "rgba(0, 0, 0, 1)";
336                 this.context.font = "normal 20px sans-serif";
337                 var w = this.canvas.width / 2;
338                 var h = this.canvas.height / 2;
339                 this.context.translate(w, h);
340                 this.displayRect = new Rectangle(-w, -h, this.canvas.width, this.canvas.height);
341                 this.currentScale = 1;
342                 this.positionOffset = new Point2D(0, 0);
343         },
344         convertPointToGraphLayerFromCanvasLayerP: function(pCanvas){
345                 var p = new Point2D(pCanvas.x, pCanvas.y);
346                 // Canvasの中心が原点
347                 p.x -= this.canvas.width / 2;
348                 p.y -= this.canvas.height / 2;
349                 // スケール変換
350                 p.x /= this.currentScale;
351                 p.y /= this.currentScale;
352                 
353                 // オフセット平行移動
354                 p.x -= this.positionOffset.x;
355                 p.y -= this.positionOffset.y;
356                 
357                 return p;
358         },
359         getNodeAtPointP: function(p){
360                 var r = new Rectangle(p.x - 10, p.y - 10, 20, 20);
361                 
362                 var nl = this.nodeList;
363                 for(var i = 0, iLen = nl.length; i < iLen; i++){
364                         if(r.isIncludePointP(nl[i].position)){
365                                 return nl[i];
366                         }
367                 }
368                 return null;
369         }
370 }
371
372 function MGNode(env, identifier){
373         this.env = env;
374         this.identifier = identifier;
375         this.position = new Point2D(0, 0);
376         this.size = 10;
377         //ランダムな初期ベクトルをもつ。
378         this.vector = new Point2D(Math.random() * 2 - 1, Math.random() * 2 - 1);
379         this.friction = (100 - 8) / 100;
380         this.repulsionLengthNode = 90;
381         this.repulsionLengthEdge = 90;
382         this.ignoreEdgeRepulsion = 0;
383         this.strokeStyle = "rgba(0, 0, 0, 1)";
384         this.isSelected = false;
385 }
386 MGNode.prototype = {
387         draw: function(){
388                 if(this.isSelected){
389                         this.env.context.strokeStyle = "rgba(255, 0, 0, 1)";
390                 } else{
391                         this.env.context.strokeStyle = this.strokeStyle;
392                 }
393                 this.env.drawCircle(this.position.x, this.position.y, this.size);
394                 this.env.drawText(this.identifier.toString(), this.position.x + 10, this.position.y + 10);
395         },
396         tick: function(){
397                 var e;
398                 var p;
399                 var l;
400                 var q;
401                 this.position.x += this.vector.x;
402                 this.position.y += this.vector.y;
403                 this.vector.x *= this.friction;
404                 this.vector.y *= this.friction;
405                 
406                 if(!this.ignoreEdgeRepulsion){
407                         //node
408                         //距離の近い点同士には斥力が働くとする。
409                         p = this.env.nodeList;
410                         for(var i = 0, iLen = p.length; i < iLen; i++){
411                                 var q = this.env.nodeList[i];
412                                 if(q != this){
413                                         l = this.env.getVectorLengthP(this.position, q.position);
414                                         if(l < this.repulsionLengthNode && l != 0){
415                                                 e = this.env.getUnitVectorP(q.position, this.position);
416                                                 e.x *= this.repulsionLengthNode / l;
417                                                 e.y *= this.repulsionLengthNode / l;
418                                                 this.vector.x += e.x;
419                                                 this.vector.y += e.y;
420                                         }
421                                 }
422                         }
423                         //edge
424                         //自分を端点に含まないエッジとの距離が近い場合、そのエッジから遠ざかろうとする。
425                         p = this.env.edgeList;
426                         for(var i = 0, iLen = p.length; i < iLen; i++){
427                                 var q = this.env.edgeList[i];
428                                 if(q.node0 != this && q.node1 != this){
429                                         l = this.env.getDistanceDotAndLineP(this.position, q.node0.position, q.node1.position);
430                                         if(l < this.repulsionLengthEdge && l != 0){
431                                                 if(l < 1){
432                                                         l = 1;
433                                                 }
434                                                 e = this.env.getNormalUnitVectorSideOfP(q.node0.position, q.node1.position, this.position);
435                                                 e.x *= this.repulsionLengthEdge / l;
436                                                 e.y *= this.repulsionLengthEdge / l;
437                                                 this.vector.x += e.x;
438                                                 this.vector.y += e.y;
439                                                 q.node0.vector.x -= e.x / 2;
440                                                 q.node0.vector.y -= e.y / 2;
441                                                 q.node1.vector.x -= e.x / 2;
442                                                 q.node1.vector.y -= e.y / 2;
443                                         }
444                                 }
445                         }
446                 } else{
447                         this.ignoreEdgeRepulsion--;
448                 }
449         },
450 }
451
452 function MGEdge(env, identifier, node0, node1){
453         this.env = env;
454         this.identifier = identifier;
455         this.node0 = node0;
456         this.node1 = node1;
457         this.freeLength = 250;
458 }
459 MGEdge.prototype = {
460         draw: function(){
461                 if(this.node0 && this.node1){
462                         this.env.drawLineP(this.node0.position, this.node1.position);
463                 }
464         },
465         tick: function(){
466                 var l = this.env.getVectorLengthP(this.node0.position, this.node1.position);
467                 var e;
468                 if(l > this.freeLength){
469                         e = this.env.getUnitVectorP(this.node0.position, this.node1.position);
470                         e.x *= l / this.freeLength;
471                         e.y *= l / this.freeLength;
472                         this.node0.vector.x += e.x;
473                         this.node0.vector.y += e.y;
474                         this.node1.vector.x -= e.x;
475                         this.node1.vector.y -= e.y;
476                 }
477         },
478 }
479
480 function Point2D(x, y){
481         this.x = x;
482         this.y = y;
483 }
484 Point2D.prototype = {
485         
486 }
487
488 function Rectangle(x, y, width, height){
489         this.origin = new Point2D(x,y);
490         this.size = new Point2D(width,height);
491 }
492 Rectangle.prototype = {
493         isIncludePointP: function(p){
494                 return  (this.origin.x <= p.x) && (p.x <= this.origin.x + this.size.x) &&
495                                 (this.origin.y <= p.y) && (p.y <= this.origin.y + this.size.y);
496         },
497 }
498