Published
Edited
Sep 27, 2022
2 stars
Insert cell
Insert cell
{

var two = new Two({
width: width,
height: width / 1.45833333333,
autostart: true
});
var initialized = false;
var keys;
var data;
var index = 0;
var amount = 100;
var stage = two.makeGroup();
var hud = two.makeGroup();
var styles = {
fontSize: 20,
weight: 700
};
var loading = new Two.Text('0', two.width / 2, two.height / 2, styles);
var stats = new Two.Text(928, 25, 37.5, styles);
stats.alignment = 'left';
stats.baseline = 'top';
hud.add(loading, stats);
var options = {};

var solver = new Matter.Engine.create();
solver.world.gravity.y = 0;
two.bind('update', function(frameCount, timeDelta) {
if (!initialized && xhr.user.isReady) {
initialized = true;
setup();
} else if (initialized) {
draw(frameCount, timeDelta);
} else {
loading.value = xhr.user.pct;
}
});
function setup() {

data = xhr.user.data;
keys = Object.keys(data);

loading.translation.set(25, 25);
loading.alignment = 'left';
loading.baseline = 'top';

}
function draw(frameCount, timeDelta) {

var length = keys.length;
var l = Math.min(index + amount, length);
var circles = [];

for (var i = index; i < l; i++) {

var key = keys[i];
var weight = data[key];
var x = two.width * Math.random();
var y = two.height * Math.random();

var radius = (two.height * 0.2) * Math.pow(weight / stats.value, 0.85);
var circle = new Two.Circle(x, y, radius * 0.9);

circle.entity = Matter.Bodies.circle(x, y, radius, options, Two.Resolution);
var mass = Math.max(radius, 1);
Matter.Body.set(circle.entity, 'mass', mass);
Matter.Body.set(circle.entity, 'density', mass);
circle.entity.position = circle.translation;

circles.push(circle.entity);
stage.add(circle);

}
if (circles.length > 0) {

Matter.World.add(solver.world, circles);
if (typeof timeDelta === 'number') {
Matter.Engine.update(solver, timeDelta);
}

// for (var j = 0; j < stage.children.length; j++) {
// var child = stage.children[j];
// var entity = child.entity;
// if (!entity) {
// continue;
// }
// child.translation.x = entity.position.x;
// child.translation.y = entity.position.y;
// }

}

index = l;
loading.value = index;

}
return two.renderer.domElement;

}
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