File size: 4,214 Bytes
0936134
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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

	(function(){
		var strokeColor = [ 'rgba(89,48,27,1)','rgba(223,134,74,1)', 
							'rgba(76,31,115,1)','rgba(204,95,211,1)', 
							'rgba(4,100,102,1)','rgba(26,228,225,1)', 
							'rgba(2,64,144,1)','rgba(0,128,254,1)', 
							'rgba(51,75,40,1)','rgba(148,194,99,1)'];
		
		var canvas = document.getElementById("canvas");	
		var ctx = canvas.getContext('2d');
		var startAngles = [], endAngles = [], nextAngles = [];
		var xc, yc, innerCircleRad, outerCircleRad;
		var toValue, fromValue, steps = 1000, piIndex;
		

		function draw(){
			if (canvas.getContext){
				canvas.width = window.innerWidth;
	    		canvas.height = window.innerHeight;
	    		xc = (canvas.width/2);
	    		yc = (canvas.height/2);
	    		innerCircleRad = getRad(10);
				outerCircleRad = getRad(15);

	    		drawOuterCircle(xc, yc, outerCircleRad);
	    		drawText();
	    		fromValue = pi[0];
	    		piIndex = 1;
				setInterval(drawLines,100);					
			}
		}

		function getRad(r){
			if(canvas.height < canvas.width){
				return (yc - (canvas.height/r)) ;
			}else{
				return (xc - (canvas.width/r)) ;
			}
		}

		function xParametricCoord(xc, r, angle){
			return xc + r * Math.cos(angle); 
		}

		function yParametricCoord(yc, r, angle){
			return yc + r * Math.sin(angle);
		}

		function degreesToRad(degrees){
			return (degrees * Math.PI / 180);
		}

		function radToDegrees(rad){
			return (rad * 180 / Math.PI);
		}

		function drawText() {
		    ctx.fillStyle = 'white';
		    ctx.font = "10pt Roboto";
		    var xt, yt, segmentMed;
	    	textCircleRad = getRad(25);
	    	for(var i=0; i<10; i++){
	    		segmentMed = (startAngles[i] + endAngles[i])/2;
	    		xt = xParametricCoord(xc-10, textCircleRad, segmentMed);
	    		yt = yParametricCoord(yc+7, textCircleRad, segmentMed);
	    		ctx.fillText("//"+i, xt, yt);
	    	}
		}

		function drawInnerCircle(xc, yc, innerCircleRad){
			ctx.beginPath();
			ctx.lineWidth = 2;
			ctx.strokeStyle = 'black';
			ctx.arc(xc,yc,40,0,Math.PI*2,true);
			ctx.moveTo(xc+innerCircleRad,yc);
			ctx.arc(xc,yc,innerCircleRad,0,Math.PI*2,true);
			ctx.stroke();
		}

		function drawOuterCircle(xc, yc, outerCircleRad){
			var sectionRad = degreesToRad(36);
			var gapRad = degreesToRad(3);
			var startAngle = gapRad, endAngle;
			for(var i=1; i<=10; i++){
				endAngle = (i*sectionRad);

				ctx.beginPath();
				ctx.lineWidth = 5;
				ctx.strokeStyle = strokeColor[i-1];
				ctx.arc(xc, yc, outerCircleRad, startAngle , endAngle, false);
				ctx.stroke();

				ctx.beginPath();
				ctx.strokeStyle = 'black';
				ctx.arc(xc, yc, outerCircleRad, endAngle, endAngle+gapRad, false);
				ctx.stroke();

				startAngles.push(startAngle);
				nextAngles.push(startAngle);
				endAngles.push(endAngle);
				startAngle = endAngle+gapRad;
			}
		}

		function getNextSequentialAngle(value){
			var nextAngle = nextAngles[value];
			var spacingRatio = 0.005;
			if((nextAngles[value] + spacingRatio) < endAngles[value]){
				nextAngles[value] = nextAngles[value] + spacingRatio;
			}else{
				nextAngles[value] = startAngles[value]+ 0.002;
			}
			return nextAngle;
		}

		function getNextRandomAngle(value){
			return ((Math.random() * startAngles[value]) + endAngles[value]);
		}


		function drawLines(){
			if (canvas.getContext){
				var x0, y0, x1, y1, fromValueNextAngle, toValueNextAngle;
				if(piIndex <= steps){
					ctx.save();
					toValue = pi[piIndex];

					fromValueNextAngle = getNextSequentialAngle(fromValue), toValueNextAngle = getNextSequentialAngle(toValue);
					x0=xParametricCoord(xc, innerCircleRad, fromValueNextAngle);
					y0=yParametricCoord(yc, innerCircleRad, fromValueNextAngle); 
					x1=xParametricCoord(xc, innerCircleRad, toValueNextAngle); 
					y1=yParametricCoord(yc, innerCircleRad, toValueNextAngle);

					console.log("pi");

					ctx.beginPath();
					ctx.strokeStyle = strokeColor[toValue];
					ctx.lineWidth = 1;
					ctx.moveTo(x0, y0);
					ctx.quadraticCurveTo(xc, yc, x1,y1);

					ctx.stroke();
					ctx.restore();
					fromValue = toValue;
					piIndex++;
				}
			}
		}

	draw();	
})();