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.
<div id="container">
<div id="simon">
<div id="green-btn" class="btn-main"></div>
<div id="red-btn" class="btn-main"></div>
<div id="blue-btn" class="btn-main"></div>
<div id="yellow-btn" class="btn-main"></div>
<div id="display">
<div id="title-bg"></div>
<h1 id="title">simon</h1>
<div id="settings">
<div id="power-wrap" class="wrap">
<div id="power">
<div id="power-btn"></div>
</div>
<p class="label">Power</p>
</div>
<div id="start-wrap" class="wrap">
<div id="start"></div>
<p class="label">Start</p>
</div>
<div id="strict-wrap" class="wrap">
<div id="strict"></div>
<div id="strict-light"></div>
<p class="label">Strict</p>
</div>
<div id="count-wrap" class="wrap">
<div id="count">
<p id="num">––</p>
</div>
<p class="label">Count</p>
</div>
</div>
</div>
</div>
</div>
@import url('https://fonts.googleapis.com/css2?family=Merriweather:wght@900&display=swap');
@import url('https://fonts.googleapis.com/css2?family=Roboto+Condensed&display=swap');
$bgColor: #e0d8d7;
$mainGray: #2e2c2c;
$green: #00b300;
$greenLit: #00e600;
$redBtn: #e60000;
$red: #b30000;
$redLit: #ff0000;
$blue: #0047b3;
$blueLit: #0066ff;
$yellowBtn: #e6e600;
$yellow: #b3b300;
$yellowLit: #ffff1a;
* {
margin: 0;
padding: 0;
font-family: 'Roboto Condensed', sans-serif;
-webkit-touch-callout:none;
-webkit-user-select:none;
-khtml-user-select:none;
-moz-user-select:none;
-ms-user-select:none;
user-select:none;
}
body {
background: $bgColor;
}
#container {
width: 100vw;
height: 100vh;
display: flex;
justify-content: center;
align-items: center;
position: relative;
#simon {
width: 500px;
height: 500px;
position: absolute;
background: $mainGray;
border-radius: 50%;
box-shadow:
inset 0 -10px 10px rgba(0,0,0,0.5),
inset 0 10px 10px rgba(255,255,255,0.2);
.btn-main {
width: 230px;
height: 230px;
position: absolute;
border: 15px solid $mainGray;
box-sizing: border-box;
// box-shadow: inset 0 0 50px rgba(0,0,0,0.5);
&:hover {
cursor: pointer;
}
}
#green-btn {
top: 20px;
left: 20px;
background: $green;
border-radius: 100% 0 0 0;
}
#red-btn {
top: 20px;
right: 20px;
background: $red;
border-radius: 0 100% 0 0;
}
#blue-btn {
bottom: 20px;
right: 20px;
background: $blue;
border-radius: 0 0 100% 0;
}
#yellow-btn {
bottom: 20px;
left: 20px;
background: $yellow;
border-radius: 0 0 0 100%;
}
#display {
width: 250px;
height: 250px;
position: absolute;
z-index: 1;
transform: translate(50%, 50%);
border: 15px solid $mainGray;
border-radius: 50%;
background: silver;
box-sizing: border-box;
#title-bg {
width: 210px;
height: 105px;
position: absolute;
top: 5px;
left: 5px;
background: $mainGray;
border-radius: 105px 105px 0 0;
}
#title {
font-family: 'Merriweather', sans-serif;
font-size: 2.5rem;
color: silver;
position: absolute;
top: 40px;
left: 45px;
text-align: center;
}
#settings {
position: absolute;
z-index: 2;
top: 95px;
left: 5px;
width: 210px;
height: 110px;
display: flex;
flex-wrap: wrap;
justify-content: space-around;
background: silver;
border-radius: 0 0 100px 100px;
.wrap {
margin: 8px 6px 0 6px;
}
#power {
width: 56px;
height: 30px;
background: $mainGray;
border-radius: 4px;
padding: 3px;
box-sizing: border-box;
#power-btn {
float: left;
width: 23px;
height: 24px;
border-radius: 2px;
background: $blue;
box-shadow:
inset 0 3px 3px rgba(255,255,255,0.3),
inset 0 -3px 3px rgba(0,0,0,0.2);
&:hover {
cursor: pointer;
}
}
}
#start {
width: 30px;
height: 30px;
background: $redBtn;
border-radius: 50%;
border: 3px solid $mainGray;
box-sizing: border-box;
box-shadow:
inset 0 3px 3px rgba(255,255,255,0.3),
inset 0 -3px 3px rgba(0,0,0,0.3);
&:hover {
cursor: pointer;
}
}
#strict {
width: 30px;
height: 30px;
background: $yellowBtn;
border-radius: 50%;
border: 3px solid $mainGray;
box-sizing: border-box;
box-shadow:
inset 0 3px 3px rgba(255,255,255,0.3),
inset 0 -3px 3px rgba(0,0,0,0.3);
&:hover {
cursor: pointer;
}
}
#strict-light {
position: absolute;
top: 5px;
right: 50px;
width: 10px;
height: 10px;
border-radius: 50%;
border: 2px solid $mainGray;
background: $mainGray;
box-sizing: border-box;
}
#count {
font-size: 2rem;
color: $red;
width: 60px;
height: 40px;
display: flex;
justify-content: center;
align-items: center;
background: #330000;
border-radius: 10px;
#num {
opacity: 0.2;
}
}
.label {
font-size: 0.7rem;
text-align: center;
text-transform: uppercase;
color: $mainGray;
margin: 3px 0 0;
}
}
}
@media screen and (max-width: 767px) {
transform: scale(0.5);
}
}
}
document.addEventListener('DOMContentLoaded', () => {
// * Audio *
const audioCtx = new (window.AudioContext || window.webkitAudioContext)();
audioCtx.suspend();
const frequencies = [110, 164, 220, 329, 261];
const oscillators = frequencies.map((frequency) => {
const oscillator = audioCtx.createOscillator();
oscillator.type = 'triangle';
oscillator.frequency.setValueAtTime(frequency, audioCtx.currentTime);
oscillator.start(0.0);
return oscillator;
});
const gainNodes = oscillators.map((oscillator) => {
const gain = audioCtx.createGain();
oscillator.connect(gain);
gain.connect(audioCtx.destination);
gain.gain.value = 0;
return gain;
});
// * Buttons *
const greenBtn = document.getElementById('green-btn'),
redBtn = document.getElementById('red-btn'),
blueBtn = document.getElementById('blue-btn'),
yellowBtn = document.getElementById('yellow-btn'),
powerBtn = document.getElementById('power-btn'),
startBtn = document.getElementById('start'),
strictBtn = document.getElementById('strict');
// * Count display *
const countDisplay = document.getElementById('num');
let count = 1;
// * Strict light *
const strictLight = document.getElementById('strict-light');
// * Booleans *
let isOn = false;
let isStrict = false;
let isStarted = false;
// * Sequences *
let simonSeq = [];
let playerSeq = [];
// Assigning numbers to represent buttons
[greenBtn.value, redBtn.value, blueBtn.value, yellowBtn.value] = [1,2,3,4];
// * Functions *
const playAudio = (num) => {
gainNodes[num].gain.linearRampToValueAtTime(0.5, audioCtx.currentTime + 0.01);
}
const stopAudio = (num) => {
gainNodes[num].gain.linearRampToValueAtTime(0, audioCtx.currentTime + 0.01);
}
const greenBtnOn = () => {
greenBtn.style.background = '#00e600';
playAudio(1);
}
const greenBtnOff = () => {
greenBtn.style.background = '#00b300';
stopAudio(1);
}
const redBtnOn = () => {
redBtn.style.background = '#ff0000';
playAudio(2);
}
const redBtnOff = () => {
redBtn.style.background = '#b30000';
stopAudio(2);
}
const blueBtnOn = () => {
blueBtn.style.background = '#0066ff';
playAudio(3);
}
const blueBtnOff = () => {
blueBtn.style.background = '#0047b3';
stopAudio(3);
}
const yellowBtnOn = () => {
yellowBtn.style.background = '#ffff1a';
playAudio(4);
}
const yellowBtnOff = () => {
yellowBtn.style.background = '#b3b300';
stopAudio(4);
};
const reset = () => {
clearTimeout(timeout);
simonSeq = [];
playerSeq = [];
count = 1;
isStarted = false;
isStrict = false;
}
const togglePower = () => {
isOn = !isOn;
if (isOn) {
powerBtn.style.float = 'right';
countDisplay.style.opacity = 1;
audioCtx.resume();
} else {
powerBtn.style.float = 'left';
countDisplay.style.opacity = 0.2;
strictLight.style.background = '#2e2c2c';
countDisplay.textContent = '––';
audioCtx.suspend();
reset();
}
}
const toggleStrict = () => {
if (isOn) {
isStrict = !isStrict;
if (isStrict) {
strictLight.style.background = '#ff1a1a';
} else {
strictLight.style.background = '#2e2c2c';
}
}
}
const addToSimonSeq = () => {
if (isOn) {
simonSeq.push(Math.ceil(Math.random() * 4));
}
}
const playSimonSeq = () => {
if (isOn) {
let seqTimingA = 1200;
let seqTimingB = 600;
// These change as player levels up
if (count > 5 && count <= 10) {
seqTimingA = 1000;
seqTimingB = 500;
} else if (count > 10 && count <= 15) {
seqTimingA = 800;
seqTimingB = 400;
} else if (count > 15 && count <= 20) {
seqTimingA = 600;
seqTimingB = 300;
}
let index = 0;
const interval = setInterval(() => {
// conditions
if (simonSeq[index] === 1) {
greenBtnOn();
setTimeout(() => greenBtnOff(), seqTimingB);
} else if (simonSeq[index] === 2) {
redBtnOn();
setTimeout(() => redBtnOff(), seqTimingB);
} else if (simonSeq[index] === 3) {
blueBtnOn();
setTimeout(() => blueBtnOff(), seqTimingB);
} else if (simonSeq[index] === 4) {
yellowBtnOn();
setTimeout(() => yellowBtnOff(), seqTimingB);
}
// display simonSeq index based off of conditions above,
// then increase index / happens every second
simonSeq[index++];
if (index === count) {
clearInterval(interval);
}
}, seqTimingA);
}
}
const checkPlayerSeq = () => {
if (isOn) {
const a = playerSeq.toString(),
b = simonSeq.toString();
if (a !== b) {
countDisplay.textContent = '! !';
playerSeq = [];
playAudio(0);
setTimeout(() => stopAudio(0), 1000);
if (isStrict === false) {
setTimeout(() => playGame(), 2000);
} else if (isStrict) {
isStarted = false;
simonSeq = [];
count = 1;
}
}
if (a === b) {
count++;
if (count >= 21) {
alert('YOU WIN!!!!!!!')
reset();
} else {
playerSeq = [];
addToSimonSeq();
playGame();
}
}
}
}
const formatCount = (count) => count < 10 ? `0${count}` : count;
let timeout;
const playGame = () => {
if (isOn) {
const time = count * 2000 + 3000; // increase time slowly
countDisplay.textContent = formatCount(count);
playSimonSeq();
timeout = setTimeout(() => checkPlayerSeq(), time);
}
}
const startGame = () => {
if (isOn) {
isStarted = true;
addToSimonSeq();
playGame();
}
}
// * Event Listeners *
powerBtn.addEventListener('click', () => togglePower());
startBtn.addEventListener('mousedown', () => {
startBtn.style.boxShadow = `
inset 0 3px 3px rgba(0,0,0,0.3),
inset 0 -3px 3px rgba(255,255,255,0.3)
`;
});
startBtn.addEventListener('mouseup', () => {
startBtn.style.boxShadow = `
inset 0 -3px 3px rgba(0,0,0,0.3),
inset 0 3px 3px rgba(255,255,255,0.3)
`;
isStarted ? false : startGame();
});
strictBtn.addEventListener('mousedown', () => {
strictBtn.style.boxShadow = `
inset 0 3px 3px rgba(0,0,0,0.3),
inset 0 -3px 3px rgba(255,255,255,0.3)
`;
});
strictBtn.addEventListener('mouseup', () => {
strictBtn.style.boxShadow = `
inset 0 -3px 3px rgba(0,0,0,0.3),
inset 0 3px 3px rgba(255,255,255,0.3)
`;
toggleStrict();
});
greenBtn.addEventListener('touchstart', () => {
isOn ? greenBtnOn() : false;
});
greenBtn.addEventListener('touchend', () => {
isOn ? greenBtnOff() : false;
});
redBtn.addEventListener('touchstart', () => {
isOn ? redBtnOn() : false;
});
redBtn.addEventListener('touchend', () => {
isOn ? redBtnOff() : false;
});
blueBtn.addEventListener('touchstart', () => {
isOn ? blueBtnOn() : false;
});
blueBtn.addEventListener('touchend', () => {
isOn ? blueBtnOff() : false;
});
yellowBtn.addEventListener('touchstart', () => {
isOn ? yellowBtnOn() : false;
});
yellowBtn.addEventListener('touchend', () => {
isOn ? yellowBtnOff() : false;
});
greenBtn.addEventListener('mousedown', () => {
isOn ? greenBtnOn() : false;
isStarted ? playerSeq.push(1) : false;
});
greenBtn.addEventListener('mouseup', () => {
isOn ? greenBtnOff() : false;
});
redBtn.addEventListener('mousedown', () => {
isOn ? redBtnOn() : false;
isStarted ? playerSeq.push(2) : false;
});
redBtn.addEventListener('mouseup', () => {
isOn ? redBtnOff() : false;
});
blueBtn.addEventListener('mousedown', () => {
isOn ? blueBtnOn() : false;
isStarted ? playerSeq.push(3) : false;
});
blueBtn.addEventListener('mouseup', () => {
isOn ? blueBtnOff() : false;
});
yellowBtn.addEventListener('mousedown', () => {
isOn ? yellowBtnOn() : false;
isStarted ? playerSeq.push(4) : false;
});
yellowBtn.addEventListener('mouseup', () => {
isOn ? yellowBtnOff() : false;
});
});
Also see: Tab Triggers