Skip to content

Instantly share code, notes, and snippets.

@kevinmarx
Created August 1, 2012 01:49
Show Gist options
  • Save kevinmarx/3222617 to your computer and use it in GitHub Desktop.
Save kevinmarx/3222617 to your computer and use it in GitHub Desktop.
Mandelbrot for lightbright at Coco
<!DOCTYPE html>
<html>
<body>
<script>
// var sRandomLine = "####################################################################################";
// var sSolidRandomLine = "%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F%7F";
// var sEmptyLine = "%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20%20";
// var asRandom = [
// sRandomLine, sRandomLine, sRandomLine, sRandomLine, sRandomLine,
// sRandomLine, sRandomLine, sRandomLine, sRandomLine, sRandomLine,
// sRandomLine, sRandomLine
// ];
// var asSolidRandom = [
// sSolidRandomLine, sSolidRandomLine, sSolidRandomLine, sSolidRandomLine, sSolidRandomLine,
// sSolidRandomLine, sSolidRandomLine, sSolidRandomLine, sSolidRandomLine, sSolidRandomLine,
// sSolidRandomLine, sSolidRandomLine
// ];
var asTimeLineData = [
{time: 1.000, func: asciiMandelbrot, arg:{from:{zoom: 0.001, y:-0.4 , x:-0.70, rot:0.25, rnd: 1.0},
to: {zoom: 0.100, y:-0.39, x:-1.71, rot:0.30, rnd: 0.3}, pow: 0.2}},
{time: 4.000, func: asciiMandelbrot, arg:{from:{zoom: 0.100, y:-0.39, x:-1.71, rot:0.30, rnd: 0.3},
to: {zoom: 0.500, y:-0.35, x:-0.75, rot:0.50, rnd: 0.1}, pow: 0.2}},
{time: 60.000, func: asciiMandelbrot, arg:{from:{zoom: 0.500, y:-0.35, x:-0.75, rot:0.50, rnd: 0.1},
to: {zoom: 3.000, y:-0.1 , x:-1.00, rot:3.75, rnd: 0.01}, pow: 1}},
{time: 5.000, func: asciiMandelbrot, arg:{from:{zoom: 3.000, y:-0.1, x:-1.00, rot:3.75, rnd: 0.01},
to: {zoom: 3.500, y:-0.1 , x:-1.00, rot:5.00, rnd: 1}, pow: 2}}
];
var nLines = 12, nRows = 80;
sChars = " ``^^''\"\"*+-~=rcvunoeaszmwxgqypjt&dfhkb?27TYFPV3456890%$UOZRBNXWMQHGAKESDCJLI1li;:\\\\\\/////][)(}{|!!..,,__";
sSolidChars = "3489ABDEGHKMNRSWXZ";
var startDate = new Date();
var sMandelbrotChars = "MWNNHHDDXXOOCC553311I1i?!*;:\"',.` ";
var iRadiusSquared = 10;
var iIterations = 24;
var nRotation = 0.5;
var iFPS = 24;
var iShiftPhase = 0;
var nShift = 0;
var nColorCycles = 1;
var nMandelbrotWidth = 1, nMandelbrotHeight = 1, aMandelbrot = [];
thread();
function thread() {
var nTime = (new Date() - startDate) / 1000; // Time sinds start
var nTimeLineTime = 0;
for (var i = 0; i < asTimeLineData.length; i++) {
// If the elapsed time is smaller than the end time of our event, execute it.
if (nTime < nTimeLineTime + asTimeLineData[i].time) {
var iProgress = (nTime - nTimeLineTime) / asTimeLineData[i].time; // 0-1
asTimeLineData[i].func(iProgress, asTimeLineData[i].arg);
// Only execute one (the first) event.
setTimeout(arguments.callee, 1000/iFPS);
return;
} else {
// Calculate the time it would have taken to reach the events end.
nTimeLineTime += asTimeLineData[i].time;
}
}
}
function shiftFader() {
return nShift * Math.random() * Math.random() * Math.random() * Math.random() * Math.random();
}
function asciiImageFader(iProgress, aArgs) {
var sContents = "";
var nFader = Math.pow((1 - Math.cos(iProgress * Math.PI)) / 2, aArgs.pow); // 0-1 sine
var iShiftFader = shiftFader();
iShiftPhase += shiftFader();
var nRandom = (1 - Math.cos(iProgress * 2 * Math.PI)) / 2; // 0-1-0 sine
for (var line = 0; line < nLines; line++) {
iShift = iShiftFader * Math.cos(Math.PI * 2 * line / nLines + iShiftPhase);
for (var row = 0; row < nRows; row++) {
var nShiftRow = (row + nRows + iShift) % nRows;
var nCharFrom = getChar(aArgs.fromImage, line, nShiftRow);
var nCharTo = getChar(aArgs.toImage, line, nShiftRow);
var nChar = nCharFrom + (nCharTo - nCharFrom) * nFader;
while (nChar < 0) nChar += sChars.length;
sContents += sChars.charAt(nChar % sChars.length);
}
sContents += "<BR>";
}
document.body.innerHTML = "<div>" + sContents + "</div>";
}
function asciiMandelbrot(iProgress, aArgs) {
var sContents = "";
var nFader = Math.pow((1 - Math.cos(iProgress * Math.PI)) / 2, aArgs.pow); // 0-1 sine
var iShiftFader = shiftFader();
iShiftPhase += shiftFader();
var nRotation = aArgs.from.rot + (aArgs.to.rot - aArgs.from.rot) * iProgress;
var sinAlpha = Math.sin(-Math.PI * 2 * nRotation);
var cosAlpha = Math.cos(-Math.PI * 2 * nRotation);
var nZoom = aArgs.from.zoom + (aArgs.to.zoom - aArgs.from.zoom) * iProgress;
var yPos = aArgs.from.y + (aArgs.to.y - aArgs.from.y) * iProgress;
var xPos = aArgs.from.x + (aArgs.to.x - aArgs.from.x) * iProgress;
var nRandomness = aArgs.from.rnd + (aArgs.to.rnd - aArgs.from.rnd) * nFader;
for (var line = 0; line < nLines; line++) {
iShift = iShiftFader * Math.cos(Math.PI * 2 * line / nLines + iShiftPhase);
var dx = ((line/nLines)-0.5)/(nZoom*2);
for (var row = 0; row < nRows; row++) {
var nShiftRow = (row + nRows + iShift) % nRows;
var zr = 0, zi = 0, co = 0;
var dy = ((nShiftRow/nRows)-0.5)/(nZoom);
var ci = dx * sinAlpha + dy * cosAlpha + xPos;
var cr = -dy * sinAlpha + dx * cosAlpha + yPos;
var rsquared = 0, isquared = 0;
for (var count = 1; rsquared + isquared < iRadiusSquared && count < iIterations; count++) {
zi = zr * zi * 2 + ci;
zr = rsquared - isquared + cr;
rsquared = zr * zr;
isquared = zi * zi;
co += Math.pow(Math.E, -Math.sqrt(rsquared+isquared));
}
if (count == iIterations) co = 0;
else co /= iIterations;
var z = (1 - Math.cos(co * 4 * nColorCycles * Math.PI)) / 2;
if (isNaN(z)) z = 0;
if (z >=1) z = 1;
iChar = sChars.lastIndexOf(sMandelbrotChars.charAt(Math.floor((1-z) * (sMandelbrotChars.length - 1))));
if (Math.random()<0.05) iChar += Math.random() * 5 - 2.5;
else iChar += Math.random() * 2 - 1;
iChar += nRandomness * Math.random() * (sChars.length - sChars.length / 2);
while (iChar < 0) iChar += sChars.length;
sContents += sChars.charAt(iChar % sChars.length);
}
sContents += "<br />";
}
document.body.innerHTML = "<pre>" + sContents + "</pre>";
console.log(sContents);
}
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment