File size: 31,557 Bytes
db66552 |
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 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 |
<!doctype html>
<html lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<title>learning@home</title>
<meta name="description" content="A library to train large neural networks across the internet. Imagine training GPT-3 on thousands of nodes from universities, companies, and volunteers.">
<link rel="mask-icon" href="logo_small.png">
<link rel="alternate icon" class="js-site-favicon" type="image/png" href="logo.png">
<link rel="icon" class="js-site-favicon" type="image/png" href="logo.png">
<meta property="og:url" content="https://learning-at-home.github.io">
<meta property="og:site_name" content="learning@home">
<meta property="og:title" content="Train vast neural networks together">
<meta property="og:description" content="A library to train large neural networks across the internet. Imagine training one huge transformer on thousands of computers from universities, companies, and volunteers.">
<meta property="og:image" content="https://learning-at-home.github.io/logo_small.png">
<meta property="og:image:type" content="image/png">
<meta property="og:image:width" content="96">
<meta property="og:image:height" content="96">
<meta property="twitter:site" content="https://github.com/learning-at-home/hivemind">
<meta property="twitter:creator" content="learning@home contributors">
<meta property="twitter:card" content="summary_large_image">
<meta property="twitter:title" content="learning@home">
<meta property="twitter:description" content="Hivemind is a library to train large neural networks across the internet. Imagine training one huge transformer on thousands of computers from universities, companies, and volunteers.">
<meta property="twitter:image:src" content="https://learning-at-home.github.io/logo_horizontal.png">
<link rel="stylesheet" href="https://unpkg.com/flickity@2/dist/flickity.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://unpkg.com/flickity@2/dist/flickity.pkgd.min.js"></script>
</head>
<style>
:root {
--border-color: black;
--window-color: white;
--background-move-on-cursor: false;
--background-color: white;
--background-cursor-width: 400;
--background-cursor-height: 200;
--background-show-if-wider-than: 500;
--background-speed: 0.001;
--energy-decay: 0.3;
}
body {
width: 100%;
margin: 0 auto;
background-color: var(--background-color);
}
#container {
position: relative;
width: 100%;
margin: 0 auto;
}
#container canvas, #overlay {
width: 100%;
margin: 0 auto;
position: absolute;
}
canvas {
background-color: var(--background-color);
width: 0px; /* will be changed on init */
overflow: hidden;
}
#main_window {
width: 80%;
min-width: 350px;
margin: 0 auto;
text-align: center;
}
.faded {
margin: 0 auto;
background: var(--window-color);
box-shadow: 0 0 5px 5px var(--window-color);
font-family: cursive;
font-family: "Gill Sans", sans-serif;
display: inline-block
}
.title {
font-size: 48px;
box-shadow: 0 0 5px 5px var(--window-color);
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,
sans-serif,Apple Color Emoji,Segoe UI Emoji;
}
.text {
font-size: 16px;
box-shadow: 0 0 5px 5px var(--window-color);
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,
sans-serif,Apple Color Emoji,Segoe UI Emoji;
}
.scrollbar {
overflow-y: scroll;
}
.arxiv_button {
position: relative;
display: inline-block;
width: 80px;
height: 28px;
background-image: linear-gradient(180deg, #fafbfc, #eff3f6 90%);
color: #24292e;
border: 1px solid rgba(27,31,35,.2);
text-align: center;
cursor: pointer;
border-radius: 4px;
padding-right: 0px;
padding-top: 2.5px;
font-size: 12px;
font-family: -apple-system,BlinkMacSystemFont,Segoe UI,Helvetica,Arial,sans-serif;
font-weight: 600;
}
.arxiv_button:before {
content: "";
vertical-align:middle;
display: inline-block;
width: 24px;
height: 24px;
border: none;
margin-left: -16px;
margin-right: 4px;
margin-top: -2px;
background: url('data:image/svg+xml;charset=UTF-8,<svg xmlns="http://www.w3.org/2000/svg" class="ionicon s-ion-icon" viewBox="0 0 512 512"><path d="M428 224H288a48 48 0 01-48-48V36a4 4 0 00-4-4h-92a64 64 0 00-64 64v320a64 64 0 0064 64h224a64 64 0 0064-64V228a4 4 0 00-4-4z"></path><path d="M419.22 188.59L275.41 44.78a2 2 0 00-3.41 1.41V176a16 16 0 0016 16h129.81a2 2 0 001.41-3.41z"></path></svg>') right center no-repeat;
background-size: 18px 16px;
}
.arxiv_button:hover {
background-color:#e6ebf1;
background-position:-0.5em;
border-color: #9fa4a9;
border-color:rgba(27,31,35,.35);
background-image:linear-gradient(180deg, #f0f3f6, #e6ebf1 90%)
}
a:link {
color: #00194a;
text-decoration: none;
}
a:visited {
color: #3f004a;
text-decoration: none;
}
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 240px;
background-color: #555;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #555 transparent transparent transparent;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
</style>
<body>
<div id="container">
<canvas></canvas>
<div id="overlay">
<div id="main_window">
<div id="header">
<img src="logo.png" id="bug-logo"
style="width: 40%; max-height: 320px; max-width: 320px; z-index:1000; position: relative;">
<br>
<h1 class="faded title" style="margin-top:-3%;">
<p style="margin-top: 0px; margin-bottom:0px;">
<sup style="font-size: 18px">learning@home</sup>
<span id="title_text">hivemind</span>
<sup style="font-size: 18px">v0.10 beta </sup>
</p>
<p style="font-size: 18px; margin-top:0px; margin-bottom:0px;">train vast neural networks together</p>
</h1>
</div>
<div class="faded text" style="margin-top:35px; width: 100%; max-width: 900px;">
A library to train large neural networks across the internet. Imagine training one huge transformer on thousands of computers from universities, companies, and volunteers.
<br><br>
<div style="width:100%; margin: 0 auto;">
<a class="github-button" href="https://github.com/learning-at-home/hivemind" data-size="large" data-show-count="false" aria-label="Star learning-at-home/hivemind on GitHub">Code</a>
<div style="overflow: hidden; white-space: nowrap; margin: 0 auto; display: inline-block;">
<button onclick="window.open('https://arxiv.org/abs/2002.04013');"
class="arxiv_button">Paper</button>
</div>
<a href="https://twitter.com/intent/tweet?hashtags=learningathome,joinhivemind&text=Join%20the%20deep%20learning%20hivemind!%0Alearning-at-home.github.io"
class="twitter-hashtag-button" data-show-count="true" data-size="large">Tweet</a>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</div>
</div>
<hr class="faded" style="width: 70%; margin-top:20px;"><br>
<div class="faded" style="margin-top:35px; display: inline-block; text-align:center;">
<div style="width: 100%; max-width: 440px; display: inline-block; vertical-align: top; text-align:left; padding-right: 5px">
<p class="faded title" style="font-size:28px; margin-bottom:12px">
Why should you care?</p>
<span class="faded text" style="margin-top: 4px;">
Larger neural networks are winning:
<ul style="text-align: left; list-style-position: inside; margin-top: 12px; margin-left: -32px;">
<li style="margin-top: 12px;">
pretrained transformers <a href=https://w4ngatang.github.io/static/papers/superglue.pdf target="_blank" rel="noopener noreferrer">dominate</a> most NLP tasks;</li>
<li style="margin-top: 12px;">
bigger CNNs <a href="https://arxiv.org/abs/1912.11370" target="_blank" rel="noopener noreferrer">perform better</a> at computer vision;</li>
<li style="margin-top: 12px;">
GPT-3 has <a href="https://arxiv.org/abs/2005.14165" target="_blank" rel="noopener noreferrer">175B</a> parameters and <a target="_blank" rel="noopener noreferrer" href="https://arxiv.org/abs/2006.16668">the race continues</a></li>
</ul>
With transfer learning, these large models can harness nearly unlimited raw data to improve performance on both <a href=https://paperswithcode.com/task/language-modelling target="_blank" rel="noopener noreferrer">academic benchmarks</a> and solve <a href=https://medium.com/towards-artificial-intelligence/crazy-gpt-3-use-cases-232c22142044 target="_blank" rel="noopener noreferrer">new unexpected</a> tasks.
<center>
<span style="margin-top: 16px; font-style: italic; font-size: 14px;">
Image credit: [1] <a href="https://arxiv.org/abs/2001.08361" target="_blank" rel="noopener noreferrer">Kaplan et al. (2020)</a>, [2, 3] <a href="https://arxiv.org/abs/1811.06965" target="_blank" rel="noopener noreferrer">Huang et al. (2018)</a>
</span>
</center>
</span>
</div>
<div class style="width: 100%; max-width: 440px; vertical-align: middle; display: inline-block;">
<div class="carousel"
data-flickity='{ "draggable": ">1", "pageDots": false, "groupCells": true}'>
<div class="carousel-cell" style="padding-left:30px; padding-right:30px;">
<img width=340px height=250px src="kaplan1.png">
</div>
<div class="carousel-cell" style="padding-left:30px; padding-right:40px;">
<img width=320px height=250px src="gpipe1.png"></div>
<div class="carousel-cell" style="padding-left:40px; padding-right:30px;">
<img width=320px height=250px src="gpipe2.png"></div>
</div>
</div>
<p class="faded text" style="width: 100%; max-width: 900px; margin-top:16px; text-align: left">
That said, training large neural networks isn't cheap. The hardware used for the <a href="https://arxiv.org/abs/1909.08053" target="_blank" rel="noopener noreferrer">previous largest</a> language model costs over $25 million. A single training run for GPT-3 will set you back <a href="https://lambdalabs.com/blog/demystifying-gpt-3/" target="_blank" rel="noopener noreferrer">at least $4.6M</a> in cloud GPUs. As a result, researchers can't contribute to state-of-the-art deep learning models and practitioners can't build applications without <a href=https://blogs.microsoft.com/ai/openai-azure-supercomputer target="_blank" rel="noopener noreferrer">being supported</a> by a megacorporation. If we want the future of AI to be bright, it can't be private.
</p>
</div>
<br>
<div class="faded" style="margin-top:32px; width: 100%; max-width: 900px; display: inline-block; text-align: left;">
<p class="faded title" style="font-size:28px;">
What is hivemind?</p>
<br>
<p class="faded text" style="margin-top:16px;">
Hivemind is a library for decentralized training of large neural networks. In a nutshell, you want to train a neural network, but all you have is a bunch of enthusiasts with unreliable computers that communicate over the internet. Any peer may fail or leave at any time, but the training must go on. To meet this objective, hivemind models use a specialized layer type: the <b>D</b>ecentralized <b>M</b>ixture of <b>E</b>xperts (DMoE). Here's how it works:<br>
</p>
<div class="container">
<video src="dmoe_demonstration.mp4" style="width:100%; max-width:880px; min-width:320px; margin-top:12px; float:center;" controls poster="poster.png">
</video>
</div>
<br>
<div style="text-align: center">
<div style="width: 100%; max-width: 500px; vertical-align: top; display: inline-block; padding-right: 5px; text-align: left;">
<span class="faded text" style="margin-top:16px">
In a hivemind experiment, all peers:
<ul style="text-align: left; list-style-position: inside; margin-top: 16px; margin-left: -32px;">
<li style="margin-top: 12px;">
host one or more experts depending on their hardware;</li>
<li style="margin-top: 12px;">
run asynchronous training, calling experts from other peers,</li>
<li style="margin-top: 12px;">
form a Distributed Hash Table to discover each other's experts<br>
<span style="padding-left:24px">
- the same type of protocol that powers BitTorrent file sharing.</span>
</li>
</ul>
<p> Hivemind uses <a href=https://pdos.csail.mit.edu/~petar/papers/maymounkov-kademlia-lncs.pdf target="_blank" rel="noopener noreferrer">Kademlia</a>-based DHT that can scale to tens of thousands of peers with logarithmic search complexity.</p>
</span>
</div>
<div style="width:100%; max-width: 385px; vertical-align: top; display: inline-block; margin-top: 0px; align:center;">
<img src="hivemind_components.png" style="width:100%; max-width:240px">
</div>
<p class="faded text" style="text-align: left;">
On each forward pass, a peer first determines what "speciality" of experts is needed to process the current inputs using a small "gating function" module. Then it finds <i>k</i> (e.g. 4) most suitable experts from other peers in the network using the DHT protocol. Finally, it sends forward pass requests to the selected experts, collects their outputs and averages them for the final prediction. Compared to traditional architectures, the Mixture-of-Experts needs much less bandwidth as every input is only sent to a small fraction of all experts.
</p>
<div style="width:100%; max-width: 900px; vertical-align: top; display: inline-block; margin-top: 0px; align:center;">
<img src="dmoe-forward-backward.png" style="width:100%;">
</div>
<p class="faded text" style="text-align: left; margin-top:5px">
More importantly, the decentralized Mixture-of-Experts layers are inherently fault-tolerant: if some of the chosen experts fail to respond, the model will simply average the remaining ones and call that <a href=https://jmlr.org/papers/v15/srivastava14a.html target="_blank" rel="noopener noreferrer">dropout</a>. In the event that all <i>k</i> experts fail simultaneously, a peer will backtrack and find another <i>k</i> experts across the DHT. Finally, since every input is likely to be processed by different experts, hivemind peers run several <a href=https://papers.nips.cc/paper/4390-hogwild-a-lock-free-approach-to-parallelizing-stochastic-gradient-descent target="_blank" rel="noopener noreferrer">asynchronous training</a> batches to better utilize their hardware.
</p>
</div>
</div>
<br>
<div class="faded" style="margin-top:35px; width: 100%; max-width: 900px; align: center; vertical-align: top; display: inline-block; text-align:left;">
<p class="faded title" style="font-size:28px;">
What is hivemind for?
</p>
<br>
<span class="faded text" style="margin-top:15px">
Hivemind is designed for you to:
<ul style="text-align: middle; list-style-position: inside; margin-top: 16px; margin-left: -32px;">
<li style="margin-top: 12px;">
run crowdsourced deep learning using compute from volunteers or decentralized participants; </li>
<li style="margin-top: 12px;">
train neural networks on multiple servers with varying compute, bandwidth and reliability; </li>
<li style="margin-top: 12px;">
<i>[to be announced]</i> join a worldwide open deep learning experiment. </li>
</ul>
<br>
Conversely, here's what it <b>isn't</b> for:
<ul style="text-align: middle; list-style-position: inside; margin-top: 16px; margin-left: -32px;">
<li style="margin-top: 12px;">
splitting your model between 2-3 servers that you fully control: use <a href=https://pytorch.org/docs/stable/rpc.html target="_blank"rel="noopener noreferrer">torch.distributed.rpc</a>;</li>
<li style="margin-top: 12px;">
distributed training for a reliable, uniform and highly connected cluster: use <a href=https://github.com/microsoft/DeepSpeed target="_blank"rel="noopener noreferrer">DeepSpeed</a>; </li>
<li style="margin-top: 12px;">
training <span class="tooltip">small <span class="tooltiptext">More specifically, models that fit into a single worker's memory.</span></span> models with dynamically allocated of in-house workers: use <a href=https://pytorch.org/elastic/0.2.0/index.html>torch.elastic</a>.</li>
</ul>
<p style="margin-top: 16px; text-align:left">
Hivemind v0.8 is in the early alpha stage: the core functionality to train
decentralized models is there, but the inferface is still in active development.
If you want to try hivemind for yourself or contribute to its development,
take a look at the <a href=https://learning-at-home.readthedocs.io/en/latest/user/quickstart.html><u>quickstart tutorial</u></a>.
Feel free to contact us <a href=https://github.com/learning-at-home/hivemind/issues target="_blank" rel="noopener noreferrer">on github</a> with any questions, feedback and issues.
</p>
</span>
</div>
<div style="margin: 0 auto; margin-top: 0px;">
<a class="github-button" href="https://github.com/learning-at-home/hivemind" data-size="large" data-show-count="false" aria-label="Star learning-at-home/hivemind on GitHub">Code</a>
<div style="overflow: hidden; white-space: nowrap; margin: 0 auto; display: inline-block;">
<button onclick="window.open('https://arxiv.org/abs/2002.04013');"
class="arxiv_button">Paper</button>
</div>
<a href="https://twitter.com/intent/tweet?hashtags=joinhivemind,learningathome&text=Join%20the%20deep%20learning%20hivemind!%0Alearning-at-home.github.io"
class="twitter-hashtag-button" data-show-count="true" data-size="large">Tweet</a>
<script async src="https://platform.twitter.com/widgets.js" charset="utf-8"></script>
<script async defer src="https://buttons.github.io/buttons.js"></script>
</div>
<hr style="margin-bottom: 64px; width:0%; border: 0 solid white">
</div>
</div>
</div>
<script>
// draw background; Note: this background is based on https://codepen.io/pawelqcm/pen/oxPYox by Pawel
// Note 2: Pawel, you're awesome.
(function() {
var content_element = document.getElementById("overlay");
var canvas = document.querySelector('canvas');
var title_elem = document.getElementsByClassName("faded title")[0];
var title_text = document.getElementById("title_text");
if (screen.width < 637)
title_text.innerHTML = "<br>hivemind<br>"
ctx = canvas.getContext('2d');
if (!ctx)
console.warn("Your browser does not support canvas, content may be broken :'(");
var SENSITIVITY, SIBLINGS_LIMIT, DENSITY, TOTAL_NODES, ANCHOR_LENGTH, CURSOR_HEIGHT, CURSOR_WIDTH;
css_opts = getComputedStyle(document.documentElement);
SENSITIVITY = css_opts.getPropertyValue('--background-sensitivity') || 120;
SIBLINGS_LIMIT = css_opts.getPropertyValue('--background-siblings') || 7;
NODE_DENSITY = css_opts.getPropertyValue('--background-node-density') || 6;
CURSOR_WIDTH = css_opts.getPropertyValue('--background-cursor-width') || 250;
CURSOR_HEIGHT = css_opts.getPropertyValue('--background-cursor-height') || 250;
CURSOR_VERTICAL_SHRINK = css_opts.getPropertyValue('--background-cursor-vertical-shrink') || 0.1;
SPEED_COEF = css_opts.getPropertyValue('--background-speed') || 1;
ENERGY_DECAY = css_opts.getPropertyValue('--energy-decay') || 2;
SHOW_IF_WIDER_THAN = css_opts.getPropertyValue('--background-show-if-wider-than') || 500;
MOVE_ON_CURSOR = css_opts.getPropertyValue('--background-move-on-cursor').includes("true") || false;
var nodes = [];
choice = (choices => choices[Math.floor(Math.random() * choices.length)])
sample_color = () => choice([[40, 40, 40], [133, 133, 133]])
ANCHOR_LENGTH = 20;
var cursor = {x: 0, y: 0};
function centralize_cursor() {
var rect = document.getElementById("bug-logo").getBoundingClientRect()
var window_left = window.pageXOffset || document.documentElement.scrollLeft;
var window_top = window.pageYOffset || document.documentElement.scrollTop;
cursor.x = window_left + rect.left + rect.width / 2;
cursor.y = window_top + rect.top + rect.height / 2;
}
function Node(x, y) {
this.anchorX = x;
this.anchorY = y;
this.x = Math.random() * (x - (x - ANCHOR_LENGTH)) + (x - ANCHOR_LENGTH);
this.y = Math.random() * (y - (y - ANCHOR_LENGTH)) + (y - ANCHOR_LENGTH);
this.vx = (Math.random() * 2 - 1) * SPEED_COEF;
this.vy = (Math.random() * 2 - 1) * SPEED_COEF;
this.energy = Math.random() * 100;
this.radius = Math.random();
this.siblings = [];
[this.r, this.g, this.b] = sample_color()
this.brightness = 0;
}
Node.prototype.drawNode = function() {
var color = `rgba(${this.r}, ${this.g}, ${this.b}, ${this.brightness})`;
ctx.beginPath();
ctx.arc(this.x, this.y, 2 * this.radius + 2 * this.siblings.length / SIBLINGS_LIMIT, 0, 2 * Math.PI);
ctx.fillStyle = color;
ctx.fill();
};
Node.prototype.drawConnections = function() {
for (var i = 0; i < this.siblings.length; i++) {
var color = `rgba(133, 133, 133, ${this.brightness})`;
ctx.beginPath();
ctx.moveTo(this.x, this.y);
ctx.lineTo(this.siblings[i].x, this.siblings[i].y);
ctx.lineWidth = 1 - calcDistance(this, this.siblings[i]) / SENSITIVITY;
ctx.strokeStyle = color;
ctx.stroke();
}
};
Node.prototype.moveNode = function() {
this.energy -= ENERGY_DECAY;
if (this.energy < 1) {
this.energy = Math.random() * 100;
if (this.x - this.anchorX < -ANCHOR_LENGTH) {
this.vx = Math.random() * SPEED_COEF;
} else if (this.x - this.anchorX > ANCHOR_LENGTH) {
this.vx = Math.random() * -SPEED_COEF;
} else {
this.vx = Math.random() * SPEED_COEF * 2 - SPEED_COEF;
}
if (this.y - this.anchorY < -ANCHOR_LENGTH) {
this.vy = Math.random() * SPEED_COEF;
} else if (this.y - this.anchorY > ANCHOR_LENGTH) {
this.vy = Math.random() * -SPEED_COEF;
} else {
this.vy = Math.random() * SPEED_COEF * 2 - SPEED_COEF;
}
}
relative_speed_rate = Math.min(canvas.height / 100, 10.0)
this.x += this.vx * this.energy * relative_speed_rate;
this.y += this.vy * this.energy * relative_speed_rate;
};
function initNodes() {
centralize_cursor();
ctx.clearRect(0, 0, canvas.width, canvas.height);
if (canvas.width >= SHOW_IF_WIDER_THAN)
total_nodes = Math.round(NODE_DENSITY * (canvas.width / 100 * canvas.height / 100));
else
total_nodes = 0;
nodes = [];
for (var i = 0; i < total_nodes; i++)
nodes.push(new Node(50 + Math.random() * (canvas.width - 100),
5 + Math.random() * (canvas.height - 10)));
}
function calcDistance(node1, node2) {
return Math.sqrt(Math.pow(node1.x - node2.x, 2) + (Math.pow(node1.y - node2.y, 2)));
}
function findSiblings() {
var node1, node2, distance;
for (var i = 0; i < nodes.length; i++) {
node1 = nodes[i];
node1.siblings = [];
for (var j = 0; j < nodes.length; j++) {
node2 = nodes[j];
if (node1 !== node2) {
distance = calcDistance(node1, node2);
if (distance < SENSITIVITY) {
if (node1.siblings.length < SIBLINGS_LIMIT) {
node1.siblings.push(node2);
} else {
var node_sibling_distance = 0;
var max_distance = 0;
var s;
for (var k = 0; k < SIBLINGS_LIMIT; k++) {
node_sibling_distance = calcDistance(node1, node1.siblings[k]);
if (node_sibling_distance > max_distance) {
max_distance = node_sibling_distance;
s = k;
}
}
if (distance < max_distance) {
node1.siblings.splice(s, 1);
node1.siblings.push(node2);
}
}
}
}
}
}
}
function redrawScene() {
resizeWindow();
ctx.clearRect(0, 0, canvas.width, canvas.height);
findSiblings();
var i, node, distance;
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
scaled_distance = calcDistance({x: cursor.x / CURSOR_WIDTH, y: cursor.y / CURSOR_HEIGHT},
{x: node.x / CURSOR_WIDTH, y: node.y / CURSOR_HEIGHT});
node.brightness = Math.max(1 - scaled_distance, 0);
}
for (i = 0; i < nodes.length; i++) {
node = nodes[i];
if (node.brightness) {
node.drawConnections();
node.drawNode();
}
node.moveNode();
}
requestAnimationFrame(redrawScene);
}
function initHandlers() {
document.addEventListener('resize', resizeWindow);
document.addEventListener('orientationchange', resizeWindow);
if (MOVE_ON_CURSOR) {
document.addEventListener('mousemove', moveHandler);
document.addEventListener('touchmove', moveHandler);
}
}
function resizeWindow(evt) {
var new_width, new_height;
new_width = Math.round(Math.max(title_elem.getBoundingClientRect().right, window.innerWidth))
if (!MOVE_ON_CURSOR)
new_height = Math.round(title_elem.getBoundingClientRect().top - canvas.getBoundingClientRect().top);
else
new_height = Math.round(Math.max(
content_element.offsetHeight, content_element.scrollHeight,
content_element.clientHeight, window.innerHeight));
if (canvas.width != new_width || canvas.height != new_height) {
canvas.width = new_width;
canvas.height = new_height;
initNodes();
}
if (!MOVE_ON_CURSOR)
centralize_cursor();
}
function moveHandler(evt) {
if (evt.type == "mousemove") {
cursor.x = window.pageXOffset + evt.clientX;
cursor.y = window.pageYOffset + evt.clientY;
}
else { // touch event
cursor.x = window.pageXOffset + evt.changedTouches[0].clientX;
cursor.y = window.pageYOffset + evt.changedTouches[0].clientY;
}
}
initHandlers();
initNodes();
redrawScene();
})();
</script>
</body>
</html>
|