cssAudio - Activefile-genericCSS - ActiveGeneric - ActiveHTML - ActiveImage - ActiveJS - ActiveSVG - ActiveText - Activefile-genericVideo - ActiveLovehtmlicon-new-collectionicon-personicon-teamlog-outoctocatpop-outspinnerstartv

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.

              var normal_speed = 0.004;
var turn_rate = 4;
var turn_max = 0.5;
var sensor_length = 0.5;
var agents = [];
for (var i = 0; i < 10; i++) {
    pos: new vec2(random(), random()),
    vel: vec2.random(normal_speed),
    size: 0.02 * (1+random()),

var light = new field2D(64);
//light.set(function(r, c) { return random(); });
light.diffuse(light, 40);
var light_old = light.clone();

var source = {
  pos: new vec2(0.5, 0.5)

function mouse(e, pt) {
  if (e == "down") {
  } else {

var i = 10;
while (i--) { console.log(i); }

function update() {
  // swap old & current fields:
  var tmp = light;
  light = light_old;
  light_old = tmp;
  // smoothen:
  light.diffuse(light_old, 0.1);
  // add:
  light.deposit(1, source.pos);
  // move source:
  for (var a of agents) {

    // compute location of sensors in world:
    var sensor1 = new vec2(sensor_length, +sensor_length)
    var sensor2 = new vec2(sensor_length, -sensor_length)
    // read field at these locations:
    var sense1 = light.sample(sensor1);
    var sense2 = light.sample(sensor2);

    //console.log(sense1, sense2);
    var speed = normal_speed * (sense1 + sense2 + 0.1);
    var turn = turn_rate * (sense1 - sense2 + 0.01*srandom());
    // limit turns:
    if (turn > turn_max) { turn = turn_max; }
    else if (turn < -turn_max) { turn = -turn_max; }
    // turn that into steering:

    // move it

function draw() {


  for (var a of agents) {
      .circle([sensor_length, +sensor_length], 0.3)
      .circle([sensor_length, -sensor_length], 0.3);
🕑 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 ..................