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 id="canvas" width="500" height="500"></canvas>
body { display: flex; margin: 0; width: 100vw; height: 100vh; user-select: none; }
#canvas {
max-width: 100%;
max-height: 100%;
margin: auto;
}
// Croquet Tutorial 4
// View Smoothing
// Croquet Studios, 2019-2021
// -- Constants --
const Q = Croquet.Constants;
Q.TICK_MS = 500; // milliseconds per actor tick
Q.SPEED = 0.15; // dot movment speed in pixels per millisecond
Q.CLOSE = 0.1; // minimum distance in pixels to a new destination
Q.SMOOTH = 0.05; // weighting between extrapolated and current positions. 0 > SMOOTH >= 1
// -- Vector Functions --
//
// Because these functions don't save any state, they can be safely used by both the model and the view.
function add(a,b) {
return { x: (a.x + b.x), y: (a.y + b.y) };
}
function subtract(a,b) {
return { x: (a.x - b.x), y: (a.y - b.y) };
}
function scale(v,s) {
return {x: v.x * s, y: v.y * s};
}
function dotProduct(a, b) {
return a.x * b.x + a.y * b.y;
}
function magnitude(v) {
return Math.sqrt(v.x * v.x + v.y * v.y);
}
function normalize(v) {
const m = magnitude(v);
return {
x: v.x/m,
y: v.y/m
};
}
function lerp(a, b, f = 0.5) {
return {
x: a.x + (b.x - a.x) * f,
y: a.y + (b.y - a.y) * f,
};
}
// -- RootModel --
//
// The root model handles join and exit events. All the real work occurs in the individual actors.
class RootModel extends Croquet.Model {
init() {
this.actors = new Map();
this.actorColors = new Map();
this.subscribe(this.sessionId, "view-join", this.viewJoin);
this.subscribe(this.sessionId, "view-exit", this.viewDrop);
this.hue = 0;
}
viewJoin(viewId) {
let actorColor = this.actorColors.get(viewId);
if (!actorColor) {
actorColor = `hsl(${this.hue += 137.5}, 100%, 50%)`;
this.actorColors.set(viewId, actorColor);
}
const actor = Actor.create(viewId);
actor.color = actorColor;
this.actors.set(viewId, actor);
this.publish("actor", "join", actor);
}
viewDrop(viewId) {
const actor = this.actors.get(viewId);
this.actors.delete(viewId);
actor.destroy();
this.publish("actor", "exit", actor);
}
}
RootModel.register("RootModel");
// -- Actor --
//
// Each ball is represented by an actor in the model. Every tick the actor moves toward its goal. If
// if receives a 'goto' message from the view, it will change its destination.
class Actor extends Croquet.Model {
init(viewId) {
this.viewId = viewId;
this.position = this.randomPosition();
this.goal = {...this.position};
this.velocity = {x: 0, y: 0};
this.future(Q.TICK_MS).tick();
this.subscribe(viewId, "goto", this.goto);
}
randomPosition() {
return { x: this.random() * 500, y: this.random() * 500 };
}
goto(goal) {
this.goal = goal;
const delta = subtract(goal, this.position);
if (magnitude(delta) < Q.CLOSE) {
this.goto(randomPosition());
} else {
const unit = normalize(delta);
this.velocity = scale(unit, Q.SPEED);
}
}
arrived() {
const delta = subtract(this.goal, this.position);
return (dotProduct(this.velocity, delta) <= 0);
}
tick() {
this.position = add(this.position, scale(this.velocity,Q.TICK_MS));
if (this.arrived()) this.goto(this.randomPosition());
this.publish(this.id, "moved", this.now());
this.future(Q.TICK_MS).tick();
}
}
Actor.register("Actor");
// -- View Globals --
const canvas = document.querySelector("#canvas");
const cc = canvas.getContext('2d');
let viewTime = 0; // The last time the view was updated is saved globally so pawns can use it.
// -- RootView --
//
// The root view handles clicks and join and exit events. Every update it tells all the pawns
// to draw themselves.
class RootView extends Croquet.View {
constructor(model) {
super(model);
this.pawns = new Map();
model.actors.forEach(actor => this.addPawn(actor));
this.subscribe("actor", "join", this.addPawn);
this.subscribe("actor", "exit", this.removePawn);
canvas.onclick = e => this.onClick(e);
}
onClick(e) {
const r = canvas.getBoundingClientRect();
const scale = canvas.width / Math.min(r.width, r.height);
const x = (e.clientX - r.left) * scale;
const y = (e.clientY - r.top) * scale;
this.publish(this.viewId, "goto", {x,y});
}
addPawn(actor) {
const pawn = new Pawn(actor);
this.pawns.set(actor, pawn);
}
removePawn(actor) {
const pawn = this.pawns.get(actor);
if (!pawn) return;
pawn.detach();
this.pawns.delete(actor);
}
update(time) {
// if this is the first update in a while, jump all pawns' times
// so they don't generate huge movement deltas
if (time - viewTime > 1000) {
for (const pawn of this.pawns.values()) pawn.lastMoved = time;
}
// make frame time accessible in event handlers
viewTime = time;
cc.strokeStyle = "black";
cc.clearRect(0, 0, canvas.width, canvas.height);
cc.strokeRect(0, 0, canvas.width, canvas.height);
for (const pawn of this.pawns.values()) pawn.update();
}
}
// -- Pawn --
//
// Each actor in the model has corresponding pawn in the view. Pawns update their positions smoothly
// each frame, even if their actor is updating itself much more infrequently.
class Pawn extends Croquet.View {
constructor(actor) {
super(actor);
this.actor = actor;
this.position = {...actor.position};
this.actorMoved();
this.subscribe(actor.id, {event: "moved", handling: "oncePerFrame"}, this.actorMoved);
}
actorMoved() {
// Save when model was last updated
this.lastMoved = viewTime;
}
update() {
// If this is our pawn, draw our goal and our unsmoothed position from the model.
if (this.actor.viewId === this.viewId) {
this.draw(this.actor.goal, null, this.actor.color);
this.draw(this.actor.position, "lightgrey");
}
// Draw the smoothed positions of all the pawns, including our own.
// First we extrapolate from the last position we received from the model.
// Then we average our extrapolation with our current position.
const delta = scale(this.actor.velocity, viewTime - this.lastMoved);
const extrapolation = add(this.actor.position, delta);
this.position = lerp(this.position, extrapolation, Q.SMOOTH);
this.draw(this.position, this.actor.color);
}
draw({x, y}, color, border) {
cc.strokeStyle = border;
cc.fillStyle = color;
cc.beginPath();
cc.arc(x, y, 10, 0, 2 * Math.PI);
if (color) cc.fill();
if (border) cc.stroke();
}
}
Croquet.Session.join({
appId: "io.codepen.croquet.smooth",
apiKey: "1_9oolgb5b5wc5kju39lx8brrrhm82log9xvdn34uq",
name: "unnamed",
password: "secret",
model: RootModel,
view: RootView,
tps: 1000/Q.TICK_MS,
});
Also see: Tab Triggers