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.
html,
body {
height: 100%;
background: black;
}
body {
margin: 0;
display: flex;
justify-content: center;
align-items: center;
}
//clicktochange
//This is my attempt at cellular automata https://en.wikipedia.org/wiki/Conway%27s_Game_of_Life
//rule descriptions can be found here: https://fractalkitty.com/101-days-of-creative-coding-docc/day-91-of-101-docc/
//first draft of hexgrid is here: https://codepen.io/fractalkitty/pen/YzNoYLR
// another version of this idea with more rules is here: https://codepen.io/fractalkitty/pen/WNRVWzV
let rules = 0;//this is the counter for different sets of rules
let cnt = 0;//counter for dead alive array in loops below
let n; //number of cells
let dArray = [];//array for each cell for dead/alive status
let r = 20;//radius size - change this for different looks (this doesn't change based on canvas size)
function setup() {
createCanvas(windowWidth, windowHeight );//canvas size seet to window
co = floor(width / (2 * r)); //number of columns for hexagonal grid (filled with circles)
ro = co//rows equals columns
randomDead();//this sets a random dead/alive for each cell
background(20);//almost black
noStroke();//no lines
frameRate(5)//slow frame rate to see progression of automata
}
function draw() {
background(20);
translate(width / 2, height / 2); //make the center (0,0)
s=0.28; //scale factor
if(width>1000){ //creens larger than 1000
s=0.2;//scale factor to adjust for larger screens
}
scale(s, s)//set scale
v = 12//number of points in the circle - change this to make different pointed stars
for (let m = 0; m < v; m++) {//loop to create separate points on the star
push();//translations and rotations between push and pop only apply between push/pop
rotate(m * TWO_PI / v)//rotate the canvas to create each point
cnt = 0;//this is the counter for the alive/dead array to loop through
for (let j = 0; j <= co; j++) { //columns of hex grid (filled with circles)
for (let i = ro; i >= 0; i--) { //rows of hex grid
x = r * j * 1.5; //x coordinate of hexgrid
y = -r * 12 + r * sqrt(3) * i + r * j / 1.15; //y coordinate of hexgrid
if (dArray[cnt] === 0) {//change fill for dead
noFill(); //fill(0,0,0,100);
noStroke();
}
if (dArray[cnt] === 1) {//change fill/stroke for alive with a gradient based on row/column
fill(j * 4 + i * 5, 200 + i * 5, 255 - j * 2, 200 - m * 7);
stroke(j * 4 + i * 5, 200 + i * 5, 255 - j * 2, 200 - m * 7);
}
circle(x, y, r*1.4 );//place circle in the hexgrid
fill(20);//set fill back to background
cnt = cnt + 1; //increment dead/alive counter for next cell
}
}
pop();//end push/pop transformations
}
switchDead()//next round of alive/dead caculatiions
}
function randomDead() {//initializes random dead/alive status of cells
n = (co + 1) * (ro + 1);//n is the size of the grid
dArray = []//dead-alive array is cleared
for (let i = 0; i < n; i++) {//loop through the array and fill with random dead/alive
tmp = random(-1, 1);//base dead/alive on positive/negative numbers
if (tmp <= 0) {
dArray[i] = 1;
}
if (tmp > 0) {
dArray[i] = 0;
}
}
}
function switchDead() {//loops through and changes dead/alive status bases on neighbors
cnt1 = 0
for (let j = 0; j <= co; j++) { //columns
for (let i = ro; i >= 0; i--) { //rows
if (j != 0 && i != 0 && i != ro && j != co) {//if a cell is not in the corner of the grid
sumd = dArray[cnt1 + co + 1] + dArray[cnt1 + co + 2] + dArray[cnt1 - co - 1] + dArray[cnt1 - co - 2] + dArray[cnt1 + 1] + dArray[cnt1 - 1]; //find the sum of neighbors that are alive
}
if (i === 0 && j != 0 && j != co) {//for left side of array (but not a corner)
sumd = dArray[cnt1 + co + 1] + dArray[cnt1 - co - 1] + dArray[cnt1 - co - 2] + dArray[cnt1 - 1];//find the sum of neighbors that are alive
}
if (i === ro && j != 0 && j != co) {//for right side of array (but not a corner)
sumd = dArray[cnt1 + co + 1] + dArray[cnt1 - co - 1] + dArray[cnt1 + co + 2] + dArray[cnt1 + 1];//find the sum of neighbors that are alive
}
if (j === co && i != 0 && i != ro) {//for top side of array (but not a corner)
sumd = dArray[cnt1 - co - 1] + dArray[cnt1 - co - 2] + dArray[cnt1 + 1] + dArray[cnt1 - 1];//find the sum of neighbors that are alive
}
if (j === 0 && i != 0 && i != co) {//for bottom side of array (but not a corner)
sumd = dArray[cnt1 + co + 1] + dArray[cnt1 + co + 2] + dArray[cnt1 + 1] + dArray[cnt1 - 1];//find the sum of neighbors that are alive
}
if (i === 0 && j === 0) {//for corner lower left
sumd = dArray[cnt1 + co + 1] + dArray[cnt1 + 1];//find the sum of neighbors that are alive
}
if (i === 0 && j === co) {//for corner upper left
sumd = dArray[cnt1 - 1] + dArray[cnt1 - co - 1] + dArray[cnt1 - co - 2];//find the sum of neighbors that are alive
}
if (i === ro && j === 0) {//for corner lower right
sumd = dArray[cnt1 + 1] + dArray[cnt1 + co + 1] + dArray[cnt1 + co + 2];
}
if (i === ro && j === co) {//for corner upper right
sumd = dArray[cnt1 + 1] + dArray[cnt1 - co - 1];
}
//rule descriptions can be found here: https://fractalkitty.com/101-days-of-creative-coding-docc/day-91-of-101-docc/
//the following applies the rules and changes dead/alive to the cell
if (rules===0){
if (dArray[cnt1] === 1 && (sumd === 6 || sumd === 5 || sumd === 1 || sumd === 0)) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && (sumd === 4)) {
dArray[cnt1] = 1;
}
}
if (rules===1){
if (dArray[cnt1] === 1 && (sumd === 6 || sumd === 5 || sumd === 2 || sumd === 1 || sumd === 0)) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && ( sumd === 3 ||sumd===4||sumd===5)) {
dArray[cnt1] = 1;
}
}
if (rules===2){
if (dArray[cnt1] === 1 && (sumd === 6 || sumd === 5 )) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && (sumd === 3)) {
dArray[cnt1] = 1;
}
}
if (rules===3){
if (dArray[cnt1] === 1 && (sumd === 1 || sumd === 2 || sumd === 3)) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && (sumd === 3||sumd===4)) {
dArray[cnt1] = 1;
}
}
if (rules===4){
if (dArray[cnt1] === 1 && (sumd === 3 ||sumd === 4 || sumd === 5 || sumd === 6)) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && (sumd===0||sumd===1||sumd===2)) {
dArray[cnt1] = 1;
}
}
if (rules===5){
if (dArray[cnt1] === 1 && (sumd === 2 ||sumd === 3 ||sumd === 4 || sumd === 5 || sumd === 6)) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && (sumd===0||sumd===1||sumd===2)) {
dArray[cnt1] = 1;
}
}
if (rules===6){
if (dArray[cnt1] === 1 && (sumd === 4 || sumd === 5 || sumd === 6)) {
dArray[cnt1] = 0;
} else if (dArray[cnt1] === 0 && (sumd===3||sumd===4)) {
dArray[cnt1] = 1;
}
}
cnt1 = cnt1 + 1
}
}
stroke(255);
noFill()
push()//change scale for rule counter only between push/pop
scale(3,3)//scale change
for(let i=0;i<=6;i++){
circle(-width/2+20,-height/2+30*i+5,20)//rule circles
}
fill(100)
circle(-width/2+20,-height/2+30*rules+5,20)//current rule
pop()
}
function mousePressed() {//randomize dead alive again when the mouse is clicked
randomDead();
}
function keyPressed() {//use arrow keys to change the rules
if (keyCode === 32 || keyCode===40) { //move down with space and down arrow
rules = rules+1;
if (rules>6 ){
rules=0;
}
randomDead();
}
if (keyCode === 38){//move up with up arrow
rules = rules-1;
if (rules<0 ){
rules=6;
}
randomDead();
}
}
Also see: Tab Triggers