Created
December 15, 2016 17:16
-
-
Save creationix/f9c087f8f736e63bdb16f432f00d5885 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var neopixelWrite = require('ESP8266').neopixelWrite; | |
var count = 256; | |
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 + count) % 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 x, y, h, l, d, p = 0; | |
function init() { | |
x = rand(16); | |
y = rand(16); | |
h = rand(768); | |
l = rand(400); | |
d = rand(4); | |
} | |
function update() { | |
rgb(p, 0, 0, 0, 0); | |
if (d === 0) x = (x + 1) % 16; | |
else if (d === 1) y = (y + 1) % 16; | |
else if (d === 2) x = (x + 15) % 16; | |
else y = (y + 15) % 16; | |
p = (y % 2 ? x : 15 - x) + y * 16; | |
h =(h + 1) % 768; | |
hue(p, h, 0); | |
if ((l % 13) === 0) d = rand(4); | |
if (!(--l)) { | |
/* | |
for (i = 1; i < 4; i++) { | |
h += 27; | |
var b = i * 2; | |
hue(p + i, h, b); | |
hue(p - i, h, b); | |
hue(p + i * 16, h, b); | |
hue(p - i * 16, h, b); | |
h += 27; | |
b++; | |
hue(p + i * 17, h, b); | |
hue(p + i * 15, h, b); | |
hue(p - i * 17, h, b); | |
hue(p - i * 15, h, b); | |
} | |
*/ | |
init(); | |
} | |
} | |
init(); | |
bugs.push(update); | |
} | |
for (var i = 0; i < 10; i++) Bug(); | |
var x = 0; | |
setInterval(function () { | |
var i; | |
// global dampen | |
/* | |
for (i = 0; i < count * 3; i++) { | |
pixels[i] = (pixels[i] * 230) >> 8; | |
} | |
*/ | |
for (i = 0; i < 4; i++) { | |
x = (x + 1) % bugs.length; | |
bugs[x](); | |
} | |
update(); | |
}, 33); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment