-
Notifications
You must be signed in to change notification settings - Fork 10
/
index.html
220 lines (194 loc) · 8.96 KB
/
index.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
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<script src="https://code.jquery.com/jquery-3.1.1.min.js" crossorigin="anonymous"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.css">
<script src="https://cdn.jsdelivr.net/npm/semantic-ui@2.4.2/dist/semantic.min.js"></script>
<title>Chaos Game by rachitiitr</title>
<style>
body {
min-width: 820px;
}
#player {
margin: auto;
height: 20px;
}
#container {
display: flex;
flex-direction: column;
align-items: center;
justify-content: center;
}
#container * {
margin: 10px;
}
.footer-menu {
margin: auto;
}
</style>
</head>
<body >
<section name="header">
<div class="ui inverted menu">
<a class="item">
<img src="./rachit_dp.png"/> Chaos in Javascript
</a>
<a class="item" target="_blank" href="https://github.com/rachitiitr/chaos-in-javascript/">
<i class="github icon"></i> Star on GitHub
</a>
<a class="item">
<div id="___ytsubscribe_0" style="text-indent: 0px; margin: 0px; padding: 0px; background: transparent; border-style: none; float: none; line-height: normal; font-size: 1px; vertical-align: baseline; display: inline-block; width: 117px; height: 24px;"><iframe ng-non-bindable="" frameborder="0" hspace="0" marginheight="0" marginwidth="0" scrolling="no" style="position: static; top: 0px; width: 117px; margin: 0px; border-style: none; left: 0px; visibility: visible; height: 24px;" tabindex="0" vspace="0" width="100%" id="I0_1584296427540" name="I0_1584296427540" src="https://www.youtube.com/subscribe_embed?usegapi=1&channelid=UC9fDC_eBh9e_bogw87DbGKQ&layout=default&count=default&origin=https%3A%2F%2Frachitiitr.com&gsrc=3p&ic=1&jsh=m%3B%2F_%2Fscs%2Fapps-static%2F_%2Fjs%2Fk%3Doz.gapi.en.OfYsKuVZ3qI.O%2Fam%3DwQE%2Fd%3D1%2Fct%3Dzgms%2Frs%3DAGLTcCMVod3aO7ybjljp3cyn2IsEoP0pUg%2Fm%3D__features__#_methods=onPlusOne%2C_ready%2C_close%2C_open%2C_resizeMe%2C_renderstart%2Concircled%2Cdrefresh%2Cerefresh%2Conload&id=I0_1584296427540&_gfid=I0_1584296427540&parent=https%3A%2F%2Frachitiitr.com&pfname=&rpctoken=35354048" data-gapiattached="true"></iframe></div>
</a>
<audio controls autoplay loop id="player">
<source src="./background_music.webm">
</audio>
</div>
</section>
<section name="canvas">
<div class="ui placeholder segment">
<canvas id="mycanvas" width="800" height="600" style="display:block;margin:0 auto"></canvas>
</div>
</section>
<div id="container">
<div class="ui floating message">
Enter number of bigger vertices:
<div class="ui input focus">
<input id="nthsidedpolygon" type="number" placeholder="must be greater than 2" style="width:200px"/>
</div>
<br/>
Enter %age of distance to travel:
<div class="ui input focus">
<input id="distratio" type="number" placeholder="Try 61.8 i.e GoldenRatio Inverse" style="width:200px"/>
</div>
<br/>
</div>
<button class="ui primary button" onclick="playForSide()">
Amaze me!
</button>
<div class="ui divider"></div>
<div class="ui styled accordion">
<div class="active title">
<i class="dropdown icon"></i>
How this works?
</div>
<div class="active content">
<ul class="list">
<li>We start with a random point.</li>
<li>In each step, we chose randomly one of the bigger vertex and move 50% (or what ever %age you specified) towards it from our current position.</li>
<li>We keep repeating the previous step till infinity.</li>
</ul>
</div>
<div class="title">
<i class="dropdown icon"></i>
Why I made it?
</div>
<div class="content">
<ul class="list">
<li>Programming is my hobby and I enjoy doing random things in my free time (if I get any)</li>
<li>I saw just one example of JS canvas, and was amazed to imagine the things we can do with it.</li>
<li>I love Math + Fractals. I thoroughly enjoyed making this chaos out of nothing in JavaScript.</li>
</ul>
</div>
<div class="title">
<i class="dropdown icon"></i>
Impressed? Learn with me 😁
</div>
<div class="content">
<ul class="list">
<li>Also checkout<a href="https://rachitiitr.github.io/games-in-js">my Game Library in JavaScript</a> that I am writing from scratch</li>
<li>Watch more videos on my Programming Channel <a href="https://youtube.com/c/RachitJain">here</a></li>
<li>More about me at <a href="https://rachitiitr.com">https://rachitiitr.com</a></li>
</ul>
</div>
</div>
</div>
<section name="footer">
<div class="ui inverted borderless menu">
<a class="item footer-menu" href="https://bit.ly/rachityoutube">
Made with <i class="ui heart icon"></i> by Rachit Jain
</a>
</div>
</section>
<script>
// accordion needs initialization -.-
$(document).ready(function(){
$('.ui.accordion').accordion();
});
let canvas = document.querySelector('#mycanvas');
let canvasContext = canvas.getContext('2d');
let centerX = Math.random() * canvas.width;
let centerY = Math.random() * canvas.height;
let RAD = 1;
let POINTS = [];
let N = 3;
let PADDING = 50;
let distRatio = 0.5;
function playForSide() {
N = document.querySelector('#nthsidedpolygon').value;
if (N < 3) {
alert('vertices must be greater than 2');
return;
}
distRatio = document.querySelector('#distratio').value/100;
if (distRatio > 0 && distRatio < 1) {
// all good here
}
else {
alert('distance to travel must be less than 100%, try 61.8');
document.querySelector('#distratio').value = 61.8;
distRatio = 61.8;
}
init();
}
function colorRect(topLeftX,topLeftY, boxWidth,boxHeight, fillColor) {
canvasContext.beginPath();
canvasContext.fillStyle = fillColor;
canvasContext.fillRect(topLeftX,topLeftY, boxWidth,boxHeight);
canvasContext.closePath();
}
function colorCircle(centerX,centerY, radius, fillColor) {
canvasContext.beginPath();
canvasContext.fillStyle = fillColor;
canvasContext.arc(centerX,centerY, radius, 0,Math.PI*2, true);
canvasContext.fill();
canvasContext.closePath();
}
function draw() {
let W = canvas.width;
let H = canvas.height;
let h = H - PADDING;
let origin = [W/2, h/2+PADDING/2];
let rad = h/2;
let theta = (Math.PI*2)/N;
let target = Math.floor(Math.random() * N);
for (let i=0; i<N; i++) {
let currentTheta = i*theta - (Math.PI/2); //rotating by 90 degs for better aesthetics
POINTS[i] = [origin[0] + rad*Math.cos(currentTheta), origin[1]+rad*Math.sin(currentTheta)];
colorCircle(POINTS[i][0],POINTS[i][1], RAD*10, i == target?'red':'cyan');
}
colorCircle(centerX, centerY, RAD, 'cyan');
centerX = (distRatio * POINTS[target][0]) + (1-distRatio) * centerX;
centerY = (distRatio * POINTS[target][1]) + (1-distRatio) * centerY;
const myTimer = setTimeout(draw, 1000/300);
}
function init() {
colorRect(0,0, canvas.width,canvas.height, 'black');
draw();
}
window.onload = function() {
init();
}
</script>
<!-- Global site tag (gtag.js) - Google Analytics -->
<script async src="https://www.googletagmanager.com/gtag/js?id=UA-142508299-2"></script>
<script>
window.dataLayer = window.dataLayer || [];
function gtag(){dataLayer.push(arguments);}
gtag('js', new Date());
gtag('config', 'UA-142508299-2');
</script>
</body>
</html>