Skip to content

Instantly share code, notes, and snippets.

@marcuswestin
Created October 7, 2012 06:17
Show Gist options
  • Save marcuswestin/3847288 to your computer and use it in GitHub Desktop.
Save marcuswestin/3847288 to your computer and use it in GitHub Desktop.
In "Notes on the Synthesis of Form" Christopher Alexander describes a thought experiment of light bulbs turning on and off. This script implements that thought experiment.
<!DOCTYPE html>
<html><head>
<style>
.light { position:absolute; border-radius:5px; background:black; }
.light.on { background:yellow; }
</style>
</head><body>
<div id="lights"></div>
<script src="http://code.jquery.com/jquery-1.8.2.min.js"></script>
<script>
$(function() {
var elements = new Array(100)
var size = 8
var pad = 3
var margin = 20
$.each(elements, function(i) {
elements[i] = $('<div></div>')
.addClass('light')
.css({ width:size, height:size, top:100, left:margin + (size + pad*2)*i })
.appendTo('#lights')
// .addClass(chance() ? 'on' : 'off')
.addClass('on')
})
renderAndUpdate()
var interval = setInterval(renderAndUpdate, 500)
function renderAndUpdate() {
var $lights = $('#lights .light')
var lastWasOn = false
var oneWasOn = true
$lights.each(function(i, el) {
var $el = $(el)
if ($el.hasClass('on')) {
lastWasOn = true
oneWasOn = true
if (chance()) { $el.removeClass('on') }
} else {
lastWasOn = false
if (lastWasOn || $($lights[i + 1]).hasClass('on')) {
if (chance()) { $el.addClass('on') }
}
}
})
if (!oneWasOn) {
clearInterval(interval)
}
}
})
function chance() { return Math.random() < 0.5 }
</script>
</body></html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment