Published
Edited
Mar 4, 2020
5 stars
Insert cell
Insert cell
Insert cell
viewof regl = {
const pixelRatio = Math.min(1.5, devicePixelRatio);

const canvas = DOM.canvas(
Math.floor(width * pixelRatio),
Math.floor(height * pixelRatio)
);
canvas.style.width = `${width}px`;
canvas.style.height = `${height}px`;
const regl = createREGL({
canvas,
pixelRatio,
// This can also dramatically increase performance:
attributes: { antialias: false }
});
return Object.assign(canvas, { value: regl });
}
Insert cell
draw({ zoom })
Insert cell
draw = createDrawCommand(regl)
Insert cell
createDrawCommand = regl =>
regl({
frag: `
precision mediump float;
uniform sampler2D texture;
varying vec2 uv;
void main () {
gl_FragColor = texture2D(texture, uv);
}`,

vert: `
precision mediump float;
attribute vec2 position;
uniform vec2 aspectRatio;
uniform float zoom;
varying vec2 uv;
void main () {
uv = 0.5 + 0.5 * vec2(position) * aspectRatio;
gl_Position = vec4(position, 0, 1.0 / zoom);
}`,

attributes: {
position: [[-2, -4], [-2, 4], [4, 0]]
},

depth: { enable: false },

uniforms: {
texture: regl.texture({ data: image, flipY: true }),
aspectRatio: ctx => {
const ar = ctx.viewportWidth / ctx.viewportHeight;
return ar > 1 ? [ar, 1] : [1, 1 / ar];
},
zoom: regl.prop('zoom')
},

count: 3
})
Insert cell
image = new Promise(resolve => {
const im = new Image();
im.crossOrigin = "anonymous";
im.src =
"https://raw.githubusercontent.com/regl-project/regl/master/example/assets/peppers.png";
im.onload = () => resolve(Object.assign(im, { style: "max-width:200px" }));
})
Insert cell
createREGL = require("regl")
Insert cell
height = 500
Insert cell
import { slider } from "@jashkenas/inputs"
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more