<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>RE System Cellular Automata</title>
<style>
body {
font-family: Arial, sans-serif;
display: flex;
flex-direction: column;
align-items: center;
padding: 20px;
}
canvas {
border: 1px solid black;
cursor: grab;
}
#controls {
margin-top: 10px;
display: flex;
flex-wrap: wrap;
gap: 10px;
justify-content: center;
}
button,
select {
padding: 5px 10px;
font-size: 14px;
}
label {
display: flex;
align-items: center;
gap: 5px;
}
</style>
</head>
<body>
<h1>RE System Cellular Automata</h1>
<canvas id="caCanvas" width="1000" height="1000"></canvas>
<div id="controls">
<button id="nextGen">Next Generation</button>
<button id="startStop">Start/Stop</button>
<button id="backGen">Back a Generation</button>
<select id="cellType">
<option value="0">Dead</option>
<option value="1">Alive</option>
<option value="2">Critical</option>
</select>
<button id="reset">Reset</button>
<button id="clear">Clear</button>
<label for="cellSize">Cell Size:</label>
<input type="range" id="cellSize" min="1" max="50" value="5" />
<label for="deadColor">Dead Color:</label>
<input type="color" id="deadColor" value="#000000" />
<label for="aliveColor">Alive Color:</label>
<input type="color" id="aliveColor" value="#00ff00" />
<label for="criticalColor">Critical Color:</label>
<input type="color" id="criticalColor" value="#ff0000" />
</div>
<script>
class CellularAutomata {
constructor(size) {
this.size = size
this.grid = Array(size)
.fill()
.map(() => Array(size).fill(0))
this.nextGrid = Array(size)
.fill()
.map(() => Array(size).fill(0))
this.history = [] // Store past grids
}
initializeRandom() {
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
const rand = Math.random()
if (rand < 0.1)
this.grid[i][j] = 2 // Critical
else if (rand < 0.2)
this.grid[i][j] = 1 // Alive
else this.grid[i][j] = 0 // Dead
}
}
}
clear() {
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
this.grid[i][j] = 0 // Set all cells to Dead
}
}
}
countNeighbors(i, j) {
let count = 0
for (let di = -1; di <= 1; di++) {
for (let dj = -1; dj <= 1; dj++) {
if (di === 0 && dj === 0) continue
const ni = i + di,
nj = j + dj
if (ni >= 0 && ni < this.size && nj >= 0 && nj < this.size) {
count += this.grid[ni][nj] > 0 ? 1 : 0
}
}
}
return count
}
update() {
// Save current grid state to history before updating
this.history.push(this.grid.map((row) => row.slice()))
for (let i = 0; i < this.size; i++) {
for (let j = 0; j < this.size; j++) {
const neighbors = this.countNeighbors(i, j)
const currentState = this.grid[i][j]
if (currentState === 0) {
this.nextGrid[i][j] = neighbors === 3 ? 1 : 0 // Dead
} else if (currentState === 1) {
this.nextGrid[i][j] = neighbors >= 2 && neighbors <= 3 ? 1 : 2 // Alive
} else {
this.nextGrid[i][j] = neighbors === 1 || neighbors >= 4 ? 0 : 2 // Critical
}
}
}
;[this.grid, this.nextGrid] = [this.nextGrid, this.grid]
}
setCell(i, j, state) {
this.grid[i][j] = state
}
backGeneration() {
if (this.history.length > 0) {
this.grid = this.history.pop() // Revert to the previous generation
}
}
}
const canvas = document.getElementById("caCanvas")
const ctx = canvas.getContext("2d")
const offset = { x: 0, y: 0 }
let cellSize = parseInt(document.getElementById("cellSize").value)
let isRunning = false
let animationId = null
let ca = new CellularAutomata(200) // Increased grid size for infinite effect
ca.initializeRandom()
function drawGrid() {
ctx.clearRect(0, 0, canvas.width, canvas.height) // Clear the canvas
for (let i = 0; i < ca.size; i++) {
for (let j = 0; j < ca.size; j++) {
const cellState = ca.grid[i][j]
ctx.fillStyle =
cellState === 0
? document.getElementById("deadColor").value // Use customizable dead cell color
: cellState === 1
? document.getElementById("aliveColor").value
: document.getElementById("criticalColor").value
ctx.fillRect(
i * cellSize + offset.x,
j * cellSize + offset.y,
cellSize,
cellSize,
)
// Draw the grid lines
ctx.strokeStyle = 'lightgray'; // Grid line color
ctx.lineWidth = 1; // Grid line thickness
ctx.strokeRect(
i * cellSize + offset.x,
j * cellSize + offset.y,
cellSize,
cellSize
);
}
}
}
function animate() {
ca.update()
drawGrid()
if (isRunning) {
animationId = requestAnimationFrame(animate)
}
}
function startStop() {
isRunning = !isRunning
if (isRunning) {
animate()
} else {
cancelAnimationFrame(animationId)
}
}
function nextGeneration() {
ca.update()
drawGrid()
}
function reset() {
ca = new CellularAutomata(200) // Reset grid size
ca.initializeRandom()
drawGrid()
if (isRunning) {
cancelAnimationFrame(animationId)
isRunning = false
}
}
function clear() {
ca.clear()
drawGrid()
if (isRunning) {
cancelAnimationFrame(animationId)
isRunning = false
}
}
function backGeneration() {
ca.backGeneration()
drawGrid()
}
let placementIndicator = { x: -1, y: -1 } // Track where the cell will be placed
canvas.addEventListener("mousemove", (event) => {
const rect = canvas.getBoundingClientRect()
const x = Math.floor((event.clientX - rect.left - offset.x) / cellSize)
const y = Math.floor((event.clientY - rect.top - offset.y) / cellSize)
if (x >= 0 && x < ca.size && y >= 0 && y < ca.size) {
placementIndicator.x = x
placementIndicator.y = y
}
drawGrid()
// Draw placement indicator
if (placementIndicator.x !== -1 && placementIndicator.y !== -1) {
ctx.strokeStyle = "yellow" // Indicator color
ctx.lineWidth = 1
ctx.strokeRect(
placementIndicator.x * cellSize + offset.x,
placementIndicator.y * cellSize + offset.y,
cellSize,
cellSize,
)
}
})
canvas.addEventListener("click", (event) => {
const rect = canvas.getBoundingClientRect()
const x = Math.floor((event.clientX - rect.left - offset.x) / cellSize)
const y = Math.floor((event.clientY - rect.top - offset.y) / cellSize)
const state = parseInt(document.getElementById("cellType").value)
if (x >= 0 && x < ca.size && y >= 0 && y < ca.size) {
ca.setCell(x, y, state)
drawGrid()
}
})
document.getElementById("nextGen").addEventListener("click", nextGeneration)
document.getElementById("startStop").addEventListener("click", startStop)
document.getElementById("backGen").addEventListener("click", backGeneration)
document.getElementById("reset").addEventListener("click", reset)
document.getElementById("clear").addEventListener("click", clear)
document.getElementById("cellSize").addEventListener("input", (event) => {
cellSize = parseInt(event.target.value)
drawGrid()
})
// Panning Logic
let isDragging = false
let startDragOffset = { x: 0, y: 0 }
let lastMousePosition = { x: 0, y: 0 }
canvas.addEventListener("mousedown", (event) => {
isDragging = true
lastMousePosition.x = event.clientX
lastMousePosition.y = event.clientY
canvas.style.cursor = "grabbing"
})
canvas.addEventListener("mouseup", () => {
isDragging = false
canvas.style.cursor = "grab"
})
canvas.addEventListener("mousemove", (event) => {
if (isDragging) {
const dx = event.clientX - lastMousePosition.x
const dy = event.clientY - lastMousePosition.y
offset.x += dx
offset.y += dy
lastMousePosition.x = event.clientX
lastMousePosition.y = event.clientY
drawGrid()
}
})
canvas.addEventListener("wheel", (event) => {
event.preventDefault()
const zoomFactor = 0.1
cellSize += event.deltaY < 0 ? -zoomFactor * cellSize : zoomFactor * cellSize
cellSize = Math.max(1, Math.min(cellSize, 50)) // Limit cell size for usability
drawGrid()
})
drawGrid()
</script>
</body>
</html>
Write, Run & Share HTML code online using OneCompiler's HTML online Code editor for free. It's one of the robust, feature-rich online Code editor for HTML language, running on the latest version HTML5. Getting started with the OneCompiler's HTML compiler is simple and pretty fast. The editor shows sample boilerplate code when you choose language as HTML. You can also specify the stylesheet information in styles.css tab and scripts information in scripts.js tab and start coding.
HTML(Hyper Text Markup language) is the standard markup language for Web pages, was created by Berners-Lee in the year 1991. Almost every web page over internet might be using HTML.
<!DOCTYPE html><html> and ends with </html><h1> to <h6> where <h1> is the highest important heading and <h6> is the least important sub-heading.<p>..</p> tag.<a> tag.
<a href="https://onecompiler.com/html">HTML online compiler</a>
<img> tag, where src attribute consists of image name.<button>..</button> tag<ul> for unordered/bullet list and <ol> for ordered/number list, and the list items are defined in <li>.<a href="https://onecompiler.com/html">HTML online compiler</a>
CSS(cascading style sheets) describes how HTML elements will look on the web page like color, font-style, font-size, background color etc.
Below is a sample style sheet which displays heading in green and in Candara font with padding space of 25px.
body{
padding: 25px;
}
.title {
color: #228B22;
font-family: Candara;
}
<table> tag.<tr> tag<th> tag<td> tag<caption> tag<script> is the tag used to write scripts in HTML<script src="script.js"></script>