Skip to content

Commit c31439a

Browse files
committed
stopped hexigon squishing
1 parent 0465ece commit c31439a

File tree

2 files changed

+112
-60
lines changed

2 files changed

+112
-60
lines changed

assets/load/universal.js

Lines changed: 56 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
// Wait for the DOM content to fully load
22
document.addEventListener("DOMContentLoaded", () => {
3-
fetch('load/header.html')
3+
fetch('load/header.html') // Fetch the header HTML from the server
44
.then(response => response.text())
55
.then(data => {
6-
document.querySelector('header').innerHTML = data;
6+
document.querySelector('header').innerHTML = data; // Inject the header content
77
});
88
});
99

1010
document.addEventListener('DOMContentLoaded', () => {
11+
// Set active nav link based on current page
1112
const currentPage = window.location.pathname.split('/').pop();
1213
const navLinks = document.querySelectorAll('.nav-link');
1314

@@ -16,6 +17,7 @@ document.addEventListener('DOMContentLoaded', () => {
1617
link.classList.add('active');
1718
}
1819

20+
// Add hover effect
1921
link.addEventListener('mouseenter', (e) => {
2022
const img = e.currentTarget.querySelector('img');
2123
img.style.transform = 'scale(1.1) rotate(5deg)';
@@ -28,76 +30,88 @@ document.addEventListener('DOMContentLoaded', () => {
2830
});
2931
});
3032

33+
// Include the 'matter-wrap' plugin
3134
Matter.use('matter-wrap');
3235

36+
// HexBokeh: A customizable background effect using Matter.js
3337
let hexBokeh = {
38+
// Default options for customization
3439
options: {
35-
canvasSelector: '',
36-
radiusRange: [30, 60],
37-
xVarianceRange: [0.1, 0.3],
38-
yVarianceRange: [0.5, 1.5],
39-
airFriction: 0.03,
40-
opacity: 0.5,
41-
collisions: false,
42-
scrollVelocity: 0.025,
43-
pixelsPerBody: 40000,
44-
colors: ['#7ef2cf', '#bea6ff', '#8ccdff']
40+
canvasSelector: '', // CSS selector for the canvas element
41+
radiusRange: [30, 60], // Random range of hexagon radii
42+
xVarianceRange: [0.1, 0.3], // X velocity scaling range
43+
yVarianceRange: [0.5, 1.5], // Y velocity scaling range
44+
airFriction: 0.03, // Air friction applied to the bodies
45+
opacity: 0.5, // Opacity of the hexagons
46+
collisions: false, // Enable or disable collisions
47+
scrollVelocity: 0.025, // Effect of scroll on body velocities
48+
pixelsPerBody: 40000, // Pixels in viewport per hexagon
49+
colors: ['#7ef2cf', '#bea6ff', '#8ccdff'] // Hexagon fill colors
4550
},
4651

52+
// Delays for scroll and resize event throttling
4753
scrollDelay: 100,
4854
resizeDelay: 400,
55+
56+
// Variables to track event state
4957
lastOffset: undefined,
5058
scrollTimeout: undefined,
5159
resizeTimeout: undefined,
60+
61+
// Matter.js components
5262
engine: undefined,
5363
render: undefined,
5464
runner: undefined,
5565
bodies: undefined,
5666

67+
// Initialize the effect
5768
init(options) {
69+
// Merge provided options with defaults
5870
this.options = Object.assign({}, this.options, options);
5971

6072
let viewportWidth = document.documentElement.clientWidth;
61-
let documentHeight = Math.max(
62-
document.body.scrollHeight,
63-
document.documentElement.scrollHeight
64-
);
73+
let viewportHeight = document.documentElement.clientHeight;
6574

6675
this.lastOffset = window.pageYOffset;
6776
this.scrollTimeout = null;
6877
this.resizeTimeout = null;
6978

79+
// Create Matter.js engine
7080
this.engine = Matter.Engine.create();
7181
this.engine.world.gravity.y = 0;
7282

83+
// Create renderer
7384
this.render = Matter.Render.create({
7485
canvas: document.querySelector(this.options.canvasSelector),
7586
engine: this.engine,
7687
options: {
7788
width: viewportWidth,
78-
height: documentHeight,
89+
height: viewportHeight,
7990
wireframes: false,
8091
background: 'transparent'
8192
}
8293
});
8394
Matter.Render.run(this.render);
8495

96+
// Create runner
8597
this.runner = Matter.Runner.create();
8698
Matter.Runner.run(this.runner, this.engine);
8799

100+
// Create and add bodies to the engine
88101
this.bodies = [];
89-
let totalBodies = Math.round(viewportWidth * documentHeight / this.options.pixelsPerBody);
90-
102+
let totalBodies = Math.round(viewportWidth * viewportHeight / this.options.pixelsPerBody);
91103
for (let i = 0; i <= totalBodies; i++) {
92-
let body = this.createBody(viewportWidth, documentHeight);
104+
let body = this.createBody(viewportWidth, viewportHeight);
93105
this.bodies.push(body);
94106
}
95107
Matter.World.add(this.engine.world, this.bodies);
96108

109+
// Attach event listeners for scroll and resize
97110
window.addEventListener('scroll', this.onScrollThrottled.bind(this));
98111
window.addEventListener('resize', this.onResizeThrottled.bind(this));
99112
},
100113

114+
// Shutdown and clean up the engine
101115
shutdown() {
102116
Matter.Engine.clear(this.engine);
103117
Matter.Render.stop(this.render);
@@ -107,14 +121,16 @@ let hexBokeh = {
107121
window.removeEventListener('resize', this.onResizeThrottled);
108122
},
109123

124+
// Generate a random number within a range
110125
randomize(range) {
111126
let [min, max] = range;
112127
return Math.random() * (max - min) + min;
113128
},
114129

115-
createBody(viewportWidth, documentHeight) {
130+
// Create a hexagonal body with random properties
131+
createBody(viewportWidth, viewportHeight) {
116132
let x = this.randomize([0, viewportWidth]);
117-
let y = this.randomize([0, documentHeight]);
133+
let y = this.randomize([0, viewportHeight]);
118134
let radius = this.randomize(this.options.radiusRange);
119135
let color = this.options.colors[this.bodies.length % this.options.colors.length];
120136

@@ -130,22 +146,24 @@ let hexBokeh = {
130146
plugin: {
131147
wrap: {
132148
min: { x: 0, y: 0 },
133-
max: { x: viewportWidth, y: documentHeight }
149+
max: { x: viewportWidth, y: viewportHeight }
134150
}
135151
}
136152
});
137153
},
138154

155+
// Handle scroll events (throttled)
139156
onScrollThrottled() {
140157
if (!this.scrollTimeout) {
141158
this.scrollTimeout = setTimeout(this.onScroll.bind(this), this.scrollDelay);
142159
}
143160
},
144161

162+
// Update body velocities based on scroll movement
145163
onScroll() {
146164
this.scrollTimeout = null;
165+
147166
let delta = (this.lastOffset - window.pageYOffset) * this.options.scrollVelocity;
148-
149167
this.bodies.forEach((body) => {
150168
Matter.Body.setVelocity(body, {
151169
x: body.velocity.x + delta * this.randomize(this.options.xVarianceRange),
@@ -156,26 +174,29 @@ let hexBokeh = {
156174
this.lastOffset = window.pageYOffset;
157175
},
158176

177+
// Handle resize events (throttled)
159178
onResizeThrottled() {
160179
if (!this.resizeTimeout) {
161180
this.resizeTimeout = setTimeout(this.onResize.bind(this), this.resizeDelay);
162181
}
163182
},
164183

184+
// Restart the engine with updated dimensions
165185
onResize() {
166186
this.shutdown();
167-
this.init(this.options);
187+
this.init();
168188
}
169189
};
170190

191+
// Wait for the DOM content to load and initialize HexBokeh
171192
window.addEventListener('DOMContentLoaded', () => {
172193
Object.create(hexBokeh).init({
173194
canvasSelector: '#bg1',
174195
radiusRange: [20, 90],
175196
colors: [
176-
'rgba(87, 17, 148, 0.6)',
177-
'rgba(30, 92, 143, 0.6)',
178-
'rgba(231, 223, 105, 0.4)',
197+
'rgba(87, 17, 148, 0.6)', // Purple
198+
'rgba(30, 92, 143, 0.6)', // Blue
199+
'rgba(231, 223, 105, 0.4)', // Accent
179200
],
180201
pixelsPerBody: 25000,
181202
scrollVelocity: 0.015,
@@ -184,19 +205,24 @@ window.addEventListener('DOMContentLoaded', () => {
184205
});
185206
});
186207

187-
// Typing effect
208+
209+
// Get the text content from the element with ID "type"
188210
var string = document.getElementById("type").textContent;
189211
var array = string.split("");
190212
var timer;
191213

192214
function frameLooper() {
193215
if (array.length > 0) {
216+
// Append each character to the element with ID "type"
194217
document.getElementById("type").innerHTML += array.shift();
195218
} else {
219+
// Clear the timer when the string is fully typed
196220
clearTimeout(timer);
197221
}
198-
timer = setTimeout(frameLooper, 70);
222+
// Call the function again after the specified delay (70ms in this case)
223+
timer = setTimeout(frameLooper, 70); // change 70 for speed
199224
}
200225

226+
// Clear the initial text in the "type" element and start the typing effect
201227
document.getElementById("type").innerHTML = "";
202228
frameLooper();

0 commit comments

Comments
 (0)