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: #000;
overflow: hidden;
}
svg {
position: absolute;
margin: auto;
top: 0;
left: 0;
right: 0;
bottom: 0;
background: #000;
}
var unitsize = 8;
var numCols = 40;
var numRows = 40;
var rs = [];
// max state
var n = 128;
// how much to increment states by after averaging
var g = 15;
var cols = ["#111"];
for (var i = 0; i <= n; i++) {
cols.push("hsl("+ (0) +"," + (1) + "%," + (.066 * i) + "%)");
}
// Create an instance of Two.js
// two.js.org
var elem = document.getElementById('two');
var two = new Two({ width: (numCols * unitsize) + unitsize * .5, height: (numRows * unitsize) + unitsize * .5 }).appendTo(elem);
var arrLen = numRows * numCols;
// 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 = Math.random() > .75 ? 0 : Math.floor(Math.random() * n);
this.sickNeighbors = 0;
this.r.noStroke();
}
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);
}
// 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;
}
// Find the number of neighboring squares with a state > 0
this.findSickNeighbors = () => {
[this.n.state, this.s.state, this.e.state, this.w.state, this.nw.state, this.ne.state, this.se.state, this.sw.state].forEach((st) => {
if (st > 0) {
this.sickNeighbors += 1;
}
});
}
}
// fill the array of points
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));
}
}
// generate neighbors for each square
for (var i = 0; i < rs.length; i++ ) {
rs[i].findNeighbors();
}
// The animation loop
two.bind('update', function(frameCount, timeDelta) {
// Determine what the next state will be for each square
for (var i = 0, l = rs.length; i < l; i++) {
// If it's currently 0,
if (rs[i].state == 0) {
rs[i].findSickNeighbors();
// And there are at least 3 neighbors with a state > 0,
if (rs[i].sickNeighbors >= 3) {
// Set the state to a random, low-ish number
rs[i].nextState = Math.floor(Math.random() * 10) + 10;
} else {
// otherwise, keep the state at 0
rs[i].nextState = 0;
}
// If the state is the max state,
} else if (rs[i].state == n) {
// Make it 0
rs[i].nextState = 0;
// If the state is in between,
} else if (rs[i].state > 0 && rs[i].state < n) {
let result = rs[i].avg() + g;
// And the average neighborhood state + a constant is less than the current state,
if (result <= n) {
// Make that the new state
rs[i].nextState = Math.floor(result);
} else {
// Otherwise, it's bigger than the max state, so round down to the max state
rs[i].nextState = n;
}
}
}
// Set each square's state to it's next state
// and reset the neighbor count
for (var i = 0, l = rs.length; i < l; i++) {
rs[i].state = rs[i].nextState;
rs[i].sickNeighbors = 0;
}
// Finally, add color
for (var i = 0, l = rs.length; i < l; i++) {
rs[i].r.fill = cols[rs[i].state];
}
});
// Throttle framerate to 20FPS (Two.js tries to go at 60fps)
window.setInterval(()=> {
two.update();
}, 50);
Also see: Tab Triggers