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.
<!-- RENDER REACT APP HERE -->
<div id = "app">
</div>
/*Style body*/
body {
background-color: #000a17;
}
/* Style content container */
#app {
background-color: #fafafa;
width: 800px;
margin: auto;
border-left: 2px solid green;
border-right: 2px solid green;
padding-bottom: 10px;
}
/*Style title and generation count */
#title,
#generationCount {
text-align: center;
padding: 10px;
font-weight: 900;
border-bottom: 3px solid green;
}
#generationCount {
width: 30%;
margin: auto;
border-bottom: 3px solid green;
}
/*Style buttons*/
.buttonsContainer {
text-align: center;
width: 700px;
margin: auto;
display: block;
}
button {
width: 20%;
margin: 5px;
background-color: darkgreen;
color: white;
border: 1px solid darkgreen;
}
button:hover {
background-color: white;
color: darkgreen;
border: 1px solid darkgreen;
}
/*Style board*/
#board {
width: 704px;
margin: auto;
margin-top: 10px;
margin-bottom: 10px;
border: 2px solid green;
}
.square {
width: 10px;
height: 10px;
border: 1px solid green;
text-align: center;
font-size: 10px;
}
/* Style the rules */
#rules {
padding: 10px;
width: 704px;
margin: auto;
margin-top: 10px;
margin-bottom: 5px;
border: 2px solid green;
}
/* Style signature */
#sign {
text-align: right;
font-size: 10px;
color: black;
width: 800px;
}
#heart {
color: pink;
}
/*
Rules of the game:
1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
2. Any live cell with two or three live neighbours lives on to the next generation.
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
*/
//Define the dynamically-determined styles for dead and alive cells
const styles = {
dead: {
backgroundColor: '#fafafa',
},
alive: {
backgroundColor: 'darkgreen',
},
};
// Define the size of the board
var length = 70;
var width = 50;
//Define the generation change interval
var interval = 500;
//Generate a board with randomly created dead (0) and alive (1) cells
var board= [];
var newBoard = [];
function generateBoard (l, w) {
for (var i=0; i<l*w; i++) {
var x = Math.floor(Math.random() * 2);
board.push(x); }
};
generateBoard (length, width);
//Define the rules of the game
function play (l, w){
for (var i=0; i<l*w; i++) {
//Check the sum of the surrounding cells
//NOTE: the board acts as a continuum
var sum;
//Check the sum in the CORNERS
//top-left
if(i==0) {
sum =
board[1]+
board[l+1]+
board[l]+
board[l*(w-1)]+
board[l*(w-1)+1]+
board[l*w-1]+
board[l-1]+
board[l*2-1];
}
//top-right
else if (i==l-1) {
sum =
board[i-1]+
board[i+l]+
board[i+l-1]+
board[0]+
board[l]+
board[l*w-1]+
board[l*w-2]+
board[l*(w-1)];
}
//bottom-left
else if(i==l*(w-1)) {
sum =
board[i-l]+
board[(i-l)+1]+
board[i+1]+
board[0]+
board[1]+
board[l-1]+
board[l*w-1]+
board[l*w-l-1];
}
//bottom right
else if(i==(l*w)-1) {
sum =
board[i-1]+
board[i-l-1]+
board[i-l]+
board[0]+
board[l*(w-1)]+
board[l*(w-2)]+
board[l-1]+
board[l-2];
}
//Check the sum of the SIDES
//top
else if(i>0&&i<l-1) {
sum =
board[i-1]+
board[i+1]+
board[i+l+1]+
board[i+l]+
board[i+l-1]+
board[l*(w-1)+i-1]+
board[l*(w-1)+i]+
board[l*(w-1)+i+1];
}
//bottom
else if(i>l*(w-1)&&i<l*w-1) {
sum =
board[i-1]+
board[i-l+1]+
board[i-l]+
board[i-l-1]+
board[i+1]+
board[i-l*(w-1)-1]+
board[i-l*(w-1)]+
board[i-l*(w-1)+1];
}
//left
else if(i%l==0){
sum =
board[i-l]+
board[i-l+1]+
board[i+1]+
board[i+l+1]+
board[i+l]+
board[i-1]+
board[i+l-1]+
board[i+2*l-1];
}
//right
else if (i%l==(l-1)){
sum =
board[i-1]+
board[i-l-1]+
board[i-l]+
board[i+l]+
board[i+l-1]+
board[i-l+1]+
board[i+1]+
board[i-l*2+1];
}
//Check the sum in ALL OTHER CELLS
else {
sum =
board[i-1]+
board[i-l+1]+
board[i-l]+
board[i-l-1]+
board[i+1]+
board[i+l+1]+
board[i+l]+
board[i+l-1];
}
//Apply the rules of the game
newBoard[i] = board[i];
if (board[i] == 0&& sum==3)
{newBoard[i] = 1;}
else if (board[i] == 1 && sum<2 )
{newBoard[i] = 0;}
else if (board[i] == 1 && sum>3)
{newBoard[i] = 0;}
}
//Deep copy the newBoard array in the board variable for the next generation to be able to execute
board = JSON.parse(JSON.stringify(newBoard));
};
//Create the board
class Board extends React.Component {
constructor(props) {
super(props);
this.state= {
board: board,
count: 0
};
}
componentDidMount() {
//Play the game at an interval
this.interval = setInterval(function (){
play(length, width);
this.setState({board: newBoard, count: this.state.count+1});
}.bind(this), interval);
};
render() {
//Map over the board variable in order to generate the squares
var generateBoard = this.state.board.map((square, index) =>
<div
className = "square"
//Apply dynamic styling do dead and alive cells
style = {this.state.board[index] == 1 ? styles.alive : styles.dead}
//Change the state of a square by clicking on it
onClick = {() => {
let cells = this.state.board;
if (this.state.board[index] == 1)
{cells.splice(index, 1, 0)}
else {cells.splice(index, 1, 1)}
board = JSON.parse(JSON.stringify(cells));
this.setState({board: board})}}
>
</div>);
return (
<div>
<h1 id="title"> Conway's Game of Life</h1>
<div className = "buttonsContainer">
<button
className = "btn"
//Start button function
onClick = {() => {
clearInterval(this.interval);
this.interval = setInterval(function (){
play(length, width);
this.setState({
board: newBoard,
count: this.state.count+1}
);}.bind(this), interval)}} >
<i className="fa fa-play" aria-hidden="true"></i>
</button>
<button
className = "btn"
//Pause button function
onClick = {() => {clearInterval(this.interval)}}>
<i className="fa fa-pause" aria-hidden="true"></i>
</button>
<button
className = "btn"
//Stop button function
onClick = {()=> {
clearInterval(this.interval)
var squares = []
for (var ind=0; ind<length*width; ind++)
{squares.push(0)};
board = JSON.parse(JSON.stringify(squares));
this.setState({board: squares, count: 0})
}
}
>
<i className="fa fa-stop"></i>
</button>
</div>
<div id = "generationCount"> Generation {this.state.count} </div>
<div id = "board" className = "row"> {generateBoard}</div>
<div>
<p id="rules"><b>Rules:</b>
<br></br>
1. Any live cell with fewer than two live neighbours dies, as if caused by underpopulation.
<br></br>
2. Any live cell with two or three live neighbours lives on to the next generation.
<br></br>
3. Any live cell with more than three live neighbours dies, as if by overpopulation.
<br></br>
4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
</p>
</div>
</div>
);
}
}
//Create Sign class
class Sign extends React.Component {
render() {
return <div>
<div className = "container" id="sign">
<div>with <i id = "heart" className ="fa fa-heart"> </i> by <a href="https://codepen.io/IvaKop/#" target = "_blank">Iva </a></div>
</div>
</div>;
}
}
//Create App class
class App extends React.Component {
constructor(props) {
super(props);
this.state= {
};
}
render() {
return (
<div>
<Board />
<Sign />
</div>
);
}
}
//Render App
ReactDOM.render(
<App />,
document.getElementById('app')
);
Also see: Tab Triggers