Published
Edited
Mar 20, 2021
6 stars
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
function create3DFrame (frame, index) {
const { width: w, height: h } = stageCanvas
let canvas = stageCanvas
let ctx = stageCtx
// this isn't performant at all but frames are cool
if (viewDebugFrames) {
canvas = document.createElement('canvas')
canvas.width = w
canvas.height = h
frameDebugContainer.appendChild(canvas)
ctx = canvas.getContext('2d')
}

ctx.clearRect(0, 0, w, h)
// center the image in the texture
ctx.putImageData(frame.data, (w - imgDims.width) / 2, (h - imgDims.height) / 2)
const texture = new THREE.CanvasTexture(canvas)
const material = new THREE.ShaderMaterial({
vertexShader,
fragmentShader,
uniforms: {
tex: { type: "t", value: texture }
},
side: THREE.DoubleSide,
transparent: true
// blending: THREE.AdditiveBlending
})
const geometry = new THREE.PlaneGeometry(w, h, 2)
const mesh = new THREE.Mesh(geometry, material)
mesh.position.z = index - 30
return mesh
}
Insert cell
function processGif (img, arrayBuffer) {
let gif = SuperGIF({ gif: img, auto_play: 1 })
gif.load_raw(arrayBuffer, () => {
for (let i = 0, frames = gif.get_length(); i < frames; i++) {
const frame = gif.get_frame(i)
const makeFrame = () => {
frameNum.innerText = i
container3D.add(create3DFrame(frame, i))
}
// FIXME need to clear timeouts on Observable re-render
setTimeout(makeFrame, 250 * i)
}
})
}
Insert cell
async function loadGifArrayBuffer (url) {
try {
const res = await fetch(url)
const buf = await res.arrayBuffer()
return new Uint8Array(buf)
} catch (e) {
console.error(e)
return new Uint8Array()
}
}
Insert cell
go = {
loadGifArrayBuffer(gifURL)
.then(arr => processGif($img, arr))
.catch(e => console.error(e))
}
Insert cell
Insert cell
// dimensions for the original data image
imgDims = ({ width: 61, height: 73 })
Insert cell
Insert cell
Insert cell
Insert cell
Type JavaScript, then Shift-Enter. Ctrl-space for more options. Arrow ↑/↓ to switch modes.

Insert cell
Insert cell
Insert cell
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