Skip to content

Instantly share code, notes, and snippets.

@creationix
Last active December 7, 2016 11:35
Show Gist options
  • Select an option

  • Save creationix/2e51a312c6ff95cd48a240d95c7044a8 to your computer and use it in GitHub Desktop.

Select an option

Save creationix/2e51a312c6ff95cd48a240d95c7044a8 to your computer and use it in GitHub Desktop.

Revisions

  1. creationix revised this gist Dec 7, 2016. 1 changed file with 7 additions and 5 deletions.
    12 changes: 7 additions & 5 deletions bugs.js
    Original file line number Diff line number Diff line change
    @@ -1,5 +1,5 @@
    var neopixelWrite = require('ESP8266').neopixelWrite;
    var count = 60;
    var count = 120;
    var pixels = new Uint8Array(count * 3);
    function update() {
    neopixelWrite(14, pixels);
    @@ -33,13 +33,13 @@ function Bug() {
    p = rand(count);
    h = rand(768);
    l = rand(50) + 50;
    d = rand(2);
    d = rand(2) * 2 - 1;
    }
    function update() {
    for (var i = 0; i < 6; i++) {
    dampen(p + count - i);
    }
    p = (p + 1) % count;
    p = (p + d + count) % count;
    hue(p, h, 2);
    h =(h + 1) % 768;
    if (!(--l)) {
    @@ -59,7 +59,9 @@ for (var i = 0; i < 5; i++) Bug();

    var x = 0;
    setInterval(function () {
    x = (x + 1) % bugs.length;
    bugs[x]();
    for (var i = 0; i < 2; i++) {
    x = (x + 1) % bugs.length;
    bugs[x]();
    }
    update();
    }, 33);
  2. creationix created this gist Dec 6, 2016.
    65 changes: 65 additions & 0 deletions bugs.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,65 @@
    var neopixelWrite = require('ESP8266').neopixelWrite;
    var count = 60;
    var pixels = new Uint8Array(count * 3);
    function update() {
    neopixelWrite(14, pixels);
    }
    function dampen(p) {
    var i = (p % count) * 3;
    pixels[i]>>>=1;
    pixels[++i]>>>=1;
    pixels[++i]>>>=1;
    }
    function rgb(p, r, g, b) {
    var i = (p % count) * 3;
    pixels[i] = g;
    pixels[++i] = r;
    pixels[++i] = b;
    }
    function hue(p, h, d) {
    h = h % 768;
    d = d || 0;
    if (h < 256) return rgb(p, h >> d, (255-h) >> d, 0);
    if (h < 512) return rgb(p, (511-h)>>d,0,(h-256)>>d);
    return rgb(p, 0, (h-512)>>d, (767-h)>>d);
    }
    function rand(x) {
    return Math.floor(Math.random() * x);
    }
    var bugs = [];
    function Bug() {
    var p, h, l, d;
    function init() {
    p = rand(count);
    h = rand(768);
    l = rand(50) + 50;
    d = rand(2);
    }
    function update() {
    for (var i = 0; i < 6; i++) {
    dampen(p + count - i);
    }
    p = (p + 1) % count;
    hue(p, h, 2);
    h =(h + 1) % 768;
    if (!(--l)) {
    for (i = 1; i < 16; i++) {
    h += 27;
    hue(p + i, h, i >> 1);
    hue(p + count - i, h, i >> 1);
    }
    init();
    }
    }
    init();
    bugs.push(update);
    }

    for (var i = 0; i < 5; i++) Bug();

    var x = 0;
    setInterval(function () {
    x = (x + 1) % bugs.length;
    bugs[x]();
    update();
    }, 33);