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.
body
margin: 0
background: #000
overflow: hidden
min-height: 100%
canvas
position: absolute
top: 50%
left: 50%
transform: translateX(-50%) translateY(-50%)
const hexRadius = 20;
const hexLineWeight = 2;
const hexMargin = 2;
const drawHex = false;
const drawPoints = false;
const drawStraightLines = false;
const drawCurves = true;
const drawBoids = false;
const boidInitialVelocity = 4;
const boidMaxVelocity = 5;
const boidAvoidanceDist = hexRadius * 1.5;
const boidAvoidanceScalar = 0.02;
const boidCohesionDist = hexRadius * 2;
const boidCohesionScalar = 0.002;
const boidAlignDist = hexRadius * 3;
const boidAlignScalar = 0.1;
let hexHeight, hexWidth, columns, rows;
let hexagons = [];
let boids = [];
let boidCount = 200;
function setup() {
hexWidth = hexRadius * 2;
hexHeight = Math.sqrt(3)*hexRadius;
columns = Math.ceil(window.innerWidth / (hexRadius * 3));
rows = Math.ceil(window.innerHeight / (hexHeight / 2)) + 1;
createCanvas((columns + 1/4) * (hexRadius * 3), (rows + 1) * (hexHeight / 2));
frameRate(60);
fill(255, 100);
stroke(255);
strokeWeight(5);
noStroke();
// initialise 2D array of hexagons
for (let x = 0; x < columns; x++) {
hexagons.push([]);
for (let y = 0; y < rows; y++) {
const active = Math.round(random()*0.6);
hexagons[x].push(new Hex(x, y, active));
}
}
// neighbouring needs to be done after they're all initialised
for (let x = 0; x < columns; x++) {
for (let y = 0; y < rows; y++) {
hexagons[x][y].initialiseNeighbours(x, y);
}
}
// initialise boids
for (let i = 0; i < boidCount; i++) {
boids.push(new Boid(i));
}
}
function draw() {
background(0);
for (let y = 0; y < rows; y++) {
for (let x = 0; x < columns; x++) {
let hex = hexagons[x][y];
hex.draw();
hex.checkActive();
}
}
if (drawBoids) {
for (let i = 0; i < boidCount; i++) {
boids[i].draw();
}
}
update();
}
function update() {
for (let y = 0; y < rows; y++) {
for (let x = 0; x < columns; x++) {
let hex = hexagons[x][y];
hex.updateActive();
}
}
for (let i = 0; i < boidCount; i++) {
boids[i].update();
}
}
class Boid {
constructor(i) {
this.i = i;
// establish pixel position
this.pos = createVector(random(0, width), random(0, height));
// establish random unit velictiy
this.vel = p5.Vector.random2D().mult(boidInitialVelocity);
}
draw() {
fill(255);
ellipse(this.pos.x, this.pos.y, 5, 5);
}
update() {
this.separate();
this.cohesion();
this.align();
this.vel.limit(boidMaxVelocity);
this.pos.add(this.vel);
this.borders();
}
separate() {
let result = createVector(0, 0);
// loop through other boids
for (let i = 0; i < boidCount; i++) {
const other = boids[i];
const dist = this.pos.dist(other.pos);
// allow for other == this
if (dist < boidAvoidanceDist && dist > 0) {
result.sub(p5.Vector.sub(other.pos, this.pos));
}
}
result.mult(boidAvoidanceScalar);
this.vel.add(result);
}
cohesion() {
let result = createVector(0, 0);
let numNeighbours = 0;
// loop through other boids
for (let i = 0; i < boidCount; i++) {
const other = boids[i];
const dist = this.pos.dist(other.pos);
// allow for other == this
if (dist < boidCohesionDist && dist > 0) {
result.add(other.pos);
numNeighbours++;
}
}
if (numNeighbours == 0) return;
result.div(numNeighbours);
result.sub(this.pos);
result.mult(boidCohesionScalar);
this.vel.add(result);
}
align() {
let result = createVector(0, 0);
let numNeighbours = 0;
// loop through other boids
for (let i = 0; i < boidCount; i++) {
const other = boids[i];
const dist = this.pos.dist(other.pos);
// allow for other == this
if (dist < boidAlignDist && dist > 0) {
result.add(other.vel);
numNeighbours++;
}
}
if (numNeighbours == 0) return;
result.div(numNeighbours);
result.mult(boidAlignScalar);
this.vel.add(result);
}
// reappear on the other edge
borders() {
// right
if (this.pos.x > width) {
this.pos.x = 0;
}
// left
if (this.pos.x < 0) {
this.pos.x = width;
}
// bottom
if (this.pos.y > height) {
this.pos.y = 0;
}
// top
if (this.pos.y < 0) {
this.pos.y =height;
}
}
}
class Hex {
constructor(x, y, active) {
// establish grid position
this.pos = createVector(x, y);
// establish pixel position
this.pixelPos = createVector(0, 0);
this.pixelPos.x = hexWidth * (1.5 * x + 0.5 + y % 2 * 0.75);
this.pixelPos.y = hexHeight * (y * 0.5 + 0.5);
// establish state
this.active = active;
this.nextActive = false;
// establish neighbours
this.neighbours = [];
// chose random layout for dense (5/6 neighbours) display
// 1, 2, 3
this.denseLayout = Math.ceil(random(3));
}
initialiseNeighbours(x, y) {
let n = [false, false, false, false, false, false];
const odd = y%2;
// above
if (y >= 2) {
n[0] = hexagons[x][y-2];
}
// top right
if (y >= 1) {
if (!odd || x < columns-1) {
n[1] = hexagons[x+odd][y-1];
}
}
// bottom right
if (y < rows-1) {
if (!odd || x < columns-1) {
n[2] = hexagons[x+odd][y+1];
}
}
// bottom
if (y < rows-2) {
n[3] = hexagons[x][y+2];
}
// bottom left
if (y < rows-1) {
if (odd || x >= 1) {
n[4] = hexagons[x-1+odd][y+1];
}
}
// top left
if (y >= 1) {
if (odd || x >= 1) {
n[5] = hexagons[x-1+odd][y-1];
}
}
this.neighbours = n;
}
updateActive() {
this.active = this.nextActive;
}
countActiveNeighbours() {
let activeNeighbours = 0;
for (let i = 0; i < 6; i++) {
if (this.neighbours[i] && this.neighbours[i].active) {
activeNeighbours++;
}
}
return activeNeighbours;
}
getActiveNeighbours() {
let activeNeighbours = [];
for (let i = 0; i < 6; i++) {
if (this.neighbours[i] && this.neighbours[i].active) {
activeNeighbours.push(true);
} else {
activeNeighbours.push(false);
}
}
return activeNeighbours;
}
checkActive() {
this.nextActive = false;
// loop through every boid and check target pos
// appoximate by checking whether it's within the outer radius of the hexes
for (let i = 0; i < boidCount; i++) {
var boidPos = boids[i].pos;
if (boidPos.dist(this.pixelPos) < hexRadius) {
this.nextActive = true;
}
}
}
checkNeighbours() {
return true;
}
draw() {
push();
translate(this.pixelPos.x, this.pixelPos.y);
// draw hexagon
noStroke();
if (this.active) {
let activeNeighbours = this.countActiveNeighbours();
fill(80-10*activeNeighbours, 20+40*activeNeighbours, 130+180*activeNeighbours, 100);
} else {
fill(0, 40);
}
if (drawHex) {
beginShape();
for (let i = 0; i < 6; i++) {
vertex((hexRadius-hexMargin/2)*cos(i*Math.PI/3), (hexRadius-hexMargin/2)*sin(i*Math.PI/3));
}
endShape(CLOSE);
}
// draw shapes
if (this.active) {
let activeNeighboursCount = this.countActiveNeighbours();
let activeNeighbours = this.getActiveNeighbours();
stroke(255);
strokeWeight(hexLineWeight);
noFill();
// no neighbours
if (activeNeighboursCount == 0) {
if (drawPoints) {
point(0, 0);
// ellipse(0, 0, hexRadius/2);
}
}
// one neighbour
else if (activeNeighboursCount == 1) {
if (drawStraightLines) {
var pos = getEdgePos(activeNeighbours.indexOf(true));
line(0, 0, pos.x, pos.y);
}
}
// two or three neighbours
else if (activeNeighboursCount == 2 || activeNeighboursCount == 3) {
if (drawCurves) {
for (var i = 0; i < 6; i++) {
if (activeNeighbours[i]) {
for (var j = i+1; j < 6; j++) {
if (activeNeighbours[j]) {
var pos1 = getEdgePos(i);
var pos2 = getEdgePos(j);
drawCurveThroughOrigin(pos1, pos2);
}
}
}
}
}
}
// four neighbours
else if (activeNeighboursCount == 4) {
if (drawCurves) {
let skipped1 = activeNeighbours.indexOf(false);
let skipped2 = activeNeighbours.slice(skipped1+1).indexOf(false) + skipped1 + 1;
// make list of active edge positions
var positions = [];
for (let i = 0; i < 6; i++) {
if (i != skipped1 && i != skipped2) {
positions.push(getEdgePos(i));
}
}
// skips are adjacent
if (skipped2 - skipped1 == 1) {
if (this.denseLayout == 3) {
// connect edges to adjacent edges
drawCurveThroughOrigin(positions[0], positions[1]);
drawCurveThroughOrigin(positions[1], positions[2]);
drawCurveThroughOrigin(positions[2], positions[3]);
}
else if (this.denseLayout == 2) {
// cross over curves
drawCurveThroughOrigin(positions[0], positions[2]);
drawCurveThroughOrigin(positions[1], positions[3]);
}
else {
// pair edges with adjacent edges
drawCurveThroughOrigin(positions[0], positions[1]);
drawCurveThroughOrigin(positions[2], positions[3]);
}
}
// 1 and 3 situation
// or 2 and 2
else {
if (this.denseLayout == 3) {
// connect edges to adjacent edges
drawCurveThroughOrigin(positions[0], positions[1]);
drawCurveThroughOrigin(positions[1], positions[2]);
drawCurveThroughOrigin(positions[2], positions[3]);
drawCurveThroughOrigin(positions[3], positions[0]);
}
else if (this.denseLayout == 2) {
// pair edges with adjacent edges
drawCurveThroughOrigin(positions[3], positions[0]);
drawCurveThroughOrigin(positions[1], positions[2]);
}
else {
// pair edges with adjacent edges alt
drawCurveThroughOrigin(positions[0], positions[1]);
drawCurveThroughOrigin(positions[2], positions[3]);
}
}
}
}
// five neighbours
else if (activeNeighboursCount == 5) {
if (drawCurves) {
let skipped = activeNeighbours.indexOf(false);
if (this.denseLayout == 3) {
// connect edges to adjacent edges
for (var i = skipped; i < 5 + skipped; i++) {
var pos1 = getEdgePos(i);
if (i == skipped) {
pos1 = getEdgePos((i+5) % 6);
}
var pos2 = getEdgePos((i+1) % 6);
drawCurveThroughOrigin(pos1, pos2);
}
}
else if (this.denseLayout == 2) {
// batman logo
// curve between two adjacent to skipped one
let pos1 = getEdgePos((skipped+5) % 6);
let pos2 = getEdgePos((skipped+1) % 6);
drawCurveThroughOrigin(pos1, pos2);
// connect other 3 to eachother
let pos3 = getEdgePos((skipped+2) % 6);
let pos4 = getEdgePos((skipped+3) % 6);
let pos5 = getEdgePos((skipped+4) % 6);
drawCurveThroughOrigin(pos3, pos4);
drawCurveThroughOrigin(pos4, pos5);
}
else if (this.denseLayout == 1) {
// evil M
let pos1 = getEdgePos((skipped+1) % 6);
let pos2 = getEdgePos((skipped+2) % 6);
let pos3 = getEdgePos((skipped+3) % 6);
let pos4 = getEdgePos((skipped+4) % 6);
let pos5 = getEdgePos((skipped+5) % 6);
// curve the two skipped-adjacent edges to the opposite edge
drawCurveThroughOrigin(pos1, pos3);
drawCurveThroughOrigin(pos5, pos3);
// curve the other two edges to the skipped-adjacent edges
drawCurveThroughOrigin(pos1, pos2);
drawCurveThroughOrigin(pos5, pos4);
}
}
}
// full house
else {
if (drawCurves) {
if (this.denseLayout == 3) {
// connect edges to adjacent edges
for (var i = 0; i < 6; i++) {
var pos1 = getEdgePos(i);
var pos2 = getEdgePos((i+1) % 6);
drawCurveThroughOrigin(pos1, pos2);
}
}
else {
// pair edges with adjacent edges
for (var i = this.denseLayout - 1; i < 6; i+=2) {
var pos1 = getEdgePos(i);
var pos2 = getEdgePos((i+1) % 6);
drawCurveThroughOrigin(pos1, pos2);
}
}
}
}
}
pop();
}
}
function drawCurveThroughOrigin(pos1, pos2) {
beginShape();
vertex(pos1.x, pos1.y);
quadraticVertex(0, 0, pos2.x, pos2.y);
endShape();
}
function getEdgePos(i) {
var pos = createVector(0, -hexHeight/2);
pos.rotate(i*Math.PI/3);
return pos;
}
Also see: Tab Triggers