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.
<canvas></canvas>
html,
body {
height: 100%;
text-align: center;
margin: 0;
}
p {
z-index: 2;
position: absolute;
top: 0;
left: 0;
}
// get canvas 2D context object
const canvas = document.querySelector("canvas");
const ctx = canvas.getContext("2d");
const GLOBALS = {
anchorX: 0,
anchorY: 0,
mapAnchor: { x: 0, y: 0 },
click: { x: undefined, y: undefined },
mouse: { x: undefined, y: undefined },
mouseDown: { x: undefined, y: undefined },
mouseUp: { x: undefined, y: undefined },
distance: (a, b) =>
Math.sqrt(Math.pow(b.x - a.x, 2) + Math.pow(b.y - a.y, 2)), // formula: √ (x2 − x1)^2 + (y2 − y1)^2
angle: (a, b) => Math.atan2(b.y - a.y, b.x - a.x) // formula: atan2(y2 - y1, x2 - x1)
// Globally accessable helper functions for calculating angle and distance between two points
};
// use GLOBALS to keep track of mouse actions, and have them accessable by every sprite
const PROPS = [];
/* Our main character sprite */
class Player {
constructor(x, y) {
this.x = x;
this.y = y;
this.radius = 30;
}
render() {
//this.x += GLOBALS.charX / 10;
//this.y += GLOBALS.charY / 10;
let { x, y, radius } = this;
ctx.beginPath();
ctx.lineWidth = 2;
ctx.strokeStyle = "red";
ctx.arc(x, y, radius, 0, 2 * Math.PI);
ctx.stroke();
}
}
const CHARS = [];
let player = new Player(window.innerWidth / 2, window.innerHeight / 2);
CHARS.push(player);
// function for applying any initial settings
function init() {
// apply a fullscreen fit
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
// register event listeners to store mouse actions and coordinates inside GLOBALS, so that they can be accessed by sprites that need them.
window.addEventListener("click", (e) => {
GLOBALS.mouseUp.x = GLOBALS.mouseDown.x = GLOBALS.mouse.x = GLOBALS.click.x =
e.pageX;
GLOBALS.mouseUp.y = GLOBALS.mouseDown.y = GLOBALS.mouse.y = GLOBALS.click.y =
e.pageY;
});
function tStart(e) {
GLOBALS.mouseUp.x = undefined;
GLOBALS.mouseUp.y = undefined;
GLOBALS.mouseDown.x = GLOBALS.mouse.x = e.pageX || e.touches[0]?.pageX;
GLOBALS.mouseDown.y = GLOBALS.mouse.y = e.pageY || e.touches[0]?.pageY;
}
function tEnd(e) {
GLOBALS.mouseDown.x = GLOBALS.mouse.x = undefined;
GLOBALS.mouseDown.y = GLOBALS.mouse.y = undefined;
}
function tMove(e) {
GLOBALS.mouse.x = e.pageX || e.touches[0]?.pageX;
GLOBALS.mouse.y = e.pageY || e.touches[0]?.pageY;
}
// add listeners for both mobile and desktop (for demonstration purposes)
window.addEventListener("touchstart", tStart);
window.addEventListener("mousedown", tStart);
window.addEventListener("touchend", tEnd);
window.addEventListener("mouseup", tEnd);
window.addEventListener("touchmove", tMove);
window.addEventListener("mousemove", tMove);
}
function Pot(x, y) {
x += GLOBALS.mapAnchor.x;
y += GLOBALS.mapAnchor.y;
ctx.globalAlpha = 1;
// stem
ctx.rect(x + 14, y + 20, 4, 60);
ctx.stroke();
ctx.fill();
// flower head
ctx.fillStyle = "white";
ctx.lineWidth = 2;
ctx.rect(x, y, 10, 10);
ctx.rect(x + 22, y + 22, 10, 10);
ctx.rect(x + 22, y, 10, 10);
ctx.rect(x, y + 22, 10, 10);
ctx.stroke();
ctx.fill();
ctx.beginPath();
ctx.rect(x + 11, y - 4, 10, 10);
ctx.rect(x - 4, y + 11, 10, 10);
ctx.rect(x + 26, y + 11, 10, 10);
ctx.rect(x + 11, y + 25, 10, 10);
ctx.stroke();
ctx.fill();
ctx.beginPath();
ctx.rect(x + 6, y + 6, 20, 20);
ctx.stroke();
ctx.fill();
// pot
ctx.beginPath();
ctx.rect(x - 4, y + 90, 39, 22);
ctx.stroke();
ctx.fill();
ctx.strokeRect(x - 7, y + 80, 45, 9);
ctx.rect(x - 7, y + 80, 45, 9);
ctx.fill();
}
// function for rendering background elements
function renderBackground() {
// update map anchor..
GLOBALS.mapAnchor.x -= GLOBALS.anchorX / 10;
GLOBALS.mapAnchor.y -= GLOBALS.anchorY / 10;
Pot(window.innerWidth / 2 + 50, window.innerHeight / 2);
Pot(window.innerWidth / 2 - 100, window.innerHeight / 2 - 70);
}
// function for rendering prop objects in PROPS
function renderProps() {}
// function for rendering character objects in CHARS
function renderCharacters() {
for (let i of CHARS) {
i.render();
}
}
/* CLASS FOR JOYSTICKS */
class Joystick {
constructor(x, y) {
this.x = x;
this.y = y;
this.distance = { x: 0, y: 0 };
this.angle = 0;
}
render() {
// display distance property before every render
let { x, y } = this;
let { distance, angle } = GLOBALS;
ctx.beginPath();
ctx.lineWidth = 2;
ctx.globalAlpha = 0.7;
ctx.strokeStyle = "black";
ctx.arc(x, y, 50, 0, 2 * Math.PI);
ctx.stroke();
ctx.beginPath();
// logic for keeping thumbstick inside the circle
if (GLOBALS.mouseUp.x) {
ctx.arc(x, y, 20, 0, 2 * Math.PI);
this.distance.x = x - this.x;
this.distance.y = y - this.y;
GLOBALS.anchorX = x - this.x;
GLOBALS.anchorY = y - this.y;
this.angle = angle({ x: x, y: y }, this);
} else if (
distance(GLOBALS.mouse, this) < 50 &&
distance(GLOBALS.mouseDown, this) < 50
) {
ctx.arc(GLOBALS.mouse.x, GLOBALS.mouse.y, 30, 0, 2 * Math.PI);
this.angle = angle(GLOBALS.mouse, this);
this.distance.x = GLOBALS.mouse.x - this.x;
this.distance.y = GLOBALS.mouse.y - this.y;
GLOBALS.anchorX = GLOBALS.mouse.x - this.x;
GLOBALS.anchorY = GLOBALS.mouse.y - this.y;
} else if (
distance(GLOBALS.mouse, this) > 50 &&
distance(GLOBALS.mouseDown, this) < 50
) {
let { x, y } = GLOBALS.mouse;
let d = distance(GLOBALS.mouse, this) - 50,
ok = false;
while (ok === false) {
if (x < this.x) {
x++;
} else {
x--;
}
if (y < this.y) {
y++;
} else {
y--;
}
if (distance({ x: x, y: y }, this) < 50) {
ok = true;
}
}
ctx.arc(x, y, 30, 0, 2 * Math.PI);
this.distance.x = x - this.x;
this.distance.y = y - this.y;
GLOBALS.anchorX = x - this.x;
GLOBALS.anchorY = y - this.y;
this.angle = angle({ x: x, y: y }, this);
} else {
ctx.arc(x, y, 30, 0, 2 * Math.PI);
this.distance.x = x - this.x;
this.distance.y = y - this.y;
GLOBALS.anchorX = x - this.x;
GLOBALS.anchorY = y - this.y;
this.angle = angle({ x: x, y: y }, this);
}
ctx.stroke();
ctx.fillStyle = "gray";
ctx.fill();
}
}
// function for rendering onscreen controls
let stick = new Joystick(70, window.innerHeight - 70);
function renderControls() {
stick.render();
}
// main function to be run for rendering frames
function startFrames() {
// erase entire canvas
ctx.clearRect(0, 0, canvas.width, canvas.height);
// render each type of entity in order, relative to layers
renderBackground();
renderProps();
renderCharacters();
renderControls();
// rerun function (call next frame)
window.requestAnimationFrame(startFrames);
}
init(); // initialize game settings
startFrames(); // start running frames
Also see: Tab Triggers