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 class="container">
<div class="message-box">
<p class="message-title"></p>
<p class="message"></p>
<div class="close-btn">
<span class="close">×</span>
</div>
</div>
<div class="v-laser vl1"></div>
<div class="v-laser vl2"></div>
<div class="v-laser vl3"></div>
<div class="v-laser vl4"></div>
<div class="v-laser vl5"></div>
<div class="v-laser vl6"></div>
<div class="h-laser hl1"></div>
<div class="h-laser hl2"></div>
<div class="h-laser hl3"></div>
<div class="h-laser hl4"></div>
<div class="h-laser hl5"></div>
<div class="h-laser hl6"></div>
<div class="ruby-wrap">
<div class="ruby r-upper-left"></div>
<div class="ruby r-upper-middle"></div>
<div class="ruby r-upper-right"></div>
<div class="ruby r-bottom"></div>
<div class="ruby r-shadow"></div>
</div>
<div class="steal-this">
<h1>Steal this ruby.</h1>
</div>
</div>
@import url('https://fonts.googleapis.com/css2?family=Merriweather&display=swap');
* {
margin: 0;
padding: 0;
font-family: 'Merriweather', serif;
}
.container {
width: 100vw;
height: 100vh;
position: relative;
background: #8f9bbc;
}
.message-box {
display: none;
z-index: 2;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 250px;
height: 100px;
padding: 20px;
background: #a31414;
box-sizing: border-box;
border-radius: 8px;
box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.3)
}
.message-title, .message {
color: #fff;
font-size: 16px;
text-align: center;
}
.message {
margin-top: 10px;
font-size: 12px;
}
.close-btn {
position: absolute;
top: -15px;
right: -15px;
font-size: 36px;
font-weight: bold;
color: #bd2222;
width: 40px;
height: 40px;
background: #de4040;
border-radius: 50%;
cursor: pointer;
}
.close {
position: absolute;
top: -4px;
left: 9px;
user-select: none;
}
.steal-this {
position: absolute;
left: 50%;
bottom: calc(1/4 * 100vh);
transform: translateX(-50%);
width: 200px;
height: 50px;
display: flex;
flex-direction: column;
align-items: center;
justify-content: space-around;
color: #a31414;
}
h1 {
font-size: 24px;
user-select: none;
}
.v-laser {
display: none;
position: absolute;
width: 3px;
height: 100vh;
box-shadow:
inset 1px 0 10px #fff,
inset 3px 0 0 #f0f,
0 0 50px #fff,
0 0 20px #f0f;
}
.h-laser {
display: none;
position: absolute;
width: 100vw;
height: 3px;
box-shadow:
inset 0 1px 10px #fff,
inset 0 3px 0 #f0f,
0 0 50px #fff,
0 0 20px #f0f;
}
.vl1 { left: calc(1/10 * 100vw); }
.vl2 { left: calc(1/5 * 100vw); }
.vl3 { left: calc(1/3 * 100vw); }
.vl4 { right: calc(1/3 * 100vw); }
.vl5 { right: calc(1/5 * 100vw); }
.vl6 { right: calc(1/10 * 100vw); }
.hl1 { top: calc(1/10 * 100vh); }
.hl2 { top: calc(1/5 * 100vh); }
.hl3 { top: calc(1/4 * 100vh); }
.hl4 { bottom: calc(1/4 * 100vh); }
.hl5 { bottom: calc(1/5 * 100vh); }
.hl6 { bottom: calc(1/10 * 100vh); }
.ruby-wrap {
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
width: 150px;
height: 150px;
box-sizing: border-box;
cursor: grab;
}
.ruby {
position: absolute;
}
.r-upper-left {
width: 0;
height: 0;
border-left: 37px solid transparent;
border-right: 37px solid transparent;
border-bottom: 40px solid #bd2222;
}
.r-upper-middle {
left: 37px;
width: 0;
height: 0;
border-left: 37px solid transparent;
border-right: 37px solid transparent;
border-top: 40px solid #de4040;
}
.r-upper-right {
left: 74px;
width: 0;
height: 0;
border-left: 37px solid transparent;
border-right: 37px solid transparent;
border-bottom: 40px solid #fa6161;
}
.r-bottom {
top: 40px;
width: 0;
height: 0;
border-left: 74px solid transparent;
border-right: 74px solid transparent;
border-top: 74px solid #a31414;
}
.r-shadow {
top: 130px;
left: 10px;
width: 130px;
height: 20px;
border-radius: 50%;
background: rgba(0, 0, 0, 0.1);
}
document.addEventListener('DOMContentLoaded', () => {
// ==== ELEMENTS ==== //
const container = document.querySelector('.container');
const stealThis = document.querySelector('.steal-this');
// Game Over box
const messageBox = document.querySelector('.message-box'),
messageTitle = document.querySelector('.message-title'),
message = document.querySelector('.message'),
closeBtn = document.querySelector('.close-btn');
// Lasers
const vLasers = document.querySelectorAll('.v-laser'),
hLasers = document.querySelectorAll('.h-laser');
let vIndex, hIndex; // used to check if touching laser
// Laser positions
// Get added once ruby is clicked to account for window resizing
let vLasersPos = [];
let hLasersPos = [];
// Ruby divs
const ruby = document.querySelector('.ruby-wrap'),
upperLeft = document.querySelector('.r-upper-left'),
upperMiddle = document.querySelector('.r-upper-middle'),
upperRight = document.querySelector('.r-upper-right');
// Ruby Colors
// Colors start with red, light red, highlight red, and then shadow red.
const rubyColors = ['#bd2222', '#de4040', '#fa6161', '#a31414'];
const color0 = `40px solid ${rubyColors[0]}`,
color1 = `40px solid ${rubyColors[1]}`,
color2 = `40px solid ${rubyColors[2]}`;
let isGameOver = false;
let flashLasers;
// ==== FUNCTIONS ==== //
// Ruby Rotation Fns
function changeColors() {
upperLeft.style.borderBottom = color0;
upperMiddle.style.borderTop = color1;
upperRight.style.borderBottom = color2;
setTimeout(() => {
upperLeft.style.borderBottom = color2;
upperMiddle.style.borderTop = color1;
upperRight.style.borderBottom = color0;
}, 500);
}
function rotateRuby() {
setInterval(() => {
changeColors();
}, 1000);
}
rotateRuby();
// Ruby grab and drag
function dragRuby(elmnt) {
let pos1 = 0, pos2 = 0, pos3 = 0, pos4 = 0;
if (document.querySelector('.ruby')) {
document.querySelector('.ruby').onmousedown = dragMouseDown();
} else {
elmnt.onmousedown = dragMouseDown();
}
function dragMouseDown(e) {
e = e || window.event;
e.preventDefault();
// get the mouse cursor position at startup
pos3 = e.clientX;
pos4 = e.clientY;
document.onmouseup = closeDragElement;
// call a function whenever the cursor moves
document.onmousemove = elementDrag;
}
function elementDrag(e) {
e = e || window.event;
e.preventDefault();
// calculate the new cursor position
pos1 = pos3 - e.clientX;
pos2 = pos4 - e.clientY;
pos3 = e.clientX;
pos4 = e.clientY;
// set the element's new position
elmnt.style.top = (elmnt.offsetTop - pos2) + "px";
elmnt.style.left = (elmnt.offsetLeft - pos1) + "px";
}
function closeDragElement() {
// stop moving when mouse button is released
document.onmouseup = null;
document.onmousemove = null;
}
}
function checkforGameOver() {
// Get border of ruby
const rect = ruby.getBoundingClientRect();
const newRectBottom = rect.bottom - 35; // account for shadow
// Check if ruby is overlapping an "on" horizontal laser
if (vLasersPos[vIndex] <= rect.right && vLasersPos[vIndex] >= rect.left) {
clearInterval(flashLasers);
ruby.style.display = 'none';
gameOver('laser');
}
// Check if ruby is overlapping an "on" vertical laser
if (hLasersPos[hIndex] >= rect.top && hLasersPos[hIndex] <= newRectBottom) {
clearInterval(flashLasers);
ruby.style.display = 'none';
gameOver('laser');
}
// Check for win
if (
rect.left <= 0 ||
rect.right >= window.innerWidth ||
rect.top <= 0 ||
newRectBottom >= window.innerHeight
) {
clearInterval(flashLasers);
ruby.style.display = 'none';
win();
}
}
// Flash Lasers Randomly
function displayLasers() {
// Change background and hide text
container.style.background = 'linear-gradient(30deg, #ff0000, #ffccff)';
stealThis.style.visibility = 'hidden';
// Display lasers
vLasers.forEach(laser => laser.style.display = 'block');
hLasers.forEach(laser => laser.style.display = 'block');
// Get Laser positions - pushed into arrays
vLasers.forEach(laser => vLasersPos.push(laser.offsetLeft));
hLasers.forEach(laser => hLasersPos.push(laser.offsetTop));
// Then display one random vertical and one random horizontal laser at a time
flashLasers = setInterval(() => {
let randomI = Math.floor(Math.random() * 6);
let randomJ = Math.floor(Math.random() * 6);
setTimeout(() => {
for (i = 0; i < vLasers.length; i++) {
if (i === randomI) {
vLasers[i].style.display = 'block';
vIndex = i; // assigning which num in array to "display" as on
} else {
vLasers[i].style.display = 'none';
}
}
}, 100);
setTimeout(() => {
for (j = 0; j < hLasers.length; j++) {
if (j === randomJ) {
hLasers[j].style.display = 'block';
hIndex = j; // assigning which num in array to "display" as on
} else {
hLasers[j].style.display = 'none';
}
}
}, 200);
}, 200);
}
function gameOver(id) {
isGameOver = true;
if (isGameOver) {
messageBox.style.display = 'block';
messageTitle.textContent = 'Game Over:'
}
if (id === 'drop') {
ruby.style.display = 'none';
message.textContent = 'You dropped the ruby.'
} else if (id === 'laser') {
message.textContent = 'You ran into a security laser.'
}
}
function win() {
isGameOver = true;
if (isGameOver) {
messageBox.style.display = 'block';
messageTitle.textContent = 'You Win!';
message.textContent = 'Congratulations, you are a thief.'
}
}
function reset() {
isGameOver = false;
vLasersPos = [];
hLasersPos = [];
vLasers.forEach(laser => laser.style.display = 'none');
hLasers.forEach(laser => laser.style.display = 'none');
messageBox.style.display = 'none';
container.style.background = '#8f9bbc';
// reset the ruby in the center
ruby.style.display = 'block';
ruby.style.top = '50%';
ruby.style.left = '50%';
ruby.style.transform = 'translate(-50%, -50%)';
stealThis.style.visibility = 'visible';
}
// ==== EVENT LISTENERS ==== //
ruby.addEventListener('mousedown', () => {
displayLasers();
dragRuby(ruby);
});
ruby.addEventListener('mousemove', () => checkforGameOver());
ruby.addEventListener('mouseup', () => {
if (isGameOver) {
return;
} else {
gameOver('drop');
};
clearInterval(flashLasers);
});
closeBtn.addEventListener('click', () => reset());
// Mobile
// ruby.addEventListener('touchstart', () => {
// displayLasers();
// dragRuby(ruby);
// });
// ruby.addEventListener('touchmove', () => checkforGameOver());
// ruby.addEventListener('touchend', () => {
// if (isGameOver) {
// return;
// } else {
// gameOver('drop');
// };
// clearInterval(flashLasers);
// });
// closeBtn.addEventListener('touchstart', () => reset());
});
Also see: Tab Triggers