-
Notifications
You must be signed in to change notification settings - Fork 0
/
bkup.js
146 lines (125 loc) · 3.98 KB
/
bkup.js
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
const windowWidth = window.innerWidth;
const windowHeight = window.innerHeight;
const canvas = document.getElementById("app-canvas");
canvas.width = windowWidth;
canvas.height = windowHeight;
canvas.style.position = "absolute";
canvas.style.backgroundColor = "grey";
canvas.style.zIndex = 1;
document.body.appendChild(canvas);
class Scene {
constructor() {
this.orbs = [];
this.squares = [];
this.context = canvas.getContext("2d");
}
addOrb(orb) {
this.orbs.push(orb);
}
addSquare(square) {
this.orbs.push(square);
}
render() {
setInterval(() => {
this.context.clearRect(0, 0, windowWidth, windowHeight);
this.orbs.forEach(object => {
this.orbs.map(connectToObject => {
const color = object.options.color;
const r = object.options.r;
const x1 = object.options.x;
const y1 = object.options.y;
const x2 = connectToObject.options.x;
const y2 = connectToObject.options.y;
const distance = Math.sqrt( (x1-x2) * (x1-x2) + (y1-y2) * (y1-y2) );
if (distance < 500) {
object.connect(this.context, connectToObject);
}
});
object.draw(this.context);
});
}, 10);
}
}
class Orb {
constructor(options) {
this.options = options;
}
connect(ctx, connectTo) {
let x = this.options.x;
let y = this.options.y;
if (this.isXOutOfBounds(x)) return;
if (this.isYOutOfBounds(y)) return;
ctx.beginPath();
ctx.moveTo(this.options.x, this.options.y);
ctx.lineTo(connectTo.options.x, connectTo.options.y);
ctx.lineWidth = 1;
ctx.strokeStyle = connectTo.options.color;
ctx.stroke();
this.options.dy =- this.options.dy;
}
draw(ctx) {
let x = this.options.x;
let y = this.options.y;
let r = this.options.radius;
let dx = this.options.dx;
let dy = this.options.dy;
if (this.isXOutOfBounds(x)) this.options.dx =- dx;
if (this.isYOutOfBounds(y)) this.options.dy =- dy;
this.options.x += this.options.dx;
this.options.y += this.options.dy;
ctx.beginPath();
ctx.arc(this.options.x, this.options.y, this.options.radius, 0, 2 * Math.PI, false);
ctx.lineWidth = 1;
ctx.fillStyle = this.options.color
ctx.fill();
}
isXOutOfBounds(x) {
const r = this.options.radius;
if ( x - r < 0 || x + r > windowWidth ) return true;
}
isYOutOfBounds(y) {
const r = this.options.radius;
if ( y - r < 0 || y + r > windowHeight ) return true;
}
}
class Square {
constructor(options) {
this.options = options;
}
draw(ctx) {
let x = this.options.x;
let y = this.options.y;
let width = this.options.width;
let height = this.options.height;
ctx.moveTo(x + width, y);
ctx.rect(x, y, width, height);
ctx.fillStyle = "rgba(255,255,255,.1)";
ctx.fill();
}
};
let squares = [];
let y = 0;
for (var x = 0; x < windowWidth; x += 10) {
if (x >= windowWidth) {
x = 0;
y += 10;
};
let square = new Square({x, y, width: 10, height: 10})
squares.push(square);
//if (y > windowHeight) x = windowWidth;
}
let orbs = [];
for (let i = 0; i < windowWidth / 100; i++) {
let dx = Math.random() <= 0.5 ? -1 : 1;
let dy = Math.random() <= 0.5 ? -1 : 1;
let x = Math.floor(Math.random() * windowWidth) + 1;
let y = Math.floor(Math.random() * windowHeight) + 1;
let radius = Math.floor(Math.random() * 5) + 3;
let color = "pink";
let orb = new Orb({ x, y, dx, dy, radius, color });
orbs.push(orb);
}
let scene = new Scene();
squares.forEach(square => scene.addOrb(square));
orbs.forEach(orb => scene.addOrb(orb));
scene.render();