Skip to content

Commit a0491ca

Browse files
author
Thomas Daniel Galligan
committed
uploaded dots in-class progress
1 parent fd92585 commit a0491ca

File tree

3 files changed

+95
-0
lines changed

3 files changed

+95
-0
lines changed

classes/dots1/index.html

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
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>DOTS</title>
7+
<link rel="stylesheet" href="style.css">
8+
</head>
9+
<body>
10+
<canvas id="canvas"></canvas>
11+
<script src="script.js"></script>
12+
</body>
13+
</html>

classes/dots1/script.js

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var canvas = document.getElementById("canvas");
2+
var context = canvas.getContext("2d");
3+
4+
// frames per second
5+
const FPS = 30;
6+
// size of dots (in pixels)
7+
const DOTSIZE = 20;
8+
// number of dots to add
9+
const DOTCOUNT = 100;
10+
11+
// list of all the dots in the canvas
12+
var dots = [];
13+
14+
// set the size of the canvas to the viewport width
15+
// and height
16+
function setSize() {
17+
console.log("setting the canvas size");
18+
canvas.width = window.innerWidth;
19+
context.canvas.width = window.innerWidth;
20+
canvas.height = window.innerHeight;
21+
context.canvas.height = window.innerHeight;
22+
}
23+
24+
window.addEventListener("resize", (_) => setSize());
25+
setSize();
26+
27+
function getRandomPos(max) {
28+
return Math.floor(Math.random() * max);
29+
}
30+
31+
function getRandomVel() {
32+
var positive = Math.random();
33+
if (positive >= 0.5) {
34+
return Math.random();
35+
} else {
36+
return Math.random() * -1;
37+
}
38+
}
39+
40+
function animate() {
41+
setTimeout(function () {
42+
requestAnimationFrame(animate);
43+
console.log("animate");
44+
45+
if (dots.length === 0) {
46+
for (let i = 0; i < DOTCOUNT; i++) {
47+
dots.push({
48+
x: getRandomPos(context.canvas.width - DOTSIZE),
49+
y: getRandomPos(context.canvas.height - DOTSIZE),
50+
vx: getRandomVel(),
51+
vy: getRandomVel(),
52+
});
53+
}
54+
console.log(dots);
55+
} else {
56+
context.fillStyle = "white";
57+
context.strokeStyle = "white";
58+
context.lineWidth = 0.5;
59+
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
60+
61+
for (let i = 0; i < DOTCOUNT; i++) {
62+
dots[i].x += dots[i].vx;
63+
dots[i].y += dots[i].vy;
64+
65+
context.fillRect(dots[i].x, dots[i].y, DOTSIZE, DOTSIZE);
66+
}
67+
}
68+
}, 1000 / FPS);
69+
}
70+
71+
requestAnimationFrame(animate);

classes/dots1/style.css

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

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