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 id="two"></div>
html, body {
height: 100%;
background: #151644;
overflow: hidden;
}
canvas {
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #151644;
transform: rotate(45deg);
border-radius: 50%;
}
// Feel free to change these five variables, everything should still work as long as you don't change them _too_ much!
// integer; size of each cell
var unitsize = 2;
// integer; number of cells per side
var s = 80;
// integer; number of cells to try and find a home for each frame;
var growthRate = 15;
// k1: [1 : 100] initial percentage concentration of cells on the grid
var k1 = 60;
// k2: number of seed cells
var k2 = 100;
// Basic explanation of how this works: each tick, a random cell will be picked. If that cell is of the 'mobile' / k1 type, it will move to a nearby (8-directional) spot. If that new spot is adjacent to a 'fixed' / k2 type cell, the cell that just moved will become fixed + get a color based on the fixed cell it latched onto.
var rs = [];
// max value
var q = 14;
var k1abs = (k1 / 100) * (s * s);
var numCols = s;
var numRows = s;
// Create an instance of Two.js
// two.js.org
var elem = document.getElementById('two');
var two = new Two({type: Two.Types.canvas, width: (numCols * unitsize) + unitsize * .5, height: (numRows * unitsize) + unitsize * .5 }).appendTo(elem);
var arrLen = numRows * numCols;
var used = 0;
var radius = s / 2 * unitsize;
var xc = radius;
var yc = radius;
function isOutside(x1, y1) {
return Math.pow(x1 - xc, 2) + Math.pow(y1 - yc, 2) > Math.pow(radius, 2);
}
var colors = [
"#1E1F51",
"#EBFFEB",
"#B0FFD4",
"#8AF8A8",
"#2DF8A8",
"#2CEBB2",
"#2BD8C0",
"#2AC3D0",
"#29ABE2",
"#2C97CA",
"#2F83B4",
"#32719F",
"#39486E"
];
// neighbor-finding functions
function ne(i, top, right) {
if (top && right) {
return rs[arrLen - numCols];
} else if (top) {
return rs[i + 1 + ((numRows - 1) * numCols)];
} else if (right) {
return rs[i - (numCols * 2) + 1];
} else {
return rs[i - numCols + 1];
}
}
function nw(i, top, left) {
if (top && left) {
return rs[arrLen - 1];
} else if (top) {
return rs[((numRows - 1) * numCols) + i - 1];
} else if (left) {
return rs[i - 1];
} else {
return rs[i - numCols - 1];
}
}
function se(i, bottom, right) {
if (bottom && right) {
return rs[0];
} else if (bottom) {
return rs[i - (numCols * (numRows - 1)) + 1];
} else if (right) {
return rs[i + 1];
} else {
return rs[i + numCols + 1];
}
}
function sw(i, bottom, left) {
if (bottom && left) {
return rs[numCols -1];
} else if (bottom) {
return rs[i - ((numRows - 1) * numCols) - 1];
} else if (left) {
return rs[i+ (2 * numCols) - 1];
} else {
return rs[i + numCols - 1];
}
}
// Constructor for each square in the grid
function Pt(x, y, unitsize, i) {
// Initial setup for each square
this.setup = () => {
this.r = two.makeRectangle(x, y, unitsize, unitsize);
this.x = x;
this.y = y;
this.isTop = (this.y - (.5 * unitsize) == 0);
this.isBot = (this.y == (unitsize * numRows) - (.5 * unitsize));
this.isLef = (this.x == 0 + (.5 * unitsize));
this.isRig = (this.x == (unitsize * numCols) - (.5 * unitsize));
// Random initial state for each square
this.state = 0;
this.type = false;
this.r.noStroke();
this.r.fill = "#000";
}
this.setup();
// Store the 8 neighbors of each cell
this.findNeighbors = () => {
this.n = this.isTop ? rs[i + arrLen - numCols] : rs[i - numCols];
this.s = this.isBot ? rs[i - arrLen + numCols] : rs[i + numCols];
this.e = this.isRig ? rs[i - numCols + 1] : rs[i + 1];
this.w = this.isLef ? rs[i + numCols - 1] : rs[i - 1];
this.ne = ne(i, this.isTop, this.isRig);
this.nw = nw(i, this.isTop, this.isLef);
this.se = se(i, this.isBot, this.isRig);
this.sw = sw(i, this.isBot, this.isLef);
this.neighbors = [this.n, this.s, this.e, this.w, this.ne, this.nw, this.se, this.sw];
}
// Find the average state for the neighborhood (this square + the 8 around it)
this.avg = () => {
return (this.state + this.n.state + this.ne.state + this.e.state + this.se.state + this.s.state + this.sw.state + this.w.state + this.nw.state) / 9;
}
}
for (var y = 0; y < numCols; y++) {
for (var x = 0; x < numRows; x++) {
rs.push(new Pt(unitsize * x + (.5 * unitsize), unitsize * y + (.5 * unitsize), unitsize, rs.length));
}
}
for (var i = 0, l = rs.length; i < l; i++) {
rs[i].findNeighbors();
rs[i].r.fill = colors[0];
}
var kCells = [];
while (kCells.length < k1abs + k2) {
var ind = Math.floor(Math.random() * rs.length);
if (kCells.indexOf(ind) === -1) {
kCells.push(ind);
}
}
for (var i = 0; i < k1abs + k2; i++) {
if (i < k1abs) {
rs[kCells[i]].type = 'mobile';
rs[kCells[i]].state = 1;
rs[kCells[i]].r.fill = colors[0];
} else { // k2, seed cell/s
rs[kCells[i]].type = 'fixed';
rs[kCells[i]].state = q;
rs[kCells[i]].r.fill = colors[q - 2];
}
}
function fixedNeighbors(neighb) {
return neighb.type === 'fixed';
}
function emptyNeighbors(neighb) {
return !(neighb.type);
}
function reset() {
for (var i = 0, l = rs.length; i < l; i++) {
rs[i].state = 0;
rs[i].type = false;
rs[i].r.fill = colors[0];
}
var kCells = [];
while (kCells.length < k1abs + k2) {
var ind = Math.floor(Math.random() * rs.length);
if (kCells.indexOf(ind) === -1) {
kCells.push(ind);
}
}
for (var i = 0; i < k1abs + k2; i++) {
if (i < k1abs) {
rs[kCells[i]].type = 'mobile';
rs[kCells[i]].state = 1;
rs[kCells[i]].r.fill = colors[0];
} else { // k2, seed cell/s
rs[kCells[i]].type = 'fixed';
rs[kCells[i]].state = q;
rs[kCells[i]].r.fill = colors[q - 2];
}
}
used = 0;
}
var resetCounter = 0;
// The animation loop
two.bind('update', function(frameCount, timeDelta) {
for (var i = 0; i < growthRate; i++) {
var loc = rs[Math.floor(Math.random() * arrLen)];
if (loc.type === 'mobile') {
let fixedNeighb = loc.neighbors.find(fixedNeighbors);
if (fixedNeighb) {
used += 1;
loc.type = "fixed";
loc.state = Math.min(fixedNeighb.state - 1);
if (loc.state < 1) {
loc.state = 0;
loc.type = false;
loc.r.fill = colors[0];
} else {
loc.r.fill = colors[loc.state - 1];
}
} else {
let availNeighbs = loc.neighbors.filter(emptyNeighbors);
if (availNeighbs.length) {
let target = availNeighbs[Math.floor(Math.random() * availNeighbs.length)];
//console.log('moving, target is', target);
if (target) {
target.state = loc.state;
target.type = loc.type;
target.r.fill = loc.r.fill;
}
loc.state = 0;
loc.type = false;
loc.r.fill = colors[0];
}
}
}
}
if (used == k1abs) {
reset();
resetCounter = 0;
}
}).play();
Also see: Tab Triggers