function noiseImage(noise, options = {}) {
const {
height = 400,
width = 400,
scale = "Greys",
thresholds = 0
} = options;
let scaleFunction = d3.scaleSequential(scales[scale]).domain([0, 1]);
if (thresholds > 0)
scaleFunction = makeThresholdScale(scaleFunction, thresholds);
const colorArray = makeColorArray(scaleFunction);
const context = DOM.context2d(width, height, 1);
const image = context.createImageData(width, height);
for (let y = 0, i = 0; y < height; ++y) {
for (let x = 0; x < width; ++x, i += 4) {
const j = ~~Math.max(0, Math.min(255, (noise(x, y) + 1) * 128)) * 3;
image.data[i] = colorArray[j];
image.data[i + 1] = colorArray[j + 1];
image.data[i + 2] = colorArray[j + 2];
image.data[i + 3] = 255;
}
}
context.putImageData(image, 0, 0);
return context.canvas;
}