Public
Edited
Jan 27, 2023
5 stars
Insert cell
Insert cell
Insert cell
Insert cell
{
pathdraw // reference in cell to trigger re-draw
Stereogram.render({
el: 'obs3d',
colors: generatePalette(10),
depthMapper: new CanvasDepthMapper(document.getElementById('can')),
});
}
Insert cell
context = can.getContext("2d")
Insert cell
Insert cell
Insert cell
Insert cell
CanvasDepthMapper = Stereogram.DepthMapper.extend({
constructor: function (canvas, opts) {
opts = opts || {};
this.canvas = canvas;
this.inverted = opts.inverted !== false;
},

make: function () {
var x,
y,
offset,
canvas = this.canvas,
context = canvas.getContext("2d"),
depthMap = [],
width = canvas.width,
height = canvas.height,
pixelData = context.getImageData(0, 0, width, height).data;
for (y = 0; y < height; y++) {
depthMap[y] = new Float32Array(width);
offset = width * y * 4;
for (x = 0; x < width; x++) {
// assume grayscale (R, G, and B are equal)
if (this.inverted) {
depthMap[y][x] = 1 - pixelData[offset + x * 4] / 255;
} else {
depthMap[y][x] = pixelData[offset + x * 4] / 255;
}
}
}
return depthMap;
}
});
Insert cell
generatePalette = (numColors) => {
return Array(numColors).fill().map((c) => {
return [
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
Math.floor(Math.random() * 256),
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