File: 04-canvas.html

package info (click to toggle)
python-pattern 2.6%2Bgit20150109-3
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 78,672 kB
  • sloc: python: 53,865; xml: 11,965; ansic: 2,318; makefile: 94
file content (88 lines) | stat: -rw-r--r-- 3,046 bytes parent folder | download | duplicates (5)
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
<!-- This example demonstrates the JavaScript source code generated by pattern.graph's export().
     It is a combination of canvas.js and graph.js.
     canvas.js is a simple API for the HTML <canvas> element to generate 2D animated graphics.
     graph.js is a JavaScript implementation of the pattern.graph module.
     Try opening this file in a modern browser (e.g., Chrome).
  -->
<!doctype html>
<html>
	<head>
		<title>graph.js example</title>
		<meta charset="utf-8">
		<script type="text/javascript" src="../../pattern/canvas.js"></script>
		<script type="text/javascript" src="../../pattern/graph/graph.js"></script>
		<style type="text/css">
			body { font: 11px sans-serif; }
			#graph canvas { }
			#graph .node-label { font-size: 11px; }
			#graph { 
				display: inline-block;
				position: relative; 
				overflow: hidden; 
				border: 1px solid #ccc;
			}
			a { color: dodgerblue; }
		</style>
	</head>
	<body>
		<div id="graph" style="width:700px; height:500px;">
			<!-- A canvas.js animation has a <script type="text/canvas">
			     with a JavaScript setup() and draw() function.
			  -->
			<script type="text/canvas">
				function setup(canvas) {
					/* The canvas setup() function is called once before the animation starts.
					 * Set the canvas size and initialize the graph.
					 */
					canvas.size(700, 500);
					// Add random nodes and edges.
					g = new Graph(canvas.element);
					for (var i=0; i < 50; i++) {
						g.addNode(i+1);
					}
					for (var i=0; i < 75; i++) {
						var node1 = choice(g.nodes);
						var node2 = choice(g.nodes);
						g.addEdge(node1, node2, {weight: Math.random()});
					}
					// Calculate node weight (incoming traffic).
					// Calculate node centrality (passing traffic).
					g.eigenvectorCentrality();
					g.betweennessCentrality();
					// Two handy tricks to prettify the layout:
					// 1) Nodes with a higher weight (i.e. incoming traffic) appear bigger.
					// 2) Nodes with only one connection ("leaf" nodes) have a shorter connection.
					for (var i=0; i < g.nodes.length; i++) {
						var n = g.nodes[i];
						n.radius = n.radius + n.radius * n.weight;
					}
					for (var i=0; i < g.nodes.length; i++) {
						var e = g.nodes[i].edges();
						if (e.length == 1) {
							e[0].length *= 0.2;
						}
					}
					g.prune(0);
					g.layout.k = 4.0;        // Force constant (= edge length).
					g.layout.force = 0.01;   // Repulsive strength.
					g.layout.repulsion = 50; // Repulsive radius.
				}
				function draw(canvas) {
					/* The canvas draw() function is called each animation frame.
					 * Update the graph and draw nodes and edges to the canvas.
					 * 
					 */
					if (g.layout.iterations <= 500) {
						fill(0, 0, 0, 0);    // RGBA fill color for all nodes.
						stroke(0, 0, 0, 1);  // RGBA stroke color for all nodes & edges.
						canvas.clear();
						shadow();
						g.update(2);
						g.draw(weighted=0.5, directed=true);
					}
					g.drag(canvas.mouse);    // Enable node dragging.
				}
			</script>
		</div>
	</body>
</html>