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="wrapper">
<small class="touchscreen-message">
It seems that you are using a touchscreen.
<br />
This demo is not optimized for it, please try it on a computer.
</small>
<div class="intro">Move around using arrows and space.</div>
<div class="game">
<div class="player"></div>
<div class="trail"></div>
</div>
<pre class="status"></pre>
<!-- <a class="link" href="./index.html">Read the blog post ↗</a> -->
</div>
$light-blue: rgb(88, 170, 239);
$white: #eef3f5;
$gray: #555;
*,
*::before,
*::after {
box-sizing: border-box;
}
* {
margin: 0;
}
body {
font-family: -apple-system, BlinkMacSystemFont, Roboto, "Helvetica Neue",
Helvetica, Inter, Arial, "Noto Sans", sans-serif;
line-height: 1.5;
overflow-x: hidden;
background: #18181b;
color: $white;
}
.link {
color: #6290ff;
border-bottom: 1px solid transparent;
transition: color 250ms, border 250ms;
margin-top: 50px;
display: block;
&:hover {
color: $white;
}
}
.wrapper {
padding: 30px;
}
.status {
display: flex;
margin-top: 20px;
gap: 10px;
}
.status div {
flex-basis: 110px;
}
.status span {
color: #a783f7;
}
.jump-status {
height: 15px;
max-width: 15px;
flex-shrink: 0;
border-radius: 50%;
margin-top: 5px;
display: inline-block;
}
.jump-status--ready {
background-color: lime;
vertical-align: baseline;
}
.jump-status--not-ready {
background-color: red;
}
.game {
height: 120px;
position: relative;
}
.player {
position: absolute;
width: 20px;
height: 30px;
background: #3171f6;
// move our blocky player to the coordinate's system origin
bottom: 0;
left: -10px;
}
.trail-point {
position: absolute;
width: 2px;
height: 2px;
bottom: 0;
left: -1px;
border-radius: 2px;
}
.intro {
color: #888;
position: absolute;
}
.touchscreen-message {
margin-bottom: 20px;
color: #f46d9d;
display: none;
}
@media (max-width: 400px) {
.touchscreen-message br {
display: none;
}
}
@media (hover: none) {
.touchscreen-message {
display: block;
}
}
// ----- Types
export type Vector = {
x: number;
y: number;
};
export type Trail = Array<{
color: string;
position: Vector;
}>;
// ----- Constants
const FRAME_DURATION: number = 1000 / 60;
// ----- Game state
// Game speed
const speed: number = 1;
// Movement
const accelerationGround: number = 1 * speed;
const decelerationGround: number = 2 * speed;
const maxSpeed: number = 8 * speed;
const decelerationAir: number = 0.5 * speed;
// Jumping
const initialJumpVelocity: number = 8 * speed;
const jumpDeceleration: number = 0.4 * speed;
const jumpFall: number = 1 * speed;
const jumpAllowThreshold: number = 30;
const velocity: Vector = {
x: 0,
y: 0
};
const position: Vector = {
x: 0,
y: 0
};
let isOnGround: boolean = true;
let isJumpReady: boolean = true;
const trailMaxLength: number = 200;
const trail: Trail = [];
let trailColor: string;
// Render
const statusElement = document.querySelector(".status") as HTMLPreElement;
const playerElement = document.querySelector(".player") as HTMLDivElement;
const trailElement = document.querySelector(".trail") as HTMLDivElement;
const jumpElement = document.querySelector(".jump") as HTMLDivElement;
const introElement = document.querySelector(".intro") as HTMLDivElement;
// Add some color to stringified objects
function replacer(key, value) {
return typeof value === "number" ? `<span>${value.toFixed(2)}</span>` : value;
}
function prettyStringify(object) {
return JSON.stringify(object, replacer, 2).replace(/"/g, "");
}
function render() {
// status
statusElement.innerHTML = `<div>position: ${prettyStringify(position)}</div>`;
statusElement.innerHTML += `<div>velocity: ${prettyStringify(velocity)}</div>`;
statusElement.innerHTML += isJumpReady
? '<div class="jump-status jump-status--ready" />'
: '<div class="jump-status jump-status--not-ready" />';
// player
playerElement.style.transform = `translate(${
position.x
}px, ${-position.y}px)`;
}
function renderTrail() {
let trailHTML: string = "";
for (let i = 0; i < trail.length; i++) {
const point = trail[i];
const { x, y } = point.position;
trailHTML += `<div
class="trail-point"
style="
background: ${point.color};
transform: translate(${x}px, ${-y}px);
"></div>`;
}
trailElement.innerHTML = trailHTML;
}
// ----- Keyboard input
const activeKeys: Record<string, boolean> = {};
const keys = {
SPACE: " ",
LEFT: "ArrowLeft",
RIGHT: "ArrowRight",
UP: "ArrowUp",
DOWN: "ArrowDown"
};
window.addEventListener("keydown", (e) => {
activeKeys[e.key] = true;
// Remove intro text
introElement.innerHTML = '';
// Prevent page scrolling on space press
if (e.key === ' ') {
e.preventDefault();
}
});
window.addEventListener("keyup", (e) => {
delete activeKeys[e.key];
});
// ----- Update
function updateHorizontalMovement(delta: number) {
const isLeftPressed = activeKeys[keys.LEFT];
const isRightPressed = activeKeys[keys.RIGHT];
const isExclusivelyLeft = isLeftPressed && !isRightPressed;
const isExclusivelyRight = isRightPressed && !isLeftPressed;
const isMovingRight = velocity.x > 0;
const isMovingLeft = velocity.x < 0;
const deceleration = isOnGround ? decelerationGround : decelerationAir;
if (isExclusivelyLeft) {
trailColor = "lime";
// Only left arrow is pressed
if (isMovingRight) {
// Slow down if player is already moving right
velocity.x -= deceleration * delta;
} else {
// If not, accelerate to the left
velocity.x -= accelerationGround * delta;
}
} else if (isExclusivelyRight) {
trailColor = "lime";
// Only right arrow is pressed
if (isMovingLeft) {
// Slow down if player is already moving left
velocity.x += deceleration * delta;
} else {
// If not, accelerate to the right
velocity.x += accelerationGround * delta;
}
} else {
trailColor = "red";
// Either both or no horizontal arrows are pressed
// Decelerate to the stop
if (isMovingRight) {
// Player is moving right, decelerate
velocity.x -= deceleration * delta;
// When velocity starts going in the opposite direction, stop the player
if (velocity.x < 0) {
velocity.x = 0;
}
} else if (isMovingLeft) {
// Player is moving left, decelerate
velocity.x += deceleration * delta;
// When velocity starts going in the opposite direction, stop the player
if (velocity.x > 0) {
velocity.x = 0;
}
}
}
// Cap at maximum speed
if (velocity.x > maxSpeed) {
trailColor = "silver";
velocity.x = maxSpeed;
} else if (velocity.x < -maxSpeed) {
trailColor = "silver";
velocity.x = -maxSpeed;
}
// Update player's position using new velocity value
position.x += velocity.x * delta;
}
function updateVerticalMovement(delta: number) {
const isJumpPressed: boolean = activeKeys[keys.SPACE];
isOnGround = position.y === 0;
if (isOnGround) {
if (isJumpPressed && isJumpReady) {
velocity.y = initialJumpVelocity;
trailColor = "blue";
isJumpReady = false;
}
} else {
if (isJumpPressed && velocity.y > 0) {
trailColor = "blue";
velocity.y -= jumpDeceleration * delta;
} else {
velocity.y -= jumpFall * delta;
trailColor = "purple";
}
}
// Update position
position.y += velocity.y * delta;
// Prevent player going into the ground
if (position.y < 0) {
position.y = 0;
velocity.y = 0;
}
if (!isJumpPressed && position.y < jumpAllowThreshold) {
// allow jumping again
isJumpReady = true;
}
}
function updateTrail() {
const last = trail[trail.length - 1];
const hasMoved =
position.x !== last?.position.x || position.y !== last?.position.y;
if (hasMoved) {
trail.push({
color: trailColor,
position: {
...position
}
});
if (trail.length > trailMaxLength) {
trail.shift();
}
// For performance, trail is only rendered when it is changed
renderTrail();
}
}
// ----- Game loop
let lastUpdate: number = performance.now();
function gameLoop() {
const now = performance.now();
const delta = (now - lastUpdate) / FRAME_DURATION;
// Update game state
updateHorizontalMovement(delta);
updateVerticalMovement(delta);
updateTrail();
// Render
render();
// Update time
lastUpdate = now;
// Next frame
requestAnimationFrame(gameLoop);
// setTimeout(gameLoop, 30);
}
gameLoop();
Also see: Tab Triggers