Skip to content

Commit 31dcdfe

Browse files
committed
Create build-a-randomizer-generator-with-html-css-js.mdx
1 parent dba7275 commit 31dcdfe

File tree

1 file changed

+314
-0
lines changed

1 file changed

+314
-0
lines changed
Lines changed: 314 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,314 @@
1+
---
2+
title: Build a Randomizer Generator with HTML/CSS/JS
3+
author: Julien Kris and Ellie Popoca
4+
uid:
5+
datePublished:
6+
description:
7+
header:
8+
published: false
9+
tags:
10+
- html
11+
- css
12+
- javascript
13+
- intermediate
14+
15+
---
16+
17+
<BannerImage
18+
link=""
19+
description="Title Image"
20+
uid={true}
21+
cl="for-sidebar"
22+
/>
23+
24+
# Animate Images with keyframes using CSS
25+
26+
<AuthorAvatar
27+
author_name="Julien Kris"
28+
author_avatar=""
29+
username="julien"
30+
uid={true}
31+
/>
32+
33+
<AuthorAvatar
34+
author_name="Ellie Popoca"
35+
author_avatar="/images/projects/authors/ellie-popoca.jpg"
36+
username="ellie"
37+
uid={true}
38+
/>
39+
40+
<BannerImage
41+
link=""
42+
description="Title Image"
43+
uid={true}
44+
/>
45+
46+
# Introduction
47+
48+
Are you familiar with Cher's closet from [Clueless](https://en.wikipedia.org/wiki/Clueless)?
49+
50+
<RoundedImage link="https://i.imgur.com/TYmiJnX.gif" description="" />
51+
52+
It's an example of a randomizer generator! In Clueless, Cher used it to plan outfits by swiping through a catalogue of her entire wardrobe.
53+
54+
There are other types of cool generators out there in games, movies, and TV shows. We can use the same logic to design monsters or even avatars.
55+
56+
<RoundedImage link="https://i.imgur.com/jkgf7of.gif" description="" />
57+
…how many hours of BG3 do you think we've played? ⚔️
58+
59+
Today, we'll be learning how to use HTML, CSS, and vanilla JavaScript to build our own Randomizer Generator.
60+
61+
## Code Structure
62+
63+
Let's take a look at some [starter code](https://glitch.com/edit/#!/generator-starter-code) and some [final code](https://glitch.com/edit/#!/generator-final-code). We'll be learning how to get from point A to point B.
64+
65+
If you've taken the HTML, CSS, and JavaScript courses, our web stack for this project will look familiar. We have an HTML file that links to an external CSS stylesheet and an external JavaScript file, all contained within the same folder.
66+
67+
```terminal
68+
CluelessGenerator/
69+
├── index.html
70+
├── style.css
71+
└── script.js
72+
```
73+
74+
## HTML
75+
76+
HTML buttons allow users to interact with the outfit generator. These buttons control the behavior of the app, such as randomizing outfits and cycling through different images of accessories, tops, and bottoms. Here’s what each part does:
77+
78+
### Randomize Button:
79+
80+
<RoundedImage link="https://i.imgur.com/PL9cXAX.gif" description="" />
81+
82+
This button, labeled "randomize!", triggers the randomization of outfit pieces when clicked. It's located at the top and has the `id="random-btn"`.
83+
84+
```html
85+
<button class="random-button" id="random-btn">randomize!</button>
86+
```
87+
88+
### Arrow Buttons (Prev/Next)
89+
90+
These buttons let the user cycle through different outfit pieces within each category (add-ons, tops, bottoms). For example, the `0prevBtn` and `0nextBtn` control the add-ons (accessories) images:
91+
92+
```html
93+
<div class="carousel-buttons">
94+
<button id="0prevBtn"><--</button>
95+
<button id="0nextBtn">--></button>
96+
</div>
97+
```
98+
99+
Similarly, each outfit category has its own set of arrows to go backward or forward through the images.
100+
101+
## CSS Styling
102+
103+
### General Layout and Centering
104+
105+
The HTML and body elements use flexbox to center the content both horizontally and vertically. This ensures that all elements appear in the center of the screen, no matter the screen size.
106+
```css
107+
html {
108+
background-color: #ff5f1f;
109+
display: flex;
110+
justify-content: center; /* Center horizontally */
111+
font-family: sans-serif;
112+
margin: 0;
113+
padding: 0;
114+
height: 100%;
115+
overflow-x: hidden; /* Prevent horizontal scrolling */
116+
}
117+
```
118+
119+
### Main Container
120+
121+
The `.main-container` holds all of the sections (like the images and buttons). The flexbox layout inside the container ensures that the sections are stacked vertically with a small gap between them.
122+
123+
```css
124+
.main-container {
125+
display: flex;
126+
flex-direction: column; /* Stack elements vertically */
127+
align-items: center; /* Center content */
128+
gap: 15px; /* Add space between items */
129+
padding: 10px; /* Small padding around the container */
130+
}
131+
```
132+
133+
### Image Styling
134+
135+
The images in the outfit categories (add-ons, tops, bottoms) are set to take up 90% of the available width, with a maximum width of 250px. The aspect ratio ensures they keep their proportions. This makes sure the images look good on all screen sizes.
136+
137+
```css
138+
.addons-image,
139+
.tops-image,
140+
.bottoms-image {
141+
width: 90%; /* Take up 90% of the container's width */
142+
max-width: 250px; /* Don't exceed 250px in width */
143+
aspect-ratio: 3 / 4; /* Maintain consistent proportions */
144+
object-fit: contain; /* Preserve image aspect ratio */
145+
}
146+
```
147+
148+
### Buttons Styling
149+
150+
The buttons are styled with a bright pink background and white text. A hover effect is added, so when you move the cursor over the button, the background turns darker. The "randomize" button is styled with a green background to make it stand out.
151+
152+
```css
153+
button {
154+
font-size: 1rem;
155+
color: white;
156+
background-color: #ff69b4; /* Pink background */
157+
padding: 8px 16px;
158+
cursor: pointer;
159+
}
160+
161+
button:hover {
162+
background-color: #d90166; /* Darker pink when hovered */
163+
}
164+
165+
.random-button {
166+
background-color: green; /* Green button for "randomize" */
167+
}
168+
```
169+
## JavaScript
170+
171+
### Randomizer Logic
172+
In the starter code, the random button has a placeholder function:
173+
174+
```js
175+
document.getElementById("random-btn").addEventListener("click", () => {
176+
console.log("Randomize button clicked!");
177+
});
178+
```
179+
This means the button works, but it doesn't do anything useful yet. It just logs a message to the console.
180+
181+
We'll replace the placeholder with a function that actually changes each outfit piece randomly. First, we loop through each category:
182+
183+
```js
184+
for (const cat in categories) {
185+
const category = categories[cat];
186+
```
187+
188+
This lets us access the `addOns`, tops, and bottoms categories one by one.
189+
190+
Inside the loop, we'll generate a random number that matches an image index:
191+
192+
``` js
193+
category.index = Math.floor(Math.random() * category.images.length);
194+
```
195+
This gives us a random index between 0 and the number of images - 1.
196+
197+
After picking the new index, we'll call:
198+
199+
```js
200+
updateImage(cat);
201+
```
202+
This shows the new image on screen using the category’s updated index.
203+
204+
Finally, we'll connect the real randomize() function to the button:
205+
206+
```js
207+
document.getElementById("random-btn").addEventListener("click", randomize);
208+
```
209+
210+
### Arrow Buttons Logic
211+
212+
The arrow buttons (`prevBtn` and `nextBtn`) allow the user to navigate through the images of each category (`add-ons`, `tops`, `bottoms`). These buttons let you go forward and backward through the images in a loop:
213+
214+
When the Next button is clicked, the index for the category increases by 1, and the `updateImage()` function is called to update the displayed image.
215+
216+
```javascript
217+
document.getElementById(category.nextBtn).addEventListener("click", () => {
218+
category.index = (category.index + 1) % category.images.length; // Loop back to the first image
219+
updateImage(cat);
220+
});
221+
```
222+
223+
When the Prev button is clicked, the index decreases by 1, and again, the `updateImage()` function is called. It loops back to the last image if you go backward from the first one.
224+
225+
```javascript
226+
document.getElementById(category.prevBtn).addEventListener("click", () => {
227+
category.index = (category.index - 1 + category.images.length) % category.images.length; // Loop to last image
228+
updateImage(cat);
229+
});
230+
```
231+
232+
The modulo (%) operator is used here to make the images cycle in a continuous loop.
233+
234+
## Avatar Creator
235+
236+
You can use the same logic above to create a monster or an avatar. Here's a fun example, using the heads, torsos, and legs of monsters from Monster's Inc, Inside Out, Minions.
237+
238+
<RoundedImage link="https://i.imgur.com/HHmRCzg.png" description="" />
239+
240+
This example also shows an alternate way of designing the arrows so the three characters swap heads, torsos, and legs at the same time.
241+
242+
## Adding more categories
243+
244+
So – what if we have our avatar and want to add another category, like shoes? Here's how we'd do that:
245+
246+
### HTML
247+
248+
In the HTML, you'll need to add a new section for shoes. This will look similar to how the other sections are structured. You would add the following HTML code:
249+
250+
```html
251+
<div class="shoes-container">
252+
<img id="shoes" class="shoes-image" src="" alt="shoes" />
253+
<div class="carousel-buttons">
254+
<button id="3prevBtn"><--</button>
255+
<button id="3nextBtn">--></button>
256+
</div>
257+
</div>
258+
```
259+
260+
This creates a new section with an image for shoes and navigation buttons (previous and next). You can place this code in the `<div class="main-container">` along with the other categories.
261+
262+
### CSS
263+
To add styling for the shoes category, follow the same pattern as for other categories! Anywhere that you currently have
264+
```css
265+
.addons-image,
266+
.tops-image,
267+
.bottoms-image {
268+
```
269+
You'll also add `.shoes-image`.
270+
### JavaScript
271+
In JavaScript, you'll need to add a new category to the categories object. Each category includes an array of images, an index to track the current image, references to the HTML elements, and button IDs for navigation. Here's how you'll add the shoes category:
272+
```javascript
273+
const categories = {
274+
275+
shoes: { // Add new category for shoes
276+
images: [
277+
// Your shoe images go here
278+
],
279+
index: 0,
280+
element: document.getElementById("shoes"),
281+
nextBtn: "3nextBtn",
282+
prevBtn: "3prevBtn",
283+
},
284+
};
285+
```
286+
You'll add a `shoes` key with an array of image URLs, an index to track the current image, an element to target the HTML image element, and button IDs for the next and previous buttons.
287+
288+
## Bonus Challenge
289+
290+
If you've already added a fourth section, try making a feature that toggles a pop-up that says "mismatch!" when the outfit doesn't match. Or, if you're creating a monster, the feature could say the sound the monster would make, depending on the combinations.
291+
292+
<RoundedImage link="https://i.imgur.com/4VYyB9N.jpeg" description="" />
293+
294+
295+
## Conclusion
296+
297+
Congrats! You've built your own generator! Now you're equipped with the skills to build on this concept and create other kinds of generative projects!
298+
299+
## Additional Resources
300+
301+
[90's Fashion Nova Holiday Hackathon Project by Ciera](https://www.codedex.io/community/hackathon/OSlnGKjR7MWDaIRgvNdD)
302+
[Archive App by Vita Newstetter](https://archive.clothing/)
303+
[I Recreated Cher's Closet From Clueless](https://www.studiosophy.com/blog/2022/2/28/i-recreated-chers-closet-from-clueless)
304+
[MoMa Exquisite Corpse Generator](https://ec.moma.org/intro)
305+
306+
307+
Share your projects with the team [@codedex_io](https://www.twitter.com/codedex_io) and us, [@exrlla](https://www.twitter.com/exrlla) and [julien](https://www.codedex.io/@Julien)! Let us know what you come up with!
308+
<span style={{ fontSize: "16px", lineHeight: 1 }}>
309+
<img
310+
src="https://i.imgur.com/FOR1yp5.gif"
311+
alt="emoji"
312+
style={{ height: "1em", width: "auto", verticalAlign: "middle" }}
313+
/>
314+
</span>

0 commit comments

Comments
 (0)