Published
Edited
Nov 26, 2019
Importers
15 stars
Insert cell
Insert cell
Insert cell
viewof gl = {
const canvas = document.createElement("canvas");
canvas.width = width * devicePixelRatio;
canvas.height = height * devicePixelRatio;
canvas.style = `width: ${width}px; height: auto;`;
canvas.value = canvas.getContext("webgl");
return canvas;
}
Insert cell
init = {
gl.useProgram(program);
gl.enableVertexAttribArray(a_vertex);
gl.vertexAttribPointer(a_vertex, 2, gl.FLOAT, false, 0, 0);
gl.uniform2f(u_translate, viewof gl.width / 2, viewof gl.height / 2);
gl.uniform1f(u_scale, viewof gl.height / 2 - 1);
gl.viewport(0, 0, viewof gl.width, viewof gl.height);
}
Insert cell
month = {
let i = 0;
while (true) {
yield Promises.tick(1000, i++ % 12);
}
}
Insert cell
draw = {
init;
gl.uniform2fv(u_rotate, rotate);
gl.bindTexture(gl.TEXTURE_2D, textures[month]);
gl.drawArrays(gl.TRIANGLE_FAN, 0, 4);
}
Insert cell
rotate = [(now * -0.0002) % (2 * Math.PI), latitude]
Insert cell
fragmentShader = {
const shader = gl.createShader(gl.FRAGMENT_SHADER);
gl.shaderSource(shader, `
precision highp float;

uniform sampler2D u_image;
uniform vec2 u_translate;
uniform float u_scale;
uniform vec2 u_rotate;

const float c_pi = 3.14159265358979323846264;
const float c_halfPi = c_pi * 0.5;
const float c_twoPi = c_pi * 2.0;

float cosphi0 = cos(u_rotate.y);
float sinphi0 = sin(u_rotate.y);

void main(void) {
float x = (gl_FragCoord.x - u_translate.x) / u_scale;
float y = (u_translate.y - gl_FragCoord.y) / u_scale;

// inverse orthographic projection
float rho = sqrt(x * x + y * y);
if (rho > 1.0) return;
float c = asin(rho);
float sinc = sin(c);
float cosc = cos(c);
float lambda = atan(x * sinc, rho * cosc);
float phi = asin(y * sinc / rho);

// inverse rotation
float cosphi = cos(phi);
float x1 = cos(lambda) * cosphi;
float y1 = sin(lambda) * cosphi;
float z1 = sin(phi);
lambda = atan(y1, x1 * cosphi0 + z1 * sinphi0) + u_rotate.x;
phi = asin(z1 * cosphi0 - x1 * sinphi0);

gl_FragColor = texture2D(u_image, vec2((lambda + c_pi) / c_twoPi, (phi + c_halfPi) / c_pi));
}
`);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(shader));
return shader;
}
Insert cell
vertexShader = {
const shader = gl.createShader(gl.VERTEX_SHADER);
gl.shaderSource(shader, `
attribute vec2 a_vertex;

void main(void) {
gl_Position = vec4(a_vertex, 0.0, 1.0);
}
`);
gl.compileShader(shader);
if (!gl.getShaderParameter(shader, gl.COMPILE_STATUS)) throw new Error(gl.getShaderInfoLog(shader));
return shader;
}
Insert cell
vertexBuffer = {
const buffer = gl.createBuffer();
gl.bindBuffer(gl.ARRAY_BUFFER, buffer);
gl.bufferData(gl.ARRAY_BUFFER, Float32Array.of(-1, -1, +1, -1, +1, +1, -1, +1), gl.STATIC_DRAW);
return buffer;
}
Insert cell
images = (await Promise.all([
FileAttachment("1.jpg").image(),
FileAttachment("2.jpg").image(),
FileAttachment("3.jpg").image(),
FileAttachment("4.jpg").image(),
FileAttachment("5.jpg").image(),
FileAttachment("6.jpg").image(),
FileAttachment("7.jpg").image(),
FileAttachment("8.jpg").image(),
FileAttachment("9.jpg").image(),
FileAttachment("10.jpg").image(),
FileAttachment("11.jpg").image(),
FileAttachment("12.jpg").image()
])).map(image => {
const context = DOM.context2d(4096, 2048, 1);
context.drawImage(image, 0, 0, 4096, 2048);
return context.canvas;
})
Insert cell
textures = images.map(canvas => {
const texture = gl.createTexture();
gl.bindTexture(gl.TEXTURE_2D, texture);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MIN_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_MAG_FILTER, gl.LINEAR);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_S, gl.REPEAT);
gl.texParameteri(gl.TEXTURE_2D, gl.TEXTURE_WRAP_T, gl.REPEAT);
gl.texImage2D(gl.TEXTURE_2D, 0, gl.RGBA, gl.RGBA, gl.UNSIGNED_BYTE, canvas);
return texture;
})
Insert cell
program = {
const program = gl.createProgram();
gl.attachShader(program, vertexShader);
gl.attachShader(program, fragmentShader);
gl.linkProgram(program);
if (!gl.getProgramParameter(program, gl.LINK_STATUS)) throw new Error(gl.getProgramInfoLog(program));
return program;
}
Insert cell
a_vertex = gl.getAttribLocation(program, "a_vertex")
Insert cell
u_translate = gl.getUniformLocation(program, "u_translate")
Insert cell
u_scale = gl.getUniformLocation(program, "u_scale")
Insert cell
u_rotate = gl.getUniformLocation(program, "u_rotate")
Insert cell
height = width
Insert cell
import { slider } from "@jashkenas/inputs"
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