Skip to content

Commit fd92585

Browse files
committed
add dots
1 parent 1a5ad17 commit fd92585

File tree

4 files changed

+133
-0
lines changed

4 files changed

+133
-0
lines changed

classes/dots/index.html

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<!DOCTYPE html>
2+
<html lang="en">
3+
<head>
4+
<meta charset="UTF-8" />
5+
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
6+
<title>Moving Dots</title>
7+
<link rel="stylesheet" href="style.css" />
8+
</head>
9+
<body>
10+
<canvas id="canvas"></canvas>
11+
12+
<script type="module" src="script.js"></script>
13+
</body>
14+
</html>

classes/dots/script.js

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
// get canvas and get a context for said canvas
2+
var canvas = document.getElementById("canvas");
3+
var ctx = canvas.getContext("2d");
4+
5+
// frames per second to render
6+
const FPS = 200;
7+
8+
// constant size of dots that does not change
9+
const DOTSIZE = 3;
10+
const DOTCOUNT = 500;
11+
const DOTDISTANCE = 100;
12+
13+
// { x: number, y: number, vx: number, vy: number }
14+
var dots = [];
15+
16+
/**
17+
* return a random number between 0 and max
18+
*
19+
* @param {number} max
20+
*/
21+
function randomPosNumber(max) {
22+
return Math.floor(Math.random() * max);
23+
}
24+
25+
function randomVelNumber() {
26+
const random = Math.random();
27+
if (random > 0.5) {
28+
return Math.random() * -1;
29+
} else {
30+
return Math.random() * 0.5;
31+
}
32+
}
33+
34+
/**
35+
* resize the canvas
36+
*/
37+
function setCanvasSize() {
38+
canvas.width = window.innerWidth;
39+
ctx.canvas.width = window.innerWidth;
40+
canvas.height = window.innerHeight;
41+
ctx.canvas.height = window.innerHeight;
42+
}
43+
44+
function animate() {
45+
setTimeout(function () {
46+
requestAnimationFrame(animate);
47+
ctx.fillStyle = "white";
48+
ctx.strokeStyle = "white";
49+
ctx.lineWidth = 0.5;
50+
51+
// if dots havent been initialized yet, initialize
52+
if (dots.length == 0) {
53+
for (let i = 0; i < DOTCOUNT; i++) {
54+
dots.push({
55+
x: randomPosNumber(ctx.canvas.width),
56+
y: randomPosNumber(ctx.canvas.width),
57+
vx: randomVelNumber(),
58+
vy: randomVelNumber(),
59+
});
60+
}
61+
} else {
62+
// animate
63+
ctx.clearRect(0, 0, ctx.canvas.width, ctx.canvas.height);
64+
console.log("animate");
65+
dots.forEach((dot) => {
66+
dot.x += dot.vx;
67+
dot.y += dot.vy;
68+
69+
if (dot.x > ctx.canvas.width - DOTSIZE || dot.x < 0) {
70+
dot.vx *= -1;
71+
dot.x += dot.vx;
72+
}
73+
74+
if (dot.y > ctx.canvas.height - DOTSIZE || dot.y < 0) {
75+
dot.vy *= -1;
76+
dot.y += dot.vy;
77+
}
78+
ctx.fillRect(dot.x, dot.y, DOTSIZE, DOTSIZE);
79+
});
80+
81+
for (let i = 0; i < DOTCOUNT; i++) {
82+
for (let j = 0; j < DOTCOUNT; j++) {
83+
let dot1 = dots[i];
84+
let dot2 = dots[j];
85+
if (
86+
Math.sqrt(
87+
Math.pow(dot2.x - dot1.x, 2) + Math.pow(dot2.y - dot1.y, 2) * 1.0
88+
) < DOTDISTANCE
89+
) {
90+
ctx.beginPath(); // start a path
91+
ctx.moveTo(dot1.x, dot1.y);
92+
ctx.lineTo(dot2.x, dot2.y);
93+
ctx.stroke(); // fill line
94+
}
95+
}
96+
}
97+
}
98+
}, 1000 / FPS);
99+
}
100+
101+
// whenever window is resized, change the canvas width and height to be full screen
102+
window.addEventListener("resize", (_e) => setCanvasSize());
103+
setCanvasSize();
104+
105+
requestAnimationFrame(animate);

classes/dots/style.css

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
body {
2+
width: 100dvw;
3+
height: 100dvh;
4+
overflow: hidden;
5+
background: #121212;
6+
margin: 0;
7+
padding: 0;
8+
}
9+
10+
#canvas {
11+
width: 100dvw;
12+
height: 100dvh;
13+
}

index.html

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ <h2>Navigation links</h2>
2020
<li><a href="./classes/coder-dojo-homepage">CoderDojo homepage</a></li>
2121
<li><a href="./classes/noughts-and-crosses/">X's and O's game</a></li>
2222
<li><a href="./classes/sonic-speed/">Sonic Speed</a></li>
23+
<li><a href="./classes/dots/">Moving Dots</a></li>
2324
</ul>
2425
</nav>
2526

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy