css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

Add External Scripts/Pens

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.

+ add another resource

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

              var dimx = 256;
var dimy = dimx;

var field = new field2D(dimx, dimy);
var past = new field2D(dimx, dimy);

// three possible states:
var empty = 0;
var tree = 0.5;
var burning = 1;

// the chance of an empty cell regrowing trees by expansion:
var growth_probability = 1/15;
// the chance of an empty cell regrowing trees by random sporing:
var spore_probability = 1/100000;
// the chance of lighting striking a cell:
var lightning_probability = 1/100000;
// chance of fire spreading:
var fire_probability = 1;

function reset() {
  field.set(tree, 128, 128);

function key() { reset(); }

// how to r}er the scene
function draw(ctx)	{
	// draw the field (by default it fills the canvas):

// the rule for an individual cell (at position x, y) in the field:
function transition(x, y) {

	// check out the neighbors' previous states:
	var N  = past.get(x  , y+1);
	var NE = past.get(x+1, y+1);
	var E  = past.get(x+1, y  );
	var SE = past.get(x+1, y-1);
	var S  = past.get(x  , y-1);
	var SW = past.get(x-1, y-1);
	var W  = past.get(x-1, y  );
	var NW = past.get(x-1, y+1);
	// true if (any neighb|| is a tree:
	var neartree = N == tree || E == tree 
				|| W == tree || S == tree 
				|| NE == tree || SE == tree 
				|| NW == tree || SW == tree;
	// true if (any neighb|| is burning:
	var nearburning = N == burning || E == burning 
				|| W == burning || S == burning 
				|| NE == burning || SE == burning 
				|| NW == burning || SW == burning;
	// check my own previous state:
	var C = past.get(x, y);
	if (C == empty) {
		// are any neighbors trees?
		if (neartree) {			
			// chance of regrowing:
			if (random() < growth_probability) {
				C = tree;
		} else if  (random() < spore_probability) {
			// smaller chance of propagation by seeding:
			C = tree;
	} else if  (C == tree) {
		// are any neighbors burning?
		if (nearburning && (random() < fire_probability)) {
			// if (any neighbors are burning, start burning too:
			C = burning;
		} else if  (random() < lightning_probability) {		
			// otherwise, there's a small chance of catching fire due to atmostpheric conditions:
			C = burning;
	} else if  (C == burning) {
		// a burning tree cell becomes an empty cell
		C = empty;
	// return the new state:
	return C;

function update() {
	var tmp = past;
	past = field;
	field = tmp;
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................