HTML preprocessors can make writing HTML more powerful or convenient. For instance, Markdown is designed to be easier to write and read for text documents and you could write a loop in Pug.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. So you don't have access to higher-up elements like the <html>
tag. If you want to add classes there that can affect the whole document, this is the place to do it.
In CodePen, whatever you write in the HTML editor is what goes within the <body>
tags in a basic HTML5 template. If you need things in the <head>
of the document, put that code here.
The resource you are linking to is using the 'http' protocol, which may not work when the browser is using https.
CSS preprocessors help make authoring CSS easier. All of them offer things like variables and mixins to provide convenient abstractions.
It's a common practice to apply CSS to a page that styles elements such that they are consistent across all browsers. We offer two of the most popular choices: normalize.css and a reset. Or, choose Neither and nothing will be applied.
To get the best cross-browser support, it is a common practice to apply vendor prefixes to CSS properties and values that require them to work. For instance -webkit-
or -moz-
.
We offer two popular choices: Autoprefixer (which processes your CSS server-side) and -prefix-free (which applies prefixes via a script, client-side).
Any URLs added here will be added as <link>
s in order, and before the CSS in the editor. You can use the CSS from another Pen by using its URL and the proper URL extension.
You can apply CSS to your Pen from any stylesheet on the web. Just put a URL to it here and we'll apply it, in the order you have them, before the CSS in the Pen itself.
You can also link to another Pen here (use the .css
URL Extension) and we'll pull the CSS from that Pen and include it. If it's using a matching preprocessor, use the appropriate URL Extension and we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
JavaScript preprocessors can help make authoring JavaScript easier and more convenient.
Babel includes JSX processing.
Any URL's added here will be added as <script>
s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.
You can apply a script from anywhere on the web to your Pen. Just put a URL to it here and we'll add it, in the order you have them, before the JavaScript in the Pen itself.
If the script you link to has the file extension of a preprocessor, we'll attempt to process it before applying.
You can also link to another Pen here, and we'll pull the JavaScript from that Pen and include it. If it's using a matching preprocessor, we'll combine the code before preprocessing, so you can use the linked Pen as a true dependency.
Search for and use JavaScript packages from npm here. By selecting a package, an import
statement will be added to the top of the JavaScript editor for this package.
Using packages here is powered by esm.sh, which makes packages from npm not only available on a CDN, but prepares them for native JavaScript ESM usage.
All packages are different, so refer to their docs for how they work.
If you're using React / ReactDOM, make sure to turn on Babel for the JSX processing.
If active, Pens will autosave every 30 seconds after being saved once.
If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.
If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.
Visit your global Editor Settings.
<html lang="en">
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Hangman</title>
<!-- Google Fonts -->
<link
href="https://fonts.googleapis.com/css2?family=Poppins:wght@400;600&display=swap"
rel="stylesheet"
/>
<!-- Stylesheet -->
<link rel="stylesheet" href="style.css" />
</head>
<body>
<div class="container">
<div id="options-container"></div>
<div id="letter-container" class="letter-container hide"></div>
<div id="user-input-section"></div>
<canvas id="canvas"></canvas>
<div id="new-game-container" class="new-game-popup hide">
<div id="result-text"></div>
<button id="new-game-button">New Game</button>
</div>
</div>
<!-- Script -->
<script src="script.js"></script>
</body>
</html>
* {
padding: 0;
margin: 0;
box-sizing: border-box;
font-family: "Poppins", sans-serif;
}
body {
background-color: #f4c531;
}
.container {
font-size: 16px;
background-color: #ffffff;
width: 90vw;
max-width: 34em;
position: absolute;
transform: translate(-50%, -50%);
top: 50%;
left: 50%;
padding: 3em;
border-radius: 0.6em;
box-shadow: 0 1.2em 2.4em rgba(111, 85, 0, 0.25);
}
#options-container {
text-align: center;
}
#options-container div {
width: 100%;
display: flex;
justify-content: space-between;
margin: 1.2em 0 2.4em 0;
}
#options-container button {
padding: 0.6em 1.2em;
border: 3px solid #000000;
background-color: #ffffff;
color: #000000;
border-radius: 0.3em;
text-transform: capitalize;
}
#options-container button:disabled {
border: 3px solid #808080;
color: #808080;
background-color: #efefef;
}
#options-container button.active {
background-color: #f4c531;
border: 3px solid #000000;
color: #000000;
}
.letter-container {
width: 100%;
display: flex;
flex-wrap: wrap;
justify-content: center;
gap: 0.6em;
}
#letter-container button {
height: 2.4em;
width: 2.4em;
border-radius: 0.3em;
background-color: #ffffff;
}
.new-game-popup {
background-color: #ffffff;
position: absolute;
left: 0;
top: 0;
height: 100%;
width: 100%;
display: flex;
align-items: center;
justify-content: center;
flex-direction: column;
border-radius: 0.6em;
}
#user-input-section {
display: flex;
justify-content: center;
font-size: 1.8em;
margin: 0.6em 0 1.2em 0;
}
canvas {
display: block;
margin: auto;
border: 1px solid #000000;
}
.hide {
display: none;
}
#result-text h2 {
font-size: 1.8em;
text-align: center;
}
#result-text p {
font-size: 1.25em;
margin: 1em 0 2em 0;
}
#result-text span {
font-weight: 600;
}
#new-game-button {
font-size: 1.25em;
padding: 0.5em 1em;
background-color: #f4c531;
border: 3px solid #000000;
color: #000000;
border-radius: 0.2em;
}
.win-msg {
color: #39d78d;
}
.lose-msg {
color: #fe5152;
}
//Initial References
const letterContainer = document.getElementById("letter-container");
const optionsContainer = document.getElementById("options-container");
const userInputSection = document.getElementById("user-input-section");
const newGameContainer = document.getElementById("new-game-container");
const newGameButton = document.getElementById("new-game-button");
const canvas = document.getElementById("canvas");
const resultText = document.getElementById("result-text");
//Options values for buttons
let options = {
fruits: [
"Apple",
"Blueberry",
"Mandarin",
"Pineapple",
"Pomegranate",
"Watermelon",
],
animals: ["Hedgehog", "Rhinoceros", "Squirrel", "Panther", "Walrus", "Zebra"],
countries: [
"India",
"Hungary",
"Kyrgyzstan",
"Switzerland",
"Zimbabwe",
"Dominica",
],
};
//count
let winCount = 0;
let count = 0;
let chosenWord = "";
//Display option buttons
const displayOptions = () => {
optionsContainer.innerHTML += `<h3>Please Select An Option</h3>`;
let buttonCon = document.createElement("div");
for (let value in options) {
buttonCon.innerHTML += `<button class="options" onclick="generateWord('${value}')">${value}</button>`;
}
optionsContainer.appendChild(buttonCon);
};
//Block all the Buttons
const blocker = () => {
let optionsButtons = document.querySelectorAll(".options");
let letterButtons = document.querySelectorAll(".letters");
//disable all options
optionsButtons.forEach((button) => {
button.disabled = true;
});
//disable all letters
letterButtons.forEach((button) => {
button.disabled.true;
});
newGameContainer.classList.remove("hide");
};
//Word Generator
const generateWord = (optionValue) => {
let optionsButtons = document.querySelectorAll(".options");
//If optionValur matches the button innerText then highlight the button
optionsButtons.forEach((button) => {
if (button.innerText.toLowerCase() === optionValue) {
button.classList.add("active");
}
button.disabled = true;
});
//initially hide letters, clear previous word
letterContainer.classList.remove("hide");
userInputSection.innerText = "";
let optionArray = options[optionValue];
//choose random word
chosenWord = optionArray[Math.floor(Math.random() * optionArray.length)];
chosenWord = chosenWord.toUpperCase();
//replace every letter with span containing dash
let displayItem = chosenWord.replace(/./g, '<span class="dashes">_</span>');
//Display each element as span
userInputSection.innerHTML = displayItem;
};
//Initial Function (Called when page loads/user presses new game)
const initializer = () => {
winCount = 0;
count = 0;
//Initially erase all content and hide letteres and new game button
userInputSection.innerHTML = "";
optionsContainer.innerHTML = "";
letterContainer.classList.add("hide");
newGameContainer.classList.add("hide");
letterContainer.innerHTML = "";
//For creating letter buttons
for (let i = 65; i < 91; i++) {
let button = document.createElement("button");
button.classList.add("letters");
//Number to ASCII[A-Z]
button.innerText = String.fromCharCode(i);
//character button click
button.addEventListener("click", () => {
let charArray = chosenWord.split("");
let dashes = document.getElementsByClassName("dashes");
//if array contains clciked value replace the matched dash with letter else dram on canvas
if (charArray.includes(button.innerText)) {
charArray.forEach((char, index) => {
//if character in array is same as clicked button
if (char === button.innerText) {
//replace dash with letter
dashes[index].innerText = char;
//increment counter
winCount += 1;
//if winCount equals word lenfth
if (winCount == charArray.length) {
resultText.innerHTML = `<h2 class='win-msg'>You Win!!</h2><p>The word was <span>${chosenWord}</span></p>`;
//block all buttons
blocker();
}
}
});
} else {
//lose count
count += 1;
//for drawing man
drawMan(count);
//Count==6 because head,body,left arm, right arm,left leg,right leg
if (count == 6) {
resultText.innerHTML = `<h2 class='lose-msg'>You Lose!!</h2><p>The word was <span>${chosenWord}</span></p>`;
blocker();
}
}
//disable clicked button
button.disabled = true;
});
letterContainer.append(button);
}
displayOptions();
//Call to canvasCreator (for clearing previous canvas and creating initial canvas)
let { initialDrawing } = canvasCreator();
//initialDrawing would draw the frame
initialDrawing();
};
//Canvas
const canvasCreator = () => {
let context = canvas.getContext("2d");
context.beginPath();
context.strokeStyle = "#000";
context.lineWidth = 2;
//For drawing lines
const drawLine = (fromX, fromY, toX, toY) => {
context.moveTo(fromX, fromY);
context.lineTo(toX, toY);
context.stroke();
};
const head = () => {
context.beginPath();
context.arc(70, 30, 10, 0, Math.PI * 2, true);
context.stroke();
};
const body = () => {
drawLine(70, 40, 70, 80);
};
const leftArm = () => {
drawLine(70, 50, 50, 70);
};
const rightArm = () => {
drawLine(70, 50, 90, 70);
};
const leftLeg = () => {
drawLine(70, 80, 50, 110);
};
const rightLeg = () => {
drawLine(70, 80, 90, 110);
};
//initial frame
const initialDrawing = () => {
//clear canvas
context.clearRect(0, 0, context.canvas.width, context.canvas.height);
//bottom line
drawLine(10, 130, 130, 130);
//left line
drawLine(10, 10, 10, 131);
//top line
drawLine(10, 10, 70, 10);
//small top line
drawLine(70, 10, 70, 20);
};
return { initialDrawing, head, body, leftArm, rightArm, leftLeg, rightLeg };
};
//draw the man
const drawMan = (count) => {
let { head, body, leftArm, rightArm, leftLeg, rightLeg } = canvasCreator();
switch (count) {
case 1:
head();
break;
case 2:
body();
break;
case 3:
leftArm();
break;
case 4:
rightArm();
break;
case 5:
leftLeg();
break;
case 6:
rightLeg();
break;
default:
break;
}
};
//New Game
newGameButton.addEventListener("click", initializer);
window.onload = initializer;
Also see: Tab Triggers