Skip to content

Instantly share code, notes, and snippets.

@pbeshai pbeshai/.block
Last active Jan 27, 2019

Embed
What would you like to do?
Animate 100,000 points with regl - II
license: mit
height: 720
border: no
/**
* Given a set of points, lay them out in a phyllotaxis layout.
* Mutates the `points` passed in by updating the x and y values.
*
* @param {Object[]} points The array of points to update. Will get `x` and `y` set.
* @param {Number} pointWidth The size in pixels of the point's width. Should also include margin.
* @param {Number} xOffset The x offset to apply to all points
* @param {Number} yOffset The y offset to apply to all points
*
* @return {Object[]} points with modified x and y
*/
function phyllotaxisLayout(points, pointWidth, xOffset = 0, yOffset = 0, iOffset = 0) {
// theta determines the spiral of the layout
const theta = Math.PI * (3 - Math.sqrt(5));
const pointRadius = pointWidth / 2;
points.forEach((point, i) => {
const index = (i + iOffset) % points.length;
const phylloX = pointRadius * Math.sqrt(index) * Math.cos(index * theta);
const phylloY = pointRadius * Math.sqrt(index) * Math.sin(index * theta);
point.x = xOffset + phylloX - pointRadius;
point.y = yOffset + phylloY - pointRadius;
});
return points;
}
/**
* Given a set of points, lay them out in a grid.
* Mutates the `points` passed in by updating the x and y values.
*
* @param {Object[]} points The array of points to update. Will get `x` and `y` set.
* @param {Number} pointWidth The size in pixels of the point's width. Should also include margin.
* @param {Number} gridWidth The width of the grid of points
*
* @return {Object[]} points with modified x and y
*/
function gridLayout(points, pointWidth, gridWidth) {
const pointHeight = pointWidth;
const pointsPerRow = Math.floor(gridWidth / pointWidth);
const numRows = points.length / pointsPerRow;
points.forEach((point, i) => {
point.x = pointWidth * (i % pointsPerRow);
point.y = pointHeight * Math.floor(i / pointsPerRow);
});
return points;
}
/**
* Given a set of points, lay them out randomly.
* Mutates the `points` passed in by updating the x and y values.
*
* @param {Object[]} points The array of points to update. Will get `x` and `y` set.
* @param {Number} pointWidth The size in pixels of the point's width. Should also include margin.
* @param {Number} width The width of the area to place them in
* @param {Number} height The height of the area to place them in
*
* @return {Object[]} points with modified x and y
*/
function randomLayout(points, pointWidth, width, height) {
points.forEach((point, i) => {
point.x = Math.random() * (width - pointWidth);
point.y = Math.random() * (height - pointWidth);
});
return points;
}
/**
* Given a set of points, lay them out in a sine wave.
* Mutates the `points` passed in by updating the x and y values.
*
* @param {Object[]} points The array of points to update. Will get `x` and `y` set.
* @param {Number} pointWidth The size in pixels of the point's width. Should also include margin.
* @param {Number} width The width of the area to place them in
* @param {Number} height The height of the area to place them in
*
* @return {Object[]} points with modified x and y
*/
function sineLayout(points, pointWidth, width, height) {
const amplitude = 0.3 * (height / 2);
const yOffset = height / 2;
const periods = 3;
const yScale = d3.scaleLinear()
.domain([0, points.length - 1])
.range([0, periods * 2 * Math.PI]);
points.forEach((point, i) => {
point.x = (i / points.length) * (width - pointWidth);
point.y = amplitude * Math.sin(yScale(i)) + yOffset;
});
return points;
}
/**
* Given a set of points, lay them out in a spiral.
* Mutates the `points` passed in by updating the x and y values.
*
* @param {Object[]} points The array of points to update. Will get `x` and `y` set.
* @param {Number} pointWidth The size in pixels of the point's width. Should also include margin.
* @param {Number} width The width of the area to place them in
* @param {Number} height The height of the area to place them in
*
* @return {Object[]} points with modified x and y
*/
function spiralLayout(points, pointWidth, width, height) {
const amplitude = 0.3 * (height / 2);
const xOffset = width / 2;
const yOffset = height / 2;
const periods = 20;
const rScale = d3.scaleLinear()
.domain([0, points.length -1])
.range([0, Math.min(width / 2, height / 2) - pointWidth]);
const thetaScale = d3.scaleLinear()
.domain([0, points.length - 1])
.range([0, periods * 2 * Math.PI]);
points.forEach((point, i) => {
point.x = rScale(i) * Math.cos(thetaScale(i)) + xOffset
point.y = rScale(i) * Math.sin(thetaScale(i)) + yOffset;
});
return points;
}
/**
* Generate an object array of `numPoints` length with unique IDs
* and assigned colors
*/
function createPoints(numPoints, pointWidth, width, height) {
const colorScale = d3.scaleSequential(d3.interpolateViridis)
.domain([numPoints - 1, 0]);
const points = d3.range(numPoints).map(id => ({
id,
color: colorScale(id),
}));
return randomLayout(points, pointWidth, width, height);
}
!function(t){function n(l){if(g[l])return g[l].exports;var i=g[l]={i:l,l:!1,exports:{}};return t[l].call(i.exports,i,i.exports,n),i.l=!0,i.exports}var g={};n.m=t,n.c=g,n.i=function(t){return t},n.d=function(t,g,l){n.o(t,g)||Object.defineProperty(t,g,{configurable:!1,enumerable:!0,get:l})},n.n=function(t){var g=t&&t.__esModule?function(){return t.default}:function(){return t};return n.d(g,"a",g),g},n.o=function(t,n){return Object.prototype.hasOwnProperty.call(t,n)},n.p="",n(n.s=0)}([function(module,exports,__webpack_require__){"use strict";eval("\n\nfunction main(err, regl) {\n var numPoints = 100000;\n var pointWidth = 4;\n var pointMargin = 1;\n var width = window.innerWidth;\n var height = window.innerHeight;\n\n // duration of the animation ignoring delays\n var duration = 1500;\n\n // multiply this value by the index of a point to get its delay\n var delayByIndex = 500 / numPoints;\n\n // include max delay in here\n var maxDuration = duration + delayByIndex * numPoints;\n\n // create helpers that will layout the points in different ways (see common.js)\n var toPhyllotaxis = function toPhyllotaxis(points) {\n return phyllotaxisLayout(points, pointWidth + pointMargin, width / 2, height / 2);\n };\n var toGrid = function toGrid(points) {\n return gridLayout(points, pointWidth + pointMargin, width);\n };\n var toSine = function toSine(points) {\n return sineLayout(points, pointWidth + pointMargin, width, height);\n };\n var toSpiral = function toSpiral(points) {\n return spiralLayout(points, pointWidth + pointMargin, width, height);\n };\n\n // set the order of the layouts and some initial animation state\n var layouts = [toPhyllotaxis, toSpiral, toGrid, toSine];\n var currentLayout = 0;\n var startTime = null; // in seconds\n\n // wrap d3 color scales so they produce vec3s with values 0-1\n function wrapColorScale(scale) {\n return function (t) {\n var rgb = d3.rgb(scale(1 - t));\n return [rgb.r / 255, rgb.g / 255, rgb.b / 255];\n };\n }\n\n // the order of color scales to loop through\n var colorScales = [d3.scaleSequential(d3.interpolateViridis), d3.scaleSequential(d3.interpolateInferno), d3.scaleSequential(d3.interpolateCool)].map(wrapColorScale);\n var currentColorScale = 0;\n\n // function to compile a draw points regl func\n function createDrawPoints(points) {\n var drawPoints = regl({\n frag: '\\n\\t\\t\\t// set the precision of floating point numbers\\n\\t\\t precision highp float;\\n\\n\\t\\t // this value is populated by the vertex shader\\n\\t\\t\\tvarying vec3 fragColor;\\n\\n\\t\\t\\tvoid main() {\\n\\t\\t\\t\\t// gl_FragColor is a special variable that holds the color of a pixel\\n\\t\\t\\t\\tgl_FragColor = vec4(fragColor, 1);\\n\\t\\t\\t}\\n\\t\\t\\t',\n\n vert: '\\n\\t\\t\\t// per vertex attributes\\n\\t\\t\\tattribute vec2 positionStart;\\n\\t\\t\\tattribute vec2 positionEnd;\\n\\t\\t\\tattribute float index;\\n\\t\\t\\tattribute vec3 colorStart;\\n\\t\\t\\tattribute vec3 colorEnd;\\n\\n\\t\\t\\t// variables to send to the fragment shader\\n\\t\\t\\tvarying vec3 fragColor;\\n\\n\\t\\t\\t// values that are the same for all vertices\\n\\t\\t\\tuniform float pointWidth;\\n\\t\\t\\tuniform float stageWidth;\\n\\t\\t\\tuniform float stageHeight;\\n\\t\\t\\tuniform float elapsed;\\n\\t\\t\\tuniform float duration;\\n\\t\\t\\tuniform float delayByIndex;\\n\\n\\t\\t\\t// helper function to transform from pixel space to normalized device coordinates (NDC)\\n\\t\\t\\t// in NDC (0,0) is the middle, (-1, 1) is the top left and (1, -1) is the bottom right.\\n\\t\\t\\tvec2 normalizeCoords(vec2 position) {\\n\\t\\t\\t\\t// read in the positions into x and y vars\\n\\t float x = position[0];\\n\\t float y = position[1];\\n\\n\\t\\t\\t\\treturn vec2(\\n\\t\\t 2.0 * ((x / stageWidth) - 0.5),\\n\\t\\t // invert y since we think [0,0] is bottom left in pixel space\\n\\t\\t -(2.0 * ((y / stageHeight) - 0.5)));\\n\\t\\t\\t}\\n\\n\\t\\t\\t// helper function to handle cubic easing (copied from d3 for consistency)\\n\\t\\t\\t// note there are pre-made easing functions available via glslify.\\n\\t\\t\\tfloat easeCubicInOut(float t) {\\n\\t\\t\\t\\tt *= 2.0;\\n t = (t <= 1.0 ? t * t * t : (t -= 2.0) * t * t + 2.0) / 2.0;\\n\\n if (t > 1.0) {\\n t = 1.0;\\n }\\n\\n return t;\\n\\t\\t\\t}\\n\\n\\t\\t\\tvoid main() {\\n\\t\\t\\t\\t// update the size of a point based on the prop pointWidth\\n\\t\\t\\t\\tgl_PointSize = pointWidth;\\n\\n\\t\\t\\t\\tfloat delay = delayByIndex * index;\\n\\n\\t\\t\\t\\t// number between 0 and 1 indicating how far through the animation this\\n\\t\\t\\t\\t// vertex is.\\n\\t float t;\\n\\n\\t // drawing without animation, so show end state immediately\\n\\t if (duration == 0.0) {\\n\\t t = 1.0;\\n\\n\\t // still delaying before animating\\n\\t } else if (elapsed < delay) {\\n\\t t = 0.0;\\n\\n\\t // otherwise we are animating, so use cubic easing\\n\\t } else {\\n\\t t = easeCubicInOut((elapsed - delay) / duration);\\n\\t }\\n\\n\\t\\t\\t\\t// interpolate position\\n\\t vec2 position = mix(positionStart, positionEnd, t);\\n\\n\\t // interpolate and send color to the fragment shader\\n\\t fragColor = mix(colorStart, colorEnd, t);\\n\\n\\t\\t\\t\\t// scale to normalized device coordinates\\n\\t\\t\\t\\t// gl_Position is a special variable that holds the position of a vertex\\n\\t gl_Position = vec4(normalizeCoords(position), 0.0, 1.0);\\n\\t\\t\\t}\\n\\t\\t\\t',\n\n attributes: {\n positionStart: points.map(function (d) {\n return [d.sx, d.sy];\n }),\n positionEnd: points.map(function (d) {\n return [d.tx, d.ty];\n }),\n colorStart: points.map(function (d) {\n return d.colorStart;\n }),\n colorEnd: points.map(function (d) {\n return d.colorEnd;\n }),\n index: d3.range(points.length)\n },\n\n uniforms: {\n pointWidth: regl.prop('pointWidth'),\n stageWidth: regl.prop('stageWidth'),\n stageHeight: regl.prop('stageHeight'),\n delayByIndex: regl.prop('delayByIndex'),\n duration: regl.prop('duration'),\n\n // time in milliseconds since the prop startTime (i.e. time elapsed)\n elapsed: function elapsed(_ref, _ref2) {\n var time = _ref.time;\n var _ref2$startTime = _ref2.startTime,\n startTime = _ref2$startTime === undefined ? 0 : _ref2$startTime;\n return (time - startTime) * 1000;\n }\n },\n\n count: points.length,\n primitive: 'points'\n });\n\n return drawPoints;\n }\n\n // start animation loop (note: time is in seconds)\n function animate(layout, points) {\n console.log('animating with new layout');\n // make previous end the new beginning\n points.forEach(function (d) {\n d.sx = d.tx;\n d.sy = d.ty;\n d.colorStart = d.colorEnd;\n });\n\n // layout points\n layout(points);\n\n // copy layout x y to end positions\n var colorScale = colorScales[currentColorScale];\n points.forEach(function (d, i) {\n d.tx = d.x;\n d.ty = d.y;\n d.colorEnd = colorScale(i / points.length);\n });\n\n // create the regl function with the new start and end points\n var drawPoints = createDrawPoints(points);\n\n var frameLoop = regl.frame(function (_ref3) {\n var time = _ref3.time;\n\n if (startTime === null) {\n startTime = time;\n }\n\n // clear the buffer\n regl.clear({\n // background color (black)\n color: [0, 0, 0, 1],\n depth: 1\n });\n\n // draw the points using our created regl func\n drawPoints({\n pointWidth: pointWidth,\n stageWidth: width,\n stageHeight: height,\n duration: duration,\n delayByIndex: delayByIndex,\n startTime: startTime\n });\n\n // if we have exceeded the maximum duration, move on to the next animation\n if (time - startTime > maxDuration / 1000) {\n console.log('done animating, moving to next layout');\n frameLoop.cancel();\n\n currentLayout = (currentLayout + 1) % layouts.length;\n startTime = null;\n currentColorScale = (currentColorScale + 1) % colorScales.length;\n animate(layouts[currentLayout], points);\n }\n });\n }\n\n // create initial set of points\n var points = createPoints(numPoints, pointWidth, width, height);\n\n // initialize with all the points in the middle of the screen\n points.forEach(function (d, i) {\n d.tx = width / 2;\n d.ty = height / 2;\n d.colorEnd = [0, 0, 0];\n });\n\n animate(layouts[currentLayout], points);\n}\n\n// initialize regl\ncreateREGL({\n // callback when regl is initialized\n onDone: main\n});//# sourceURL=[module]\n//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiMC5qcyIsInNvdXJjZXMiOlsid2VicGFjazovLy9zY3JpcHQuanM/OWE5NSJdLCJzb3VyY2VzQ29udGVudCI6WyJmdW5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0FBQUE7QUFBQTtBQUFBO0FBQUE7QUFSQTtBQUNBO0FBVUE7QUFDQTtBQWxIQTtBQUNBO0FBb0hBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQUE7QUFDQTtBQUFBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUhBO0FBQ0E7QUFLQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBTkE7QUFDQTtBQVFBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUNBO0FBQ0E7QUFDQTtBQUZBIiwic291cmNlUm9vdCI6IiJ9\n//# sourceURL=webpack-internal:///0\n")}]);
function phyllotaxisLayout(points,pointWidth,xOffset,yOffset,iOffset){if(xOffset===void 0)xOffset=0;if(yOffset===void 0)yOffset=0;if(iOffset===void 0)iOffset=0;var theta=Math.PI*(3-Math.sqrt(5));var pointRadius=pointWidth/2;points.forEach(function(point,i){var index=(i+iOffset)%points.length;var phylloX=pointRadius*Math.sqrt(index)*Math.cos(index*theta);var phylloY=pointRadius*Math.sqrt(index)*Math.sin(index*theta);point.x=xOffset+phylloX-pointRadius;point.y=yOffset+phylloY-pointRadius});return points}function gridLayout(points,pointWidth,gridWidth){var pointHeight=pointWidth;var pointsPerRow=Math.floor(gridWidth/pointWidth);var numRows=points.length/pointsPerRow;points.forEach(function(point,i){point.x=pointWidth*(i%pointsPerRow);point.y=pointHeight*Math.floor(i/pointsPerRow)});return points}function randomLayout(points,pointWidth,width,height){points.forEach(function(point,i){point.x=Math.random()*(width-pointWidth);point.y=Math.random()*(height-pointWidth)});return points}function sineLayout(points,pointWidth,width,height){var amplitude=.3*(height/2);var yOffset=height/2;var periods=3;var yScale=d3.scaleLinear().domain([0,points.length-1]).range([0,periods*2*Math.PI]);points.forEach(function(point,i){point.x=i/points.length*(width-pointWidth);point.y=amplitude*Math.sin(yScale(i))+yOffset});return points}function spiralLayout(points,pointWidth,width,height){var amplitude=.3*(height/2);var xOffset=width/2;var yOffset=height/2;var periods=20;var rScale=d3.scaleLinear().domain([0,points.length-1]).range([0,Math.min(width/2,height/2)-pointWidth]);var thetaScale=d3.scaleLinear().domain([0,points.length-1]).range([0,periods*2*Math.PI]);points.forEach(function(point,i){point.x=rScale(i)*Math.cos(thetaScale(i))+xOffset;point.y=rScale(i)*Math.sin(thetaScale(i))+yOffset});return points}function createPoints(numPoints,pointWidth,width,height){var colorScale=d3.scaleSequential(d3.interpolateViridis).domain([numPoints-1,0]);var points=d3.range(numPoints).map(function(id){return{id:id,color:colorScale(id)}});return randomLayout(points,pointWidth,width,height)}
//# sourceMappingURL=data:application/json;charset=utf-8;base64,eyJ2ZXJzaW9uIjozLCJzb3VyY2VzIjpbImNvbW1vbi5qcyJdLCJuYW1lcyI6WyJwaHlsbG90YXhpc0xheW91dCIsInBvaW50cyIsInBvaW50V2lkdGgiLCJ4T2Zmc2V0IiwieU9mZnNldCIsImlPZmZzZXQiLCJjb25zdCIsInRoZXRhIiwiTWF0aCIsIlBJIiwic3FydCIsInBvaW50UmFkaXVzIiwiZm9yRWFjaCIsInBvaW50IiwiaSIsImluZGV4IiwibGVuZ3RoIiwicGh5bGxvWCIsImNvcyIsInBoeWxsb1kiLCJzaW4iLCJ4IiwieSIsImdyaWRMYXlvdXQiLCJncmlkV2lkdGgiLCJwb2ludEh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ihbbnVtUG9pbnRzIC0gMSwgMF0pO1xuXG4gIGNvbnN0IHBvaW50cyA9IGQzLnJhbmdlKG51bVBvaW50cykubWFwKGlkID0+ICh7XG4gICAgaWQsXG4gICAgY29sb3I6IGNvbG9yU2NhbGUoaWQpLFxuICB9KSk7XG5cbiAgcmV0dXJuIHJhbmRvbUxheW91dChwb2ludHMsIHBvaW50V2lkdGgsIHdpZHRoLCBoZWlnaHQpO1xufVxuIl19
<!DOCTYPE html>
<title>Animate 100,000 points with regl</title>
<body>
<script src="https://cdnjs.cloudflare.com/ajax/libs/regl/1.3.11/regl.min.js"></script>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="dist_common.js"></script>
<script src="dist.js"></script>
</body>
function main(err, regl) {
const numPoints = 100000;
const pointWidth = 4;
const pointMargin = 1;
const width = window.innerWidth;
const height = window.innerHeight;
// duration of the animation ignoring delays
const duration = 1500;
// multiply this value by the index of a point to get its delay
const delayByIndex = 500 / numPoints;
// include max delay in here
const maxDuration = duration + delayByIndex * numPoints;
// create helpers that will layout the points in different ways (see common.js)
const toPhyllotaxis = points =>
phyllotaxisLayout(points, pointWidth + pointMargin, width / 2, height / 2);
const toGrid = points => gridLayout(points, pointWidth + pointMargin, width);
const toSine = points =>
sineLayout(points, pointWidth + pointMargin, width, height);
const toSpiral = points =>
spiralLayout(points, pointWidth + pointMargin, width, height);
// set the order of the layouts and some initial animation state
const layouts = [toPhyllotaxis, toSpiral, toGrid, toSine];
let currentLayout = 0;
let startTime = null; // in seconds
// wrap d3 color scales so they produce vec3s with values 0-1
function wrapColorScale(scale) {
return t => {
const rgb = d3.rgb(scale(1 - t));
return [rgb.r / 255, rgb.g / 255, rgb.b / 255];
};
}
// the order of color scales to loop through
const colorScales = [
d3.scaleSequential(d3.interpolateViridis),
d3.scaleSequential(d3.interpolateInferno),
d3.scaleSequential(d3.interpolateCool),
].map(wrapColorScale);
let currentColorScale = 0;
// function to compile a draw points regl func
function createDrawPoints(points) {
const drawPoints = regl({
frag: `
// set the precision of floating point numbers
precision highp float;
// this value is populated by the vertex shader
varying vec3 fragColor;
void main() {
// gl_FragColor is a special variable that holds the color of a pixel
gl_FragColor = vec4(fragColor, 1);
}
`,
vert: `
// per vertex attributes
attribute vec2 positionStart;
attribute vec2 positionEnd;
attribute float index;
attribute vec3 colorStart;
attribute vec3 colorEnd;
// variables to send to the fragment shader
varying vec3 fragColor;
// values that are the same for all vertices
uniform float pointWidth;
uniform float stageWidth;
uniform float stageHeight;
uniform float elapsed;
uniform float duration;
uniform float delayByIndex;
// helper function to transform from pixel space to normalized device coordinates (NDC)
// in NDC (0,0) is the middle, (-1, 1) is the top left and (1, -1) is the bottom right.
vec2 normalizeCoords(vec2 position) {
// read in the positions into x and y vars
float x = position[0];
float y = position[1];
return vec2(
2.0 * ((x / stageWidth) - 0.5),
// invert y since we think [0,0] is bottom left in pixel space
-(2.0 * ((y / stageHeight) - 0.5)));
}
// helper function to handle cubic easing (copied from d3 for consistency)
// note there are pre-made easing functions available via glslify.
float easeCubicInOut(float t) {
t *= 2.0;
t = (t <= 1.0 ? t * t * t : (t -= 2.0) * t * t + 2.0) / 2.0;
if (t > 1.0) {
t = 1.0;
}
return t;
}
void main() {
// update the size of a point based on the prop pointWidth
gl_PointSize = pointWidth;
float delay = delayByIndex * index;
// number between 0 and 1 indicating how far through the animation this
// vertex is.
float t;
// drawing without animation, so show end state immediately
if (duration == 0.0) {
t = 1.0;
// still delaying before animating
} else if (elapsed < delay) {
t = 0.0;
// otherwise we are animating, so use cubic easing
} else {
t = easeCubicInOut((elapsed - delay) / duration);
}
// interpolate position
vec2 position = mix(positionStart, positionEnd, t);
// interpolate and send color to the fragment shader
fragColor = mix(colorStart, colorEnd, t);
// scale to normalized device coordinates
// gl_Position is a special variable that holds the position of a vertex
gl_Position = vec4(normalizeCoords(position), 0.0, 1.0);
}
`,
attributes: {
positionStart: points.map(d => [d.sx, d.sy]),
positionEnd: points.map(d => [d.tx, d.ty]),
colorStart: points.map(d => d.colorStart),
colorEnd: points.map(d => d.colorEnd),
index: d3.range(points.length),
},
uniforms: {
pointWidth: regl.prop('pointWidth'),
stageWidth: regl.prop('stageWidth'),
stageHeight: regl.prop('stageHeight'),
delayByIndex: regl.prop('delayByIndex'),
duration: regl.prop('duration'),
// time in milliseconds since the prop startTime (i.e. time elapsed)
elapsed: ({ time }, { startTime = 0 }) => (time - startTime) * 1000,
},
count: points.length,
primitive: 'points',
});
return drawPoints;
}
// start animation loop (note: time is in seconds)
function animate(layout, points) {
console.log('animating with new layout');
// make previous end the new beginning
points.forEach(d => {
d.sx = d.tx;
d.sy = d.ty;
d.colorStart = d.colorEnd;
});
// layout points
layout(points);
// copy layout x y to end positions
const colorScale = colorScales[currentColorScale];
points.forEach((d, i) => {
d.tx = d.x;
d.ty = d.y;
d.colorEnd = colorScale(i / points.length);
});
// create the regl function with the new start and end points
const drawPoints = createDrawPoints(points);
const frameLoop = regl.frame(({ time }) => {
if (startTime === null) {
startTime = time;
}
// clear the buffer
regl.clear({
// background color (black)
color: [0, 0, 0, 1],
depth: 1,
});
// draw the points using our created regl func
drawPoints({
pointWidth,
stageWidth: width,
stageHeight: height,
duration,
delayByIndex,
startTime,
});
// if we have exceeded the maximum duration, move on to the next animation
if (time - startTime > maxDuration / 1000) {
console.log('done animating, moving to next layout');
frameLoop.cancel();
currentLayout = (currentLayout + 1) % layouts.length;
startTime = null;
currentColorScale = (currentColorScale + 1) % colorScales.length;
animate(layouts[currentLayout], points);
}
});
}
// create initial set of points
const points = createPoints(numPoints, pointWidth, width, height);
// initialize with all the points in the middle of the screen
points.forEach((d, i) => {
d.tx = width / 2;
d.ty = height / 2;
d.colorEnd = [0, 0, 0];
});
animate(layouts[currentLayout], points);
}
// initialize regl
createREGL({
// callback when regl is initialized
onDone: main,
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.