Public
Edited
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
// This is the main algorithm, based on the pseudocode provided in the Wikipedia article.
algo = {
const imageData = ctx.getImageData(0, 0, w, h);
const data = imageData.data;
for (let py = 0; py < imageData.height; py++) {
const y0 = scaleY(py);
for (let px = 0; px < imageData.width; px++) {
const x0 = scaleX(px);
let iteration = 0;
let x = 0;
let y = 0;
while (x*x + y*y <= 2*2 && iteration < maxIteration) {
let xtemp = x*x - y*y + x0;
y = 2*x*y + y0;
x = xtemp;
iteration++;
}
const color = parseRgbString(scaleColor(iteration));
plot(data, px, py, color);
}
}
ctx.putImageData(imageData, 0, 0);
}
Insert cell
ctx = DOM.context2d(w, h)
Insert cell
w = width
Insert cell
h = Math.floor(w * 0.6)
Insert cell
scaleX = d3.scaleLinear([-2.00, 0.47]).domain([0, w])
Insert cell
scaleY = d3.scaleLinear([-1.12, 1.12]).domain([0, h])
Insert cell
maxIteration = 250
Insert cell
scaleColor = d3.scaleSequential(d3.interpolateRainbow).domain([0, maxIteration]);
Insert cell
// Parse a string of form "rgb(255, 255, 255)" to an object
function parseRgbString(rgbString) {
const match = rgbString.match(/rgb\((\d+,\s?\d+,\s?\d+)\)/);
const rgb = match[1].split(',').map(d => parseInt(d.trim()));
return { r: rgb[0], g: rgb[1], b: rgb[2] };
}
Insert cell
function plot(data, x, y, color) {
const idx = y * 4 * w + x * 4;
data[idx] = color.r;
data[idx+1] = color.g;
data[idx+2] = color.b;
data[idx+3] = 255;
}
Insert cell

One platform to build and deploy the best data apps

Experiment and prototype by building visualizations in live JavaScript notebooks. Collaborate with your team and decide which concepts to build out.
Use Observable Framework to build data apps locally. Use data loaders to build in any language or library, including Python, SQL, and R.
Seamlessly deploy to Observable. Test before you ship, use automatic deploy-on-commit, and ensure your projects are always up-to-date.
Learn more