-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsketch.js
More file actions
207 lines (175 loc) · 5.29 KB
/
sketch.js
File metadata and controls
207 lines (175 loc) · 5.29 KB
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
let bubbles = [];
let weather;
let apiKey = "33ce41172974df39fe3e630c84c82abb";
let city = "Maastricht,nl";
let apiUrl = `https://api.openweathermap.org/data/2.5/weather?q=${city}&units=metric&appid=${apiKey}`;
let windForce;
let bgImg,
preRenderedBg,
blurLevels = [];
let bgLoaded = false,
titleLoaded = false;
let percy = "JTPercy";
let colors = ["#5AC2F6", "#FFAEEB", "#FFFD75", "#695AF6", "#FFFFFF", "#6ABE6C"];
let titleImg;
function preload() {
weather = loadJSON(apiUrl, updateWind);
bgImg = loadImage("assets/chateau.jpg");
titleImg = loadImage("assets/title.png");
}
function setup() {
createCanvas(windowWidth, windowHeight);
pixelDensity(1);
preRenderedBg = createGraphics(width, height);
drawFullBackground(preRenderedBg);
for (let i = 0; i <= 5; i++) {
let blurCanvas = createGraphics(width, height);
blurCanvas.image(preRenderedBg, 0, 0, width, height);
blurCanvas.filter(BLUR, i * 2);
blurLevels.push(blurCanvas);
}
// Initialize bubbles
for (let i = 0; i < 10; i++) {
let x = random(width);
let y = random(height);
let r = random(20, 300);
let col = color(random(colors));
col.setAlpha(150);
bubbles.push(new Bubble(x, y, r, col));
}
}
function getWindDirection(degrees) {
const directions = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"];
let adjustedDegrees = (degrees - 90 + 360) % 360; // ✅ Adjust to match p5.js physics
let index = Math.round(adjustedDegrees / 45) % 8;
return directions[index];
}
function draw() {
image(preRenderedBg, 0, 0, width, height);
// Draw the link text
text('Agenda', textX, textY);
// Draw underline
let textWidthValue = textWidth('Agenda');
stroke(255);
line(textX, textY + 5, textX + textWidthValue, textY + 5);
drawingContext.save();
drawingContext.filter = "blur(1.5px)";
for (let b of bubbles) {
if (windForce && frameCount % 2 === 0) {
b.applyForce(windForce);
}
b.move();
b.blurBackground();
b.display();
}
drawingContext.filter = "none";
drawingContext.restore();
if (imgVisible) {
drawImageOnTop();
}
}
function drawBubbles() {
// Example function to draw bubbles
fill(100, 150, 255, 150);
noStroke();
for (let i = 0; i < 10; i++) {
ellipse(random(width), random(height), 20, 20);
}
}
function drawImageOnTop() {
// Use the stored position and dimensions to draw the image
image(agendaImg, imgX, imgY, imgWidth, imgHeight);
}
function mousePressed() {
// Check if the mouse is over the text
let textWidthValue = textWidth('Agenda');
let textHeight = textSize(); // Use textSize to get the height of the text
if (mouseX > textX && mouseX < textX + textWidthValue && mouseY > textY - textHeight && mouseY < textY) {
imgVisible = !imgVisible;
}
// Check if the image is visible and if the mouse is over the image
if (imgVisible) {
if (mouseX > imgX && mouseX < imgX + imgWidth && mouseY > imgY && mouseY < imgY + imgHeight) {
imgVisible = false;
}
}
}
function updateWind(data) {
let windAngle = radians(data.wind.deg + 180);
windForce = p5.Vector.fromAngle(windAngle).mult(data.wind.speed * 0.1);
}
function drawFullBackground(gfx) {
gfx.image(bgImg, 0, 0, width, height);
drawGradientOverlay(gfx);
drawTextOnBuffer(gfx);
if (titleImg && titleImg.width) {
let maxWidth = width * 0.9;
let scaleFactor = maxWidth / titleImg.width;
let newHeight = titleImg.height * scaleFactor;
gfx.image(
titleImg,
(width - maxWidth) / 2,
height / 2 - newHeight / 2,
maxWidth,
newHeight
);
} else {
console.warn("⚠️ titleImg is not loaded yet, skipping draw.");
}
}
function drawGradientOverlay(gfx) {
let gradient = gfx.drawingContext.createLinearGradient(0, 0, 0, height);
gradient.addColorStop(1, "rgba(247, 163, 18, 0.4)");
gradient.addColorStop(0, "rgba(45, 137, 212, 0.4)");
gfx.drawingContext.fillStyle = gradient;
gfx.noStroke();
gfx.rect(0, 0, width, height);
}
function drawTextOnBuffer(gfx) {
gfx.fill(255);
gfx.textFont(percy);
gfx.textAlign(LEFT);
gfx.textSize(28);
gfx.text("Château-St-Gerlach", 40, height - 70);
gfx.textFont("FKGrotesk");
gfx.textSize(16);
gfx.text("Joseph Corneli Allee 1, 6301 KK Valkenburg", 40, height - 40);
gfx.textAlign(CENTER);
gfx.text("Fabrique trip 2025", width / 2, height - 40);
gfx.textAlign(RIGHT);
gfx.text("04.04.2025 — 06.04.2025", width - 40, height - 40);
if (weather) {
let temp = constrain(weather.main.temp, 5, 40);
let r = 255;
let g = lerp(200, 0, temp / 40);
let b = 0;
gfx.textAlign(LEFT);
gfx.fill(r, g, b);
gfx.textSize(28);
gfx.textFont("JTPercy");
gfx.text(
`« Maastricht is ${weather.main.temp} degrees now, wind blowing: ${
weather.wind.speed
} m/s in direction of ${getWindDirection(weather.wind.deg)} »`,
30,
50
);
}
}
function windowResized() {
resizeCanvas(windowWidth, windowHeight);
// Recreate the background graphics at new size
preRenderedBg = createGraphics(width, height);
drawFullBackground(preRenderedBg);
}
setInterval(() => {
// Redraw one random bubble
let randomIndex = Math.floor(random(bubbles.length));
console.log(bubbles);
bubbles[randomIndex].redefine(
random(width),
random(height),
random(20, 350),
color(random(colors))
);
}, 300);