-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsketch.js
162 lines (142 loc) · 3.58 KB
/
sketch.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
let walls = [];
let ray;
let particle;
let xoff = 0;
let yoff = 124124;
let paused = false;
let sceneWidth = rendering ? window.innerWidth / 2 : window.innerWidth;
document.getElementById("heading").innerText = rendering
? "Move and Rotate using Arrow Keys"
: "Press spacebar to toggle between User-Guided and Random Movement.";
function setup() {
canv = createCanvas(window.innerWidth - 100, window.innerHeight - 150);
canv.id("myCanvas");
for (let i = 0; i < random(4, 7); i++) {
let x1, x2, y1, y2, col;
col = rendering
? color(random(0, 255), random(0, 255), random(0, 255))
: color(255, 255, 255);
x1 = rendering ? random(width / 2 - 20) : random(width - 20);
y1 = random(height);
x2 = rendering ? random(width / 2 - 20) : random(width - 20);
y2 = random(height);
walls[i] = new Boundary(x1, y1, x2, y2, col);
}
// walls[0] = new Boundary(150, 150, 150, 300);
// walls[1] = new Boundary(400, 400, 400, 300);
walls.push(
new Boundary(
0,
0,
rendering ? width / 2 - 20 : width,
0,
color(255, 0, 0)
)
);
walls.push(
new Boundary(
rendering ? width / 2 - 20 : width,
0,
rendering ? width / 2 - 20 : width,
height,
color(255, 0, 0)
)
);
walls.push(
new Boundary(
rendering ? width / 2 - 20 : width,
height,
0,
height,
color(255, 0, 0)
)
);
walls.push(new Boundary(0, height, 0, 0, color(255, 0, 0)));
particle = new Particle();
ray = new Ray(100, 200);
}
function draw() {
background(0);
for (let wall of walls) {
wall.show();
}
if (usingMouse) {
if (rendering) {
if (keyIsDown(RIGHT_ARROW)) {
particle.rotate(0.05);
} else if (keyIsDown(LEFT_ARROW)) {
particle.rotate(-0.05);
} else if (keyIsDown(UP_ARROW)) {
particle.move(2);
} else if (keyIsDown(DOWN_ARROW)) {
particle.move(-2);
}
} else {
particle.update(mouseX, mouseY);
}
} else {
if (rendering) {
particle.update((noise(xoff) * width) / 2, noise(yoff) * height);
} else {
particle.update(noise(xoff) * width, noise(yoff) * height);
}
// particle.rotate(random(-0.05, 0.05));
}
if (!paused) {
xoff += 0.005;
yoff += 0.005;
}
particle.show();
reqArr = particle.look(walls);
const scene = reqArr[0];
const wallcolors = reqArr[1];
if (rendering) {
push();
translate(window.innerHeight, 0);
const w = window.innerWidth / (scene.length * 2);
for (let i = 0; i < scene.length; i++) {
noStroke();
const sq = scene[i] * scene[i];
const wSq = sceneWidth * sceneWidth;
const b = map(sq, 0, wSq, 255, 0);
// const b = map(scene[i], 0, sceneWidth / 2, 255, 0);
const h = map(scene[i], 0, sceneWidth, window.innerHeight, 0);
const bright = b / 255;
wallc = wallcolors[i];
fill(
`rgba(${wallc.levels[0]}, ${wallc.levels[1]}, ${
wallc.levels[2]
}, ${bright >= 0 ? bright : 0})`
);
// fill(
// `rgba(${wallc.levels[0]}, ${wallc.levels[1]}, ${wallc.levels[2]}, ${b})`
// );
rectMode(CENTER);
if (keyIsDown(32)) {
console.log(b / 255 + 0.1);
}
rect(i * w + w / 2, window.innerHeight / 2, w + 1.1, h);
}
pop();
}
// ray.show();
// ray.lookAt(mouseX, mouseY);
// let pt = ray.cast(wall);
// // console.log(pt);
// if (pt) {
// fill(255);
// ellipse(pt.x - 100, pt.y - 200, 10);
// }
}
document.onkeypress = function (e) {
if (e.keyCode == 32 && !paused && !rendering) {
usingMouse = !usingMouse;
} else if ((e.key == "p" || e.key == "P") && !usingMouse) {
paused = !paused;
}
};
document.onclick = () => {
c = document.getElementById("myCanvas").style.cursor;
document.getElementById("myCanvas").style.cursor =
c != "none" ? "none" : "";
};