Published
Edited
Dec 3, 2019
33 stars
Also listed in…
WebGL
Insert cell
Insert cell
Insert cell
Insert cell
function createDrawQuasicrystal(regl) {
return regl({
vert: `
precision highp float;
attribute vec2 aBigTri;
uniform mat3 inverseView3;
varying vec2 xy;
void main () {
// Use an inverse view matrix computed from the x-scale and y-scale
// to pass an x-y pair to the fragment shader
xy = (inverseView3 * vec3(aBigTri, 1)).xy;

gl_Position = vec4(aBigTri, 0, 1);
}`,
frag: `
precision highp float;

uniform float t;
varying vec2 xy;

const float PI = 3.14159265358979323846264;

vec4 color(float t) {
const float ar = 0.50, ag = 0.50, ab = 0.50;
const float br = 0.50, bg = 0.50, bb = 0.50;
const float cr = 1.00, cg = 1.00, cb = 1.00;
const float dr = 0.00, dg = 0.10, db = 0.20;
return vec4(
ar + br * cos(2.0 * PI * (cr * t + dr)),
ag + bg * cos(2.0 * PI * (cg * t + dg)),
ab + bb * cos(2.0 * PI * (cb * t + db)),
1.0
);
}

void main(void) {
const int n = ${N};
const float step = PI / float(n);
float alpha;
for (int i = 0; i < n; i++) {
float theta = float(i) * step;
alpha += cos(cos(theta) * xy.x + sin(theta) * xy.y + t);
}
gl_FragColor = color(alpha * 2.0 / float(n));
}`,
attributes: {
// Render a single big triangle that covers the viewport
aBigTri: [-4, -4, 4, -4, 0, 4]
},
uniforms: {
t: regl.context('time')
},
count: 3,
depth: { enable: false }
});
}
Insert cell
plotContext = {
return createPlotContext(this, {
width: width,
xrange: [-100, 100],
yrange: [-100, 100]
});
}
Insert cell
function createPlotContext(existingContext, opts) {
const xrange = opts.xrange || [-1, 1];
const yrange = opts.yrange || [-1, 1];
const w = opts.width || 640;
const margin =
opts.margin ||
(width < 640 ? { t: 3, r: 0, b: 1, l: 1 } : { t: 5, r: 5, b: 20, l: 40 });

const height = opts.height || Math.max(500, Math.min(900, w * 0.7));
const viewport = { width: w, height, dpi: devicePixelRatio, margin };

const yScale = d3
.scaleLinear()
.domain(xrange)
.range([viewport.height - viewport.margin.b, viewport.margin.t]);

const xScale = constrainLinearScaleAspectRatio(
d3
.scaleLinear()
.domain(yrange)
.range([viewport.margin.l, viewport.width - viewport.margin.r]),
yScale,
1
);

return Object.assign(
existingContext || {
element: d3.create('svg').node()
},
{
viewport,
xScale,
yScale,
currentXScale: xScale.copy(),
currentYScale: yScale.copy()
}
);
}
Insert cell
d3 = require('d3@5')
Insert cell
createREGL = require('regl')
Insert cell
import {
getOrAttachReglFO,
constrainLinearScaleAspectRatio,
createReglViewportConfiguration,
createReglLinearScaleConfiguration,
persistentZoom,
viewportAxes
} from '@rreusser/regl-tools'
Insert cell
isNarrowScreen = width <= 640
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